2023-08-14 09:56:06 +00:00
|
|
|
|
package repositories
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fusenapi/constants"
|
|
|
|
|
"fusenapi/model/gmodel"
|
|
|
|
|
"fusenapi/utils/curl"
|
|
|
|
|
"fusenapi/utils/file"
|
|
|
|
|
"fusenapi/utils/hash"
|
|
|
|
|
|
|
|
|
|
"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 {
|
2023-08-23 03:09:14 +00:00
|
|
|
|
// logo信息
|
|
|
|
|
LogoInfoSet(ctx context.Context, in *LogoInfoSetReq) (*LogoInfoSetRes, error)
|
2023-08-14 09:56:06 +00:00
|
|
|
|
|
|
|
|
|
// logo合图
|
|
|
|
|
LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error)
|
2023-08-18 08:47:22 +00:00
|
|
|
|
|
|
|
|
|
// logo裁剪
|
|
|
|
|
LogoStandard(ctx context.Context, in *LogoStandardReq) (*LogoStandardRes, error)
|
2023-08-14 09:56:06 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2023-08-23 03:09:14 +00:00
|
|
|
|
/* logo信息 */
|
|
|
|
|
type (
|
|
|
|
|
LogoInfoSetReq struct {
|
|
|
|
|
LogoUrl string `json:"logo_url"`
|
|
|
|
|
}
|
2023-08-23 06:22:36 +00:00
|
|
|
|
LogoInfoSetRes struct {
|
|
|
|
|
Res string `json:"res"`
|
|
|
|
|
}
|
2023-08-23 03:09:14 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (l *defaultImageHandle) LogoInfoSet(ctx context.Context, in *LogoInfoSetReq) (*LogoInfoSetRes, error) {
|
|
|
|
|
var resultBLM constants.BLMServiceUrlResult
|
|
|
|
|
postMap := make(map[string]string, 1)
|
|
|
|
|
postMap["logo_url"] = in.LogoUrl
|
|
|
|
|
|
2023-08-23 06:22:36 +00:00
|
|
|
|
err := curl.NewClient(ctx, &curl.Config{
|
2023-08-23 03:09:14 +00:00
|
|
|
|
BaseUrl: *l.BLMServiceUrl,
|
2023-08-23 07:10:22 +00:00
|
|
|
|
Url: constants.BLMServiceUrlLogoFeatureExtraction,
|
2023-08-23 06:22:36 +00:00
|
|
|
|
}).PostJson(postMap, &resultBLM)
|
2023-08-23 03:09:14 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-08-23 06:22:36 +00:00
|
|
|
|
if resultBLM.Code != "200" {
|
|
|
|
|
err = errors.New(resultBLM.Msg)
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-08-23 07:10:22 +00:00
|
|
|
|
return &LogoInfoSetRes{
|
|
|
|
|
Res: resultBLM.Data.(string),
|
|
|
|
|
}, nil
|
2023-08-23 03:09:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* logo信息 */
|
|
|
|
|
|
2023-08-14 09:56:06 +00:00
|
|
|
|
/* logo合图 */
|
|
|
|
|
type (
|
|
|
|
|
LogoCombineReq struct {
|
2023-08-15 02:58:24 +00:00
|
|
|
|
UserId int64 `json:"user_id"`
|
|
|
|
|
GuestId int64 `json:"guest_id"`
|
|
|
|
|
TemplateId int64 `json:"template_id"`
|
2023-08-16 08:08:48 +00:00
|
|
|
|
TemplateTag string `json:"template_tag"`
|
2023-08-18 09:16:49 +00:00
|
|
|
|
Website string `json:"website"` // 合图参数
|
|
|
|
|
Slogan string `json:"slogan"` // 合图参数
|
|
|
|
|
Address string `json:"address"` // 合图参数
|
|
|
|
|
Phone string `json:"phone"` // 合图参数
|
|
|
|
|
Qrcode string `json:"qrcode"` // 合图参数
|
|
|
|
|
LogoUrl string `json:"logo_url"` // 合图参数
|
2023-08-14 09:56:06 +00:00
|
|
|
|
}
|
|
|
|
|
LogoCombineRes struct {
|
|
|
|
|
ResourceId string
|
|
|
|
|
ResourceUrl *string
|
|
|
|
|
Metadata *string
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (l *defaultImageHandle) LogoCombine(ctx context.Context, in *LogoCombineReq) (*LogoCombineRes, error) {
|
2023-08-18 09:16:49 +00:00
|
|
|
|
// 查询logo最新基础信息
|
|
|
|
|
var metadata *string
|
2023-08-23 03:23:26 +00:00
|
|
|
|
var logoUrl *string
|
2023-08-18 09:16:49 +00:00
|
|
|
|
userMaterialModel := gmodel.NewFsUserMaterialModel(l.MysqlConn)
|
|
|
|
|
userMaterialInfo, err := userMaterialModel.FindLatestOne(ctx, in.UserId, in.GuestId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
userMaterialInfoDefault, err := userMaterialModel.FindOneById(ctx, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
metadata = userMaterialInfoDefault.Metadata
|
2023-08-23 03:23:26 +00:00
|
|
|
|
logoUrl = userMaterialInfoDefault.ResourceUrl
|
2023-08-18 09:16:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
metadata = userMaterialInfo.Metadata
|
2023-08-23 03:23:26 +00:00
|
|
|
|
logoUrl = userMaterialInfo.ResourceUrl
|
2023-08-18 09:16:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 09:56:06 +00:00
|
|
|
|
// 根据hash 查询数据资源
|
2023-08-16 08:33:25 +00:00
|
|
|
|
var hashKeyData = *in
|
|
|
|
|
hashKeyData.GuestId = 0
|
|
|
|
|
hashKeyData.UserId = 0
|
2023-08-23 03:23:26 +00:00
|
|
|
|
hashKeyData.LogoUrl = *logoUrl
|
2023-08-18 11:37:11 +00:00
|
|
|
|
var hashKeyDataMap map[string]interface{}
|
|
|
|
|
hashKeyDataB, _ := json.Marshal(hashKeyData)
|
|
|
|
|
json.Unmarshal(hashKeyDataB, &hashKeyDataMap)
|
|
|
|
|
var resourceId string = hash.JsonHashKey(hashKeyDataMap)
|
2023-08-15 02:58:24 +00:00
|
|
|
|
|
2023-08-14 09:56:06 +00:00
|
|
|
|
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{}
|
|
|
|
|
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{})
|
2023-08-17 03:29:32 +00:00
|
|
|
|
groupOptions = templateInfo["groupOptions"].(map[string]interface{})
|
2023-08-14 09:56:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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{}
|
2023-08-15 06:51:54 +00:00
|
|
|
|
json.Unmarshal([]byte(*metadata), &combineParam)
|
2023-08-15 02:58:24 +00:00
|
|
|
|
combineParam["template_tagid"] = in.TemplateTag
|
|
|
|
|
combineParam["website"] = in.Website
|
|
|
|
|
combineParam["slogan"] = in.Slogan
|
|
|
|
|
combineParam["phone"] = in.Phone
|
|
|
|
|
combineParam["address"] = in.Address
|
2023-08-15 03:20:09 +00:00
|
|
|
|
combineParam["qrcode"] = in.Qrcode
|
2023-08-15 02:58:24 +00:00
|
|
|
|
|
2023-08-14 09:56:06 +00:00
|
|
|
|
var postMap = make(map[string]interface{}, 2)
|
|
|
|
|
postMap["module_data"] = moduleDataMap
|
|
|
|
|
postMap["param_data"] = combineParam
|
|
|
|
|
|
2023-08-23 06:22:36 +00:00
|
|
|
|
var resultBLM constants.BLMServiceUrlResult
|
|
|
|
|
err = curl.NewClient(ctx, &curl.Config{
|
|
|
|
|
BaseUrl: *l.BLMServiceUrl,
|
|
|
|
|
Url: constants.BLMServiceUrlLogoCombine,
|
|
|
|
|
}).PostJson(postMap, &resultBLM)
|
2023-08-14 09:56:06 +00:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-08-22 02:02:50 +00:00
|
|
|
|
|
2023-08-23 06:22:36 +00:00
|
|
|
|
if resultBLM.Code != "200" {
|
|
|
|
|
err = errors.New(resultBLM.Msg)
|
2023-08-15 03:59:35 +00:00
|
|
|
|
logx.Error(err)
|
2023-08-14 09:56:06 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 06:22:36 +00:00
|
|
|
|
var resultStr string = resultBLM.Data.(string)
|
2023-08-14 09:56:06 +00:00
|
|
|
|
var resultData map[string]interface{}
|
2023-08-15 10:35:16 +00:00
|
|
|
|
err = json.Unmarshal([]byte(resultStr), &resultData)
|
|
|
|
|
if err != nil || resultData == nil {
|
2023-08-14 09:56:06 +00:00
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-08-18 10:41:51 +00:00
|
|
|
|
|
2023-08-17 09:20:43 +00:00
|
|
|
|
var fileBase = resultData["result"].(string)
|
2023-08-14 09:56:06 +00:00
|
|
|
|
|
|
|
|
|
// 上传文件
|
|
|
|
|
var upload = file.Upload{
|
|
|
|
|
Ctx: ctx,
|
|
|
|
|
MysqlConn: l.MysqlConn,
|
|
|
|
|
AwsSession: l.AwsSession,
|
|
|
|
|
}
|
|
|
|
|
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
2023-08-16 09:33:34 +00:00
|
|
|
|
Source: "combine-image",
|
2023-08-14 09:56:06 +00:00
|
|
|
|
FileHash: resourceId,
|
2023-08-17 09:20:43 +00:00
|
|
|
|
FileData: fileBase,
|
2023-08-14 09:56:06 +00:00
|
|
|
|
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合图 */
|
2023-08-18 08:47:22 +00:00
|
|
|
|
type (
|
|
|
|
|
LogoStandardReq struct {
|
|
|
|
|
IsRemoveBg string `json:"is_remove_bg"`
|
|
|
|
|
LogoFile string `json:"logo_file"`
|
|
|
|
|
Width string `json:"width"`
|
|
|
|
|
Height string `json:"height"`
|
2023-08-23 03:09:14 +00:00
|
|
|
|
Proportion string `json:"proportion"`
|
2023-08-18 08:47:22 +00:00
|
|
|
|
}
|
2023-08-18 09:16:49 +00:00
|
|
|
|
LogoStandardRes struct {
|
|
|
|
|
ResourceId string
|
|
|
|
|
ResourceUrl string
|
|
|
|
|
IsmaxProportion bool
|
|
|
|
|
ImgColor []string
|
|
|
|
|
}
|
2023-08-21 06:39:03 +00:00
|
|
|
|
LogoStandardMetaData struct {
|
|
|
|
|
Param LogoStandardReq `json:"param"`
|
|
|
|
|
Result LogoStandardRes `json:"result"`
|
|
|
|
|
}
|
2023-08-18 08:47:22 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/* 图片裁剪 */
|
|
|
|
|
func (l *defaultImageHandle) LogoStandard(ctx context.Context, in *LogoStandardReq) (*LogoStandardRes, error) {
|
2023-08-21 06:39:03 +00:00
|
|
|
|
var ismaxProportion bool
|
|
|
|
|
var imgColor []string
|
|
|
|
|
var logoStandardMetaData LogoStandardMetaData
|
|
|
|
|
|
2023-08-21 02:09:26 +00:00
|
|
|
|
var hashKeyDataMap map[string]interface{}
|
|
|
|
|
hashKeyDataB, _ := json.Marshal(in)
|
|
|
|
|
json.Unmarshal(hashKeyDataB, &hashKeyDataMap)
|
|
|
|
|
var resourceId string = hash.JsonHashKey(hashKeyDataMap)
|
|
|
|
|
|
2023-08-21 06:39:03 +00:00
|
|
|
|
resourceModel := gmodel.NewFsResourceModel(l.MysqlConn)
|
|
|
|
|
resourceInfo, err := resourceModel.FindOneById(ctx, resourceId)
|
|
|
|
|
if err == nil && resourceInfo.ResourceId != "" {
|
|
|
|
|
if resourceInfo.Metadata != nil {
|
|
|
|
|
json.Unmarshal([]byte(*resourceInfo.Metadata), &logoStandardMetaData)
|
|
|
|
|
}
|
|
|
|
|
// 取出参数
|
2023-08-21 06:59:21 +00:00
|
|
|
|
ismaxProportion = logoStandardMetaData.Result.IsmaxProportion
|
|
|
|
|
imgColor = logoStandardMetaData.Result.ImgColor
|
2023-08-21 06:39:03 +00:00
|
|
|
|
|
|
|
|
|
return &LogoStandardRes{
|
|
|
|
|
ResourceId: resourceInfo.ResourceId,
|
|
|
|
|
ResourceUrl: *resourceInfo.ResourceUrl,
|
|
|
|
|
IsmaxProportion: ismaxProportion,
|
|
|
|
|
ImgColor: imgColor,
|
|
|
|
|
}, nil
|
|
|
|
|
} else {
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 03:09:14 +00:00
|
|
|
|
var resultBLM constants.BLMServiceUrlResult
|
2023-08-18 09:51:51 +00:00
|
|
|
|
var postMap = make(map[string]interface{}, 5)
|
2023-08-18 09:16:49 +00:00
|
|
|
|
postMap["is_remove_bg"] = in.IsRemoveBg
|
|
|
|
|
postMap["logo_file"] = in.LogoFile
|
|
|
|
|
postMap["width"] = in.Width
|
|
|
|
|
postMap["height"] = in.Height
|
|
|
|
|
postMap["proportion"] = in.Proportion
|
|
|
|
|
|
2023-08-23 03:09:14 +00:00
|
|
|
|
err = curl.NewClient(ctx, &curl.Config{
|
|
|
|
|
BaseUrl: *l.BLMServiceUrl,
|
|
|
|
|
Url: constants.BLMServiceUrlLogoRemovebg,
|
|
|
|
|
}).PostJson(postMap, &resultBLM)
|
|
|
|
|
|
2023-08-18 09:16:49 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-08-23 06:22:36 +00:00
|
|
|
|
|
|
|
|
|
if resultBLM.Code != "200" {
|
|
|
|
|
err = errors.New(resultBLM.Msg)
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 03:09:14 +00:00
|
|
|
|
var resultStr string = resultBLM.Data.(string)
|
2023-08-18 09:16:49 +00:00
|
|
|
|
var resultData map[string]interface{}
|
|
|
|
|
err = json.Unmarshal([]byte(resultStr), &resultData)
|
|
|
|
|
if err != nil || resultData == nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-08-21 06:39:03 +00:00
|
|
|
|
|
2023-08-18 09:38:12 +00:00
|
|
|
|
var fileBase = resultData["nobg_url"].(string)
|
2023-08-21 06:39:03 +00:00
|
|
|
|
ismaxProportion = resultData["ismax_proportion"].(bool)
|
2023-08-18 09:38:12 +00:00
|
|
|
|
|
|
|
|
|
for _, v := range resultData["img_color"].([]interface{}) {
|
|
|
|
|
imgColor = append(imgColor, v.(string))
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 06:59:21 +00:00
|
|
|
|
var logoStandardRes LogoStandardRes
|
|
|
|
|
logoStandardRes.IsmaxProportion = ismaxProportion
|
|
|
|
|
logoStandardRes.ImgColor = imgColor
|
|
|
|
|
|
|
|
|
|
logoStandardMetaData.Param = *in
|
|
|
|
|
logoStandardMetaData.Result = logoStandardRes
|
|
|
|
|
metadataB, err := json.Marshal(logoStandardMetaData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var metadata = string(metadataB)
|
|
|
|
|
|
2023-08-18 09:38:12 +00:00
|
|
|
|
// 上传文件
|
|
|
|
|
var upload = file.Upload{
|
|
|
|
|
Ctx: ctx,
|
|
|
|
|
MysqlConn: l.MysqlConn,
|
|
|
|
|
AwsSession: l.AwsSession,
|
|
|
|
|
}
|
|
|
|
|
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
|
|
|
|
|
Source: "combine-removebg",
|
|
|
|
|
FileHash: resourceId,
|
|
|
|
|
FileData: fileBase,
|
|
|
|
|
UploadBucket: 1,
|
|
|
|
|
ApiType: 2,
|
2023-08-21 06:59:21 +00:00
|
|
|
|
Metadata: metadata,
|
2023-08-18 09:38:12 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &LogoStandardRes{
|
|
|
|
|
ResourceId: uploadRes.ResourceId,
|
|
|
|
|
ResourceUrl: uploadRes.ResourceUrl,
|
|
|
|
|
IsmaxProportion: ismaxProportion,
|
|
|
|
|
ImgColor: imgColor,
|
|
|
|
|
}, nil
|
2023-08-18 08:47:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 图片裁剪 */
|