浏览代码

no message

xingzai 9 月之前
父节点
当前提交
e33dd126d4
共有 3 个文件被更改,包括 24 次插入2 次删除
  1. 2 0
      controllers/yanxuan_special.go
  2. 4 0
      services/cygx_yanxuan_special.go
  3. 18 2
      utils/common.go

+ 2 - 0
controllers/yanxuan_special.go

@@ -263,6 +263,8 @@ func (this *YanxuanSpecialController) Detail() {
 		if followCount > 0 {
 			resp.IsFollowAuthor = true
 		}
+	} else {
+		item.Content = utils.InterceptHtmlLength(item.Content, 150)
 	}
 
 	//校验研选专栏权限,以及无权限的时候的对应状态码

+ 4 - 0
services/cygx_yanxuan_special.go

@@ -510,6 +510,10 @@ func UpdateYanxuanSpecialAuthoMomentsImg(cont context.Context) (err error) {
 
 // GetYanxuanSpecialDetailUserPower 处理用户查看研选专栏详情的权限
 func GetYanxuanSpecialDetailUserPower(user *models.WxUserItem) (havePower bool, err error) {
+	if user.UserId == 0 {
+		havePower = true
+		return
+	}
 	//研选专栏是否需要校验权限
 	detailChart, e := models.GetConfigByCode("yanxuan_special_power_check")
 	if e != nil {

+ 18 - 2
utils/common.go

@@ -966,10 +966,26 @@ func InterceptHtmlLength(body string, length int) (newbody string) {
 		return
 	}
 	bodyText := doc.Text()
-	if len(bodyText) < length {
+	//if len(bodyText) < length {
+	//	length = len(bodyText)
+	//}
+	//newbody = bodyText[0:length]
+
+	totalCharCount := utf8.RuneCountInString(bodyText)
+	if totalCharCount < length {
 		length = len(bodyText)
 	}
-	newbody = bodyText[0:length]
+	// 计算前15个汉字所需的字节位置
+	hanziCount := 0
+	byteIndex := 0
+	for byteIndex < len(bodyText) && hanziCount < length {
+		r, size := utf8.DecodeRuneInString(bodyText[byteIndex:])
+		if r != utf8.RuneError {
+			hanziCount++
+		}
+		byteIndex += size
+	}
+	newbody = bodyText[:byteIndex] + "…"
 	return
 }