作者 tangxvhui

获取文章详情

@@ -22,6 +22,7 @@ type Author { @@ -22,6 +22,7 @@ type Author {
22 Avatar string `json:"avatar"` // 人员头像URL 22 Avatar string `json:"avatar"` // 人员头像URL
23 Group string `json:"group"` // 人员的分组 23 Group string `json:"group"` // 人员的分组
24 Position string `json:"position"` // 职位 24 Position string `json:"position"` // 职位
  25 + Company string `json:"company"` // 公司
25 } 26 }
26 27
27 //小程序端创建发布文章 28 //小程序端创建发布文章
@@ -44,20 +45,29 @@ type ( @@ -44,20 +45,29 @@ type (
44 type ( 45 type (
45 MiniArticleGetRequest { 46 MiniArticleGetRequest {
46 Id int64 `path:"id"` //id 47 Id int64 `path:"id"` //id
  48 + CompanyId int64 `path:"-"`
47 } 49 }
48 MiniArticleGetResponse { 50 MiniArticleGetResponse {
  51 + Id int64 `json:"id"` //id
49 Title string `json:"title"` //标题 52 Title string `json:"title"` //标题
50 - AuthorId int `json:"authorId"` //发布人id 53 + AuthorId int64 `json:"authorId"` //发布人id
51 Author Author `json:"author"` //发布人 54 Author Author `json:"author"` //发布人
52 CreatedAt int64 `json:"createdAt"` //文章的发布时间 55 CreatedAt int64 `json:"createdAt"` //文章的发布时间
53 - Section []string `json:"section"` //文章的文本内容 56 + Section []ArticleSection `json:"section"` //文章的文本内容
54 Images []string `json:"images"` //图片 57 Images []string `json:"images"` //图片
55 WhoRead []int64 `json:"whoRead"` //谁可查看 58 WhoRead []int64 `json:"whoRead"` //谁可查看
56 WhoReview []int64 `json:"whoReview"` //谁可评论 59 WhoReview []int64 `json:"whoReview"` //谁可评论
57 Location Location `json:"location"` //定位坐标 60 Location Location `json:"location"` //定位坐标
58 CountLove int `json:"countLove"` // 点赞数量 61 CountLove int `json:"countLove"` // 点赞数量
59 CountComment int `json:"countComment"` // 评论数量 62 CountComment int `json:"countComment"` // 评论数量
60 - Show int `json:"showState"` // 评论的展示状态(0显示、1不显示) 63 + CountRead int `json:"countRead"` // 浏览数量
  64 + Show int `json:"show"` // 评论的展示状态(0显示、1不显示)
  65 + }
  66 + ArticleSection {
  67 + Id int64 `json:"id"` //段落id
  68 + Content string `json:"content"` // 文本内容
  69 + SortBy int `json:"sortBy"` // 排序
  70 + TotalComment int `json:"totalComment"` // 评论的数量
61 } 71 }
62 ) 72 )
63 73
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ 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"
10 ) 11 )
11 12
12 func MiniGetArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { 13 func MiniGetArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
@@ -18,7 +19,8 @@ func MiniGetArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -18,7 +19,8 @@ func MiniGetArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
18 } 19 }
19 20
20 l := article.NewMiniGetArticleLogic(r.Context(), svcCtx) 21 l := article.NewMiniGetArticleLogic(r.Context(), svcCtx)
21 - 22 + token := contextdata.GetUserTokenFromCtx(r.Context())
  23 + req.CompanyId = token.CompanyId
22 resp, err := l.MiniGetArticle(&req) 24 resp, err := l.MiniGetArticle(&req)
23 if err != nil { 25 if err != nil {
24 httpx.ErrorCtx(r.Context(), w, err) 26 httpx.ErrorCtx(r.Context(), w, err)
@@ -46,7 +46,9 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR @@ -46,7 +46,9 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
46 Company: "", 46 Company: "",
47 CompanyId: author.CompanyId, 47 CompanyId: author.CompanyId,
48 } 48 }
49 - 49 + if len(req.Images) > 9 {
  50 + return nil, xerr.NewErrMsg("图片数量最多9张")
  51 + }
50 //TODO 获取图片的尺寸大小 52 //TODO 获取图片的尺寸大小
51 images := []domain.Image{} 53 images := []domain.Image{}
52 for _, val := range req.Images { 54 for _, val := range req.Images {
@@ -56,6 +58,7 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR @@ -56,6 +58,7 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
56 Height: 0, 58 Height: 0,
57 }) 59 })
58 } 60 }
  61 +
59 //检查文章可被哪些人查看 62 //检查文章可被哪些人查看
60 whoRead := []int64{} 63 whoRead := []int64{}
61 if len(req.WhoRead) > 0 { 64 if len(req.WhoRead) > 0 {
@@ -2,9 +2,12 @@ package article @@ -2,9 +2,12 @@ package article
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "sort"
5 6
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/svc"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
  9 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
8 11
9 "github.com/zeromicro/go-zero/core/logx" 12 "github.com/zeromicro/go-zero/core/logx"
10 ) 13 )
@@ -24,7 +27,76 @@ func NewMiniGetArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Mi @@ -24,7 +27,76 @@ func NewMiniGetArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Mi
24 } 27 }
25 28
26 func (l *MiniGetArticleLogic) MiniGetArticle(req *types.MiniArticleGetRequest) (resp *types.MiniArticleGetResponse, err error) { 29 func (l *MiniGetArticleLogic) MiniGetArticle(req *types.MiniArticleGetRequest) (resp *types.MiniArticleGetResponse, err error) {
27 - // todo: add your logic here and delete this line 30 + // 获取文章内容
  31 + var conn = l.svcCtx.DefaultDBConn()
  32 +
  33 + articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.Id)
  34 + if err != nil {
  35 + return nil, xerr.NewErrMsgErr("读取文章内容失败", err)
  36 + }
  37 + if articleInfo.CompanyId != req.CompanyId {
  38 + return nil, xerr.NewErrMsg("没有查看权限")
  39 + }
  40 +
  41 + if articleInfo.Show == domain.ArticleShowDisable {
  42 + resp = &types.MiniArticleGetResponse{
  43 + Id: articleInfo.Id,
  44 + Title: articleInfo.Title,
  45 + Show: int(domain.ArticleShowDisable),
  46 + }
  47 + return resp, nil
  48 + }
  49 + queryOption := domain.NewQueryOptions().
  50 + WithFindOnly().
  51 + MustWithKV("articleId", articleInfo.Id)
  52 +
  53 + _, sectionList, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, conn, queryOption)
  54 + if err != nil {
  55 + return nil, xerr.NewErrMsgErr("读取文章内容失败", err)
  56 + }
  57 + sortBy := domain.SortArticleSection(sectionList)
  58 + sort.Sort(sortBy)
  59 + articleSection := []types.ArticleSection{}
  60 +
  61 + for _, val := range sortBy {
  62 + articleSection = append(articleSection, types.ArticleSection{
  63 + Id: val.Id,
  64 + Content: val.Content,
  65 + SortBy: val.SortBy,
  66 + TotalComment: val.TotalComment,
  67 + })
  68 + }
  69 + resp = &types.MiniArticleGetResponse{
  70 + Id: articleInfo.Id,
  71 + Title: articleInfo.Title,
  72 + AuthorId: articleInfo.AuthorId,
  73 + Author: types.Author{
  74 + Id: articleInfo.Author.Id,
  75 + Name: articleInfo.Author.Name,
  76 + Avatar: articleInfo.Author.Avatar,
  77 + Group: articleInfo.Author.Group,
  78 + Position: articleInfo.Author.Position,
  79 + Company: articleInfo.Author.Company,
  80 + },
  81 + CreatedAt: articleInfo.CreatedAt,
  82 + Section: articleSection,
  83 + Images: []string{},
  84 + WhoRead: articleInfo.WhoRead,
  85 + WhoReview: articleInfo.WhoReview,
  86 + Location: types.Location{
  87 + Longitude: articleInfo.Location.Longitude,
  88 + Latitude: articleInfo.Location.Latitude,
  89 + Descript: articleInfo.Location.Descript,
  90 + },
  91 + CountLove: articleInfo.CountLove,
  92 + CountComment: articleInfo.CountComment,
  93 + CountRead: articleInfo.CountRead,
  94 + Show: int(articleInfo.Show),
  95 + }
  96 +
  97 + for _, val := range articleInfo.Images {
  98 + resp.Images = append(resp.Images, val.Url)
  99 + }
28 100
29 return 101 return
30 } 102 }
@@ -262,6 +262,7 @@ type Author struct { @@ -262,6 +262,7 @@ type Author struct {
262 Avatar string `json:"avatar"` // 人员头像URL 262 Avatar string `json:"avatar"` // 人员头像URL
263 Group string `json:"group"` // 人员的分组 263 Group string `json:"group"` // 人员的分组
264 Position string `json:"position"` // 职位 264 Position string `json:"position"` // 职位
  265 + Company string `json:"company"` // 公司
265 } 266 }
266 267
267 type MiniArticleCreateRequest struct { 268 type MiniArticleCreateRequest struct {
@@ -280,21 +281,31 @@ type MiniArticleCreateResponse struct { @@ -280,21 +281,31 @@ type MiniArticleCreateResponse struct {
280 281
281 type MiniArticleGetRequest struct { 282 type MiniArticleGetRequest struct {
282 Id int64 `path:"id"` //id 283 Id int64 `path:"id"` //id
  284 + CompanyId int64 `path:"-"`
283 } 285 }
284 286
285 type MiniArticleGetResponse struct { 287 type MiniArticleGetResponse struct {
  288 + Id int64 `json:"id"` //id
286 Title string `json:"title"` //标题 289 Title string `json:"title"` //标题
287 - AuthorId int `json:"authorId"` //发布人id 290 + AuthorId int64 `json:"authorId"` //发布人id
288 Author Author `json:"author"` //发布人 291 Author Author `json:"author"` //发布人
289 CreatedAt int64 `json:"createdAt"` //文章的发布时间 292 CreatedAt int64 `json:"createdAt"` //文章的发布时间
290 - Section []string `json:"section"` //文章的文本内容 293 + Section []ArticleSection `json:"section"` //文章的文本内容
291 Images []string `json:"images"` //图片 294 Images []string `json:"images"` //图片
292 WhoRead []int64 `json:"whoRead"` //谁可查看 295 WhoRead []int64 `json:"whoRead"` //谁可查看
293 WhoReview []int64 `json:"whoReview"` //谁可评论 296 WhoReview []int64 `json:"whoReview"` //谁可评论
294 Location Location `json:"location"` //定位坐标 297 Location Location `json:"location"` //定位坐标
295 CountLove int `json:"countLove"` // 点赞数量 298 CountLove int `json:"countLove"` // 点赞数量
296 CountComment int `json:"countComment"` // 评论数量 299 CountComment int `json:"countComment"` // 评论数量
297 - Show int `json:"showState"` // 评论的展示状态(0显示、1不显示) 300 + CountRead int `json:"countRead"` // 浏览数量
  301 + Show int `json:"show"` // 评论的展示状态(0显示、1不显示)
  302 +}
  303 +
  304 +type ArticleSection struct {
  305 + Id int64 `json:"id"` //段落id
  306 + Content string `json:"content"` // 文本内容
  307 + SortBy int `json:"sortBy"` // 排序
  308 + TotalComment int `json:"totalComment"` // 评论的数量
298 } 309 }
299 310
300 type MiniArticleSearchMeRequest struct { 311 type MiniArticleSearchMeRequest struct {
@@ -120,6 +120,10 @@ func (repository *ArticleSectionRepository) Find(ctx context.Context, conn trans @@ -120,6 +120,10 @@ func (repository *ArticleSectionRepository) Find(ctx context.Context, conn trans
120 ) 120 )
121 queryFunc := func() (interface{}, error) { 121 queryFunc := func() (interface{}, error) {
122 tx = tx.Model(&ms).Order("id desc") 122 tx = tx.Model(&ms).Order("id desc")
  123 +
  124 + if v, ok := queryOptions["articleId"]; ok {
  125 + tx = tx.Where("article_id = ?", v)
  126 + }
123 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { 127 if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
124 return dms, tx.Error 128 return dms, tx.Error
125 } 129 }