fix
This commit is contained in:
35
server/render/internal/handler/getfaceslicehandler.go
Normal file
35
server/render/internal/handler/getfaceslicehandler.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/render/internal/logic"
|
||||
"fusenapi/server/render/internal/svc"
|
||||
"fusenapi/server/render/internal/types"
|
||||
)
|
||||
|
||||
func GetFaceSliceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.Request
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewGetFaceSliceLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.GetFaceSlice(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/api/render/render_notify",
|
||||
Handler: RenderNotifyHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/render/get_face_slice",
|
||||
Handler: GetFaceSliceHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
69
server/render/internal/logic/getfaceslicelogic.go
Normal file
69
server/render/internal/logic/getfaceslicelogic.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/render/internal/svc"
|
||||
"fusenapi/server/render/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetFaceSliceLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetFaceSliceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFaceSliceLogic {
|
||||
return &GetFaceSliceLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *GetFaceSliceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *GetFaceSliceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *GetFaceSliceLogic) GetFaceSlice(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if !userinfo.IsUser() && !userinfo.IsGuest() {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login or access cookie")
|
||||
}
|
||||
//获取用户素材信息
|
||||
materialInfo, err := l.svcCtx.AllModels.FsUserMaterial.FindLatestOne(l.ctx, userinfo.UserId, userinfo.GuestId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "user material info is not exists")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user material info")
|
||||
}
|
||||
if materialInfo.Metadata == nil || *materialInfo.Metadata == "" {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "user material info`Metadata is empty")
|
||||
}
|
||||
var info map[string]interface{}
|
||||
if err = json.Unmarshal([]byte(*materialInfo.Metadata), &info); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "invalid json format of metadata")
|
||||
}
|
||||
str := strings.ReplaceAll(constants.RENDER_FACE_SLICE_TEMPLATE_JSON, "{{MainColorFill}}", info["main_color_fill"].(string))
|
||||
str = strings.ReplaceAll(str, "{{SecondaryColorFill}}", info["secondary_color_fill"].(string))
|
||||
str = strings.ReplaceAll(str, "{{LogoMaterial}}", info["logo_material"].(string))
|
||||
var rspInfo interface{}
|
||||
_ = json.Unmarshal([]byte(str), &rspInfo)
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", rspInfo)
|
||||
}
|
||||
Reference in New Issue
Block a user