From 43ddd232580f420e629c3421c6d7480f80b96f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 7 Dec 2023 22:38:49 +0800 Subject: [PATCH 1/4] :bug: Fix a bug. --- handler/parse.go | 5 ++--- model/message.go | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/handler/parse.go b/handler/parse.go index 494261e..c031eaa 100644 --- a/handler/parse.go +++ b/handler/parse.go @@ -24,9 +24,8 @@ func Parse(remoteAddr net.Addr, msg []byte) { return } // 提取出群成员信息 - //groupUser := "" - //msgStr := m.Content - if strings.Contains(m.FromUser, "@") { + // Sys类型的消息正文不包含微信 Id,所以不需要处理 + if m.IsGroup() && m.Type != types.MsgTypeSys { // 群消息,处理一下消息和发信人 groupUser := strings.Split(m.Content, "\n")[0] groupUser = strings.ReplaceAll(groupUser, ":", "") diff --git a/model/message.go b/model/message.go index 1465c7a..43531df 100644 --- a/model/message.go +++ b/model/message.go @@ -33,6 +33,10 @@ type systemMsgDataXml struct { // @description: 消息主体 type sysMsg struct{} +func (m Message) IsGroup() bool { + return strings.HasSuffix(m.FromUser, "@chatroom") +} + // IsPat // @description: 是否是拍一拍消息 // @receiver m From ac9f68c80dcf658bd5ac9dcf6719413572819c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 7 Dec 2023 23:00:16 +0800 Subject: [PATCH 2/4] =?UTF-8?q?:art:=20=E6=89=93=E5=8D=B0=20sql=20?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/mysql.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/client/mysql.go b/client/mysql.go index d40ae9f..eb774c1 100644 --- a/client/mysql.go +++ b/client/mysql.go @@ -4,6 +4,7 @@ import ( "go-wechat/config" "gorm.io/driver/mysql" "gorm.io/gorm" + "gorm.io/gorm/logger" "log" ) @@ -17,7 +18,13 @@ func InitMySQLClient() { DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式 DontSupportRenameColumn: true, // 用 `change` 重命名列 } - conn, err := gorm.Open(mysql.New(mysqlConfig)) + + // gorm 配置 + gormConfig := gorm.Config{ + Logger: logger.Default.LogMode(logger.Info), + } + + conn, err := gorm.Open(mysql.New(mysqlConfig), &gormConfig) if err != nil { log.Panicf("初始化MySQL连接失败, 错误信息: %v", err) } else { From 8b7487b42b81e4f1b0c8bf7097e69d44eb4d48d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 7 Dec 2023 23:00:57 +0800 Subject: [PATCH 3/4] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/sys_message.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/handler/sys_message.go b/handler/sys_message.go index d963bcd..7b0ea09 100644 --- a/handler/sys_message.go +++ b/handler/sys_message.go @@ -6,7 +6,6 @@ import ( "go-wechat/entity" "go-wechat/model" "go-wechat/utils" - "log" ) // handleNewUserJoin @@ -15,12 +14,8 @@ import ( func handleNewUserJoin(m model.Message) { // 判断是否开启迎新 var count int64 - err := client.MySQL.Model(&entity.Friend{}). - Where("enable_welcome IS TRUE"). - Where("wxid = ?", m.FromUser). - Count(&count).Error - if err != nil { - log.Printf("查询是否开启迎新失败: %s", err.Error()) + client.MySQL.Model(&entity.Friend{}).Where("enable_welcome IS TRUE").Where("wxid = ?", m.FromUser).Count(&count) + if count < 1 { return } if count < 1 { From b49247bf898e02d9737f278b2627a1bb3c6981a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 7 Dec 2023 23:27:07 +0800 Subject: [PATCH 4/4] :bug: Fix a bug. --- config.yaml | 5 +++-- config/config.go | 5 +++-- handler/sys_message.go | 5 +---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/config.yaml b/config.yaml index 3828de2..1b212d6 100644 --- a/config.yaml +++ b/config.yaml @@ -8,7 +8,7 @@ wechat: callback: 10.0.0.51 # 转发到其他地址 forward: - - 10.0.0.247:4299 +# - 10.0.0.247:4299 # 数据库 mysql: @@ -44,8 +44,9 @@ ai: personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。 # 资源配置 +# map[k]v结构,k 会变成全小写,所以这儿不能用大写字母 resource: # 欢迎新成员表情包 - welcomeNew: + welcome-new: type: emotion path: 58e4150be2bba8f7b71974b10391f9e9 diff --git a/config/config.go b/config/config.go index 9a59ff5..8e6fca6 100644 --- a/config/config.go +++ b/config/config.go @@ -1,10 +1,11 @@ package config -var Conf Config +// Conf 配置 +var Conf conf // Config // @description: 配置 -type Config struct { +type conf struct { Task task `json:"task" yaml:"task"` // 定时任务配置 MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置 Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手 diff --git a/handler/sys_message.go b/handler/sys_message.go index 7b0ea09..425a87e 100644 --- a/handler/sys_message.go +++ b/handler/sys_message.go @@ -18,12 +18,9 @@ func handleNewUserJoin(m model.Message) { if count < 1 { return } - if count < 1 { - return - } // 读取欢迎新成员配置 - conf, ok := config.Conf.Resource["welcomeNew"] + conf, ok := config.Conf.Resource["welcome-new"] if !ok { // 未配置,跳过 return