From 3e8051906f6e108629cc9ad0f48b45ef574dbfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Fri, 22 Sep 2023 08:39:09 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=A5=BD=E5=8F=8B=E5=88=97=E8=A1=A8=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entity/friend.go | 20 +++++++--- tasks/friends.go | 101 ++++++++++++++++++++++++++++++++++++++++++----- tasks/tasks.go | 1 + 3 files changed, 107 insertions(+), 15 deletions(-) diff --git a/entity/friend.go b/entity/friend.go index c03ca1c6..7935a756 100644 --- a/entity/friend.go +++ b/entity/friend.go @@ -4,17 +4,27 @@ package entity // @description: 好友列表 type Friend struct { CustomAccount string `json:"customAccount"` // 微信号 - EncryptName string `json:"encryptName"` // 不知道 Nickname string `json:"nickname"` // 昵称 Pinyin string `json:"pinyin"` // 昵称拼音大写首字母 PinyinAll string `json:"pinyinAll"` // 昵称全拼 - Reserved1 int `json:"reserved1"` // 未知 - Reserved2 int `json:"reserved2"` // 未知 - Type int `json:"type"` // 类型 - VerifyFlag int `json:"verifyFlag"` // 未知 Wxid string `json:"wxid"` // 微信原始Id } func (Friend) TableName() string { return "t_friend" } + +// GroupUser +// @description: 群成员 +type GroupUser struct { + GroupId string `json:"groupId"` // 群Id + Account string `json:"account"` // 账号 + HeadImage string `json:"headImage"` // 头像 + Nickname string `json:"nickname"` // 昵称 + Wxid string `json:"wxid"` // 微信Id + IsMember bool `json:"isMember" gorm:"type:tinyint(1)"` // 是否群成员 +} + +func (GroupUser) TableName() string { + return "t_group_user" +} diff --git a/tasks/friends.go b/tasks/friends.go index aa432c1d..e898739c 100644 --- a/tasks/friends.go +++ b/tasks/friends.go @@ -3,9 +3,11 @@ package tasks import ( "encoding/json" "github.com/go-resty/resty/v2" + "go-wechat/client" "go-wechat/constant" "go-wechat/entity" "go-wechat/model" + "gorm.io/gorm" "log" "slices" "strings" @@ -16,10 +18,10 @@ import ( // syncFriends // @description: 同步好友列表 func syncFriends() { - var base model.Response[[]entity.Friend] + var base model.Response[[]model.FriendItem] - client := resty.New() - resp, err := client.R(). + res := resty.New() + resp, err := res.R(). SetHeader("Content-Type", "application/json;chartset=utf-8"). SetResult(&base). Post("http://10.0.0.73:19088/api/getContactList") @@ -28,6 +30,10 @@ func syncFriends() { return } log.Printf("获取好友列表结果: %s", resp.String()) + + tx := client.MySQL.Begin() + defer tx.Commit() + for _, friend := range base.Data { if strings.Contains(friend.Wxid, "gh_") || strings.Contains(friend.Wxid, "@openim") { continue @@ -38,18 +44,52 @@ func syncFriends() { } log.Printf("昵称: %s -> 类型: %d -> 微信号: %s -> 微信原始Id: %s", friend.Nickname, friend.Type, friend.CustomAccount, friend.Wxid) - // 群成员,同步一下成员信息 - if strings.Contains(friend.Wxid, "@chatroom") { - syncGroupUsers(friend.Wxid) + // 判断是否存在,不存在的话就新增,存在就修改一下名字 + var count int64 + err = tx.Model(&entity.Friend{}).Where("wxid = ?", friend.Wxid).Count(&count).Error + if err != nil { + continue + } + if count == 0 { + // 新增 + err = tx.Create(&entity.Friend{ + CustomAccount: friend.CustomAccount, + Nickname: friend.Nickname, + Pinyin: friend.Pinyin, + PinyinAll: friend.PinyinAll, + Wxid: friend.Wxid, + }).Error + if err != nil { + log.Printf("新增好友失败: %s", err.Error()) + continue + } + } else { + pm := map[string]any{ + "nickname": friend.Nickname, + "custom_account": friend.CustomAccount, + "pinyin": friend.Pinyin, + "pinyin_all": friend.PinyinAll, + } + err = tx.Model(&entity.Friend{}).Where("wxid = ?", friend.Wxid).Updates(pm).Error + if err != nil { + log.Printf("修改好友失败: %s", err.Error()) + continue + } } + // 群成员,同步一下成员信息 + if strings.Contains(friend.Wxid, "@chatroom") { + syncGroupUsers(tx, friend.Wxid) + } } + + log.Println("同步好友列表完成") } // syncGroupUsers // @description: 同步群成员 // @param gid -func syncGroupUsers(gid string) { +func syncGroupUsers(tx *gorm.DB, gid string) { var baseResp model.Response[model.GroupUser] // 组装参数 @@ -58,8 +98,8 @@ func syncGroupUsers(gid string) { } pbs, _ := json.Marshal(param) - client := resty.New() - _, err := client.R(). + res := resty.New() + _, err := res.R(). SetHeader("Content-Type", "application/json;chartset=utf-8"). SetBody(string(pbs)). SetResult(&baseResp). @@ -71,13 +111,54 @@ func syncGroupUsers(gid string) { // 昵称Id wxIds := strings.Split(baseResp.Data.Members, "^G") - log.Printf(" 群成员数: %d", len(wxIds)) + + // 修改不在数组的群成员状态为不在 + err = tx.Model(&entity.GroupUser{}).Where("wxid NOT IN (?)", wxIds).Update("is_member", false).Error + if err != nil { + log.Printf("修改群成员状态失败: %s", err.Error()) + return + } + for _, wxid := range wxIds { // 获取成员信息 cp, _ := getContactProfile(wxid) if cp.Wxid != "" { log.Printf(" 微信Id: %s -> 昵称: %s -> 微信号: %s", wxid, cp.Nickname, cp.Account) + // 查询成员是否存在,不在就新增,否则修改 + var count int64 + err = tx.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Where("wxid = ?", wxid).Count(&count).Error + if err != nil { + log.Printf("查询群成员失败: %s", err.Error()) + continue + } + if count == 0 { + // 新增 + err = tx.Create(&entity.GroupUser{ + GroupId: gid, + Account: cp.Account, + HeadImage: cp.HeadImage, + Nickname: cp.Nickname, + Wxid: cp.Wxid, + IsMember: true, + }).Error + if err != nil { + log.Printf("新增群成员失败: %s", err.Error()) + continue + } + } else { + // 修改 + pm := map[string]any{ + "account": cp.Account, + "head_image": cp.HeadImage, + "nickname": cp.Nickname, + } + err = tx.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Where("wxid = ?", wxid).Updates(pm).Error + if err != nil { + log.Printf("修改群成员失败: %s", err.Error()) + continue + } + } } } } diff --git a/tasks/tasks.go b/tasks/tasks.go index 578252b2..501fd335 100644 --- a/tasks/tasks.go +++ b/tasks/tasks.go @@ -16,6 +16,7 @@ func InitTasks() { _, _ = s.Every(1).Day().At("09:30").Do(yesterday) // 每小时更新一次好友列表 + //_, _ = s.Every(5).Minute().Do(syncFriends) _, _ = s.Every(1).Hour().Do(syncFriends) // 开启定时任务