101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
package admin
|
|
|
|
import (
|
|
"blog/internal/consts"
|
|
"blog/internal/model/AjaxResult"
|
|
"blog/internal/model/admin"
|
|
"blog/internal/model/vo"
|
|
"blog/third_party/SessionUtil"
|
|
"blog/third_party/database"
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/sessions"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
/*
|
|
日记管理
|
|
*/
|
|
type DiaryController struct {
|
|
Ctx iris.Context
|
|
Session *sessions.Session
|
|
}
|
|
|
|
func (ctrl *DiaryController) Get() {
|
|
ctrl.Ctx.View("/admin/diary/index.html")
|
|
}
|
|
|
|
func (ctrl *DiaryController) PostSubmit() {
|
|
user := SessionUtil.GetUser(ctrl.Session)
|
|
userId := user.Id
|
|
|
|
content := ctrl.Ctx.FormValue("content")
|
|
var err error
|
|
|
|
ctx := context.Background()
|
|
|
|
diaryId := uuid.NewString()
|
|
database.WGorm().Transaction(func(tx *gorm.DB) error {
|
|
now := time.Now().UnixMilli()
|
|
diary := admin.AdminDiary{Id: diaryId, PublishTime: now, Del: 0, CreateTime: now, CreateBy: userId}
|
|
ctn := vo.CommonContent{Id: uuid.NewString(), RelId: diaryId, Content: content, State: consts.CONTENT_STATE_PUBLISH}
|
|
err = tx.Table("common_contents").Create(ctn).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
files, headers, fileErr := ctrl.Ctx.FormFiles("file")
|
|
if fileErr != nil {
|
|
err = fileErr
|
|
return fileErr
|
|
}
|
|
fileIds := []string{}
|
|
for i, file := range files {
|
|
defer file.Close()
|
|
header := headers[i]
|
|
fileSize := header.Size
|
|
var bytes []byte = make([]byte, fileSize)
|
|
_, err = file.Read(bytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fileName := header.Filename
|
|
fileId := uuid.NewString()
|
|
fileIds = append(fileIds, fileId)
|
|
sysFile := admin.SysFile{Id: fileId, FileName: fileName, FileSize: fileSize, Data: bytes, Sort: i, Del: 0, CreateBy: userId, CreateTime: now}
|
|
err = tx.Table("common_files").Create(&sysFile).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if len(fileIds) > 0 {
|
|
diary.Images = strings.Join(fileIds, ",")
|
|
}
|
|
|
|
err := tx.Table("blog_diaries").Create(&diary).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// database.GormTemplate.Table("blog_diaries").Where("id = ?", id).First(&diary)
|
|
err = database.RedisTemplate.ZAdd(ctx, consts.REDIS_BLOG_DIARY_LATEST, redis.Z{Score: float64(diary.PublishTime), Member: &diary}).Err()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = database.RedisTemplate.Set(ctx, consts.REDIS_BLOG_CONTENT+diaryId, content, time.Duration(0)).Err()
|
|
return err
|
|
})
|
|
defer database.WGormUnlock()
|
|
|
|
if err != nil {
|
|
ctrl.Ctx.JSON(AjaxResult.OkMsg("发布成功", nil))
|
|
return
|
|
}
|
|
ctrl.Ctx.JSON(AjaxResult.Error("发布失败"))
|
|
}
|