xingzai 1 год назад
Родитель
Сommit
0ad22e97a4
2 измененных файлов с 29 добавлено и 14 удалено
  1. 17 5
      controllers/yanxuan_special.go
  2. 12 9
      utils/common.go

+ 17 - 5
controllers/yanxuan_special.go

@@ -242,14 +242,26 @@ func (this *BaseAuthMobileController) Detail() {
 		}
 	}
 
+	if userId == 0 || user.CompanyId == 1 {
+		hasPermission = 1
+		resp.Content = utils.InterceptHtmlLength(resp.Content, 240) // 截取前80个字
+	} else {
+		hasPermission, _, _, _, err = services.GetUserHasPermissionArticle(user)
+		if err != nil {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
+			return
+		}
+	}
+
 	resp.HasPermission = hasPermission
 	//if userId == 0 {
-	//	resp.Content = utils.InterceptHtmlLength(resp.Content, 240) // 截取前80个字
+
+	//}
+	//if hasPermission != 0 && hasPermission != 1 {
+	//	//resp.Content = utils.InterceptHtmlLength(resp.Content, 240) // 截取前80个字
+	//	resp.Content = ""
 	//}
-	if hasPermission != 0 && hasPermission != 1 {
-		//resp.Content = utils.InterceptHtmlLength(resp.Content, 240) // 截取前80个字
-		resp.Content = ""
-	}
 	br.Data = resp
 	br.Ret = 200
 	br.Success = true

+ 12 - 9
utils/common.go

@@ -8,6 +8,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"github.com/PuerkitoBio/goquery"
+	"html"
 	"image"
 	"image/png"
 	"math"
@@ -936,14 +937,16 @@ func ExtractText(body string) (result string, err error) {
 	return
 }
 func InterceptHtmlLength(body string, length int) (newbody string) {
-	fmt.Println(body)
-	re := regexp.MustCompile(`s*<p>(.*?)</p>`)
-	matches := re.FindAllStringSubmatch(body, 1)
-	// 提取匹配到的文本
-	text := matches[0][1]
-	// 截取前 多少 个字
-	text = text[:length]
-	// 格式化输出
-	newbody = fmt.Sprint("<p>", strings.TrimSpace(text), "</p>")
+	content := html.UnescapeString(body)
+	doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
+	if err != nil {
+		fmt.Println("create doc err:", err.Error())
+		return
+	}
+	bodyText := doc.Text()
+	if len(bodyText) < length {
+		length = len(bodyText)
+	}
+	newbody = bodyText[0:length]
 	return
 }