From 79c22600d824709f966cad65e17d57e9d7660df4 Mon Sep 17 00:00:00 2001 From: dustoair <107600816+dustoair@users.noreply.github.com> Date: Wed, 10 Aug 2022 19:25:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A1=E6=A0=B8ai=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handle/blockService.go | 43 ++++++++++++++++++++++++++++++++++++++++++ handle/logModel.go | 12 ------------ handle/logService.go | 10 ++++++++++ handle/replyText.go | 13 +++++++++++++ main.go | 1 + utils/md5.go | 18 ++++++++++++++++++ utils/md5_test.go | 18 ++++++++++++++++++ 7 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 handle/blockService.go delete mode 100644 handle/logModel.go create mode 100644 utils/md5.go create mode 100644 utils/md5_test.go diff --git a/handle/blockService.go b/handle/blockService.go new file mode 100644 index 0000000..9c6b473 --- /dev/null +++ b/handle/blockService.go @@ -0,0 +1,43 @@ +package handle + +import ( + "WechatGateWay/global" + "fmt" + "github.com/jinzhu/gorm" + "log" +) + +/** +@author: sre +@date: 2022/8/10 0010 +@desc: RecordWechatBlockWords +**/ + +type WechatBlockWords struct { + gorm.Model + Content string + ContentMd5 string `sql:"unique_index:unique_index_md5"` + OpUser string +} + +func RecordWechatBlockWords(text, textMd5, opUser string) { + newRecord := WechatBlockWords{ + Content: text, + ContentMd5: textMd5, + OpUser: opUser, + } + result := global.DB.Create(&newRecord) // 通过数据的指针来创建 + if result.RowsAffected != 1 { + log.Println(fmt.Sprintf("记录失败:text: %s textMd5: %s opUser: %s", text, textMd5, opUser)) + } + +} + +func WordsBlocked(textMd5 string) bool { + var Record WechatBlockWords + if global.DB.Where("ContentMd5 = ?", textMd5).First(&Record).RecordNotFound() { + return false + } + return true + +} diff --git a/handle/logModel.go b/handle/logModel.go deleted file mode 100644 index 6ab5b1e..0000000 --- a/handle/logModel.go +++ /dev/null @@ -1,12 +0,0 @@ -package handle - -import "github.com/jinzhu/gorm" - -type WechatLog struct { - gorm.Model - ToUsername string `xml:"ToUserName"` - FromUsername string `xml:"FromUserName"` - CreateTime uint32 `xml:"CreateTime"` - MsgType string `xml:"MsgType"` - Content string `xml:"Content"` -} diff --git a/handle/logService.go b/handle/logService.go index ffede28..f55ccad 100644 --- a/handle/logService.go +++ b/handle/logService.go @@ -3,9 +3,19 @@ package handle import ( "WechatGateWay/global" "fmt" + "github.com/jinzhu/gorm" "log" ) +type WechatLog struct { + gorm.Model + ToUsername string `xml:"ToUserName"` + FromUsername string `xml:"FromUserName"` + CreateTime uint32 `xml:"CreateTime"` + MsgType string `xml:"MsgType"` + Content string `xml:"Content"` +} + func RecordWechatLog(ToUsername, FromUsername string, CreateTime uint32, MsgType, Content string) { newRecord := WechatLog{ ToUsername: ToUsername, diff --git a/handle/replyText.go b/handle/replyText.go index 266eb96..3dbd396 100644 --- a/handle/replyText.go +++ b/handle/replyText.go @@ -20,6 +20,7 @@ func replyText(msgContent MsgContent, timestamp, nonce string, w http.ResponseWr } else if msgContent.Content == "history" { cmdString = utils.SlicePrint(global.HistoryCmds) } else if msgContent.Content == "删除图片" { + //通过文本消息删除上一张图片 lastImg := GetLastWechatLog(msgContent.FromUsername, "image") err := utils.DeleteFile(lastImg.Content) if err != nil { @@ -30,6 +31,13 @@ func replyText(msgContent MsgContent, timestamp, nonce string, w http.ResponseWr return } + } else if msgContent.Content == "删除文本" { + //通过文本消息删除上一条文本消息 + lastText := GetLastWechatLog(msgContent.FromUsername, "text") + textMd5 := utils.Md5String(lastText.Content) + go RecordWechatBlockWords(lastText.Content, textMd5, msgContent.FromUsername) + return + } else { cmdString = msgContent.Content } @@ -58,6 +66,11 @@ func replyText(msgContent MsgContent, timestamp, nonce string, w http.ResponseWr } } + //文本消息审核 + if WordsBlocked(utils.Md5String(replyContent)) { + replyContent = "***(太文明,无法显示)" + } + //构造回复消息 replyMsg, _ := xml.Marshal(ReplyTextMsg{ ToUsername: msgContent.FromUsername, diff --git a/main.go b/main.go index cbea40a..6a226d9 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,7 @@ func InitDB() *gorm.DB { //自动建表 db.AutoMigrate(&handle.WechatLog{}) + db.AutoMigrate(&handle.WechatBlockWords{}) global.DB = db return db diff --git a/utils/md5.go b/utils/md5.go new file mode 100644 index 0000000..f144854 --- /dev/null +++ b/utils/md5.go @@ -0,0 +1,18 @@ +package utils + +import ( + "crypto/md5" + "encoding/hex" +) + +/** +@author: sre +@date: 2022/8/10 0010 +@desc: md5sum a string +**/ +//Md5String that md5sum a string +func Md5String(s string) string { + h := md5.New() + h.Write([]byte(s)) + return hex.EncodeToString(h.Sum(nil)) +} diff --git a/utils/md5_test.go b/utils/md5_test.go new file mode 100644 index 0000000..233740f --- /dev/null +++ b/utils/md5_test.go @@ -0,0 +1,18 @@ +package utils + +import ( + "fmt" + "testing" +) + +/* +* +@author: sre +@date: 2022/8/10 0010 +@desc: test md5String +* +*/ +func TestMd5String(t *testing.T) { + s := Md5String("hello") + fmt.Println(s) +}