get_friends_and_groups_logic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, req.Id)
  25. if err != nil {
  26. l.Error("查询微信信息失败", err)
  27. return
  28. }
  29. serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
  30. if err != nil {
  31. l.Error("查询服务器信息失败", err)
  32. return
  33. }
  34. hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
  35. friendAndChatRoomList, err := hookClient.GetFriendAndChatRoomList("0")
  36. if err != nil {
  37. l.Error("获取好友列表失败", err)
  38. return
  39. }
  40. friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
  41. if friendCnt > 0 {
  42. for _, friend := range friendAndChatRoomList.Friend {
  43. l.svcCtx.DB.Contact.Create().
  44. SetWxWxid(wxInfo.Wxid).
  45. SetType(1).
  46. SetWxid(friend.Wxid).
  47. SetAccount(friend.Account).
  48. SetNickname(friend.Nickname).
  49. SetMarkname(friend.Markname).
  50. SetHeadimg(friend.Headimg).
  51. SetSex(cast.ToInt(friend.Sex)).
  52. SetStarrole(friend.Starrole).
  53. SetDontseeit(cast.ToInt(friend.Dontseeit)).
  54. SetDontseeme(cast.ToInt(friend.Dontseeme)).
  55. SetLag(friend.Lag).
  56. SetV3(friend.V3).
  57. OnConflict().
  58. UpdateNewValues().
  59. ID(l.ctx)
  60. }
  61. }
  62. chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
  63. if chatroomCnt > 0 {
  64. for _, chatroom := range friendAndChatRoomList.Chatroom {
  65. l.svcCtx.DB.Contact.Create().
  66. SetWxWxid(wxInfo.Wxid).
  67. SetType(2).
  68. SetWxid(chatroom.Gid).
  69. SetNickname(chatroom.Gname).
  70. SetGid(chatroom.Gid).
  71. SetGname(chatroom.Gname).
  72. SetMarkname(chatroom.Markname).
  73. SetV3(chatroom.V3).
  74. OnConflict().
  75. UpdateNewValues().
  76. ID(l.ctx)
  77. }
  78. }
  79. ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
  80. if ghCnt > 0 {
  81. for _, gh := range friendAndChatRoomList.Gh {
  82. l.svcCtx.DB.Contact.Create().
  83. SetWxWxid(wxInfo.Wxid).
  84. SetType(3).
  85. SetWxid(gh.Wxid).
  86. SetAccount(gh.Account).
  87. SetNickname(gh.Nickname).
  88. SetMarkname(gh.Markname).
  89. SetV3(gh.V3).
  90. OnConflict().
  91. UpdateNewValues().
  92. ID(l.ctx)
  93. }
  94. }
  95. resp = &types.BaseMsgResp{
  96. Msg: errormsg.Success,
  97. Code: errorcode.OK,
  98. }
  99. return
  100. }