fusenapi/server/resource/internal/logic/logocombinelogic.go

190 lines
5.6 KiB
Go
Raw Normal View History

package logic
import (
"encoding/json"
2023-08-11 07:28:03 +00:00
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-08-11 07:30:55 +00:00
"fusenapi/utils/curl"
"fusenapi/utils/file"
"fusenapi/utils/hash"
"io"
"strings"
2023-08-11 07:30:55 +00:00
"time"
"context"
"fusenapi/server/resource/internal/svc"
"fusenapi/server/resource/internal/types"
"github.com/zeromicro/go-zero/core/logx"
2023-08-11 07:28:03 +00:00
"gorm.io/gorm"
)
type LogoCombineLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewLogoCombineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoCombineLogic {
return &LogoCombineLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *LogoCombineLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *LogoCombineLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *LogoCombineLogic) LogoCombine(req *types.LogoCombineReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if userinfo.IsOnlooker() {
// 如果是,返回未授权的错误码
return resp.SetStatus(basic.CodeUnAuth)
}
var userId int64
var guestId int64
// 检查用户是否是游客
if userinfo.IsGuest() {
// 如果是使用游客ID和游客键名格式
guestId = userinfo.GuestId
} else {
// 否则使用用户ID和用户键名格式
userId = userinfo.UserId
}
// 根据hash 查询数据资源
var resourceId string = hash.JsonHashKey(req.ResourceKey)
resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn)
resourceInfo, err := resourceModel.FindOneById(l.ctx, resourceId)
if err == nil && resourceInfo.ResourceId != "" {
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"resource_id": resourceId,
"resource_url": resourceInfo.ResourceUrl,
"resource_metadata": resourceInfo.Metadata,
})
} else {
if err != nil {
2023-08-11 07:28:03 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error")
}
}
}
// 没有查到先根据模版id 查询模版数据 请求算法数据
productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, req.TemplateId)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error")
}
var groupOptions map[string]interface{}
if productTemplateV2Info.GroupOptions != nil {
err = json.Unmarshal([]byte(*productTemplateV2Info.GroupOptions), &groupOptions)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal groupOptions")
}
}
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 resp.SetStatus(basic.CodeDbSqlErr, "LogoCombine error Unmarshal templateInfo")
}
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(req.CombineParam), &combineParam)
var postMap = make(map[string]interface{}, 2)
postMap["module_data"] = moduleDataMap
postMap["param_data"] = combineParam
postMapB, _ := json.Marshal(postMap)
2023-08-11 07:30:55 +00:00
//result, err := http.Post(l.svcCtx.Config.BLMService.LogoCombine.Url, "application/json", strings.NewReader(string(postMapB)))
var headerData = make(map[string]string, 1)
headerData["Content-Type"] = "application/json"
result, err := curl.ApiCall(l.svcCtx.Config.BLMService.LogoCombine.Url, "POST", headerData, strings.NewReader(string(postMapB)), time.Second*20)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service post fail")
}
defer result.Body.Close()
b, err := io.ReadAll(result.Body)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail")
}
2023-08-11 07:28:03 +00:00
ress := string(b)
2023-08-11 07:28:03 +00:00
if ress == "Internal Server Error" {
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail")
}
var resultData map[string]interface{}
err = json.Unmarshal(b, &resultData)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileLogoCombineErr, "service read fail")
}
// {
// id: "",
// logo_url:"https://s3.amazon.com/xxxx"
// result: "$saa541afaldjaldjasldjsadjsapsaasda"
// }
var fileBase = resultData["result"]
// 上传文件
var upload = file.Upload{
Ctx: l.ctx,
MysqlConn: l.svcCtx.MysqlConn,
AwsSession: l.svcCtx.AwsSession,
}
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
FileHash: resourceId,
FileData: fileBase.(string),
UploadBucket: 1,
ApiType: 2,
UserId: userId,
GuestId: guestId,
})
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileLogoCombineErr, "LogoCombine error upload file failed")
}
// 返回成功的响应和上传URL
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"resource_id": resourceId,
"resource_url": uploadRes.ResourceUrl,
"resource_metadata": "",
})
}