zwxi 11 months ago
parent
commit
99a20eac22
2 changed files with 24 additions and 0 deletions
  1. 1 0
      services/wx_category_template_msg.go
  2. 23 0
      utils/common.go

+ 1 - 0
services/wx_category_template_msg.go

@@ -296,6 +296,7 @@ func SendCommentWxCategoryTemplateMsg(req models.AddCygxArticleCommentReq, user
 	keyword3 = companyItem.SellerName
 	keyword4 = time.Now().Format(utils.FormatDateTimeMinute2)
 	keyword5 = "报告留言:" + req.Content
+	keyword5 = utils.TruncateActivityNameString(keyword5)
 	keywords = append(keywords, keyword1, keyword2, keyword3, keyword4, keyword5)
 
 	if articleInfo.ArticleTypeId > 0 {

+ 23 - 0
utils/common.go

@@ -20,6 +20,7 @@ import (
 	"strconv"
 	"strings"
 	"time"
+	"unicode/utf8"
 )
 
 // 随机数种子
@@ -960,3 +961,25 @@ func StrDateToDate(strTime string) time.Time {
 	resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
 	return resultTime
 }
+
+// 处理活动名称
+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] + "…"
+}