From 3bc95e1317e1a1c72f699cef319559886c15d77b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Sun, 10 Dec 2023 08:14:09 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/at_message.go | 4 +++- service/message.go | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/handler/at_message.go b/handler/at_message.go index c28db253..cbc96b81 100644 --- a/handler/at_message.go +++ b/handler/at_message.go @@ -10,6 +10,7 @@ import ( "go-wechat/config" "go-wechat/entity" "go-wechat/model" + "go-wechat/service" "go-wechat/types" "go-wechat/utils" "log" @@ -54,6 +55,7 @@ func handleAtMessage(m model.Message) { // 查询发信人前面几条文字信息,组装进来 var oldMessages []entity.Message client.MySQL.Model(&entity.Message{}). + Where("msg_id != ?", m.MsgId). Where("create_at >= DATE_SUB(NOW(),INTERVAL 30 MINUTE)"). Where("from_user = ? AND group_user = ? AND display_full_content LIKE ?", m.FromUser, m.GroupUser, "%在群聊中@了你"). Or("to_user = ? AND group_user = ?", m.FromUser, m.GroupUser). @@ -124,7 +126,7 @@ func handleAtMessage(m model.Message) { replyMessage.GroupUser = m.GroupUser // 群成员 replyMessage.ToUser = m.FromUser // 收信人是发信人 replyMessage.Type = types.MsgTypeText - client.MySQL.Create(&replyMessage) // 保存入库 + service.SaveMessage(replyMessage) // 保存消息 // 发送消息 utils.SendMessage(m.FromUser, m.GroupUser, "\n"+resp.Choices[0].Message.Content, 0) diff --git a/service/message.go b/service/message.go index f76a55d1..18c2a0b5 100644 --- a/service/message.go +++ b/service/message.go @@ -4,12 +4,18 @@ import ( "go-wechat/client" "go-wechat/entity" "log" + "os" + "strconv" ) // SaveMessage // @description: 消息入库 // @param msg func SaveMessage(msg entity.Message) { + if flag, _ := strconv.ParseBool(os.Getenv("DONT_SAVE")); flag { + return + } + // 检查消息是否存在,存在就跳过 var count int64 err := client.MySQL.Model(&entity.Message{}).Where("msg_id = ?", msg.MsgId).Count(&count).Error