瀏覽代碼

wip:荟聚图片

wangshan 10 月之前
父節點
當前提交
da98cf8d51
共有 1 個文件被更改,包括 99 次插入94 次删除
  1. 99 94
      src/jfw/modules/weixin/src/wx/reply.go

+ 99 - 94
src/jfw/modules/weixin/src/wx/reply.go

@@ -1,31 +1,33 @@
 package wx
 
 import (
-	"app.yhyue.com/moapp/jybase/common"
-	"app.yhyue.com/moapp/jybase/redis"
-	"fmt"
-	"html"
-	"jy/src/jfw/modules/weixin/src/github.com/wizjin/weixin"
-	"jy/src/jfw/modules/weixin/src/tools"
-	"log"
-	"regexp"
-	"strings"
+    "app.yhyue.com/moapp/jybase/common"
+    "app.yhyue.com/moapp/jybase/redis"
+    "crypto/md5"
+    "encoding/hex"
+    "fmt"
+    "html"
+    "jy/src/jfw/modules/weixin/src/github.com/wizjin/weixin"
+    "jy/src/jfw/modules/weixin/src/tools"
+    "log"
+    "regexp"
+    "strings"
 )
 
 const (
-	TableWxReplyRule    = "jianyu.wx_reply_rule"    // 规则表
-	TableWxReplyKeys    = "jianyu.wx_reply_keys"    // 关键词表
-	TableWxReplyContent = "jianyu.wx_reply_content" // 回复内容表
-	ReplyContentTEXT    = 1                         // 回复类型: 文本
-	ReplyContentImage   = 2                         // 图片
-	ReplyContentArticle = 3                         // 图文
-	ReplayImagePath     = "./res/reply/upload"      // 回复图片上传路径
+    TableWxReplyRule    = "jianyu.wx_reply_rule"    // 规则表
+    TableWxReplyKeys    = "jianyu.wx_reply_keys"    // 关键词表
+    TableWxReplyContent = "jianyu.wx_reply_content" // 回复内容表
+    ReplyContentTEXT    = 1                         // 回复类型: 文本
+    ReplyContentImage   = 2                         // 图片
+    ReplyContentArticle = 3                         // 图文
+    ReplayImagePath     = "./res/reply/upload"      // 回复图片上传路径
 )
 
 // KeysReply 关键词回复
 func KeysReply(keys string, w weixin.ResponseWriter) bool {
-	// 获取回复内容
-	query := fmt.Sprintf(`SELECT
+    // 获取回复内容
+    query := fmt.Sprintf(`SELECT
     wrc.*
 FROM
     %s wrk
@@ -37,80 +39,83 @@ WHERE
     wrk.key = ? AND wrr.state = 1
         AND wrr.start_time <= NOW()
         AND wrr.end_time >= NOW();`, TableWxReplyKeys, TableWxReplyRule, TableWxReplyContent)
-	rs := tools.Mysql.SelectBySql(query, keys)
-	// 没匹配上规则
-	if rs == nil || len(*rs) == 0 {
-		return false
-	}
-	// 处理规则
-	for i := 0; i < len(*rs); i++ {
-		reply_type := common.IntAll((*rs)[i]["reply_type"])
-		title := common.ObjToString((*rs)[i]["title"])
-		url := common.ObjToString((*rs)[i]["url"])
-		pic_url := common.ObjToString((*rs)[i]["pic_url"])
-		desc := common.ObjToString((*rs)[i]["desc"])
-		switch reply_type {
-		case ReplyContentTEXT:
-			//编码及换行处理
-			title = html.UnescapeString(title)
-			title = strings.ReplaceAll(strings.ReplaceAll(title, "<br />", "\n"), "\\n", "\n")
-			// 使用正则表达式匹配HTML标签
-			re, err := regexp.Compile("\\<[^a]+?\\>")
-			if err != nil {
-				fmt.Println("Error compiling regex:", err)
-			} else {
-				log.Println("---1---:", title)
-				// 使用正则表达式的ReplaceAllString函数移除HTML标签
-				title = re.ReplaceAllString(title, "")
-				log.Println("---2---:", title)
-				err := w.PostText(title)
-				if err != nil {
-					log.Println("文字 发送失败", err)
-				}
-			}
-		case ReplyContentImage:
-			// 先上传 获得id
-			var i int
-			for {
-				i++
-				CBKey := fmt.Sprintf("reply_%s_%s", keys, pic_url)
-				mediaId := redis.GetStr("other", CBKey)
-				if mediaId == "" {
-					_mediaId, err := w.UploadMediaFromFile("image", pic_url)
-					if _mediaId != "" && err == nil {
-						mediaId = _mediaId
-					}
-				}
-				log.Println("发送图片", mediaId)
-				if mediaId != "" {
-					go func() {
-						redis.Put("other", CBKey, mediaId, 24*60*60*3)
-					}()
-					err := w.PostImage(mediaId)
-					if err == nil {
-						break
-					}
-					redis.Del("other", CBKey)
-					log.Println("发送失败", err)
-				}
-				if i > 3 {
-					break
-				}
-			}
-		case ReplyContentArticle:
-			article := weixin.Article{
-				Title:       title,
-				Url:         url,
-				PicUrl:      pic_url,
-				Description: desc,
-			}
-			err := w.PostNews([]weixin.Article{
-				article,
-			})
-			if err != nil {
-				log.Println("reply 图文消息 err:", err, article)
-			}
-		}
-	}
-	return true
+    rs := tools.Mysql.SelectBySql(query, keys)
+    // 没匹配上规则
+    if rs == nil || len(*rs) == 0 {
+        return false
+    }
+    // 处理规则
+    for i := 0; i < len(*rs); i++ {
+        reply_type := common.IntAll((*rs)[i]["reply_type"])
+        title := common.ObjToString((*rs)[i]["title"])
+        url := common.ObjToString((*rs)[i]["url"])
+        pic_url := common.ObjToString((*rs)[i]["pic_url"])
+        desc := common.ObjToString((*rs)[i]["desc"])
+        switch reply_type {
+        case ReplyContentTEXT:
+            //编码及换行处理
+            title = html.UnescapeString(title)
+            title = strings.ReplaceAll(strings.ReplaceAll(title, "<br />", "\n"), "\\n", "\n")
+            // 使用正则表达式匹配HTML标签
+            re, err := regexp.Compile("\\<[^a]+?\\>")
+            if err != nil {
+                fmt.Println("Error compiling regex:", err)
+            } else {
+                log.Println("---1---:", title)
+                // 使用正则表达式的ReplaceAllString函数移除HTML标签
+                title = re.ReplaceAllString(title, "")
+                log.Println("---2---:", title)
+                err := w.PostText(title)
+                if err != nil {
+                    log.Println("文字 发送失败", err)
+                }
+            }
+        case ReplyContentImage:
+            // 先上传 获得id
+            var i int
+            for {
+                i++
+                // 计算MD5哈希值
+                hash := md5.Sum([]byte(url))
+                CBKey := fmt.Sprintf("reply_%s_%s", keys, hex.EncodeToString(hash[:]))
+                mediaId := redis.GetStr("other", CBKey)
+                if mediaId == "" {
+                    _mediaId, err := w.UploadMediaFromFile("image", pic_url)
+                    log.Println(_mediaId, "图片上传微信", err)
+                    if _mediaId != "" && err == nil {
+                        mediaId = _mediaId
+                    }
+                }
+                log.Println("发送图片", mediaId)
+                if mediaId != "" {
+                    go func() {
+                        redis.Put("other", CBKey, mediaId, 24*60*60*3)
+                    }()
+                    err := w.PostImage(mediaId)
+                    if err == nil {
+                        break
+                    }
+                    redis.Del("other", CBKey)
+                    log.Println("发送失败", err)
+                }
+                if i > 3 {
+                    break
+                }
+            }
+        case ReplyContentArticle:
+            article := weixin.Article{
+                Title:       title,
+                Url:         url,
+                PicUrl:      pic_url,
+                Description: desc,
+            }
+            err := w.PostNews([]weixin.Article{
+                article,
+            })
+            if err != nil {
+                log.Println("reply 图文消息 err:", err, article)
+            }
+        }
+    }
+    return true
 }