Browse Source

收藏列表标题高亮

hsun 2 years ago
parent
commit
2319dbb1b2
2 changed files with 12 additions and 4 deletions
  1. 0 2
      controller/collection/collection.go
  2. 12 2
      services/collection/collection.go

+ 0 - 2
controller/collection/collection.go

@@ -1,7 +1,6 @@
 package collection
 
 import (
-	"fmt"
 	"github.com/gin-gonic/gin"
 	"hongze/hongze_yb/controller/response"
 	"hongze/hongze_yb/models/request"
@@ -33,7 +32,6 @@ func List(c *gin.Context) {
 
 	total, list, e := collection.GetCollectionList(int(userInfo.UserID), req.FromType, page, pageSize, req.Keywords)
 	if e != nil {
-		fmt.Println(e.Error())
 		response.FailMsg("获取收藏列表失败", "获取收藏列表失败, Err: "+e.Error(), c)
 		return
 	}

+ 12 - 2
services/collection/collection.go

@@ -10,6 +10,7 @@ import (
 	"hongze/hongze_yb/models/tables/yb_road_video"
 	"hongze/hongze_yb/models/tables/yb_user_collection"
 	"hongze/hongze_yb/utils"
+	"strings"
 	"sync"
 	"time"
 )
@@ -177,9 +178,8 @@ func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string
 		pars = append(pars, fromType)
 	}
 	if keywords != "" {
-		keywords = "%" + keywords + "%"
 		cond += ` AND title LIKE ?`
-		pars = append(pars, keywords)
+		pars = append(pars, fmt.Sprint("%", keywords, "%"))
 	}
 	collectionTotal, e := yb_user_collection.GetPageListTotalByCondition(cond, pars)
 	if e != nil {
@@ -314,6 +314,10 @@ func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string
 	}
 
 	// 响应列表
+	titlePre := `<div style="-webkit-line-clamp: 2;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;">`
+	titleSuf := `</div>`
+	highlightPre := `<span style="color:#E3B377">`
+	highlightSuf := `</span>`
 	for i := range collections {
 		v := &response.CollectionList{
 			CollectionId:   collections[i].CollectionID,
@@ -362,6 +366,12 @@ func GetCollectionList(userId, fromType, currPage, pageSize int, keywords string
 		default:
 			break
 		}
+		// 标题富文本及高亮
+		v.Title = fmt.Sprint(titlePre, v.Title, titleSuf)
+		if keywords != "" {
+			kw := fmt.Sprint(highlightPre, keywords, highlightSuf)
+			v.Title = strings.ReplaceAll(v.Title, keywords, kw)
+		}
 		respList = append(respList, v)
 	}