浏览代码

特殊处理列表中的内容

xiexiaoyuan 3 年之前
父节点
当前提交
779f38099a
共有 1 个文件被更改,包括 30 次插入6 次删除
  1. 30 6
      services/report/report.go

+ 30 - 6
services/report/report.go

@@ -25,6 +25,7 @@ import (
 	"hongze/hongze_yb/task"
 	"hongze/hongze_yb/utils"
 	"html"
+	"regexp"
 	"sort"
 	"strconv"
 	"strings"
@@ -709,10 +710,10 @@ func GetReportList(user user.UserInfo, keyWord string, classifyIdFirst, classify
 			if stageStr == "" {
 				stageStr = strconv.Itoa(reportInfo.Stage)
 			}
-			reportItem.TitleInfo = fmt.Sprintf("【第%s期|FICC|%s】%s", stageStr, reportItem.ClassifyNameSecond, reportItem.Title)
+			reportItem.TitleInfo = fmt.Sprintf("【第%s期|FICC|%s】", stageStr, reportItem.ClassifyNameSecond)
 			if reportInfo.ClassifyNameFirst == "晨报" || reportInfo.ClassifyNameFirst == "周报" || classifyIdSecond > 0{
 				reportItem.ReportImgUrl = utils.ALIYUN_YBIMG_HOST + reportImgUrl
-				reportItem.TitleInfo = fmt.Sprintf("【第%s期|FICC|%s】%s", stageStr, reportItem.ClassifyNameFirst, reportItem.Title)
+				reportItem.TitleInfo = fmt.Sprintf("【第%s期|FICC|%s】", stageStr, reportItem.ClassifyNameFirst)
 			} else if url,ok := permissionImageMap[reportInfo.ClassifyNameSecond]; ok {
 				reportItem.ReportImgUrl = utils.ALIYUN_YBIMG_HOST + url
 			}
@@ -817,7 +818,12 @@ func GetCollectReportList(user user.UserInfo, chartPermissionId, pageIndex, page
 		}
 		listMap := make(map[string]*response.ReportCollectList)
 		for _, v := range reportList {
-			v.ContentSub = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">"+html.UnescapeString(v.ContentSub)+"</div>"
+			v.ContentSub, err = GetReportContentSub(v.ContentSub)
+			if err != nil {
+				errMsg = err.Error()
+				err = errors.New("解析内容出错")
+				return
+			}
 			if _, ok := listMap[v.PublishTime.Format("2006-01-02")]; !ok {
 				temp := new(response.ReportCollectList)
 				temp.Date = v.PublishTime.Format("2006-01-02")
@@ -898,14 +904,14 @@ func SearchReport(user user.UserInfo, keyWord string, pageIndex, pageSize int)(r
 			temp.ClassifyIdSecond = reportItem.ClassifyIdSecond
 			temp.ClassifyNameSecond = reportItem.ClassifyNameSecond
 			temp.Title = reportItem.Title
-			temp.ContentSub = html.UnescapeString(html.UnescapeString(reportItem.BodyContent))
+			temp.ContentSub = html.UnescapeString(reportItem.BodyContent)
 			temp.PublishTime, err = time.Parse("2006-01-02 15:04:05", reportItem.PublishTime)
 
 			if len(v.Highlight["Title"]) > 0 {
 				temp.Title = v.Highlight["Title"][0]
 			}
 			if len(v.Highlight["BodyContent"]) > 0 {
-				temp.ContentSub = html.UnescapeString(html.UnescapeString(v.Highlight["BodyContent"][0]))
+				temp.ContentSub = html.UnescapeString(v.Highlight["BodyContent"][0])
 			}
 			if len(v.Highlight["Categories"]) > 0 {
 				if temp.ClassifyNameSecond != "" {
@@ -915,7 +921,12 @@ func SearchReport(user user.UserInfo, keyWord string, pageIndex, pageSize int)(r
 					temp.ClassifyNameFirst = "<span style=\"color:#E3B377\">"+temp.ClassifyNameFirst+"</span>"
 				}
 			}
-			temp.ContentSub = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">"+temp.ContentSub+"</div>"
+			temp.ContentSub, err = GetReportContentSub(temp.ContentSub)
+			if err != nil {
+				errMsg = err.Error()
+				err = errors.New("解析内容出错")
+				return
+			}
 			reportList = append(reportList, temp)
 		}
 	}
@@ -1007,3 +1018,16 @@ func GetTickerData(user user.UserInfo, reportChapterId int) (ret *response.Ticke
 	return
 }
 
+// GetReportContentSub 特殊处理contentSub
+func GetReportContentSub(content string) (contentSub string, err error) {
+	content = html.UnescapeString(content)
+	if strings.Contains(content, "src=") {
+		flysnowRegexp := regexp.MustCompile(`<img[^>]*?/>`)
+		content = flysnowRegexp.ReplaceAllString(content, "")
+	}
+	contentSub = content
+	contentSub = strings.ReplaceAll(contentSub, "<p", "<span")
+	contentSub = strings.ReplaceAll(contentSub, "</p>", "</span>")
+	contentSub = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">"+contentSub+"</div>"
+	return
+}