This commit is contained in:
laodaming 2023-07-19 10:48:52 +08:00
parent c6f445708d
commit d466364385
6 changed files with 122 additions and 66 deletions

View File

@ -15,7 +15,7 @@ import (
"fusenapi/server/product/internal/types"
)
func GetThousandFaceDesignByPidHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func GetLastProductDesignHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
@ -53,7 +53,7 @@ func GetThousandFaceDesignByPidHandler(svcCtx *svc.ServiceContext) http.HandlerF
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
}
var req types.GetThousandFaceDesignByPidReq
var req types.Request
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
@ -64,8 +64,8 @@ func GetThousandFaceDesignByPidHandler(svcCtx *svc.ServiceContext) http.HandlerF
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetThousandFaceDesignByPidLogic(r.Context(), svcCtx)
resp := l.GetThousandFaceDesignByPid(&req, userinfo)
l := logic.NewGetLastProductDesignLogic(r.Context(), svcCtx)
resp := l.GetLastProductDesign(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)

View File

@ -104,8 +104,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodGet,
Path: "/api/product/get_thousand_face_design_by_pid",
Handler: GetThousandFaceDesignByPidHandler(serverCtx),
Path: "/api/product/get_last_product_design",
Handler: GetLastProductDesignHandler(serverCtx),
},
},
)

View File

@ -0,0 +1,110 @@
package logic
import (
"context"
"encoding/json"
"errors"
"fusenapi/constants"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetLastProductDesignLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetLastProductDesignLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLastProductDesignLogic {
return &GetLastProductDesignLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetLastProductDesignLogic) GetLastProductDesign(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
if !userinfo.IsUser() {
return resp.SetStatusAddMessage(basic.CodeUnAuth, "please login")
}
//获取用户信息
user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "user not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user info")
}
//若没打开了个性化渲染按钮
if *user.IsOpenRender != 1 {
return resp.SetStatusWithMessage(basic.CodeOK, "success:IsOpenRender switch is closed")
}
//查询用户最近下单成功的数据
orderInfo, err := l.svcCtx.AllModels.FsOrder.FindLastSuccessOneOrder(l.ctx, user.Id, int64(constants.STATUS_NEW_NOT_PAY))
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "none of order is found")
}
logx.Error(err)
return resp.SetStatusAddMessage(basic.CodeDbSqlErr, "failed to get your last order")
}
//获取该订单相关设计信息
orderDetail, err := l.svcCtx.AllModels.FsOrderDetail.GetOneOrderDetailByOrderId(l.ctx, orderInfo.Id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order detail is not found")
}
logx.Error(err)
return resp.SetStatusAddMessage(basic.CodeDbSqlErr, "failed to get order detail")
}
//获取设计模板详情便于获得design_id
orderDetailTemplate, err := l.svcCtx.AllModels.FsOrderDetailTemplate.FindOne(l.ctx, *orderDetail.OrderDetailTemplateId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order detail template is not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get order detail template")
}
//最后一次设计不存在,则不返回该设计相关数据
if *orderDetailTemplate.DesignId <= 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success:last design id is not set")
}
//获取设计数据
productDesign, err := l.svcCtx.AllModels.FsProductDesign.FindOne(l.ctx, *orderDetailTemplate.DesignId, user.Id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product design is not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product design")
}
var info interface{}
if productDesign.Info != nil && *productDesign.Info != "" {
if err := json.Unmarshal([]byte(*productDesign.Info), &info); err != nil {
logx.Error(err)
return nil
}
}
var logoColor interface{}
if productDesign.LogoColor != nil && *productDesign.LogoColor != "" {
if err := json.Unmarshal([]byte(*productDesign.LogoColor), &logoColor); err != nil {
logx.Error(err)
return nil
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLastProductDesignRsp{
Id: productDesign.Id,
OptionalId: *productDesign.OptionalId,
SizeId: *productDesign.SizeId,
LogoColor: logoColor,
Info: info,
})
}

View File

@ -1,47 +0,0 @@
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"strings"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetThousandFaceDesignByPidLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetThousandFaceDesignByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetThousandFaceDesignByPidLogic {
return &GetThousandFaceDesignByPidLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetThousandFaceDesignByPidLogic) GetThousandFaceDesignByPid(req *types.GetThousandFaceDesignByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Pid = strings.Trim(req.Pid, " ")
if req.Pid == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
}
//获取产品信息(只是获取id)
/* productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
}*/
//获取产品设计
return resp.SetStatus(basic.CodeOK)
}

View File

@ -366,15 +366,11 @@ type GetRenderSettingByPidRsp struct {
Colors interface{} `json:"colors"`
}
type GetThousandFaceDesignByPidReq struct {
Pid string `form:"pid"`
}
type GetThousandFaceDesignByPidRsp struct {
type GetLastProductDesignRsp struct {
Id int64 `json:"id"`
OptionalId int64 `json:"optional_id"`
SizeId int64 `json:"size_id"`
LogoColor []string `json:"logo_color"`
LogoColor interface{} `json:"logo_color"`
Info interface{} `json:"info"`
}

View File

@ -66,8 +66,8 @@ service product {
@handler GetRenderSettingByPidHandler
get /api/product/get_render_setting_by_pid(GetRenderSettingByPidReq) returns (response);
//获取产品千人千面设计方案
@handler GetThousandFaceDesignByPidHandler
get /api/product/get_thousand_face_design_by_pid(GetThousandFaceDesignByPidReq) returns (response);
@handler GetLastProductDesignHandler
get /api/product/get_last_product_design(request) returns (response);
//*********************产品详情分解接口结束***********************
}
@ -404,13 +404,10 @@ type GetRenderSettingByPidRsp {
Colors interface{} `json:"colors"`
}
//获取产品千人千面设计方案
type GetThousandFaceDesignByPidReq {
Pid string `form:"pid"`
}
type GetThousandFaceDesignByPidRsp {
type GetLastProductDesignRsp {
Id int64 `json:"id"`
OptionalId int64 `json:"optional_id"`
SizeId int64 `json:"size_id"`
LogoColor []string `json:"logo_color"`
LogoColor interface{} `json:"logo_color"`
Info interface{} `json:"info"`
}