123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package Wxhook
- import (
- "context"
- "github.com/spf13/cast"
- "github.com/suyuan32/simple-admin-common/enum/errorcode"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "wechat-api/hook"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GetFriendsAndGroupsLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewGetFriendsAndGroupsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFriendsAndGroupsLogic {
- return &GetFriendsAndGroupsLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
- wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, req.Id)
- if err != nil {
- l.Error("查询微信信息失败", err)
- return
- }
- serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
- if err != nil {
- l.Error("查询服务器信息失败", err)
- return
- }
- hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
- friendAndChatRoomList, err := hookClient.GetFriendAndChatRoomList("0")
- if err != nil {
- l.Error("获取好友列表失败", err)
- return
- }
- friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
- if friendCnt > 0 {
- for _, friend := range friendAndChatRoomList.Friend {
- l.svcCtx.DB.Contact.Create().
- SetWxWxid(wxInfo.Wxid).
- SetType(1).
- SetWxid(friend.Wxid).
- SetAccount(friend.Account).
- SetNickname(friend.Nickname).
- SetMarkname(friend.Markname).
- SetHeadimg(friend.Headimg).
- SetSex(cast.ToInt(friend.Sex)).
- SetStarrole(friend.Starrole).
- SetDontseeit(cast.ToInt(friend.Dontseeit)).
- SetDontseeme(cast.ToInt(friend.Dontseeme)).
- SetLag(friend.Lag).
- SetV3(friend.V3).
- OnConflict().
- UpdateNewValues().
- ID(l.ctx)
- }
- }
- chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
- if chatroomCnt > 0 {
- for _, chatroom := range friendAndChatRoomList.Chatroom {
- l.svcCtx.DB.Contact.Create().
- SetWxWxid(wxInfo.Wxid).
- SetType(2).
- SetWxid(chatroom.Gid).
- SetNickname(chatroom.Gname).
- SetGid(chatroom.Gid).
- SetGname(chatroom.Gname).
- SetMarkname(chatroom.Markname).
- SetV3(chatroom.V3).
- OnConflict().
- UpdateNewValues().
- ID(l.ctx)
- }
- }
- ghCnt := cast.ToInt(friendAndChatRoomList.CountGh)
- if ghCnt > 0 {
- for _, gh := range friendAndChatRoomList.Gh {
- l.svcCtx.DB.Contact.Create().
- SetWxWxid(wxInfo.Wxid).
- SetType(3).
- SetWxid(gh.Wxid).
- SetAccount(gh.Account).
- SetNickname(gh.Nickname).
- SetMarkname(gh.Markname).
- SetV3(gh.V3).
- OnConflict().
- UpdateNewValues().
- ID(l.ctx)
- }
- }
- resp = &types.BaseMsgResp{
- Msg: errormsg.Success,
- Code: errorcode.OK,
- }
- return
- }
|