get_friends_and_groups_logic.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. if lagId == "" {
  88. continue
  89. }
  90. name := wxLagIdsSet[lagId]
  91. label, err := l.svcCtx.DB.Label.Query().
  92. Where(
  93. label.NameEQ(name), // Filter by ID
  94. label.TypeEQ(1),
  95. label.OrganizationID(organizationId), // Additional filter by organizationId
  96. ).
  97. Only(l.ctx)
  98. if err != nil {
  99. // 如果标签不存在则创建
  100. newLabel, err := l.svcCtx.DB.Label.Create().
  101. SetType(1).
  102. SetName(name).
  103. SetFrom(2).
  104. SetMode(2).
  105. SetConditions("{}").
  106. SetOrganizationID(organizationId).
  107. Save(l.ctx)
  108. if err == nil {
  109. wxSysSet[lagId] = newLabel.ID
  110. }
  111. } else {
  112. // 如果标签存在
  113. wxSysSet[lagId] = label.ID
  114. }
  115. }
  116. // 为新联系人打标签
  117. if len(newFriendIds) > 0 && len(newLagIds) > 0 {
  118. for id, lags := range newFriendIds {
  119. for _, lag := range lags {
  120. l.svcCtx.DB.LabelRelationship.Create().
  121. SetLabelID(wxSysSet[lag]).
  122. SetContactID(id).
  123. SetOrganizationID(organizationId).
  124. Save(l.ctx)
  125. }
  126. }
  127. }
  128. }
  129. }
  130. chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
  131. if chatroomCnt > 0 {
  132. for _, chatroom := range friendAndChatRoomList.Chatroom {
  133. l.svcCtx.DB.Contact.Create().
  134. SetWxWxid(wxInfo.Wxid).
  135. SetType(2).
  136. SetWxid(chatroom.Gid).
  137. SetNickname(chatroom.Gname).
  138. SetGid(chatroom.Gid).
  139. SetGname(chatroom.Gname).
  140. SetMarkname(chatroom.Markname).
  141. SetV3(chatroom.V3).
  142. OnConflict().
  143. UpdateNewValues().
  144. ID(l.ctx)
  145. }
  146. }
  147. ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
  148. if ghCnt > 0 {
  149. for _, gh := range friendAndChatRoomList.Gh {
  150. l.svcCtx.DB.Contact.Create().
  151. SetWxWxid(wxInfo.Wxid).
  152. SetType(3).
  153. SetWxid(gh.Wxid).
  154. SetAccount(gh.Account).
  155. SetNickname(gh.Nickname).
  156. SetMarkname(gh.Markname).
  157. SetV3(gh.V3).
  158. OnConflict().
  159. UpdateNewValues().
  160. ID(l.ctx)
  161. }
  162. }
  163. resp = &types.BaseMsgResp{
  164. Msg: errormsg.Success,
  165. Code: errorcode.OK,
  166. }
  167. return
  168. }
  169. func splitStringToIntArray(input string) []string {
  170. strArray := strings.Split(input, ",")
  171. intArray := make([]string, len(strArray))
  172. for i, str := range strArray {
  173. intArray[i] = str
  174. }
  175. return intArray
  176. }