新增了编辑器相关接口
This commit is contained in:
@@ -10,4 +10,5 @@ type Api struct {
|
||||
UserVoteDay // 投票记录
|
||||
System // 配置
|
||||
BaseHospital // 基础数据-医院
|
||||
Editor // 配置-编辑器
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"math/rand"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"time"
|
||||
"vote-admin-api/api/responses"
|
||||
e "vote-admin-api/extend/Editor"
|
||||
"vote-admin-api/extend/aliyun"
|
||||
"vote-admin-api/utils"
|
||||
)
|
||||
|
||||
type Editor struct{}
|
||||
|
||||
// GetEditorConfig 编辑器-获取配置
|
||||
func (b *Editor) GetEditorConfig(c *gin.Context) {
|
||||
action := c.Query("action")
|
||||
if action == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取配置 config
|
||||
if action == "config" {
|
||||
config := e.GetConfig()
|
||||
c.JSON(http.StatusOK, config)
|
||||
return
|
||||
}
|
||||
|
||||
// 图片列表 listImage
|
||||
if action == "listImage" {
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 文件列表 listFile
|
||||
if action == "listFile" {
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
|
||||
// EditorUpload 编辑器-上传
|
||||
func (b *Editor) EditorUpload(c *gin.Context) {
|
||||
action := c.Query("action")
|
||||
if action == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文件错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := file.Open()
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文件错误", c)
|
||||
return
|
||||
}
|
||||
defer func(f multipart.File) {
|
||||
err := f.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}(f)
|
||||
|
||||
// 读取文件内容到字节切片
|
||||
fileBytes, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
responses.FailWithMessage("文件错误", c)
|
||||
return
|
||||
}
|
||||
|
||||
fileType := c.PostForm("type")
|
||||
if fileType == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
fileType = utils.GetExtension(fileType)
|
||||
if fileType == "" {
|
||||
fileType = "jpg"
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
dateTimeString := now.Format("20060102150405") // 当前时间字符串
|
||||
rand.New(rand.NewSource(time.Now().UnixNano())) // 设置随机数
|
||||
|
||||
var ossPath string
|
||||
|
||||
// 上传图片 image
|
||||
if action == "image" {
|
||||
ossPath = "static/images/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType
|
||||
}
|
||||
|
||||
// 上传视频 video
|
||||
if action == "video" {
|
||||
ossPath = "static/video/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType
|
||||
}
|
||||
|
||||
// 上传文件 file
|
||||
if action == "file" {
|
||||
ossPath = "static/file/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType
|
||||
}
|
||||
|
||||
if ossPath == "" {
|
||||
responses.FailWithMessage("上传失败", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 上传oss
|
||||
_, err = aliyun.PutObjectByte(ossPath, fileBytes)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
var g e.UploadDto
|
||||
g.Url = utils.AddOssDomain("/" + ossPath)
|
||||
g.State = "SUCCESS"
|
||||
g.Title = dateTimeString + "." + fileType
|
||||
g.Original = dateTimeString + "." + fileType
|
||||
g.Type = fileType
|
||||
|
||||
c.JSON(http.StatusOK, g)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user