作者 tangxvhui

新增接口, 获取单条文章编辑历史

... ... @@ -65,6 +65,9 @@ service Core {
@handler MiniArticleBackupSearch
post /article_backup/search (MiniArticleBackupSearchRequest) returns (MiniArticleBackupSearchResponse)
@doc "小程序获取文章的编辑记录"
@handler MiniGetArticleBackup
get /article_backup/:id (MiniGetArticleBackupRequest) returns (MiniGetArticleBackupResponse)
@doc "小程序设置文章的定性标签"
@handler MiniArticleSetTag
... ...
... ... @@ -176,8 +176,32 @@ type (
ChangeField []string `json:"changeField"`
CreatedAt int64 `json:"createdAt"`
Location Location `json:"location"`
Action string `json:"action"`
Show int `json:"show"`
}
)
type (
MiniGetArticleBackupRequest {
BackupId int64 `path:"id"`
CompanyId int64 `path:",optional"` // 服务端自动获取
}
MiniGetArticleBackupResponse {
Id int64 `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Images []string `json:"images"`
Videos []Video `json:"videos"`
ChangeField []string `json:"changeField"`
CreatedAt int64 `json:"createdAt"`
Location Location `json:"location"`
Action string `json:"action"`
Show int `json:"show"`
}
)
// 标记人员浏览了那个文章
type (
MiniArticleMarkUserReadRequest {
... ...
package article
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article"
"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/pkg/result"
)
func MiniGetArticleBackupHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniGetArticleBackupRequest
if err := httpx.Parse(r, &req); err != nil {
result.HttpResult(r, w, nil, err)
return
}
l := article.NewMiniGetArticleBackupLogic(r.Context(), svcCtx)
resp, err := l.MiniGetArticleBackup(&req)
result.HttpResult(r, w, resp, err)
}
}
... ...
... ... @@ -529,6 +529,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Handler: article.MiniArticleBackupSearchHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/article_backup/:id",
Handler: article.MiniGetArticleBackupHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/article/set_tag",
Handler: article.MiniArticleSetTagHandler(serverCtx),
... ...
... ... @@ -74,6 +74,8 @@ func (l *MiniArticleBackupSearchLogic) MiniArticleBackupSearch(req *types.MiniAr
Descript: backupList[i].Location.Descript,
},
ChangeField: backupList[i].ChangeField,
Action: backupList[i].Action,
Show: int(backupList[i].Show),
}
}
return resp, nil
... ...
... ... @@ -192,8 +192,9 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
}
// 生成 备份数据
backupData := newArticle.MakeBackup(articleAuthor, sectionList, "原始版本")
_, err = l.svcCtx.ArticleBackupRepository.Insert(l.ctx, conn, backupData)
var backupData domain.ArticleBackup
backupData.MakeBackup(articleAuthor, newArticle, sectionList, "原始版本")
_, err = l.svcCtx.ArticleBackupRepository.Insert(l.ctx, conn, &backupData)
if err != nil {
return xerr.NewErrMsgErr("创建文章失败", err)
}
... ...
package article
import (
"context"
"strings"
"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/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniGetArticleBackupLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniGetArticleBackupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniGetArticleBackupLogic {
return &MiniGetArticleBackupLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniGetArticleBackupLogic) MiniGetArticleBackup(req *types.MiniGetArticleBackupRequest) (resp *types.MiniGetArticleBackupResponse, err error) {
var conn = l.svcCtx.DefaultDBConn()
backupInfo, err := l.svcCtx.ArticleBackupRepository.FindOne(l.ctx, conn, req.BackupId)
if err != nil {
return &types.MiniGetArticleBackupResponse{}, xerr.NewErrMsgErr("获取编辑记录失败", err)
}
content := strings.Builder{}
for _, val2 := range backupInfo.Section {
content.WriteString(val2.Content)
}
images := []string{}
for _, val2 := range backupInfo.Images {
images = append(images, val2.Url)
}
videos := []types.Video{}
for _, val2 := range backupInfo.Videos {
videos = append(videos, types.Video{
Url: val2.Url,
Cover: val2.Cover,
Width: val2.Width,
Height: val2.Height,
})
}
resp = &types.MiniGetArticleBackupResponse{
Id: backupInfo.Id,
Title: backupInfo.Title,
Content: content.String(),
Images: images,
Videos: videos,
CreatedAt: backupInfo.CreatedAt,
Location: types.Location{
Longitude: backupInfo.Location.Longitude,
Latitude: backupInfo.Location.Latitude,
Descript: backupInfo.Location.Descript,
},
ChangeField: backupInfo.ChangeField,
Action: backupInfo.Action,
Show: int(backupInfo.Show),
}
return
}
... ...
... ... @@ -52,13 +52,14 @@ func (l *SystemArticleRestoreLogic) SystemArticleRestore(req *types.SystemArticl
}
// 备份数据
newBackUp := article.MakeBackup(domain.UserSimple{
var newBackUp domain.ArticleBackup
newBackUp.MakeBackup(domain.UserSimple{
Id: userToken.UserId,
Name: userMe.User.NickName,
Avatar: userMe.User.Avatar,
CompanyId: userToken.CompanyId,
Company: userMe.CurrentCompany.Name,
}, sectionList, "恢复")
}, article, sectionList, "恢复")
_ = newBackUp.CheckChangeField(oldBackup)
article.Version = article.Version + 1
... ... @@ -114,7 +115,7 @@ func (l *SystemArticleRestoreLogic) SystemArticleRestore(req *types.SystemArticl
}
}
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, newBackUp)
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, &newBackUp)
if err != nil {
return xerr.NewErrMsgErr("恢复文章版本失败", err)
}
... ...
... ... @@ -75,7 +75,9 @@ func (l *SystemUpdateArticleLogic) SystemUpdateArticle(req *types.SystemArticleU
if err != nil {
return nil, xerr.NewErrMsgErr("帖子不存在", err)
}
oldBackup := article.MakeBackup(operator, sectionList, "编辑")
var oldBackup domain.ArticleBackup
oldBackup.MakeBackup(operator, article, sectionList, "编辑")
// 获取图片的尺寸大小
images, err := l.getImages(req)
if err != nil {
... ... @@ -158,11 +160,11 @@ func (l *SystemUpdateArticleLogic) SystemUpdateArticle(req *types.SystemArticleU
return xerr.NewErrMsgErr("保存文章内容失败", err)
}
}
var backup domain.ArticleBackup
backup.MakeBackup(operator, article, updateSection, "编辑")
backup := article.MakeBackup(operator, updateSection, "编辑")
if ok := backup.CheckChangeField(oldBackup); ok {
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, backup)
if ok := backup.CheckChangeField(&oldBackup); ok {
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, &backup)
if err != nil {
return xerr.NewErrMsgErr("保存文章内容失败", err)
}
... ...
... ... @@ -489,10 +489,10 @@ type MiniHomepageUserNewsRequest struct {
LastArticleId int64 `json:"lastArticleId,optional"` // 最后文章ID
Size int `json:"size"` // 数量
OrderByKey string `json:"orderByKey,options=HotScore|All|Time,optional,default=desc"` // 按规则排序 (热度:HotScore All:时间排序 Time:时间排序)
OrderByValue string `json:"orderByValue,options=asc||desc,optional"`
Keywords string `json:"keywords,optional"` // 关键字
BeginTime int64 `json:"beginTime,optional"` // 开始时间
EndTime int64 `json:"endTime,optional"` // 结束时间
OrderByValue string `json:"orderByValue,options=asc||desc,optional"` // 排序值 升序 asc 降序 desc
Keywords string `json:"keywords,optional"` // 关键字
BeginTime int64 `json:"beginTime,optional"` // 开始时间
EndTime int64 `json:"endTime,optional"` // 结束时间
}
type MiniHomepageUserNewsResposne struct {
... ... @@ -1037,6 +1037,26 @@ type MiniArticleBackupItem struct {
ChangeField []string `json:"changeField"`
CreatedAt int64 `json:"createdAt"`
Location Location `json:"location"`
Action string `json:"action"`
Show int `json:"show"`
}
type MiniGetArticleBackupRequest struct {
BackupId int64 `path:"id"`
CompanyId int64 `path:",optional"` // 服务端自动获取
}
type MiniGetArticleBackupResponse struct {
Id int64 `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Images []string `json:"images"`
Videos []Video `json:"videos"`
ChangeField []string `json:"changeField"`
CreatedAt int64 `json:"createdAt"`
Location Location `json:"location"`
Action string `json:"action"`
Show int `json:"show"`
}
type MiniArticleMarkUserReadRequest struct {
... ...
... ... @@ -95,44 +95,6 @@ func (a ArticleShow) Named() string {
return ""
}
// 设置文章的备份数据
func (m *Article) MakeBackup(operator UserSimple, section []*ArticleSection, action string) *ArticleBackup {
sectionBackup := make([]ArticleSection, len(section))
for i := range section {
sectionBackup[i] = *section[i]
}
b := ArticleBackup{
Id: 0,
CompanyId: m.CompanyId,
CreatedAt: 0,
UpdatedAt: 0,
DeletedAt: 0,
Version: m.Version,
Operator: operator,
ArticleId: m.Id,
Title: m.Title,
Section: sectionBackup,
Images: make([]Image, len(m.Images)),
Videos: make([]Video, len(m.Videos)),
Action: action,
TargetUser: m.TargetUser,
WhoRead: m.WhoRead,
WhoReview: m.WhoReview,
Tags: m.Tags,
MatchUrl: map[string]string{},
Location: m.Location,
}
if action == "原始版本" {
b.ChangeField = append(b.ChangeField, "Section")
}
copy(b.Videos, m.Videos)
copy(b.Images, m.Images)
for k, v := range m.MatchUrl {
b.MatchUrl[k] = v
}
return &b
}
func (m *Article) SetSummary(sectionList []*ArticleSection) {
if len(sectionList) == 0 {
return
... ...
... ... @@ -33,6 +33,7 @@ type ArticleBackup struct {
WhoReview []int64 `json:"whoReview"` // 评论人
Tags []int64 `json:"tags"` // 标签
MatchUrl map[string]string `json:"matchUrl"` // 匹配文章内容中的url文本
Show ArticleShow `json:"show"` // 评论的展示状态(1显示,2不显示、)
}
type ArticleBackupRepository interface {
... ... @@ -138,3 +139,42 @@ func (bk *ArticleBackup) CheckChangeField(oldBackup *ArticleBackup) bool {
}
return len(bk.ChangeField) > 0
}
// 创建备份信息
func (bk *ArticleBackup) MakeBackup(operator UserSimple, article *Article, section []*ArticleSection, action string) {
sectionBackup := make([]ArticleSection, len(section))
for i := range section {
sectionBackup[i] = *section[i]
}
b := ArticleBackup{
Id: 0,
CompanyId: article.CompanyId,
CreatedAt: 0,
UpdatedAt: 0,
DeletedAt: 0,
Version: article.Version,
Operator: operator,
ArticleId: article.Id,
Title: article.Title,
Section: sectionBackup,
Images: make([]Image, len(article.Images)),
Videos: make([]Video, len(article.Videos)),
Action: action,
TargetUser: article.TargetUser,
WhoRead: article.WhoRead,
WhoReview: article.WhoReview,
Tags: article.Tags,
MatchUrl: map[string]string{},
Show: article.Show,
Location: article.Location,
}
if action == "原始版本" {
b.ChangeField = append(b.ChangeField, "Section")
}
copy(b.Videos, article.Videos)
copy(b.Images, article.Images)
for k, v := range article.MatchUrl {
b.MatchUrl[k] = v
}
*bk = b
}
... ...