xingzai hai 1 ano
pai
achega
4c5ba76c14
Modificáronse 2 ficheiros con 24 adicións e 0 borrados
  1. 1 0
      services/wx_template_msg.go
  2. 23 0
      utils/common.go

+ 1 - 0
services/wx_template_msg.go

@@ -67,6 +67,7 @@ func SendWxMsgWithFrequencyBycategory(keyWord1, keyWord2 string, openIdList []*m
 		msg = "accessToken is empty"
 		return
 	}
+	keyWord1 = utils.TruncateActivityNameString(keyWord1)
 	//keyword1 := keyWord2
 	//keyword1 := keyWord2
 	sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken

+ 23 - 0
utils/common.go

@@ -21,6 +21,7 @@ import (
 	"strings"
 	"time"
 	"unicode"
+	"unicode/utf8"
 )
 
 // 随机数种子
@@ -982,3 +983,25 @@ func GetArabicNumbers(str string) string {
 	}
 	return string(numbers)
 }
+
+// 处理活动名称
+func TruncateActivityNameString(s string) string {
+	// 计算字符串总字数(按汉字、数字、字母和特殊符号计算)
+	totalCharCount := utf8.RuneCountInString(s)
+	// 如果总字数不超过18,则直接返回整个字符串
+	if totalCharCount <= 18 {
+		return s
+	}
+	// 计算前15个汉字所需的字节位置
+	hanziCount := 0
+	byteIndex := 0
+	for byteIndex < len(s) && hanziCount < 15 {
+		r, size := utf8.DecodeRuneInString(s[byteIndex:])
+		if r != utf8.RuneError {
+			hanziCount++
+		}
+		byteIndex += size
+	}
+	// 截取前15个汉字,并添加省略号
+	return s[:byteIndex] + "…"
+}