Compare commits

..

12 Commits

Author SHA1 Message Date
李寻欢
a9f6c9ff0d Merge pull request '🐛 Fix a bug.' (#20) from hotfix into main
Reviewed-on: https://gitee.ltd/lxh/go-wxhelper/pulls/20
2024-01-26 12:00:53 +08:00
李寻欢
244bff5714 🐛 Fix a bug. 2024-01-26 12:00:29 +08:00
李寻欢
b46271a111 Merge pull request '🐛 Fix a bug.' (#19) from hotfix into main
Reviewed-on: https://gitee.ltd/lxh/go-wxhelper/pulls/19
2024-01-25 16:33:59 +08:00
李寻欢
5d11cc7c8a 🐛 Fix a bug. 2024-01-25 16:33:45 +08:00
李寻欢
c6da056f98 Merge pull request '🎨 逻辑优化,那个网页,大大地提速' (#18) from hotfix into main
Reviewed-on: https://gitee.ltd/lxh/go-wxhelper/pulls/18
2024-01-25 16:27:51 +08:00
李寻欢
61e03a6a7b 🎨 逻辑优化,那个网页,大大地提速 2024-01-25 16:27:12 +08:00
李寻欢
8ff18c3626 Merge pull request '🎨 逻辑优化' (#17) from hotfix into main
Reviewed-on: https://gitee.ltd/lxh/go-wxhelper/pulls/17
2024-01-25 11:46:30 +08:00
李寻欢
6acb3b79ec 🎨 逻辑优化 2024-01-25 11:46:03 +08:00
李寻欢
f895d045a7 Merge pull request '🐛 修复AI开关未生效的BUG' (#16) from hotfix into main
Reviewed-on: https://gitee.ltd/lxh/go-wxhelper/pulls/16
2024-01-23 16:23:52 +08:00
李寻欢
eb2e292e8e 🐛 修复AI开关未生效的BUG 2024-01-23 16:23:07 +08:00
李寻欢
8f378e7fc9 Merge pull request '🔧 新增数据库配置模型' (#15) from hotfix into main
Reviewed-on: https://gitee.ltd/lxh/go-wxhelper/pulls/15
2024-01-22 08:32:00 +08:00
李寻欢
497e17ec62 🔧 新增数据库配置模型 2024-01-22 08:31:38 +08:00
9 changed files with 96 additions and 46 deletions

View File

@@ -7,15 +7,17 @@ import (
// Friend
// @description: 好友列表
type Friend struct {
Wxid string `json:"wxid"` // 微信原始Id
CustomAccount string `json:"customAccount"` // 微信号
Nickname string `json:"nickname"` // 昵称
Pinyin string `json:"pinyin"` // 昵称拼音大写首字母
PinyinAll string `json:"pinyinAll"` // 昵称全拼
EnableAi bool `json:"enableAI" gorm:"type:tinyint(1) default 0 not null"` // 是否使用AI
EnableChatRank bool `json:"enableChatRank" gorm:"type:tinyint(1) default 0 not null"` // 是否使用聊天排行
EnableWelcome bool `json:"enableWelcome" gorm:"type:tinyint(1) default 0 not null"` // 是否启用迎新
IsOk bool `json:"isOk" gorm:"type:tinyint(1) default 0 not null"` // 是否正常
Wxid string `json:"wxid"` // 微信原始Id
CustomAccount string `json:"customAccount"` // 微信号
Nickname string `json:"nickname"` // 昵称
Pinyin string `json:"pinyin"` // 昵称拼音大写首字母
PinyinAll string `json:"pinyinAll"` // 昵称全拼
LastActive time.Time `json:"lastActive"` // 最后活跃时间
EnableAi bool `json:"enableAI" gorm:"type:tinyint(1) default 0 not null"` // 是否使用AI
AiModel string `json:"aiModel"` // AI模型
EnableChatRank bool `json:"enableChatRank" gorm:"type:tinyint(1) default 0 not null"` // 是否使用聊天排行
EnableWelcome bool `json:"enableWelcome" gorm:"type:tinyint(1) default 0 not null"` // 是否启用迎新
IsOk bool `json:"isOk" gorm:"type:tinyint(1) default 0 not null"` // 是否正常
}
func (Friend) TableName() string {
@@ -33,6 +35,7 @@ type GroupUser struct {
IsMember bool `json:"isMember" gorm:"type:tinyint(1) default 0 not null"` // 是否群成员
IsAdmin bool `json:"isAdmin" gorm:"type:tinyint(1) default 0 not null"` // 是否群主
JoinTime time.Time `json:"joinTime"` // 加入时间
LastActive time.Time `json:"lastActive"` // 最后活跃时间
LeaveTime *time.Time `json:"leaveTime"` // 离开时间
SkipChatRank bool `json:"skipChatRank" gorm:"type:tinyint(1) default 0 not null"` // 是否跳过聊天排行
}

View File

@@ -28,9 +28,13 @@ func AI(m *plugin.MessageContext) {
}
// 取出所有启用了AI的好友或群组
var count int64
client.MySQL.Model(&entity.Friend{}).Where("enable_ai IS TRUE").Where("wxid = ?", m.FromUser).Count(&count)
if count < 1 {
var friendInfo entity.Friend
client.MySQL.Where("wxid = ?", m.FromUser).First(&friendInfo)
if friendInfo.Wxid == "" {
return
}
// 判断有没有启用AI
if !friendInfo.EnableAi {
return
}
@@ -93,7 +97,9 @@ func AI(m *plugin.MessageContext) {
// 配置模型
chatModel := openai.GPT3Dot5Turbo0613
if config.Conf.Ai.Model != "" {
if friendInfo.AiModel != "" {
chatModel = friendInfo.AiModel
} else if config.Conf.Ai.Model != "" {
chatModel = config.Conf.Ai.Model
}
@@ -117,6 +123,12 @@ func AI(m *plugin.MessageContext) {
return
}
// 返回消息为空
if resp.Choices[0].Message.Content == "" {
utils.SendMessage(m.FromUser, m.GroupUser, "AI似乎抽风了没有告诉我你需要的回答~", 0)
return
}
// 保存一下AI 返回的消息,消息 Id 使用传入 Id 的负数
var replyMessage entity.Message
replyMessage.MsgId = -m.MsgId

View File

@@ -4,6 +4,7 @@ import (
"go-wechat/client"
"go-wechat/entity"
"go-wechat/vo"
"log"
"strings"
)
@@ -16,10 +17,11 @@ func GetAllFriend() (friends, groups []vo.FriendItem, err error) {
var records []vo.FriendItem
err = client.MySQL.
Table("t_friend AS tf").
Joins("LEFT JOIN t_message AS tm ON tf.wxid = tm.from_user").
Select("tf.*", "MAX(tm.create_at) AS last_active_time").
Group("tf.wxid").
Order("last_active_time DESC").
//Joins("LEFT JOIN t_message AS tm ON tf.wxid = tm.from_user").
//Select("tf.*", "MAX(tm.create_at) AS last_active").
Select("tf.*").
//Group("tf.wxid").
Order("tf.last_active DESC").
Find(&records).Error
if err != nil {
return
@@ -60,3 +62,27 @@ func CheckIsEnableCommand(userId string) (flag bool) {
client.MySQL.Model(&entity.Friend{}).Where("enable_command = 1").Where("wxid = ?", userId).Count(&coo)
return coo > 0
}
// updateLastActive
// @description: 更新最后活跃时间
// @param msg
func updateLastActive(msg entity.Message) {
var err error
// 如果是群,更新群成员最后活跃时间
if strings.HasSuffix(msg.FromUser, "@chatroom") {
err = client.MySQL.Model(&entity.GroupUser{}).
Where("group_id = ?", msg.FromUser).
Where("wxid = ?", msg.GroupUser).
Update("last_active", msg.CreateAt).Error
if err != nil {
log.Printf("更新群成员最后活跃时间失败, 错误信息: %v", err)
}
}
// 更新群或者好友活跃时间
err = client.MySQL.Model(&entity.Friend{}).
Where("wxid = ?", msg.FromUser).
Update("last_active", msg.CreateAt).Error
if err != nil {
log.Printf("更新群或者好友活跃时间失败, 错误信息: %v", err)
}
}

View File

@@ -13,10 +13,9 @@ import (
func GetGroupUsersByGroupId(groupId string) (records []vo.GroupUserItem, err error) {
err = client.MySQL.
Table("t_group_user AS tgu").
Joins("LEFT JOIN t_message AS tm ON tm.from_user = tgu.group_id AND tm.group_user = tgu.wxid").
//Select("tgu.wxid", "tgu.nickname", "tgu.head_image", "tgu.is_member", "tgu.leave_time",
// "tgu.skip_chat_rank", "MAX(tm.create_at) AS last_active_time").
Select("tgu.*", "MAX(tm.create_at) AS last_active_time").
//Joins("LEFT JOIN t_message AS tm ON tm.from_user = tgu.group_id AND tm.group_user = tgu.wxid").
//Select("tgu.*", "MAX(tm.create_at) AS last_active").
Select("tgu.*").
Where("tgu.group_id = ?", groupId).
Group("tgu.group_id, tgu.wxid").
Order("tgu.join_time DESC").

View File

@@ -32,4 +32,10 @@ func SaveMessage(msg entity.Message) {
log.Printf("消息入库失败, 错误信息: %v", err)
}
log.Printf("消息入库成功消息Id: %d", msg.MsgId)
// 更新最后活跃时间
// 只处理收到的消息
if msg.MsgId > 1 {
go updateLastActive(msg)
}
}

View File

@@ -66,6 +66,7 @@ func Sync() {
PinyinAll: friend.PinyinAll,
Wxid: friend.Wxid,
IsOk: true,
LastActive: time.Now().Local(),
}).Error
if err != nil {
log.Printf("新增好友失败: %s", err.Error())
@@ -149,14 +150,15 @@ func syncGroupUsers(tx *gorm.DB, gid string) {
if count == 0 {
// 新增
err = tx.Create(&entity.GroupUser{
GroupId: gid,
Account: cp.Account,
HeadImage: cp.HeadImage,
Nickname: cp.Nickname,
Wxid: cp.Wxid,
IsMember: true,
IsAdmin: wxid == baseResp.Data.Admin,
JoinTime: time.Now().Local(),
GroupId: gid,
Account: cp.Account,
HeadImage: cp.HeadImage,
Nickname: cp.Nickname,
Wxid: cp.Wxid,
IsMember: true,
IsAdmin: wxid == baseResp.Data.Admin,
JoinTime: time.Now().Local(),
LastActive: time.Now().Local(),
}).Error
if err != nil {
log.Printf("新增群成员失败: %s", err.Error())

View File

@@ -47,10 +47,10 @@
<td>{{ .CustomAccount }}</td>
<td>{{ .Nickname }}</td>
<td>
{{ if eq .LastActiveTime.IsNil true }}
{{ if eq .LastActive.IsNil true }}
无数据
{{ else }}
{{ .LastActiveTime }}
{{ .LastActive }}
{{ end }}
</td>
<td>
@@ -110,10 +110,10 @@
<td>{{ .Wxid }}</td>
<td>{{ .Nickname }}</td>
<td>
{{ if eq .LastActiveTime.IsNil true }}
{{ if eq .LastActive.IsNil true }}
无数据
{{ else }}
{{ .LastActiveTime }}
{{ .LastActive }}
{{ end }}
</td>
<td>

View File

@@ -113,7 +113,8 @@ function getGroupUsers(groupId, groupName) {
const groupUsers = response.data
// 循环渲染数据
groupUsers.forEach((groupUser, i) => {
const { wxid, nickname, isMember, isAdmin, joinTime, lastActiveTime, leaveTime, skipChatRank } = groupUser;
console.log(groupUser)
const { wxid, nickname, isMember, isAdmin, joinTime, lastActive, leaveTime, skipChatRank } = groupUser;
let row = tbody.insertRow(i);
// Insert data into cells
@@ -122,7 +123,7 @@ function getGroupUsers(groupId, groupName) {
row.insertCell(2).innerHTML = `<div class="badge badge-${isMember ? 'info' : 'error'} gap-2">${isMember ? '是' : '否'}</div>`;
row.insertCell(3).innerHTML = `<div class="badge badge-${isAdmin ? 'info' : 'error'} gap-2">${isAdmin ? '是' : '否'}</div>`;
row.insertCell(4).innerHTML = joinTime;
row.insertCell(5).innerHTML = lastActiveTime;
row.insertCell(5).innerHTML = lastActive;
row.insertCell(6).innerHTML = leaveTime;
row.insertCell(7).innerHTML = `<input type="checkbox" class="toggle toggle-error" ${skipChatRank ? 'checked' : ''} onclick="changeUserGroupRankSkipStatus('${groupId}', '${wxid}')" />`;
});

View File

@@ -12,25 +12,26 @@ type FriendItem struct {
Pinyin string // 昵称拼音大写首字母
PinyinAll string // 昵称全拼
Wxid string // 微信原始Id
LastActive types.DateTime // 最后活跃时间
EnableAi bool // 是否使用AI
AiModel string // AI模型
EnableChatRank bool // 是否使用聊天排行
EnableWelcome bool // 是否使用迎新
EnableCommand bool // 是否启用指令
IsOk bool // 是否还在通讯库(群聊是要还在群里也算)
LastActiveTime types.DateTime // 最后活跃时间
}
// GroupUserItem
// @description: 群成员列表数据
type GroupUserItem struct {
Wxid string `json:"wxid"` // 微信Id
Account string `json:"account"` // 账号
HeadImage string `json:"headImage"` // 头像
Nickname string `json:"nickname"` // 昵称
IsMember bool `json:"isMember" ` // 是否群成员
IsAdmin bool `json:"isAdmin"` // 是否群主
JoinTime types.DateTime `json:"joinTime"` // 加入时间
LastActiveTime types.DateTime `json:"lastActiveTime"` // 最后活跃时间
LeaveTime types.DateTime `json:"leaveTime"` // 离开时间
SkipChatRank bool `json:"skipChatRank" ` // 是否跳过聊天排行
Wxid string `json:"wxid"` // 微信Id
Account string `json:"account"` // 账号
HeadImage string `json:"headImage"` // 头像
Nickname string `json:"nickname"` // 昵称
IsMember bool `json:"isMember" ` // 是否群成员
IsAdmin bool `json:"isAdmin"` // 是否群主
JoinTime types.DateTime `json:"joinTime"` // 加入时间
LastActive types.DateTime `json:"lastActive"` // 最后活跃时间
LeaveTime types.DateTime `json:"leaveTime"` // 离开时间
SkipChatRank bool `json:"skipChatRank" ` // 是否跳过聊天排行
}