Răsfoiți Sursa

截取内容

rdluck 4 ani în urmă
părinte
comite
a08b9d740e
4 a modificat fișierele cu 80 adăugiri și 24 ștergeri
  1. 5 0
      controllers/home.go
  2. 33 22
      controllers/user.go
  3. 9 2
      models/article.go
  4. 33 0
      services/article.go

+ 5 - 0
controllers/home.go

@@ -2,6 +2,7 @@ package controllers
 
 import (
 	"hongze/hongze_cygx/models"
+	"hongze/hongze_cygx/services"
 	"hongze/hongze_cygx/utils"
 	"rdluck_tools/paging"
 )
@@ -55,6 +56,10 @@ func (this *HomeController) ListHome() {
 		br.Msg = "获取帖子数据失败,Err:" + err.Error()
 		return
 	}
+	lenList := len(list)
+	for i := 0; i < lenList; i++ {
+		list[i].Body, _ = services.GetReportContentSub(list[i].Body)
+	}
 	page := paging.GetPaging(currentIndex, pageSize, total)
 	resp := new(models.HomeListResp)
 	resp.List = list

+ 33 - 22
controllers/user.go

@@ -307,28 +307,39 @@ func (this *UserController) CollectList() {
 		br.ErrMsg = "获取数据失败,Err:" + err.Error()
 		return
 	}
-	//listLen := len(list)
-	//for i := 0; i < listLen; i++ {
-	//	item := list[i]
-	//	uf, err := cache.GetUserFunnyCache(userId, item.FunnyId)
-	//	if err != nil && err.Error()!=utils.ErrNoRow() {
-	//		br.Msg = "获取信息失败"
-	//		br.ErrMsg = "判断是否收藏失败,Err:" + err.Error()
-	//		return
-	//	}
-	//	if uf!=nil {
-	//		if uf.IsCollect > 0 {
-	//			list[i].IsCollect=true
-	//		}
-	//		if uf.IsComment > 0 {
-	//			list[i].IsComment=true
-	//		}
-	//		if uf.IsLike > 0 {
-	//			list[i].IsLike=true
-	//		}
-	//	}
-	//	list[i].CreateTimeStamp=list[i].CreateTime.Unix()
-	//}
+	var articleIds []string
+	for _, v := range list {
+		articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
+	}
+	articleIdStr := strings.Join(articleIds, ",")
+	articleMap := make(map[int]*models.ArticleDetail)
+	if articleIdStr != "" {
+		articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
+		if err != nil {
+			br.Msg = "获取数据失败"
+			br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
+			return
+		}
+		for _, v := range articleList {
+			if _, ok := articleMap[v.ArticleId]; !ok {
+				articleMap[v.ArticleId] = v
+			}
+		}
+	}
+	lenList := len(list)
+	for i := 0; i < lenList; i++ {
+		item := list[i]
+		article := articleMap[item.ArticleId]
+		list[i].Title = article.Title
+		list[i].TitleEn = article.TitleEn
+		list[i].UpdateFrequency = article.UpdateFrequency
+		list[i].CreateDate = article.CreateDate
+		list[i].PublishDate = article.PublishDate
+		list[i].Body = article.Body
+		list[i].Abstract = article.Abstract
+		list[i].CategoryName = article.CategoryName
+		list[i].SubCategoryName = article.SubCategoryName
+	}
 	page := paging.GetPaging(currentIndex, pageSize, total)
 	resp := new(models.ArticleCollectListResp)
 	resp.List = list

+ 9 - 2
models/article.go

@@ -30,7 +30,7 @@ type HomeArticle struct {
 }
 
 type ArticleDetail struct {
-	Article_id      int    `description:"文章id"`
+	ArticleId      int    `description:"报告id"`
 	Title           string `description:"标题"`
 	TitleEn         string `description:"英文标题 "`
 	UpdateFrequency string `description:"更新周期"`
@@ -47,4 +47,11 @@ func GetArticleDetailById(articleId int) (item *ArticleDetail, err error) {
 	sql := `SELECT * FROM cygx_article WHERE article_id = ? `
 	err = o.Raw(sql, articleId).QueryRow(&item)
 	return
-}
+}
+
+func GetArticleDetailByIdStr(articleIdStr string) (items []*ArticleDetail, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM cygx_article WHERE article_id IN(` + articleIdStr + `) `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}

+ 33 - 0
services/article.go

@@ -0,0 +1,33 @@
+package services
+
+import (
+	"fmt"
+	"html"
+	"strings"
+	"github.com/PuerkitoBio/goquery"
+)
+
+func GetReportContentSub(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 >= 5 {
+			return
+		}
+		n++
+		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
+}