Files
easygoadmin/app/controller/upload.go
yaoyilin 772cc9ff7e fix: 增加基于mongodb的图片模型
增加基于mongodb的图片模型
2022-11-06 23:35:20 +08:00

73 lines
1.4 KiB
Go

/**
* 文件上传-控制器
* @author
* @since 2021/11/18
* @File : upload
*/
package controller
import (
"github.com/gin-gonic/gin"
"gitlab.52pay.top/go/easygoadmin/app/service"
"gitlab.52pay.top/go/easygoadmin/utils"
"gitlab.52pay.top/go/easygoadmin/utils/common"
"net/http"
)
// 控制器管理对象
var Upload = new(uploadCtl)
type uploadCtl struct{}
func (u *uploadCtl) UploadImage(ctx *gin.Context) {
// 调用上传方法
result, err := service.Upload.UploadImage(ctx)
if err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 拼接图片地址
result.FileUrl = utils.GetImageUrl(result.FileUrl)
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "上传成功",
Data: result,
})
}
func (u *uploadCtl) UploadImage2Db(ctx *gin.Context) {
typ := ctx.Param("typ")
if typ == "" {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: "业务类型不能为空",
})
return
}
// 调用上传方法
result, err := service.Upload.UploadImage2Db(ctx, typ)
if err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 拼接图片地址
result.FileUrl = utils.GetImageUrl(result.FileUrl)
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "上传成功",
Data: result,
})
}