get_friends_and_groups_logic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "wechat-api/hook"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type GetFriendsAndGroupsLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewGetFriendsAndGroupsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFriendsAndGroupsLogic {
  18. return &GetFriendsAndGroupsLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
  24. organizationId := l.ctx.Value("organizationId").(uint64)
  25. wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, req.Id)
  26. if err != nil {
  27. l.Error("查询微信信息失败", err)
  28. return
  29. }
  30. serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
  31. if err != nil {
  32. l.Error("查询服务器信息失败", err)
  33. return
  34. }
  35. hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
  36. friendAndChatRoomList, err := hookClient.GetFriendAndChatRoomList("0")
  37. if err != nil {
  38. l.Error("获取好友列表失败", err)
  39. return
  40. }
  41. friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
  42. if friendCnt > 0 {
  43. for _, friend := range friendAndChatRoomList.Friend {
  44. l.svcCtx.DB.Contact.Create().
  45. SetWxWxid(wxInfo.Wxid).
  46. SetType(1).
  47. SetWxid(friend.Wxid).
  48. SetAccount(friend.Account).
  49. SetNickname(friend.Nickname).
  50. SetMarkname(friend.Markname).
  51. SetHeadimg(friend.Headimg).
  52. SetSex(cast.ToInt(friend.Sex)).
  53. SetStarrole(friend.Starrole).
  54. SetDontseeit(cast.ToInt(friend.Dontseeit)).
  55. SetDontseeme(cast.ToInt(friend.Dontseeme)).
  56. SetLag(friend.Lag).
  57. SetV3(friend.V3).
  58. OnConflict().
  59. UpdateNewValues().
  60. SetOrganizationID(organizationId).
  61. ID(l.ctx)
  62. }
  63. }
  64. chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
  65. if chatroomCnt > 0 {
  66. for _, chatroom := range friendAndChatRoomList.Chatroom {
  67. l.svcCtx.DB.Contact.Create().
  68. SetWxWxid(wxInfo.Wxid).
  69. SetType(2).
  70. SetWxid(chatroom.Gid).
  71. SetNickname(chatroom.Gname).
  72. SetGid(chatroom.Gid).
  73. SetGname(chatroom.Gname).
  74. SetMarkname(chatroom.Markname).
  75. SetV3(chatroom.V3).
  76. OnConflict().
  77. UpdateNewValues().
  78. ID(l.ctx)
  79. }
  80. }
  81. ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
  82. if ghCnt > 0 {
  83. for _, gh := range friendAndChatRoomList.Gh {
  84. l.svcCtx.DB.Contact.Create().
  85. SetWxWxid(wxInfo.Wxid).
  86. SetType(3).
  87. SetWxid(gh.Wxid).
  88. SetAccount(gh.Account).
  89. SetNickname(gh.Nickname).
  90. SetMarkname(gh.Markname).
  91. SetV3(gh.V3).
  92. OnConflict().
  93. UpdateNewValues().
  94. ID(l.ctx)
  95. }
  96. }
  97. resp = &types.BaseMsgResp{
  98. Msg: errormsg.Success,
  99. Code: errorcode.OK,
  100. }
  101. return
  102. }