rdluck 4 éve
szülő
commit
e4eb0bf8ed
3 módosított fájl, 28 hozzáadás és 2 törlés
  1. 1 1
      controllers/home.go
  2. 1 1
      controllers/user.go
  3. 26 0
      services/article.go

+ 1 - 1
controllers/home.go

@@ -58,7 +58,7 @@ func (this *HomeController) ListHome() {
 	}
 	lenList := len(list)
 	for i := 0; i < lenList; i++ {
-		list[i].Body, _ = services.GetReportContentSub(list[i].Body)
+		list[i].Body, _ = services.GetReportContentTextSub(list[i].Body)
 	}
 	page := paging.GetPaging(currentIndex, pageSize, total)
 	resp := new(models.HomeListResp)

+ 1 - 1
controllers/user.go

@@ -336,7 +336,7 @@ func (this *UserController) CollectList() {
 		list[i].UpdateFrequency = article.UpdateFrequency
 		list[i].CreateDate = article.CreateDate
 		list[i].PublishDate = article.PublishDate
-		list[i].Body, _ = services.GetReportContentSub(article.Body)
+		list[i].Body, _ = services.GetReportContentTextSub(article.Body)
 		list[i].Abstract = article.Abstract
 		list[i].CategoryName = article.CategoryName
 		list[i].SubCategoryName = article.SubCategoryName

+ 26 - 0
services/article.go

@@ -31,3 +31,29 @@ func GetReportContentSub(content string) (contentSub string, err error) {
 	})
 	return
 }
+
+func GetReportContentTextSub(content string) (contentSub string, err error) {
+	content = html.UnescapeString(content)
+	doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
+	if err != nil {
+		fmt.Println("create doc err:", err.Error())
+		return
+	}
+	n := 0
+	doc.Find("p").Each(func(i int, s *goquery.Selection) {
+		if n > 3 {
+			return
+		}
+		n++
+		contentSub = contentSub +s.Text()
+		//phtml, err := s.Html()
+		//if err != nil {
+		//	fmt.Println("get html err", err.Error())
+		//	return
+		//}
+		//if s.Text() != "" || !strings.Contains(phtml, "src") {
+		//	contentSub = contentSub + "<p>" + phtml + "</p>"
+		//}
+	})
+	return
+}