From 89d8a8963cf3cb2a9301b77ade1da961dffce964 Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Wed, 23 Aug 2023 14:22:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=A3=81=E5=89=AA=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/logic/uploadfilebaselogic.go | 1 + .../upload/internal/logic/uploadlogologic.go | 75 ++++++------------ server/upload/internal/types/types.go | 1 + server_api/upload.api | 1 + service/repositories/image_handle.go | 78 +++++++------------ utils/file/upload.go | 6 +- 6 files changed, 56 insertions(+), 106 deletions(-) diff --git a/server/upload/internal/logic/uploadfilebaselogic.go b/server/upload/internal/logic/uploadfilebaselogic.go index 887d6297..1a25fa1b 100644 --- a/server/upload/internal/logic/uploadfilebaselogic.go +++ b/server/upload/internal/logic/uploadfilebaselogic.go @@ -80,6 +80,7 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri Source: req.Source, Refresh: req.Refresh, Metadata: req.Metadata, + ResourceId: req.ResourceId, }) if err != nil { diff --git a/server/upload/internal/logic/uploadlogologic.go b/server/upload/internal/logic/uploadlogologic.go index 8cdf3f53..8120d330 100644 --- a/server/upload/internal/logic/uploadlogologic.go +++ b/server/upload/internal/logic/uploadlogologic.go @@ -1,18 +1,14 @@ package logic import ( - "encoding/json" - "errors" - "fmt" "fusenapi/model/gmodel" + "fusenapi/service/repositories" "fusenapi/utils/auth" "fusenapi/utils/basic" - "fusenapi/utils/curl" "fusenapi/utils/file" "fusenapi/utils/hash" "io" "net/http" - "strings" "time" "context" @@ -126,13 +122,25 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us Source: "upload-logo", }) + var logoUrl string + // 是否去背景 if req.IsRemoveBg == 1 { - // res, err := l.svcCtx.Repositories.ImageHandle.LogoInfoSet(l.ctx, &repositories.LogoInfoSetReq{}) + resLogoStandard, err := l.svcCtx.Repositories.ImageHandle.LogoStandard(l.ctx, &repositories.LogoStandardReq{ + LogoFile: uploadRes.ResourceUrl, + IsRemoveBg: "true", + Width: "0", + Height: "0", + Proportion: "100", + }) + logoUrl = resLogoStandard.ResourceUrl - // if err != nil { - // return resp.SetStatus(basic.CodeServiceErr) - // } + if err != nil { + logx.Error(err) + return resp.SetStatus(basic.CodeServiceErr) + } + } else { + logoUrl = uploadRes.ResourceUrl } if err != nil { @@ -140,8 +148,6 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us return resp.SetStatus(basic.CodeFileUploadErr, "upload file failed") } - logx.Infof("上传logo请求算法--开始时间:%v", time.Now()) - var logoWidth int64 var logoHeight int64 // 查看sku是否存在 @@ -165,52 +171,17 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us var resultStr string var postMap = make(map[string]string, 1) - postMap["logo_url"] = uploadRes.ResourceUrl - postMapB, _ := json.Marshal(postMap) - fmt.Println(string(postMapB)) + postMap["logo_url"] = logoUrl - var headerData = make(map[string]string, 1) - headerData["Content-Type"] = "application/json" - result, err := curl.ApiCall(l.svcCtx.Config.BLMService.ImageProcess.Url, "POST", headerData, strings.NewReader(string(postMapB)), time.Minute*5) + resLogoStandard, err := l.svcCtx.Repositories.ImageHandle.LogoInfoSet(l.ctx, &repositories.LogoInfoSetReq{ + LogoUrl: logoUrl, + }) if err != nil { logx.Error(err) - return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 01") - } - defer result.Body.Close() - b, err := io.ReadAll(result.Body) - if err != nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 02") - } - - logx.Infof("上传logo请求算法--结束时间:%v", time.Now()) - logx.Infof("上传logo请求算法--返回结果:%v", string(b)) - - if string(b) == "Internal Server Error" { - err = errors.New("BLMService fail Internal Server Error") - logx.Error(err) - return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 03") - } else { - var resData map[string]interface{} - err = json.Unmarshal(b, &resData) - if err != nil || resData == nil { - logx.Error(err) - return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 04") - } - - if resData != nil { - if resData["code"].(string) == "200" { - resultStr = resData["data"].(string) - } else { - logx.Error(err) - return resp.SetStatus(basic.CodeFileUploadLogoErrType, "service fail 05") - } - } else { - logx.Error(err) - return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 06") - } + return resp.SetStatus(basic.CodeServiceErr) } + resultStr = resLogoStandard.Res var module = "logo" var nowTime = time.Now().Unix() diff --git a/server/upload/internal/types/types.go b/server/upload/internal/types/types.go index ef9f9a62..b633d713 100644 --- a/server/upload/internal/types/types.go +++ b/server/upload/internal/types/types.go @@ -23,6 +23,7 @@ type UploadFileBaseReq struct { UploadBucket int64 `form:"upload_bucket,options=[1,2],default=1"` // 上传桶名:1=缓存,2=持久 Source string `form:"source"` // 上传来源 Refresh int64 `form:"refresh,optional"` // 强制更新 10 + ResourceId string `form:"resource_id,optional"` // 资源ID } type UploadLogoReq struct { diff --git a/server_api/upload.api b/server_api/upload.api index 3b847250..5711e58c 100644 --- a/server_api/upload.api +++ b/server_api/upload.api @@ -68,6 +68,7 @@ type ( UploadBucket int64 `form:"upload_bucket,options=[1,2],default=1"` // 上传桶名:1=缓存,2=持久 Source string `form:"source"` // 上传来源 Refresh int64 `form:"refresh,optional"` // 强制更新 10 + ResourceId string `form:"resource_id,optional"` // 资源ID } ) diff --git a/service/repositories/image_handle.go b/service/repositories/image_handle.go index 28ee5061..43a68c54 100644 --- a/service/repositories/image_handle.go +++ b/service/repositories/image_handle.go @@ -4,15 +4,11 @@ import ( "context" "encoding/json" "errors" - "fmt" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/curl" "fusenapi/utils/file" "fusenapi/utils/hash" - "io" - "strings" - "time" "github.com/aws/aws-sdk-go/aws/session" "github.com/zeromicro/go-zero/core/logx" @@ -51,7 +47,9 @@ type ( LogoInfoSetReq struct { LogoUrl string `json:"logo_url"` } - LogoInfoSetRes struct{} + LogoInfoSetRes struct { + Res string `json:"res"` + } ) func (l *defaultImageHandle) LogoInfoSet(ctx context.Context, in *LogoInfoSetReq) (*LogoInfoSetRes, error) { @@ -59,14 +57,19 @@ func (l *defaultImageHandle) LogoInfoSet(ctx context.Context, in *LogoInfoSetReq postMap := make(map[string]string, 1) postMap["logo_url"] = in.LogoUrl - _, err := curl.NewClient(ctx, &curl.Config{ + err := curl.NewClient(ctx, &curl.Config{ BaseUrl: *l.BLMServiceUrl, Url: constants.BLMServiceUrlLogoCombine, - }).PostForm(postMap, &resultBLM) + }).PostJson(postMap, &resultBLM) if err != nil { logx.Error(err) return nil, err } + if resultBLM.Code != "200" { + err = errors.New(resultBLM.Msg) + logx.Error(err) + return nil, err + } return nil, nil } @@ -166,9 +169,6 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq groupOptions = templateInfo["groupOptions"].(map[string]interface{}) } - fmt.Println("合图开始时间:", time.Now()) - logx.Infof("合图请求算法--开始时间:%v", time.Now()) - var moduleDataMap = make(map[string]interface{}, 4) moduleDataMap["id"] = productTemplateV2Info.Id moduleDataMap["material"] = productTemplateV2Info.MaterialImg @@ -185,54 +185,27 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq combineParam["qrcode"] = in.Qrcode var postMap = make(map[string]interface{}, 2) - fmt.Println(combineParam) postMap["module_data"] = moduleDataMap postMap["param_data"] = combineParam - postMapB, _ := json.Marshal(postMap) - var headerData = make(map[string]string, 1) - headerData["Content-Type"] = "application/json" - result, err := curl.ApiCall(*l.BLMServiceUrl+constants.BLMServiceUrlLogoCombine, "POST", headerData, strings.NewReader(string(postMapB)), time.Minute*5) + var resultBLM constants.BLMServiceUrlResult + err = curl.NewClient(ctx, &curl.Config{ + BaseUrl: *l.BLMServiceUrl, + Url: constants.BLMServiceUrlLogoCombine, + }).PostJson(postMap, &resultBLM) if err != nil { logx.Error(err) return nil, err } - defer result.Body.Close() - b, err := io.ReadAll(result.Body) - if err != nil { + + if resultBLM.Code != "200" { + err = errors.New(resultBLM.Msg) logx.Error(err) return nil, err } - logx.Infof("合图请求算法--结束时间:%v", time.Now()) - - var resultStr string - if string(b) == "Internal Server Error" { - err = errors.New("BLMService fail Internal Server Error") - logx.Error(err) - return nil, err - } else { - var resData map[string]interface{} - err = json.Unmarshal(b, &resData) - if err != nil || resData == nil { - logx.Error(err) - return nil, err - } - - if resData != nil { - if resData["code"].(string) == "200" { - resultStr = resData["data"].(string) - } else { - logx.Error(err) - return nil, err - } - } else { - logx.Error(err) - return nil, err - } - } - + var resultStr string = resultBLM.Data.(string) var resultData map[string]interface{} err = json.Unmarshal([]byte(resultStr), &resultData) if err != nil || resultData == nil { @@ -240,12 +213,6 @@ func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq return nil, err } - fmt.Println("合图结束时间:", time.Now()) - // { - // id: "", - // logo_url:"https://s3.amazon.com/xxxx", - // result: "$saa541afaldjaldjasldjsadjsapsaasda" - // } var fileBase = resultData["result"].(string) // 上传文件 @@ -347,6 +314,13 @@ func (l *defaultImageHandle) LogoStandard(ctx context.Context, in *LogoStandardR logx.Error(err) return nil, err } + + if resultBLM.Code != "200" { + err = errors.New(resultBLM.Msg) + logx.Error(err) + return nil, err + } + var resultStr string = resultBLM.Data.(string) var resultData map[string]interface{} err = json.Unmarshal([]byte(resultStr), &resultData) diff --git a/utils/file/upload.go b/utils/file/upload.go index 937579e7..4539ef2f 100644 --- a/utils/file/upload.go +++ b/utils/file/upload.go @@ -22,6 +22,7 @@ type Upload struct { } type UploadBaseReq struct { + ResourceId string Refresh int64 Source string FileHash string @@ -71,15 +72,16 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er err := upload.MysqlConn.Transaction(func(tx *gorm.DB) (err error) { var resourceInfo *gmodel.FsResource - if req.Refresh == 0 { + if req.ResourceId == "" { err = tx.Where("resource_id =?", resourceId).Take(&resourceInfo).Error } - if req.Refresh == 0 && err == nil && resourceInfo.ResourceId != "" { + if req.ResourceId == "" && err == nil && resourceInfo.ResourceId != "" { uploadBaseRes.Status = 1 uploadBaseRes.ResourceId = resourceId uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl } else { + resourceId = req.ResourceId dist, contentType, err := FileBase64ToByte(req.FileData) if err != nil {