fix:裁剪业务逻辑调整

This commit is contained in:
momo 2023-08-23 14:22:36 +08:00
parent c942855683
commit 89d8a8963c
6 changed files with 56 additions and 106 deletions

View File

@ -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 {

View File

@ -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()

View File

@ -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 {

View File

@ -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
}
)

View File

@ -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)

View File

@ -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 {