package hdl import ( "applet/app/db/implement" "applet/app/e" "applet/app/lib/validate" "applet/app/md" "applet/app/svc" "applet/app/utils" "applet/app/utils/logx" md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md" qiniu "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/qiniuyun" "fmt" "github.com/gin-gonic/gin" ) // ImgReqUpload // @Summary 七牛云上传 // @Tags 七牛云 // @Description 七牛云-七牛云上传 // @param Authorization header string false "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Param args body md.ImgReqUpload true "请求参数" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /api/qiniuyun/upload [POST] func ImgReqUpload(c *gin.Context) { var args md.ImgReqUpload err := c.ShouldBindJSON(&args) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } // 文件名名称 if args.FileSize < 1 || args.FileName == "" { logx.Warn(err) e.OutErr(c, e.ERR_INVALID_ARGS) return } scheme := "http" if c.Request.TLS != nil { scheme = "https" } // 拼装回调地址 callbackUrl := scheme + "://" + c.Request.Host + "/api/qiniuyun/callback?master_id=" + c.GetString("mid") NewSysCfgDb := implement.NewSysCfgDb(svc.MasterDb(c), c.GetString("mid")) stgInfo := NewSysCfgDb.SysCfgFindWithDb( md2.KEY_CFG_FILE_BUCKET, md2.KEY_CFG_FILE_HOST, md2.KEY_CFG_FILE_AK, md2.KEY_CFG_FILE_SK, md2.KEY_CFG_FILE_PVD, md2.KEY_CFG_FILE_REGION, md2.KEY_CFG_FILE_MAX_SIZE, md2.KEY_CFG_FILE_EXT, md2.KEY_CFG_FILE_SCHEME, md2.KEY_CFG_FILE_AVATAR_THUMBNAIL, ) if stgInfo == nil { e.OutErr(c, 400, e.NewErr(400, "配置未设置")) return } res, err := qiniu.ImgReqUpload(stgInfo, "", args.Dir, args.FileName, callbackUrl, args.FileSize) if err != nil { e.OutErr(c, 400, err.Error()) return } my := utils.SerializeStr(res) var my1 map[string]interface{} utils.Unserialize([]byte(my), &my1) e.OutSuc(c, my1, nil) return } func FileImgCallback(c *gin.Context) { masterID := c.Query("master_id") c.Set("mid", masterID) var args md2.FileCallback err := c.ShouldBindJSON(&args) if err != nil { fmt.Println("七牛云上传回调参数错误:>>>>>>>>>>", err) e.OutErr(c, 200, e.ERR_INVALID_ARGS) return } res := map[string]interface{}{ "name": args.FileName, "fname": getFileNameURL(c, args.FileName), "fsize": args.FileSize, "provider": args.Provider, "uid": args.Uid, "dir_id": args.DirId, "w": args.Width, "h": args.Height, } e.OutSuc(c, &res, nil) } func getFileNameURL(c *gin.Context, filename string) string { NewSysCfgDb := implement.NewSysCfgDb(svc.MasterDb(c), c.GetString("mid")) protocol := NewSysCfgDb.SysCfgGetWithDb("file_bucket_scheme") domain := NewSysCfgDb.SysCfgGetWithDb("file_bucket_host") imgformat := NewSysCfgDb.SysCfgGetWithDb("file_avatar_thumbnail") if protocol != "" && domain != "" && imgformat != "" { return protocol + "://" + domain + "/" + filename } return filename }