xingzai 2 gadi atpakaļ
vecāks
revīzija
66acad7b87
1 mainītis faili ar 144 papildinājumiem un 0 dzēšanām
  1. 144 0
      services/article.go

+ 144 - 0
services/article.go

@@ -7,6 +7,8 @@ import (
 	"hongze/hongze_clpt/models"
 	"hongze/hongze_clpt/utils"
 	"html"
+	"regexp"
+	"sort"
 	"strconv"
 	"strings"
 	"time"
@@ -288,10 +290,152 @@ func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *mode
 	return
 }
 
+//处理核心观点的展示规则
+//func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
+//	if item.Annotation != "" {
+//		annotation = strings.Replace(item.Annotation, "<br>", "", -1)
+//	}
+//	return
+//}
+
+func GetReportContentTextSubNew(content string) (contentSub string, err error) {
+	content = html.UnescapeString(content)
+	doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
+	if errdoc != nil {
+		err = errdoc
+		return
+	}
+	docText := doc.Text()
+	bodyRune := []rune(docText)
+	bodyRuneLen := len(bodyRune)
+	body := string(bodyRune[:bodyRuneLen])
+	contentSub = body
+	contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
+	contentSub = strings.Replace(contentSub, " ", "", -1)
+	contentSub = strings.Replace(contentSub, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sanered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
+	return
+}
+
 //处理核心观点的展示规则
 func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
+	if item.ArticleId >= utils.SummaryArticleId {
+		item.Annotation = YxArticleAnnotation(item)
+	}
 	if item.Annotation != "" {
 		annotation = strings.Replace(item.Annotation, "<br>", "", -1)
+	} else {
+		return
+	}
+	bodyText, _ := GetReportContentTextSubNew(annotation)
+	if bodyText == "" {
+		return
+	}
+	if annotation != "" {
+		annotation = html.UnescapeString(annotation)
+		doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
+		docText := doc.Text()
+		mapDoc := make(map[int]string)
+		var mapSort []int
+		p := doc.Find("p")
+		p.Each(func(tk int, pd *goquery.Selection) {
+			pdText := pd.Text()
+			pdText = strings.Replace(pdText, " ", "", -1)
+			if pdText != "" {
+				textLen := strings.Index(docText, pdText)
+				mapDoc[(strings.Index(docText, pdText))] = pdText
+				mapSort = append(mapSort, textLen)
+			}
+		})
+		li := doc.Find("li")
+		li.Each(func(tk int, li *goquery.Selection) {
+			liText := li.Text()
+			liText = strings.Replace(liText, " ", "", -1)
+			if liText != "" {
+				textLen := strings.Index(docText, liText)
+				mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
+				mapSort = append(mapSort, textLen)
+			}
+		})
+
+		ul := doc.Find("ul")
+		ul.Each(func(tk int, ul *goquery.Selection) {
+			ulText := ul.Text()
+			ulText = strings.Replace(ulText, " ", "", -1)
+			if ulText != "" {
+				textLen := strings.Index(docText, ulText)
+				mapDoc[(strings.Index(docText, ulText))] = ulText
+				mapSort = append(mapSort, textLen)
+			}
+		})
+		if len(mapSort) == 0 {
+			return
+		} else {
+			//排序
+			sort.Ints(mapSort)
+			var annotationHtml string
+			for _, vSort := range mapSort {
+				for k, v := range mapDoc {
+					if k == vSort && v != "" {
+						annotationHtml += v + "<br>"
+					}
+				}
+			}
+			annotationHtml = strings.TrimRight(annotationHtml, "<br>")
+			annotationHtml = "<p>" + annotationHtml + "</p>"
+			annotation = annotationHtml
+		}
+	}
+	return
+}
+
+//解析研选内容中的核心观点
+func YxArticleAnnotation(article *models.ArticleListResp) (annotation string) {
+	//如果不规范,就获取内容主体
+	if strings.Count(article.Body, "<hr") == 0 {
+		//如果内容不规范而且,还有图片,就把核心观点置空
+		if article.BodyImg != "" {
+			return
+		}
+		annotation, _ = GetReportContentTextSub(article.Body)
+		return
+	}
+	body := strings.ReplaceAll(article.Body, "<strong>", "")
+	body = strings.ReplaceAll(body, "</strong>", "")
+	body = strings.ReplaceAll(body, "</ol>", "</div>")
+	body = strings.ReplaceAll(body, "<ol>", "<div>")
+	body = strings.ReplaceAll(body, "</li>", "</p>")
+	body = strings.ReplaceAll(body, "<li>", "<p>")
+	re, _ := regexp.Compile("<strong.*?>")
+	body = re.ReplaceAllString(body, "")
+	reLi, _ := regexp.Compile("<li.*?>")
+	body = reLi.ReplaceAllString(body, "")
+	var plus int
+	coreIndex := strings.Index(body, "核心观点:")
+	plus = 15
+	if coreIndex == -1 {
+		coreIndex = strings.Index(body, "核心观点:")
+		plus = 13
+	}
+	if coreIndex == -1 {
+		coreIndex = strings.Index(body, "核心观点")
+		plus = 12
+	}
+	if coreIndex == -1 {
+		coreIndex = strings.Index(body, "核心结论:")
+		plus = 15
+	}
+	if coreIndex == -1 {
+		coreIndex = strings.Index(body, "核心结论:")
+		plus = 13
+	}
+	if coreIndex == -1 {
+		coreIndex = strings.Index(body, "核心结论")
+		plus = 12
+	}
+	endIndex := strings.Index(body, "<hr")
+	if coreIndex != -1 && endIndex != -1 {
+		body = body[coreIndex+plus : endIndex]
 	}
+	annotation = body
 	return
 }