get_friends_and_groups_logic.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package Wxhook
  2. import (
  3. "context"
  4. "github.com/spf13/cast"
  5. "github.com/suyuan32/simple-admin-common/enum/errorcode"
  6. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  7. "strings"
  8. "wechat-api/ent/label"
  9. "wechat-api/hook"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetFriendsAndGroupsLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewGetFriendsAndGroupsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFriendsAndGroupsLogic {
  20. return &GetFriendsAndGroupsLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, req.Id)
  28. if err != nil {
  29. l.Error("查询微信信息失败", err)
  30. return
  31. }
  32. serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
  33. if err != nil {
  34. l.Error("查询服务器信息失败", err)
  35. return
  36. }
  37. hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
  38. friendAndChatRoomList, err := hookClient.GetFriendAndChatRoomList("0")
  39. if err != nil {
  40. l.Error("获取好友列表失败", err)
  41. return
  42. }
  43. newFriendIds := make(map[uint64][]string)
  44. newLagIds := make(map[string]struct{})
  45. friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
  46. if friendCnt > 0 {
  47. for _, friend := range friendAndChatRoomList.Friend {
  48. id, err := l.svcCtx.DB.Contact.Create().
  49. SetWxWxid(wxInfo.Wxid).
  50. SetType(1).
  51. SetWxid(friend.Wxid).
  52. SetAccount(friend.Account).
  53. SetNickname(friend.Nickname).
  54. SetMarkname(friend.Markname).
  55. SetHeadimg(friend.Headimg).
  56. SetSex(cast.ToInt(friend.Sex)).
  57. SetStarrole(friend.Starrole).
  58. SetDontseeit(cast.ToInt(friend.Dontseeit)).
  59. SetDontseeme(cast.ToInt(friend.Dontseeme)).
  60. SetLag(friend.Lag).
  61. SetV3(friend.V3).
  62. OnConflict().
  63. UpdateNewValues().
  64. SetOrganizationID(organizationId).
  65. ID(l.ctx)
  66. if err == nil {
  67. lags := splitStringToIntArray(friend.Lag)
  68. newFriendIds[id] = lags
  69. for _, lag := range lags {
  70. if _, exists := newLagIds[lag]; !exists {
  71. newLagIds[lag] = struct{}{}
  72. }
  73. }
  74. }
  75. }
  76. }
  77. // 平台和微信标签映射
  78. if len(newLagIds) > 0 {
  79. wxLagIdsSet := make(map[string]string)
  80. wxLagIds, err := hookClient.GetContactLabelList()
  81. if err == nil {
  82. wxSysSet := make(map[string]uint64)
  83. for _, wxLagId := range wxLagIds.Label {
  84. wxLagIdsSet[wxLagId.Id] = wxLagId.Name
  85. }
  86. for lagId := range newLagIds {
  87. name := wxLagIdsSet[lagId]
  88. label, err := l.svcCtx.DB.Label.Query().
  89. Where(
  90. label.NameEQ(name), // Filter by ID
  91. label.TypeEQ(1),
  92. label.OrganizationID(organizationId), // Additional filter by organizationId
  93. ).
  94. Only(l.ctx)
  95. if err != nil {
  96. // 如果标签不存在则创建
  97. newLabel, err := l.svcCtx.DB.Label.Create().
  98. SetType(1).
  99. SetName(name).
  100. SetFrom(2).
  101. SetMode(2).
  102. SetConditions("{}").
  103. SetOrganizationID(organizationId).
  104. Save(l.ctx)
  105. if err == nil {
  106. wxSysSet[lagId] = newLabel.ID
  107. }
  108. } else {
  109. // 如果标签存在
  110. wxSysSet[lagId] = label.ID
  111. }
  112. }
  113. // 为新联系人打标签
  114. if len(newFriendIds) > 0 && len(newLagIds) > 0 {
  115. for id, lags := range newFriendIds {
  116. for _, lag := range lags {
  117. l.svcCtx.DB.LabelRelationship.Create().
  118. SetLabelID(wxSysSet[lag]).
  119. SetContactID(id).
  120. SetOrganizationID(organizationId).
  121. Save(l.ctx)
  122. }
  123. }
  124. }
  125. }
  126. }
  127. chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
  128. if chatroomCnt > 0 {
  129. for _, chatroom := range friendAndChatRoomList.Chatroom {
  130. l.svcCtx.DB.Contact.Create().
  131. SetWxWxid(wxInfo.Wxid).
  132. SetType(2).
  133. SetWxid(chatroom.Gid).
  134. SetNickname(chatroom.Gname).
  135. SetGid(chatroom.Gid).
  136. SetGname(chatroom.Gname).
  137. SetMarkname(chatroom.Markname).
  138. SetV3(chatroom.V3).
  139. OnConflict().
  140. UpdateNewValues().
  141. ID(l.ctx)
  142. }
  143. }
  144. ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
  145. if ghCnt > 0 {
  146. for _, gh := range friendAndChatRoomList.Gh {
  147. l.svcCtx.DB.Contact.Create().
  148. SetWxWxid(wxInfo.Wxid).
  149. SetType(3).
  150. SetWxid(gh.Wxid).
  151. SetAccount(gh.Account).
  152. SetNickname(gh.Nickname).
  153. SetMarkname(gh.Markname).
  154. SetV3(gh.V3).
  155. OnConflict().
  156. UpdateNewValues().
  157. ID(l.ctx)
  158. }
  159. }
  160. resp = &types.BaseMsgResp{
  161. Msg: errormsg.Success,
  162. Code: errorcode.OK,
  163. }
  164. return
  165. }
  166. func splitStringToIntArray(input string) []string {
  167. strArray := strings.Split(input, ",")
  168. intArray := make([]string, len(strArray))
  169. for i, str := range strArray {
  170. intArray[i] = str
  171. }
  172. return intArray
  173. }