180 lines
4.4 KiB
Go
180 lines
4.4 KiB
Go
package repositories
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"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"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
func NewImageHandle(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) ImageHandle {
|
||
return &defaultImageHandle{
|
||
MysqlConn: gormDB,
|
||
BLMServiceUrl: bLMServiceUrl,
|
||
AwsSession: awsSession,
|
||
}
|
||
}
|
||
|
||
type (
|
||
defaultImageHandle struct {
|
||
MysqlConn *gorm.DB
|
||
BLMServiceUrl *string
|
||
AwsSession *session.Session
|
||
}
|
||
|
||
ImageHandle = interface {
|
||
|
||
// logo合图
|
||
LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error)
|
||
}
|
||
)
|
||
|
||
/* logo合图 */
|
||
type (
|
||
LogoCombineReq struct {
|
||
ResourceKey string `json:"resource_key"`
|
||
TemplateId int64 `json:"template_id"`
|
||
CombineParam string `json:"combine_param"`
|
||
UserId int64 `json:"user_id"`
|
||
GuestId int64 `json:"guest_id"`
|
||
}
|
||
LogoCombineRes struct {
|
||
ResourceId string
|
||
ResourceUrl *string
|
||
Metadata *string
|
||
}
|
||
)
|
||
|
||
func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) {
|
||
// 根据hash 查询数据资源
|
||
var resourceId string = hash.JsonHashKey(in.ResourceKey)
|
||
resourceModel := gmodel.NewFsResourceModel(l.MysqlConn)
|
||
resourceInfo, err := resourceModel.FindOneById(ctx, resourceId)
|
||
if err == nil && resourceInfo.ResourceId != "" {
|
||
return &LogoCombineRes{
|
||
ResourceId: resourceId,
|
||
ResourceUrl: resourceInfo.ResourceUrl,
|
||
}, nil
|
||
} else {
|
||
if err != nil {
|
||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
}
|
||
}
|
||
|
||
// 没有查到,先根据模版id 查询模版数据 请求算法数据
|
||
productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.MysqlConn)
|
||
productTemplateV2Info, err := productTemplateV2Model.FindOne(ctx, in.TemplateId)
|
||
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
var groupOptions map[string]interface{}
|
||
if productTemplateV2Info.GroupOptions != nil {
|
||
err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions)
|
||
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
}
|
||
|
||
var materialList []interface{}
|
||
if productTemplateV2Info.TemplateInfo != nil {
|
||
var templateInfo map[string]interface{}
|
||
err = json.Unmarshal([]byte(*productTemplateV2Info.TemplateInfo), &templateInfo)
|
||
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
materialList = templateInfo["materialList"].([]interface{})
|
||
}
|
||
|
||
var moduleDataMap = make(map[string]interface{}, 4)
|
||
moduleDataMap["id"] = productTemplateV2Info.Id
|
||
moduleDataMap["material"] = productTemplateV2Info.MaterialImg
|
||
moduleDataMap["groupOptions"] = groupOptions
|
||
moduleDataMap["materialList"] = materialList
|
||
|
||
var combineParam map[string]interface{}
|
||
json.Unmarshal([]byte(in.CombineParam), &combineParam)
|
||
var postMap = make(map[string]interface{}, 2)
|
||
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.Second*20)
|
||
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
defer result.Body.Close()
|
||
b, err := io.ReadAll(result.Body)
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
ress := string(b)
|
||
|
||
if ress == "Internal Server Error" {
|
||
logx.Error(errors.New("BLMService fail Internal Server Error"))
|
||
return nil, err
|
||
}
|
||
|
||
var resultData map[string]interface{}
|
||
err = json.Unmarshal(b, &resultData)
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
// {
|
||
// id: "",
|
||
// logo_url:"https://s3.amazon.com/xxxx",
|
||
// result: "$saa541afaldjaldjasldjsadjsapsaasda"
|
||
// }
|
||
var fileBase = resultData["result"]
|
||
|
||
// 上传文件
|
||
var upload = file.Upload{
|
||
Ctx: ctx,
|
||
MysqlConn: l.MysqlConn,
|
||
AwsSession: l.AwsSession,
|
||
}
|
||
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
||
FileHash: resourceId,
|
||
FileData: fileBase.(string),
|
||
UploadBucket: 1,
|
||
ApiType: 2,
|
||
UserId: in.UserId,
|
||
GuestId: in.GuestId,
|
||
})
|
||
if err != nil {
|
||
logx.Error(err)
|
||
return nil, err
|
||
}
|
||
return &LogoCombineRes{
|
||
ResourceId: uploadRes.ResourceId,
|
||
ResourceUrl: &uploadRes.ResourceUrl,
|
||
}, nil
|
||
}
|
||
|
||
/* logo合图 */
|