🎨 新增批量上传文章功能

This commit is contained in:
2025-09-12 23:54:54 +08:00
parent 57289a24e7
commit f9b37fb1aa
5 changed files with 94 additions and 5 deletions

View File

@@ -1,12 +1,15 @@
package article
import (
"fmt"
"git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/app"
"git.echol.cn/loser/lckt/model/article"
"git.echol.cn/loser/lckt/model/article/request"
"git.echol.cn/loser/lckt/model/article/vo"
"git.echol.cn/loser/lckt/model/user"
"go.uber.org/zap"
"strings"
)
type ArticleService struct{}
@@ -200,3 +203,52 @@ func (s ArticleService) AppDelete(id string) error {
}
return nil
}
func (s ArticleService) BulkUpload(p request.BulkUpload) (err error) {
var articles []article.Article
var failedFiles []string
for _, a := range p.Files {
teacher := user.User{}
if dbErr := global.GVA_DB.Model(&teacher).Where("nick_name = ?", getTeacherName(a)).First(&teacher).Error; dbErr != nil {
global.GVA_LOG.Error("获取讲师信息失败", zap.Error(dbErr))
failedFiles = append(failedFiles, a)
continue
}
content := "<p><img src=" + a + " alt=\"" + a + "\" data-href=\"\" style=\"width: 100%;height: auto;\"/></p>"
articles = append(articles, article.Article{
Title: p.Title,
Desc: p.Desc,
CategoryId: p.CategoryId,
TeacherId: int(teacher.ID),
TeacherName: teacher.NickName,
CoverImg: teacher.Avatar,
Content: content,
IsFree: p.IsFree,
Price: int64(p.Price),
})
}
if len(articles) > 0 {
if dbErr := global.GVA_DB.Create(&articles).Error; dbErr != nil {
global.GVA_LOG.Error("批量上传文章失败", zap.Error(dbErr))
return dbErr
}
}
if len(failedFiles) > 0 {
global.GVA_LOG.Error("部分文件上传失败", zap.Strings("failedFiles", failedFiles))
return fmt.Errorf("部分文件上传失败: %v", failedFiles)
}
return nil
}
func getTeacherName(url string) string {
lastSlash := strings.LastIndex(url, "/")
underscore := strings.Index(url[lastSlash+1:], "_")
if lastSlash == -1 || underscore == -1 {
return ""
}
return url[lastSlash+1 : lastSlash+1+underscore]
}

View File

@@ -101,6 +101,9 @@ func (e *FileUploadAndDownloadService) UploadFile(header *multipart.FileHeader,
if classId == 2 {
header.Filename = utils.GenerateRandomString(12) + "_" + strconv.FormatInt(time.Now().Unix(), 10) + "." + s[len(s)-1]
}
if classId == 3 {
header.Filename = s[0] + "_" + utils.GenerateRandomString(12) + "_" + strconv.FormatInt(time.Now().Unix(), 10) + "." + s[len(s)-1]
}
filePath, key, uploadErr := oss.UploadFile(header)
if uploadErr != nil {
return file, uploadErr