get_friends_and_groups_logic.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 {
  54. l.Error("查询微信信息失败", err)
  55. return
  56. }
  57. if wxInfo.ServerID > 0 {
  58. //云托管版本
  59. serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
  60. if err != nil {
  61. l.Error("查询服务器信息失败", err)
  62. return nil, err
  63. }
  64. hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
  65. friendAndChatRoomList, err := hookClient.GetFriendAndChatRoomList("0")
  66. if err != nil {
  67. l.Error("获取好友列表失败", err)
  68. return nil, err
  69. }
  70. newFriendIds := make(map[uint64][]string)
  71. newLagIds := make(map[string]struct{})
  72. friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
  73. if friendCnt > 0 {
  74. for _, friend := range friendAndChatRoomList.Friend {
  75. id, err := l.svcCtx.DB.Contact.Create().
  76. SetWxWxid(wxInfo.Wxid).
  77. SetType(1).
  78. SetWxid(friend.Wxid).
  79. SetAccount(friend.Account).
  80. SetNickname(friend.Nickname).
  81. SetMarkname(friend.Markname).
  82. SetHeadimg(friend.Headimg).
  83. SetSex(cast.ToInt(friend.Sex)).
  84. SetStarrole(friend.Starrole).
  85. SetDontseeit(cast.ToInt(friend.Dontseeit)).
  86. SetDontseeme(cast.ToInt(friend.Dontseeme)).
  87. SetLag(friend.Lag).
  88. SetV3(friend.V3).
  89. OnConflict().
  90. UpdateNewValues().
  91. SetOrganizationID(organizationId).
  92. ID(l.ctx)
  93. if err == nil {
  94. lags := splitStringToIntArray(friend.Lag)
  95. newFriendIds[id] = lags
  96. for _, lag := range lags {
  97. if _, exists := newLagIds[lag]; !exists {
  98. newLagIds[lag] = struct{}{}
  99. }
  100. }
  101. }
  102. }
  103. }
  104. // 平台和微信标签映射
  105. if len(newLagIds) > 0 {
  106. wxLagIdsSet := make(map[string]string)
  107. wxLagIds, err := hookClient.GetContactLabelList()
  108. if err == nil {
  109. wxSysSet := make(map[string]uint64)
  110. for _, wxLagId := range wxLagIds.Label {
  111. wxLagIdsSet[wxLagId.Id] = wxLagId.Name
  112. }
  113. for lagId := range newLagIds {
  114. if lagId == "" {
  115. continue
  116. }
  117. name := wxLagIdsSet[lagId]
  118. label, err := l.svcCtx.DB.Label.Query().
  119. Where(
  120. label.NameEQ(name), // Filter by ID
  121. label.TypeEQ(1),
  122. label.OrganizationID(organizationId), // Additional filter by organizationId
  123. ).
  124. Only(l.ctx)
  125. if err != nil {
  126. // 如果标签不存在则创建
  127. newLabel, err := l.svcCtx.DB.Label.Create().
  128. SetType(1).
  129. SetName(name).
  130. SetFrom(2).
  131. SetMode(2).
  132. SetConditions("{}").
  133. SetOrganizationID(organizationId).
  134. Save(l.ctx)
  135. if err == nil {
  136. wxSysSet[lagId] = newLabel.ID
  137. }
  138. } else {
  139. // 如果标签存在
  140. wxSysSet[lagId] = label.ID
  141. }
  142. }
  143. // 为新联系人打标签
  144. if len(newFriendIds) > 0 && len(newLagIds) > 0 {
  145. for id, lags := range newFriendIds {
  146. for _, lag := range lags {
  147. l.svcCtx.DB.LabelRelationship.Create().
  148. SetLabelID(wxSysSet[lag]).
  149. SetContactID(id).
  150. SetOrganizationID(organizationId).
  151. Save(l.ctx)
  152. }
  153. }
  154. }
  155. }
  156. }
  157. chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
  158. if chatroomCnt > 0 {
  159. for _, chatroom := range friendAndChatRoomList.Chatroom {
  160. l.svcCtx.DB.Contact.Create().
  161. SetWxWxid(wxInfo.Wxid).
  162. SetType(2).
  163. SetWxid(chatroom.Gid).
  164. SetNickname(chatroom.Gname).
  165. SetGid(chatroom.Gid).
  166. SetGname(chatroom.Gname).
  167. SetMarkname(chatroom.Markname).
  168. SetV3(chatroom.V3).
  169. OnConflict().
  170. UpdateNewValues().
  171. ID(l.ctx)
  172. }
  173. }
  174. ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
  175. if ghCnt > 0 {
  176. for _, gh := range friendAndChatRoomList.Gh {
  177. l.svcCtx.DB.Contact.Create().
  178. SetWxWxid(wxInfo.Wxid).
  179. SetType(3).
  180. SetWxid(gh.Wxid).
  181. SetAccount(gh.Account).
  182. SetNickname(gh.Nickname).
  183. SetMarkname(gh.Markname).
  184. SetV3(gh.V3).
  185. OnConflict().
  186. UpdateNewValues().
  187. ID(l.ctx)
  188. }
  189. }
  190. } else {
  191. // 工作手机版本
  192. hookClient := hook.NewHook("", "", "")
  193. var result GetWechatFriendListResp
  194. res, err := reqv3.C().DevMode().R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWechatFriendList?cid=" + wxInfo.ProcessID + "&wechatid=" + wxInfo.Wxid)
  195. if err != nil {
  196. return nil, err
  197. }
  198. if !res.IsSuccessState() {
  199. err = fmt.Errorf("GetFriendAndChatRoomList failed with status code %d", res.StatusCode)
  200. l.Error("GetWechatFriendList 请求失败", err)
  201. return nil, err
  202. }
  203. _ = hookClient.TriggerChatroomPush(wxInfo.Wxid)
  204. for _, friend := range result.Data {
  205. friendType := 1
  206. if friend.Type == 1 {
  207. friendType = 2
  208. _ = hookClient.RequestChatRoomInfo(friend.Friendid, wxInfo.Wxid)
  209. } else {
  210. friendType = 1
  211. }
  212. _, err = l.svcCtx.DB.Contact.Create().
  213. SetWxWxid(wxInfo.Wxid).
  214. SetType(friendType).
  215. SetWxid(friend.Friendid).
  216. SetAccount(friend.FriendWechatno).
  217. SetNickname(friend.Nickname).
  218. SetHeadimg(friend.Avatar).
  219. SetSex(cast.ToInt(friend.Gender)).
  220. OnConflict().
  221. UpdateNewValues().
  222. SetOrganizationID(organizationId).
  223. ID(l.ctx)
  224. if err != nil {
  225. l.Error("创建联系人失败", err)
  226. continue
  227. }
  228. }
  229. }
  230. resp = &types.BaseMsgResp{
  231. Msg: errormsg.Success,
  232. Code: errorcode.OK,
  233. }
  234. return
  235. }
  236. func splitStringToIntArray(input string) []string {
  237. strArray := strings.Split(input, ",")
  238. intArray := make([]string, len(strArray))
  239. for i, str := range strArray {
  240. intArray[i] = str
  241. }
  242. return intArray
  243. }