This commit is contained in:
laodaming 2023-06-12 20:05:51 +08:00
parent b6095d67ad
commit fdcd8c46e0
5 changed files with 47 additions and 12 deletions

View File

@ -53,7 +53,7 @@ func UploadLogoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
// 创建一个业务逻辑层实例
l := logic.NewUploadLogoLogic(r.Context(), svcCtx)
resp := l.UploadLogo(&req, userinfo)
resp := l.UploadLogo(&req, userinfo, r)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {

View File

@ -4,6 +4,11 @@ import (
"context"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/image"
"io/ioutil"
"net/http"
"os"
"strings"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
@ -25,8 +30,29 @@ func NewUploadLogoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upload
}
}
func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, loginInfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, loginInfo *auth.UserInfo, r *http.Request) (resp *basic.Response) {
req.LogoFile = strings.Trim(req.LogoFile, " ")
//图片是否做缩放[php代码不是bool是字符串然后给python处理]
onlyScale := "false"
imageUrl := ""
if req.LogoFile != "" {
onlyScale = "true"
imageUrl = req.LogoFile
} else {
file, fileInfo, err := r.FormFile("upfile")
if err != nil {
return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error())
}
defer file.Close()
if !image.CheckUploadImageFormat(fileInfo.Filename) {
return resp.SetStatusWithMessage(basic.CodeApiErr, "format of the file is invalid ")
}
fileData, err := ioutil.ReadAll(file)
if err != nil {
return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error())
}
_ = os.WriteFile("a.jpeg", fileData, 0666)
}
return resp.SetStatus(basic.CodeOK)
}

View File

@ -27,9 +27,10 @@ type UploadQrcodeRsp struct {
}
type UploadLogoReq struct {
SkuId int64 `form:"skuId"`
IsRemoveBg bool `form:"is_remove_bg"`
Proportion int64 `form:"proportion"`
LogoFile string `form:"logo_file"` //logo地址路径
SkuId int64 `form:"skuId"`
IsRemoveBg bool `form:"is_remove_bg"`
Proportion int64 `form:"proportion"`
}
type UploadLogoRsp struct {
@ -39,9 +40,6 @@ type UploadLogoRsp struct {
ImgColor []string `json:"img_color"`
}
type Request struct {
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`

View File

@ -47,9 +47,10 @@ type UploadQrcodeRsp {
//上传logo
type UploadLogoReq {
SkuId int64 `form:"skuId"`
IsRemoveBg bool `form:"is_remove_bg"`
Proportion int64 `form:"proportion"`
LogoFile string `form:"logo_file"` //logo地址路径
SkuId int64 `form:"skuId"`
IsRemoveBg bool `form:"is_remove_bg"`
Proportion int64 `form:"proportion"`
}
type UploadLogoRsp {
NobgUrl string `json:"nobg_url"`

View File

@ -0,0 +1,10 @@
package image
// 校验上传图片类型合法不合法
func CheckUploadImageFormat(fileName string) bool {
switch ext {
case "jpg", "png", "fbx", "glb", "hdr", "jpeg", "svg", "pdf", "psd", "webp", "gif":
return true
}
return false
}