2023-11-29 10:26:02 +08:00
|
|
|
package friends
|
2023-09-21 17:33:59 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/go-resty/resty/v2"
|
2023-09-22 08:39:09 +08:00
|
|
|
"go-wechat/client"
|
2023-11-30 11:37:02 +08:00
|
|
|
"go-wechat/common/constant"
|
2023-10-26 10:07:08 +08:00
|
|
|
"go-wechat/config"
|
2023-09-21 17:33:59 +08:00
|
|
|
"go-wechat/entity"
|
|
|
|
"go-wechat/model"
|
2023-09-22 08:39:09 +08:00
|
|
|
"gorm.io/gorm"
|
2023-09-21 17:33:59 +08:00
|
|
|
"log"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
2023-10-11 14:46:47 +08:00
|
|
|
"time"
|
2023-09-21 17:33:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 同步群成员
|
|
|
|
|
2023-09-26 14:29:41 +08:00
|
|
|
// http客户端
|
|
|
|
var hc = resty.New()
|
|
|
|
|
2023-11-29 10:26:02 +08:00
|
|
|
// Sync
|
2023-09-21 17:33:59 +08:00
|
|
|
// @description: 同步好友列表
|
2023-11-29 10:26:02 +08:00
|
|
|
func Sync() {
|
2023-09-22 08:39:09 +08:00
|
|
|
var base model.Response[[]model.FriendItem]
|
2023-09-21 17:33:59 +08:00
|
|
|
|
2023-09-26 14:29:41 +08:00
|
|
|
resp, err := hc.R().
|
2023-09-21 17:33:59 +08:00
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
|
|
|
SetResult(&base).
|
2023-10-26 10:07:08 +08:00
|
|
|
Post(config.Conf.Wechat.GetURL("/api/getContactList"))
|
2023-09-21 17:33:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("获取好友列表失败: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("获取好友列表结果: %s", resp.String())
|
2023-09-22 08:39:09 +08:00
|
|
|
|
|
|
|
tx := client.MySQL.Begin()
|
|
|
|
defer tx.Commit()
|
|
|
|
|
2023-11-30 11:37:02 +08:00
|
|
|
nowIds := []string{}
|
|
|
|
|
2023-09-21 17:33:59 +08:00
|
|
|
for _, friend := range base.Data {
|
|
|
|
if strings.Contains(friend.Wxid, "gh_") || strings.Contains(friend.Wxid, "@openim") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// 特殊Id跳过
|
|
|
|
if slices.Contains(constant.SpecialId, friend.Wxid) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("昵称: %s -> 类型: %d -> 微信号: %s -> 微信原始Id: %s", friend.Nickname, friend.Type, friend.CustomAccount, friend.Wxid)
|
2023-11-30 11:37:02 +08:00
|
|
|
nowIds = append(nowIds, friend.Wxid)
|
2023-09-21 17:33:59 +08:00
|
|
|
|
2023-09-22 08:39:09 +08:00
|
|
|
// 判断是否存在,不存在的话就新增,存在就修改一下名字
|
|
|
|
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,
|
2023-12-04 14:12:15 +08:00
|
|
|
IsOk: true,
|
2023-09-22 08:39:09 +08:00
|
|
|
}).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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 17:33:59 +08:00
|
|
|
// 群成员,同步一下成员信息
|
|
|
|
if strings.Contains(friend.Wxid, "@chatroom") {
|
2023-09-22 08:39:09 +08:00
|
|
|
syncGroupUsers(tx, friend.Wxid)
|
2023-09-21 17:33:59 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-22 08:39:09 +08:00
|
|
|
|
2023-11-30 11:37:02 +08:00
|
|
|
// 清理不在列表中的好友
|
|
|
|
err = tx.Model(&entity.Friend{}).Where("wxid NOT IN (?)", nowIds).Update("is_ok", false).Error
|
|
|
|
|
2023-09-22 08:39:09 +08:00
|
|
|
log.Println("同步好友列表完成")
|
2023-09-21 17:33:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// syncGroupUsers
|
|
|
|
// @description: 同步群成员
|
|
|
|
// @param gid
|
2023-09-22 08:39:09 +08:00
|
|
|
func syncGroupUsers(tx *gorm.DB, gid string) {
|
2023-09-21 17:33:59 +08:00
|
|
|
var baseResp model.Response[model.GroupUser]
|
|
|
|
|
|
|
|
// 组装参数
|
|
|
|
param := map[string]any{
|
|
|
|
"chatRoomId": gid, // 群Id
|
|
|
|
}
|
|
|
|
pbs, _ := json.Marshal(param)
|
|
|
|
|
2023-09-26 14:29:41 +08:00
|
|
|
_, err := hc.R().
|
2023-09-21 17:33:59 +08:00
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
|
|
|
SetBody(string(pbs)).
|
|
|
|
SetResult(&baseResp).
|
2023-10-26 10:07:08 +08:00
|
|
|
Post(config.Conf.Wechat.GetURL("/api/getMemberFromChatRoom"))
|
2023-09-21 17:33:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("获取群成员信息失败: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 昵称Id
|
|
|
|
wxIds := strings.Split(baseResp.Data.Members, "^G")
|
|
|
|
log.Printf(" 群成员数: %d", len(wxIds))
|
2023-09-22 08:39:09 +08:00
|
|
|
|
|
|
|
// 修改不在数组的群成员状态为不在
|
2023-10-11 14:46:47 +08:00
|
|
|
pm := map[string]any{
|
|
|
|
"is_member": false,
|
|
|
|
"leave_time": time.Now().Local(),
|
|
|
|
}
|
2023-10-12 14:12:33 +08:00
|
|
|
err = tx.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Where("is_member IS TRUE").Where("wxid NOT IN (?)", wxIds).Updates(pm).Error
|
2023-09-22 08:39:09 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("修改群成员状态失败: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-21 17:33:59 +08:00
|
|
|
for _, wxid := range wxIds {
|
|
|
|
// 获取成员信息
|
|
|
|
cp, _ := getContactProfile(wxid)
|
|
|
|
if cp.Wxid != "" {
|
|
|
|
log.Printf(" 微信Id: %s -> 昵称: %s -> 微信号: %s", wxid, cp.Nickname, cp.Account)
|
2023-09-22 08:39:09 +08:00
|
|
|
// 查询成员是否存在,不在就新增,否则修改
|
|
|
|
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,
|
2023-12-01 10:22:01 +08:00
|
|
|
JoinTime: time.Now().Local(),
|
2023-09-22 08:39:09 +08:00
|
|
|
}).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,
|
2023-10-16 08:45:00 +08:00
|
|
|
"is_member": true,
|
|
|
|
"leave_time": nil,
|
2023-09-22 08:39:09 +08:00
|
|
|
}
|
|
|
|
err = tx.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Where("wxid = ?", wxid).Updates(pm).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("修改群成员失败: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 17:33:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getContactProfile
|
|
|
|
// @description: 获取成员详情
|
|
|
|
// @param wxid
|
|
|
|
// @return ent
|
|
|
|
// @return err
|
|
|
|
func getContactProfile(wxid string) (ent model.ContactProfile, err error) {
|
|
|
|
var baseResp model.Response[model.ContactProfile]
|
|
|
|
|
|
|
|
// 组装参数
|
|
|
|
param := map[string]any{
|
|
|
|
"wxid": wxid, // 群Id
|
|
|
|
}
|
|
|
|
pbs, _ := json.Marshal(param)
|
|
|
|
|
2023-09-26 14:29:41 +08:00
|
|
|
_, err = hc.R().
|
2023-09-21 17:33:59 +08:00
|
|
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
|
|
|
SetBody(string(pbs)).
|
|
|
|
SetResult(&baseResp).
|
2023-10-26 10:07:08 +08:00
|
|
|
Post(config.Conf.Wechat.GetURL("/api/getContactProfile"))
|
2023-09-21 17:33:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("获取成员详情失败: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ent = baseResp.Data
|
|
|
|
return
|
|
|
|
}
|