This commit is contained in:
laodaming 2023-09-20 16:49:05 +08:00
parent e1364838da
commit 6699a64169
2 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/product-template-tag/internal/logic"
"fusenapi/server/product-template-tag/internal/svc"
"fusenapi/server/product-template-tag/internal/types"
)
func GetTemplateTagColorHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetTemplateTagColorReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetTemplateTagColorLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.GetTemplateTagColor(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@ -0,0 +1,97 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"strings"
"context"
"fusenapi/server/product-template-tag/internal/svc"
"fusenapi/server/product-template-tag/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetTemplateTagColorLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetTemplateTagColorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTemplateTagColorLogic {
return &GetTemplateTagColorLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetTemplateTagColorLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetTemplateTagColorLogic) GetTemplateTagColor(req *types.GetTemplateTagColorReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.SelectedColorIndex < 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param selected_color_index is invalid")
}
//根据logo查询素材资源
s := strings.Split(req.Logo, "/")
if len(s) <= 1 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid logo")
}
resourceId := s[len(s)-1]
var (
userMaterial *gmodel.FsUserMaterial
err error
)
//游客或者用户
if userinfo.IsUser() || userinfo.IsGuest() {
userMaterial, err = l.svcCtx.AllModels.FsUserMaterial.FindOneByUserAndLogoUrl(l.ctx, userinfo.UserId, userinfo.GuestId, resourceId)
} else {
//白板用户
userMaterial, err = l.svcCtx.AllModels.FsUserMaterial.FindOneById(l.ctx, 0)
}
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the logo is not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user material")
}
if userMaterial.Metadata == nil || len(*userMaterial.Metadata) == 0 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the user material is empty")
}
//解析用户素材元数据
var metaData map[string]interface{}
if err = json.Unmarshal(*userMaterial.Metadata, &metaData); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse user metadata")
}
var mapMaterialTemplateTag map[string][][]string
b, _ := json.Marshal(metaData["template_tag"])
if err = json.Unmarshal(b, &mapMaterialTemplateTag); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "invalid format of metadata`s template_tag")
}
colors, ok := mapMaterialTemplateTag[req.TemplateTag]
if !ok {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "the template tag is not found from this logo material`s metadata")
}
if req.SelectedColorIndex >= len(colors) {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "select color index is out of range !!")
}
return resp.SetStatus(basic.CodeOK, "success", types.GetTemplateTagColorRsp{
Colors: colors,
SelectedColorIndex: req.SelectedColorIndex,
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetTemplateTagColorLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }