get_friends_and_groups_logic.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package Wxhook
  2. import (
  3. "context"
  4. "encoding/json"
  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. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetFriendsAndGroupsLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewGetFriendsAndGroupsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFriendsAndGroupsLogic {
  21. return &GetFriendsAndGroupsLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. type GetWechatFriendListResp struct {
  27. Code int64 `json:"code"`
  28. Data []struct {
  29. Avatar string `json:"avatar"`
  30. Cid int64 `json:"cid"`
  31. City string `json:"city"`
  32. Country string `json:"country"`
  33. CreateTime int64 `json:"create_time"`
  34. FriendWechatno string `json:"friend_wechatno"`
  35. Friendid string `json:"friendid"`
  36. Gender int64 `json:"gender"`
  37. ID int64 `json:"id"`
  38. Memo string `json:"memo"`
  39. ModifyTime int64 `json:"modify_time"`
  40. Nickname string `json:"nickname"`
  41. Phone string `json:"phone"`
  42. Province string `json:"province"`
  43. Remark string `json:"remark"`
  44. Type int64 `json:"type"`
  45. Wechatid string `json:"wechatid"`
  46. } `json:"data"`
  47. Msg string `json:"msg"`
  48. }
  49. func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
  50. //organizationId := l.ctx.Value("organizationId").(uint64)
  51. wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, req.Id)
  52. if err != nil || wxInfo == nil {
  53. l.Error("查询微信信息失败", err)
  54. return
  55. }
  56. organizationId := wxInfo.OrganizationID
  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. SetOrganizationID(organizationId).
  90. OnConflict().
  91. UpdateNewValues().
  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. label_id := wxSysSet[lag]
  148. if label_id != 0 {
  149. l.svcCtx.DB.LabelRelationship.Create().
  150. SetLabelID(label_id).
  151. SetContactID(id).
  152. SetOrganizationID(organizationId).
  153. Save(l.ctx)
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
  161. if chatroomCnt > 0 {
  162. for _, chatroom := range friendAndChatRoomList.Chatroom {
  163. l.svcCtx.DB.Contact.Create().
  164. SetWxWxid(wxInfo.Wxid).
  165. SetType(2).
  166. SetWxid(chatroom.Gid).
  167. SetNickname(chatroom.Gname).
  168. SetGid(chatroom.Gid).
  169. SetGname(chatroom.Gname).
  170. SetMarkname(chatroom.Markname).
  171. SetV3(chatroom.V3).
  172. SetOrganizationID(organizationId).
  173. OnConflict().
  174. UpdateNewValues().
  175. ID(l.ctx)
  176. }
  177. }
  178. ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
  179. if ghCnt > 0 {
  180. for _, gh := range friendAndChatRoomList.Gh {
  181. l.svcCtx.DB.Contact.Create().
  182. SetWxWxid(wxInfo.Wxid).
  183. SetType(3).
  184. SetWxid(gh.Wxid).
  185. SetAccount(gh.Account).
  186. SetNickname(gh.Nickname).
  187. SetMarkname(gh.Markname).
  188. SetV3(gh.V3).
  189. SetOrganizationID(organizationId).
  190. OnConflict().
  191. UpdateNewValues().
  192. ID(l.ctx)
  193. }
  194. }
  195. } else {
  196. // 工作手机版本
  197. var hookClient *hook.Hook
  198. //var result GetWechatFriendListResp
  199. //var wsUrl string
  200. if wxInfo.Ctype == uint64(3) {
  201. hookClient = hook.NewWecomHook("", "", "")
  202. _ = hookClient.TriggerCustomerPushTask(wxInfo.Wxid)
  203. _ = hookClient.TriggerConversationPushTask(wxInfo.Wxid)
  204. } else {
  205. data := map[string]interface{}{
  206. "MsgType": "TriggerFriendPushTask",
  207. "Content": map[string]interface{}{
  208. "WeChatId": wxInfo.Wxid,
  209. },
  210. }
  211. jsonStr, err := json.Marshal(data)
  212. err = l.svcCtx.WechatWs["default"].SendMsg([]byte(jsonStr))
  213. if err != nil {
  214. logx.Error("syncWx: ", err)
  215. return nil, err
  216. }
  217. dataChatroom := map[string]interface{}{
  218. "MsgType": "TriggerChatroomPushTask",
  219. "Content": map[string]interface{}{
  220. "WeChatId": wxInfo.Wxid,
  221. "Flag": 1,
  222. },
  223. }
  224. jsonStrChatroom, err := json.Marshal(dataChatroom)
  225. err = l.svcCtx.WechatWs["default"].SendMsg([]byte(jsonStrChatroom))
  226. //hookClient = hook.NewHook("", "", "")
  227. //wsUrl = "http://chat.gkscrm.com:13086/pc/GetWechatFriendList?cid=" + wxInfo.ProcessID + "&wechatid=" + wxInfo.Wxid
  228. //
  229. //res, err := reqv3.C().DevMode().R().SetSuccessResult(&result).Post(wsUrl)
  230. //if err != nil {
  231. // return nil, err
  232. //}
  233. //if !res.IsSuccessState() {
  234. // err = fmt.Errorf("GetFriendAndChatRoomList failed with status code %d", res.StatusCode)
  235. // l.Error("GetWechatFriendList 请求失败", err)
  236. // return nil, err
  237. //}
  238. //
  239. //_ = hookClient.TriggerChatroomPush(wxInfo.Wxid)
  240. //
  241. //for _, friend := range result.Data {
  242. // friendType := 1
  243. // if friend.Type == 1 {
  244. // friendType = 2
  245. // _ = hookClient.RequestChatRoomInfo(friend.Friendid, wxInfo.Wxid)
  246. // } else {
  247. // friendType = 1
  248. // }
  249. //
  250. // _, err = l.svcCtx.DB.Contact.Create().
  251. // SetWxWxid(wxInfo.Wxid).
  252. // SetType(friendType).
  253. // SetWxid(friend.Friendid).
  254. // SetAccount(friend.FriendWechatno).
  255. // SetNickname(friend.Nickname).
  256. // SetMarkname(friend.Memo).
  257. // SetHeadimg(friend.Avatar).
  258. // SetSex(cast.ToInt(friend.Gender)).
  259. // SetOrganizationID(organizationId).
  260. // OnConflict().
  261. // UpdateNewValues().
  262. // SetOrganizationID(organizationId).
  263. // ID(l.ctx)
  264. //
  265. // if err != nil {
  266. // l.Error("创建联系人失败", err)
  267. // continue
  268. // }
  269. //}
  270. }
  271. }
  272. resp = &types.BaseMsgResp{
  273. Msg: errormsg.Success,
  274. Code: errorcode.OK,
  275. }
  276. return
  277. }
  278. func splitStringToIntArray(input string) []string {
  279. strArray := strings.Split(input, ",")
  280. intArray := make([]string, len(strArray))
  281. for i, str := range strArray {
  282. intArray[i] = str
  283. }
  284. return intArray
  285. }