mini_get_article_logic.go
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package article
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/core"
"sort"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniGetArticleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniGetArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniGetArticleLogic {
return &MiniGetArticleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 小程序端展示文章内容
func (l *MiniGetArticleLogic) MiniGetArticle(req *types.MiniArticleGetRequest) (resp *types.MiniArticleGetResponse, err error) {
// 获取文章内容
var conn = l.svcCtx.DefaultDBConn()
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, req.CompanyId)
if err != nil {
return nil, xerr.NewErrMsgErr("读取公司数据失败", err)
}
articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.Id)
if err != nil {
return nil, xerr.NewErrMsgErr("读取文章内容失败", err)
}
if articleInfo.CompanyId != req.CompanyId {
return nil, xerr.NewErrMsg("没有查看权限")
}
// 检查文章的可查看人
if ok := articleInfo.WhoCanRead(int64(req.UserId)); !ok {
return nil, xerr.NewErrMsg("没有查看权限")
}
if articleInfo.Show == domain.ArticleShowDisable {
// 文章内容不显示
// resp = &types.MiniArticleGetResponse{
// Id: articleInfo.Id,
// Title: articleInfo.Title,
// Show: int(domain.ArticleShowDisable),
// }
// return resp, nil
return nil, xerr.NewErrMsg("没有查看权限")
}
queryOption := domain.NewQueryOptions().
WithFindOnly().
MustWithKV("articleId", articleInfo.Id)
_, sectionList, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("读取文章内容失败", err)
}
//获取作者信息
author, _ := l.svcCtx.UserRepository.FindOne(l.ctx, conn, int64(articleInfo.AuthorId))
var meLoveFlag int
if req.UserId > 0 {
// 获取我对文章的点赞标识
queryOption = domain.NewQueryOptions().
WithCountOnly().
MustWithKV("articleId", articleInfo.Id).
MustWithKV("commentId", 0).
MustWithKV("userId", req.UserId)
cnt, _, _ := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
if cnt > 0 {
meLoveFlag = 1
}
}
tags := []string{}
if len(articleInfo.Tags) > 0 {
queryOption := domain.NewQueryOptions().WithFindOnly().MustWithKV("ids", articleInfo.Tags)
_, tagList, _ := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, req.CompanyId, queryOption)
for _, val := range tagList {
tags = append(tags, val.Name)
}
}
// 查询我是否已关注文章的作者
follow, _ := l.svcCtx.UserFollowRepository.FindOneUserFollowing(l.ctx, conn, int64(req.UserId), articleInfo.AuthorId)
queryOption = domain.NewQueryOptions().WithCountOnly().WithOffsetLimit(1, 1).WithKV("articleId", articleInfo.Id)
backupCount, _, _ := l.svcCtx.ArticleBackupRepository.Find(l.ctx, conn, queryOption)
sortBy := domain.SortArticleSection(sectionList)
sort.Sort(sortBy)
articleSection := []types.ArticleSection{}
for _, val := range sortBy {
articleSection = append(articleSection, types.ArticleSection{
Id: val.Id,
Content: val.Content,
SortBy: val.SortBy,
TotalComment: val.TotalComment,
Images: val.Images,
ParagraphTemplate: core.NewTypesParagraph(val.ParagraphTemplate),
ParagraphType: val.ParagraphType,
})
}
resp = &types.MiniArticleGetResponse{
Id: articleInfo.Id,
Title: articleInfo.Title,
AuthorId: articleInfo.AuthorId,
Author: types.ArticleAuthor{
Id: articleInfo.Author.Id,
Name: "",
Avatar: "",
Position: "",
Company: companyInfo.Name,
},
CreatedAt: articleInfo.CreatedAt,
Section: articleSection,
Images: []string{},
WhoRead: articleInfo.WhoRead,
WhoReview: articleInfo.WhoReview,
Location: types.Location{
Longitude: articleInfo.Location.Longitude,
Latitude: articleInfo.Location.Latitude,
Descript: articleInfo.Location.Descript,
},
CountLove: articleInfo.CountLove,
CountComment: articleInfo.CountComment,
CountRead: articleInfo.CountRead,
Show: int(articleInfo.Show),
Edit: 0,
MeLoveFlag: meLoveFlag,
MeFollowFlag: 0,
Tags: tags,
MatchUrl: map[string]string{},
Videos: []types.Video{},
}
if backupCount > 1 {
resp.Edit = 1
}
if author != nil {
resp.Author = types.ArticleAuthor{
Id: articleInfo.Author.Id,
Name: author.Name,
Avatar: author.Avatar,
Position: author.Position,
Company: companyInfo.Name,
}
}
for k, v := range articleInfo.MatchUrl {
resp.MatchUrl[k] = v
}
for _, val := range articleInfo.Images {
resp.Images = append(resp.Images, val.Url)
}
for _, val := range articleInfo.Videos {
resp.Videos = append(resp.Videos, types.Video{
Url: val.Url,
Cover: val.Cover,
Width: val.Width,
Height: val.Height,
})
}
if follow != nil {
resp.MeFollowFlag = 1
}
if resp.AuthorId == int64(req.UserId) {
// 作者本人在查看
resp.MeFollowFlag = 1
}
return
}