xingzai 11 mesi fa
parent
commit
4141b08a75
2 ha cambiato i file con 21 aggiunte e 2 eliminazioni
  1. 3 0
      controllers/yanxuan_special.go
  2. 18 2
      utils/common.go

+ 3 - 0
controllers/yanxuan_special.go

@@ -187,6 +187,9 @@ func (this *BaseAuthMobileController) Detail() {
 	if inviteShareCode != "" {
 	if inviteShareCode != "" {
 		go services.AddCygxUserAdminShareHistory(user, utils.CYGX_OBJ_YANXUANSPECIAL, item.Title, inviteShareCode, specialId)
 		go services.AddCygxUserAdminShareHistory(user, utils.CYGX_OBJ_YANXUANSPECIAL, item.Title, inviteShareCode, specialId)
 	}
 	}
+	if userId == 0 {
+		item.Content = utils.InterceptHtmlLength(item.Content, 150)
+	}
 	var resp models.CygxYanxuanSpecialResp
 	var resp models.CygxYanxuanSpecialResp
 	resp.HasPermission = 1
 	resp.HasPermission = 1
 	resp.CygxYanxuanSpecialItem = *item
 	resp.CygxYanxuanSpecialItem = *item

+ 18 - 2
utils/common.go

@@ -948,10 +948,26 @@ func InterceptHtmlLength(body string, length int) (newbody string) {
 		return
 		return
 	}
 	}
 	bodyText := doc.Text()
 	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)
 		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
 	return
 }
 }