/** * 文件上传-控制器 * @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/library/cfg" "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) { config := cfg.Instance() if config.EasyGoAdmin.Storage == "mongodb" { u.UploadImage2Db(ctx) } else { u.UploadImage2Local(ctx) } } func (u *uploadCtl) UploadImage2Local(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, }) }