作者 tangxvhui

首页搜索文章列表

@@ -511,7 +511,7 @@ type ( @@ -511,7 +511,7 @@ type (
511 } 511 }
512 // 512 //
513 MiniSearchArticleResponse { 513 MiniSearchArticleResponse {
514 - Total int `json:"total"` 514 + Total int `json:"total"`
515 List []MiniSearchArticleItem `json:"list"` 515 List []MiniSearchArticleItem `json:"list"`
516 } 516 }
517 // 517 //
@@ -7,8 +7,11 @@ import ( @@ -7,8 +7,11 @@ import (
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
  11 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
10 ) 12 )
11 13
  14 +// 小程序端搜索
12 func MiniSearchArticlePageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 15 func MiniSearchArticlePageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
13 return func(w http.ResponseWriter, r *http.Request) { 16 return func(w http.ResponseWriter, r *http.Request) {
14 var req types.MiniSearchArticleRequest 17 var req types.MiniSearchArticleRequest
@@ -18,11 +21,10 @@ func MiniSearchArticlePageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -18,11 +21,10 @@ func MiniSearchArticlePageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
18 } 21 }
19 22
20 l := article.NewMiniSearchArticlePageLogic(r.Context(), svcCtx) 23 l := article.NewMiniSearchArticlePageLogic(r.Context(), svcCtx)
  24 + token := contextdata.GetUserTokenFromCtx(r.Context())
  25 + req.UserId = token.UserId
  26 + req.CompanyId = token.CompanyId
21 resp, err := l.MiniSearchArticlePage(&req) 27 resp, err := l.MiniSearchArticlePage(&req)
22 - if err != nil {  
23 - httpx.ErrorCtx(r.Context(), w, err)  
24 - } else {  
25 - httpx.OkJsonCtx(r.Context(), w, resp)  
26 - } 28 + result.HttpResult(r, w, resp, err)
27 } 29 }
28 } 30 }
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 5
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  8 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
9 10
10 "github.com/zeromicro/go-zero/core/logx" 11 "github.com/zeromicro/go-zero/core/logx"
@@ -32,7 +33,7 @@ func (l *MiniSearchArticlePageLogic) MiniSearchArticlePage(req *types.MiniSearch @@ -32,7 +33,7 @@ func (l *MiniSearchArticlePageLogic) MiniSearchArticlePage(req *types.MiniSearch
32 cnt, articleList, err := l.svcCtx.ArticleRepository.CustomSearchBy(l.ctx, conn, req.UserId, req.CompanyId, 33 cnt, articleList, err := l.svcCtx.ArticleRepository.CustomSearchBy(l.ctx, conn, req.UserId, req.CompanyId,
33 req.TagCategory, req.TagId, [2]int64{req.BeginTime, req.EndTime}, req.SearchWord, req.Page, req.Size) 34 req.TagCategory, req.TagId, [2]int64{req.BeginTime, req.EndTime}, req.SearchWord, req.Page, req.Size)
34 if err != nil { 35 if err != nil {
35 - return nil, xerr.NewErrMsgErr("获取文章列表", err) 36 + return nil, xerr.NewErrMsgErr("获取文章列表失败", err)
36 } 37 }
37 38
38 articleIds := []int64{} 39 articleIds := []int64{}
@@ -40,8 +41,43 @@ func (l *MiniSearchArticlePageLogic) MiniSearchArticlePage(req *types.MiniSearch @@ -40,8 +41,43 @@ func (l *MiniSearchArticlePageLogic) MiniSearchArticlePage(req *types.MiniSearch
40 articleIds = append(articleIds, val.Id) 41 articleIds = append(articleIds, val.Id)
41 } 42 }
42 43
  44 + readFlag := map[int64]struct{}{}
  45 +
  46 + if len(articleIds) > 0 {
  47 + queryOption := domain.NewQueryOptions().WithFindOnly().
  48 + MustWithKV("userId", req.UserId).MustWithKV("articleIds", articleIds)
  49 + _, userReadFlag, err := l.svcCtx.UserReadArticleRepository.Find(l.ctx, conn, queryOption)
  50 + if err != nil {
  51 + return nil, xerr.NewErrMsgErr("获取文章列表失败", err)
  52 + }
  53 + for _, val := range userReadFlag {
  54 + readFlag[val.ArticleId] = struct{}{}
  55 + }
  56 + }
  57 +
43 resp = &types.MiniSearchArticleResponse{ 58 resp = &types.MiniSearchArticleResponse{
44 Total: int(cnt), 59 Total: int(cnt),
  60 + List: make([]types.MiniSearchArticleItem, len(articleList)),
45 } 61 }
46 - return 62 +
  63 + for i, val := range articleList {
  64 +
  65 + item := types.MiniSearchArticleItem{
  66 + ArticleId: val.Id,
  67 + Title: val.Title,
  68 + Author: val.Author.Name,
  69 + Images: []string{},
  70 + CreatedAt: val.CreatedAt,
  71 + MeReadFlag: 0,
  72 + }
  73 + if _, ok := readFlag[val.Id]; ok {
  74 + item.MeReadFlag = 1
  75 + }
  76 + for _, img := range val.Images {
  77 + item.Images = append(item.Images, img.Url)
  78 + }
  79 + resp.List[i] = item
  80 + }
  81 +
  82 + return resp, nil
47 } 83 }