get_friends_and_groups_logic.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package Wxhook
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/spf13/cast"
  6. "github.com/suyuan32/simple-admin-common/enum/errorcode"
  7. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  8. "strings"
  9. "wechat-api/ent/label"
  10. "wechat-api/hook"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. reqv3 "github.com/imroc/req/v3"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetFriendsAndGroupsLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewGetFriendsAndGroupsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFriendsAndGroupsLogic {
  22. return &GetFriendsAndGroupsLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. type GetWechatFriendListResp struct {
  28. Code int64 `json:"code"`
  29. Data []struct {
  30. Avatar string `json:"avatar"`
  31. Cid int64 `json:"cid"`
  32. City string `json:"city"`
  33. Country string `json:"country"`
  34. CreateTime int64 `json:"create_time"`
  35. FriendWechatno string `json:"friend_wechatno"`
  36. Friendid string `json:"friendid"`
  37. Gender int64 `json:"gender"`
  38. ID int64 `json:"id"`
  39. Memo string `json:"memo"`
  40. ModifyTime int64 `json:"modify_time"`
  41. Nickname string `json:"nickname"`
  42. Phone string `json:"phone"`
  43. Province string `json:"province"`
  44. Remark string `json:"remark"`
  45. Type int64 `json:"type"`
  46. Wechatid string `json:"wechatid"`
  47. } `json:"data"`
  48. Msg string `json:"msg"`
  49. }
  50. func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
  51. //organizationId := l.ctx.Value("organizationId").(uint64)
  52. wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, req.Id)
  53. if err != nil || wxInfo == nil {
  54. l.Error("查询微信信息失败", err)
  55. return
  56. }
  57. organizationId := wxInfo.OrganizationID
  58. if wxInfo.ServerID > 0 {
  59. //云托管版本
  60. serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
  61. if err != nil {
  62. l.Error("查询服务器信息失败", err)
  63. return nil, err
  64. }
  65. hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
  66. friendAndChatRoomList, err := hookClient.GetFriendAndChatRoomList("0")
  67. if err != nil {
  68. l.Error("获取好友列表失败", err)
  69. return nil, err
  70. }
  71. newFriendIds := make(map[uint64][]string)
  72. newLagIds := make(map[string]struct{})
  73. friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
  74. if friendCnt > 0 {
  75. for _, friend := range friendAndChatRoomList.Friend {
  76. id, err := l.svcCtx.DB.Contact.Create().
  77. SetWxWxid(wxInfo.Wxid).
  78. SetType(1).
  79. SetWxid(friend.Wxid).
  80. SetAccount(friend.Account).
  81. SetNickname(friend.Nickname).
  82. SetMarkname(friend.Markname).
  83. SetHeadimg(friend.Headimg).
  84. SetSex(cast.ToInt(friend.Sex)).
  85. SetStarrole(friend.Starrole).
  86. SetDontseeit(cast.ToInt(friend.Dontseeit)).
  87. SetDontseeme(cast.ToInt(friend.Dontseeme)).
  88. SetLag(friend.Lag).
  89. SetV3(friend.V3).
  90. SetOrganizationID(organizationId).
  91. OnConflict().
  92. UpdateNewValues().
  93. ID(l.ctx)
  94. if err == nil {
  95. lags := splitStringToIntArray(friend.Lag)
  96. newFriendIds[id] = lags
  97. for _, lag := range lags {
  98. if _, exists := newLagIds[lag]; !exists {
  99. newLagIds[lag] = struct{}{}
  100. }
  101. }
  102. }
  103. }
  104. }
  105. // 平台和微信标签映射
  106. if len(newLagIds) > 0 {
  107. wxLagIdsSet := make(map[string]string)
  108. wxLagIds, err := hookClient.GetContactLabelList()
  109. if err == nil {
  110. wxSysSet := make(map[string]uint64)
  111. for _, wxLagId := range wxLagIds.Label {
  112. wxLagIdsSet[wxLagId.Id] = wxLagId.Name
  113. }
  114. for lagId := range newLagIds {
  115. if lagId == "" {
  116. continue
  117. }
  118. name := wxLagIdsSet[lagId]
  119. label, err := l.svcCtx.DB.Label.Query().
  120. Where(
  121. label.NameEQ(name), // Filter by ID
  122. label.TypeEQ(1),
  123. label.OrganizationID(organizationId), // Additional filter by organizationId
  124. ).
  125. Only(l.ctx)
  126. if err != nil {
  127. // 如果标签不存在则创建
  128. newLabel, err := l.svcCtx.DB.Label.Create().
  129. SetType(1).
  130. SetName(name).
  131. SetFrom(2).
  132. SetMode(2).
  133. SetConditions("{}").
  134. SetOrganizationID(organizationId).
  135. Save(l.ctx)
  136. if err == nil {
  137. wxSysSet[lagId] = newLabel.ID
  138. }
  139. } else {
  140. // 如果标签存在
  141. wxSysSet[lagId] = label.ID
  142. }
  143. }
  144. // 为新联系人打标签
  145. if len(newFriendIds) > 0 && len(newLagIds) > 0 {
  146. for id, lags := range newFriendIds {
  147. for _, lag := range lags {
  148. label_id := wxSysSet[lag]
  149. if label_id != 0 {
  150. l.svcCtx.DB.LabelRelationship.Create().
  151. SetLabelID(label_id).
  152. SetContactID(id).
  153. SetOrganizationID(organizationId).
  154. Save(l.ctx)
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
  162. if chatroomCnt > 0 {
  163. for _, chatroom := range friendAndChatRoomList.Chatroom {
  164. l.svcCtx.DB.Contact.Create().
  165. SetWxWxid(wxInfo.Wxid).
  166. SetType(2).
  167. SetWxid(chatroom.Gid).
  168. SetNickname(chatroom.Gname).
  169. SetGid(chatroom.Gid).
  170. SetGname(chatroom.Gname).
  171. SetMarkname(chatroom.Markname).
  172. SetV3(chatroom.V3).
  173. SetOrganizationID(organizationId).
  174. OnConflict().
  175. UpdateNewValues().
  176. ID(l.ctx)
  177. }
  178. }
  179. ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
  180. if ghCnt > 0 {
  181. for _, gh := range friendAndChatRoomList.Gh {
  182. l.svcCtx.DB.Contact.Create().
  183. SetWxWxid(wxInfo.Wxid).
  184. SetType(3).
  185. SetWxid(gh.Wxid).
  186. SetAccount(gh.Account).
  187. SetNickname(gh.Nickname).
  188. SetMarkname(gh.Markname).
  189. SetV3(gh.V3).
  190. SetOrganizationID(organizationId).
  191. OnConflict().
  192. UpdateNewValues().
  193. ID(l.ctx)
  194. }
  195. }
  196. } else {
  197. // 工作手机版本
  198. hookClient := hook.NewHook("", "", "")
  199. var result GetWechatFriendListResp
  200. res, err := reqv3.C().DevMode().R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWechatFriendList?cid=" + wxInfo.ProcessID + "&wechatid=" + wxInfo.Wxid)
  201. if err != nil {
  202. return nil, err
  203. }
  204. if !res.IsSuccessState() {
  205. err = fmt.Errorf("GetFriendAndChatRoomList failed with status code %d", res.StatusCode)
  206. l.Error("GetWechatFriendList 请求失败", err)
  207. return nil, err
  208. }
  209. _ = hookClient.TriggerChatroomPush(wxInfo.Wxid)
  210. for _, friend := range result.Data {
  211. friendType := 1
  212. if friend.Type == 1 {
  213. friendType = 2
  214. _ = hookClient.RequestChatRoomInfo(friend.Friendid, wxInfo.Wxid)
  215. } else {
  216. friendType = 1
  217. }
  218. _, err = l.svcCtx.DB.Contact.Create().
  219. SetWxWxid(wxInfo.Wxid).
  220. SetType(friendType).
  221. SetWxid(friend.Friendid).
  222. SetAccount(friend.FriendWechatno).
  223. SetNickname(friend.Nickname).
  224. SetMarkname(friend.Memo).
  225. SetHeadimg(friend.Avatar).
  226. SetSex(cast.ToInt(friend.Gender)).
  227. SetOrganizationID(organizationId).
  228. OnConflict().
  229. UpdateNewValues().
  230. SetOrganizationID(organizationId).
  231. ID(l.ctx)
  232. if err != nil {
  233. l.Error("创建联系人失败", err)
  234. continue
  235. }
  236. }
  237. }
  238. resp = &types.BaseMsgResp{
  239. Msg: errormsg.Success,
  240. Code: errorcode.OK,
  241. }
  242. return
  243. }
  244. func splitStringToIntArray(input string) []string {
  245. strArray := strings.Split(input, ",")
  246. intArray := make([]string, len(strArray))
  247. for i, str := range strArray {
  248. intArray[i] = str
  249. }
  250. return intArray
  251. }