119 lines
3.8 KiB
Go
119 lines
3.8 KiB
Go
package logic
|
||
|
||
import (
|
||
"fmt"
|
||
"fusenapi/model/gmodel"
|
||
"fusenapi/service/repositories"
|
||
"fusenapi/utils/auth"
|
||
"fusenapi/utils/basic"
|
||
"fusenapi/utils/template_switch_info"
|
||
|
||
"context"
|
||
|
||
"fusenapi/server/resource/internal/svc"
|
||
"fusenapi/server/resource/internal/types"
|
||
|
||
"github.com/zeromicro/go-zero/core/logc"
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
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
|
||
}
|
||
// 获取默认
|
||
if req.Address == "" || req.Phone == "" || req.Website == "" || req.Qrcode == "" || req.Slogan == "" {
|
||
// 没有查到,先根据模版id 查询模版数据 请求算法数据
|
||
productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
|
||
productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, req.TemplateId)
|
||
|
||
if err != nil {
|
||
logc.Errorf(l.ctx, "productTemplateV2Model.FindOne:%v", err)
|
||
return resp.SetStatus(basic.CodeServiceErr, "模版不存在")
|
||
}
|
||
templateSwitchInfo, err := template_switch_info.GetTemplateSwitchInfo(req.TemplateId, productTemplateV2Info.TemplateInfo, *productTemplateV2Info.MaterialImg)
|
||
if err != nil {
|
||
logc.Errorf(l.ctx, "template_switch_info.GetTemplateSwitchInfo,err:%v", err)
|
||
return resp.SetStatus(basic.CodeServiceErr, "默认模版不存在")
|
||
}
|
||
if req.Address == "" && templateSwitchInfo.MaterialData.Address.IfShow {
|
||
req.Address = templateSwitchInfo.MaterialData.Address.DefaultValue
|
||
}
|
||
if req.Phone == "" && templateSwitchInfo.MaterialData.Phone.IfShow {
|
||
req.Phone = templateSwitchInfo.MaterialData.Phone.DefaultValue
|
||
}
|
||
if req.Website == "" && templateSwitchInfo.MaterialData.Website.IfShow {
|
||
req.Website = templateSwitchInfo.MaterialData.Website.DefaultValue
|
||
}
|
||
if req.Qrcode == "" && templateSwitchInfo.MaterialData.QRcode.IfShow {
|
||
req.Qrcode = templateSwitchInfo.MaterialData.QRcode.DefaultValue
|
||
}
|
||
if req.Slogan == "" && templateSwitchInfo.MaterialData.Slogan.IfShow {
|
||
req.Slogan = templateSwitchInfo.MaterialData.Slogan.DefaultValue
|
||
}
|
||
}
|
||
|
||
res, err := l.svcCtx.Repositories.ImageHandle.LogoCombine(l.ctx, &repositories.LogoCombineReq{
|
||
UserId: userId,
|
||
GuestId: guestId,
|
||
TemplateId: req.TemplateId,
|
||
TemplateTag: req.TemplateTag,
|
||
Website: req.Website,
|
||
Slogan: req.Slogan,
|
||
Phone: req.Phone,
|
||
Address: req.Address,
|
||
|
||
Qrcode: req.Qrcode,
|
||
})
|
||
|
||
if err != nil {
|
||
return resp.SetStatus(basic.CodeServiceErr, fmt.Sprintf("算法请求--LOGO信息--错误:%+v", err))
|
||
}
|
||
|
||
// 返回成功的响应和上传URL
|
||
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
|
||
"resource_id": res.ResourceId,
|
||
"resource_url": res.ResourceUrl,
|
||
"resource_metadata": "",
|
||
})
|
||
}
|