作者 tangxvhui

调整文章概要的设置方式

@@ -175,19 +175,7 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR @@ -175,19 +175,7 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
175 newArticle.MatchUrl[k] = v 175 newArticle.MatchUrl[k] = v
176 } 176 }
177 //设置内容概要 177 //设置内容概要
178 - if len(sectionList) > 0 {  
179 - // 截取内容 50个字  
180 - runeNumber := 0 //字数  
181 - stringIndex := 0 //字符串长度  
182 - for i := range sectionList[0].Content {  
183 - if runeNumber > 50 {  
184 - break  
185 - }  
186 - runeNumber += 1  
187 - stringIndex = i  
188 - }  
189 - newArticle.Summary = sectionList[0].Content[0:stringIndex]  
190 - } 178 + newArticle.SetSummary(sectionList)
191 179
192 err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { 180 err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
193 newArticle, err = l.svcCtx.ArticleRepository.Insert(ctx, c, newArticle) 181 newArticle, err = l.svcCtx.ArticleRepository.Insert(ctx, c, newArticle)
@@ -77,6 +77,8 @@ func (l *SystemArticleRestoreLogic) SystemArticleRestore(req *types.SystemArticl @@ -77,6 +77,8 @@ func (l *SystemArticleRestoreLogic) SystemArticleRestore(req *types.SystemArticl
77 }) 77 })
78 }) 78 })
79 79
  80 + article.SetSummary(articleSections)
  81 +
80 err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { 82 err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
81 //保存文章 83 //保存文章
82 _, err = l.svcCtx.ArticleRepository.Update(ctx, c, article) 84 _, err = l.svcCtx.ArticleRepository.Update(ctx, c, article)
@@ -117,19 +117,7 @@ func (l *SystemUpdateArticleLogic) SystemUpdateArticle(req *types.SystemArticleU @@ -117,19 +117,7 @@ func (l *SystemUpdateArticleLogic) SystemUpdateArticle(req *types.SystemArticleU
117 //文章内容 117 //文章内容
118 articleSections := l.getSections(req, article) 118 articleSections := l.getSections(req, article)
119 //设置内容概要 119 //设置内容概要
120 - if len(req.Section) > 0 {  
121 - // 截取内容 50个字  
122 - runeNumber := 0 //字数  
123 - stringIndex := 0 //字符串长度  
124 - for i := range req.Section[0].Content {  
125 - if runeNumber > 50 {  
126 - break  
127 - }  
128 - runeNumber += 1  
129 - stringIndex = i  
130 - }  
131 - article.Summary = req.Section[0].Content[0:stringIndex]  
132 - } 120 + article.SetSummary(articleSections)
133 121
134 err = transaction.UseTrans(l.ctx, l.conn.DB(), func(ctx context.Context, c transaction.Conn) error { 122 err = transaction.UseTrans(l.ctx, l.conn.DB(), func(ctx context.Context, c transaction.Conn) error {
135 _, err = l.svcCtx.ArticleRepository.Update(l.ctx, c, article) 123 _, err = l.svcCtx.ArticleRepository.Update(l.ctx, c, article)
@@ -2,6 +2,7 @@ package domain @@ -2,6 +2,7 @@ package domain
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "strings"
5 6
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
7 ) 8 )
@@ -128,3 +129,30 @@ func (m *Article) MakeBackup(operator UserSimple, section []*ArticleSection) *Ar @@ -128,3 +129,30 @@ func (m *Article) MakeBackup(operator UserSimple, section []*ArticleSection) *Ar
128 } 129 }
129 return &b 130 return &b
130 } 131 }
  132 +
  133 +func (m *Article) SetSummary(sectionList []ArticleSection) {
  134 + if len(sectionList) == 0 {
  135 + return
  136 + }
  137 + //设置内容概要
  138 +
  139 + // 截取内容 50个字
  140 + runeNumber := 0 //字数
  141 + stringIndex := 0 //字符串长度
  142 + content := ""
  143 + for i := range sectionList {
  144 + str := strings.TrimSpace(sectionList[i].Content)
  145 + if len(str) > 0 {
  146 + content = str
  147 + break
  148 + }
  149 + }
  150 + for i := range content {
  151 + if runeNumber > 50 {
  152 + break
  153 + }
  154 + runeNumber += 1
  155 + stringIndex = i
  156 + }
  157 + m.Summary = content[0:stringIndex]
  158 +}