This commit is contained in:
laodaming 2023-07-14 15:57:27 +08:00
parent d122f9b0c0
commit 3a6ab7a666
8 changed files with 198 additions and 8 deletions

View File

@ -6,14 +6,18 @@ import "context"
type FindOneRenderDesignByParamsReq struct {
ClientNo *string
Sn *string
UserId *int64
SortType int
OrderBy string
Fields string
Id *int
}
func (r *FsProductRenderDesignModel) FindOneRenderDesignByParams(ctx context.Context, req FindOneRenderDesignByParamsReq) (resp *FsProductRenderDesign, err error) {
db := r.db.WithContext(ctx).Model(&FsProductRenderDesign{})
if req.Sn != nil {
db = db.Where("`sn` = ?", *req.Sn)
}
if req.ClientNo != nil {
db = db.Where("`client_no` = ?", *req.ClientNo)
}
@ -26,11 +30,11 @@ func (r *FsProductRenderDesignModel) FindOneRenderDesignByParams(ctx context.Con
if req.Fields != "" {
db = db.Select(req.Fields)
}
switch req.SortType {
case 1: //id asc
switch req.OrderBy {
case "": //id asc
db = db.Order("`id` ASC")
case 2: //id desc
db = db.Order("`id` DESC")
default:
db = db.Order(req.OrderBy)
}
err = db.Take(&resp).Error
return resp, err

View File

@ -7,17 +7,17 @@ import (
// fs_tags 产品分类表
type FsTags struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID
LevelPrefix *string `gorm:"default:'';" json:"level_prefix"` //
Title *string `gorm:"default:'';" json:"title"` // 标题
Level *int64 `gorm:"default:0;" json:"level"` // 层级、分类 1 => 二维码分类
ClickNum *int64 `gorm:"default:0;" json:"click_num"` // 点击次数
Sort *int64 `gorm:"default:0;" json:"sort"` // 排序(从大到小)
CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 创建时间
Icon *string `gorm:"default:'';" json:"icon"` //
Icon *string `gorm:"default:'';" json:"icon"` // 标签图标
Status *int64 `gorm:"default:1;" json:"status"` // 状态 1可用
Description *string `gorm:"default:'';" json:"description"` // 介绍 Seo
RecommendProduct *string `gorm:"default:'';" json:"recommend_product"` //
RecommendProductSort *string `gorm:"default:'';" json:"recommend_product_sort"` //
LevelPrefix *string `gorm:"default:'';" json:"level_prefix"` //归属等级前缀
}
type FsTagsModel struct{ db *gorm.DB }

View File

@ -0,0 +1,78 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
)
func GetRenderDesignHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.UserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
if claims != nil {
// 从token中获取对应的用户信息
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
} else {
// 如果claims为nil,则认为用户身份为白板用户
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
}
var req types.GetRenderDesignReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetRenderDesignLogic(r.Context(), svcCtx)
resp := l.GetRenderDesign(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -62,6 +62,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/product/tag_product_list",
Handler: GetTagProductListHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/product/render_design",
Handler: GetRenderDesignHandler(serverCtx),
},
},
)
}

View File

@ -431,7 +431,7 @@ func (l *GetProductInfoLogic) getRenderDesign(clientNo string) interface{} {
renderDesign, err := l.svcCtx.AllModels.FsProductRenderDesign.FindOneRenderDesignByParams(l.ctx, gmodel.FindOneRenderDesignByParamsReq{
ClientNo: &clientNo,
Fields: "id,info,material_id,optional_id,size_id,template_id",
SortType: 2,
OrderBy: "`id` DESC",
})
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {

View File

@ -0,0 +1,73 @@
package logic
import (
"encoding/json"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"gorm.io/gorm"
"strings"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetRenderDesignLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetRenderDesignLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRenderDesignLogic {
return &GetRenderDesignLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetRenderDesignLogic) GetRenderDesign(req *types.GetRenderDesignReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Sn = strings.Trim(req.Sn, " ")
if req.Sn == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:sn is empty")
}
renderDesign, err := l.svcCtx.AllModels.FsProductRenderDesign.FindOneRenderDesignByParams(l.ctx, gmodel.FindOneRenderDesignByParamsReq{
Sn: &req.Sn,
Fields: "id,info,material_id,optional_id,size_id,template_id",
OrderBy: "`id` DESC",
})
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "render design is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get render design info")
}
var color []string
if renderDesign.LogoColor != nil && *renderDesign.LogoColor != "" {
if err = json.Unmarshal([]byte(*renderDesign.LogoColor), &color); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse render logo color")
}
}
var info interface{}
if renderDesign.Info != nil && *renderDesign.Info != "" {
if err = json.Unmarshal([]byte(*renderDesign.Info), &info); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse render design info")
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetRenderDesignRsp{
Id: renderDesign.Id,
Info: info,
OptionalId: *renderDesign.OptionalId,
SizeId: *renderDesign.SizeId,
TemplateId: *renderDesign.TemplateId,
LogoColor: color,
})
}

View File

@ -280,6 +280,19 @@ type TagProduct struct {
CoverDefault string `json:"cover_default"`
}
type GetRenderDesignReq struct {
Sn string `form:"sn"`
}
type GetRenderDesignRsp struct {
Id int64 `json:"id"`
Info interface{} `json:"info"`
OptionalId int64 `json:"optional_id"`
SizeId int64 `json:"size_id"`
TemplateId int64 `json:"template_id"`
LogoColor []string `json:"logo_color"`
}
type Request struct {
}

View File

@ -40,6 +40,11 @@ service product {
//获取分类产品列表
@handler GetTagProductListHandler
get /api/product/tag_product_list(GetTagProductListReq) returns (response);
//*********************产品详情分解接口开始***********************
//获取云渲染设计方案信息
@handler GetRenderDesignHandler
get /api/product/render_design(GetRenderDesignReq) returns (response);
//*********************产品详情分解接口结束***********************
}
//获取产品列表
@ -292,4 +297,16 @@ type TagProduct {
SizeNum uint32 `json:"size_num"`
MiniPrice int64 `json:"mini_price"`
CoverDefault string `json:"cover_default"`
}
//获取云渲染设计方案信息
type GetRenderDesignReq {
Sn string `form:"sn"`
}
type GetRenderDesignRsp {
Id int64 `json:"id"`
Info interface{} `json:"info"`
OptionalId int64 `json:"optional_id"`
SizeId int64 `json:"size_id"`
TemplateId int64 `json:"template_id"`
LogoColor []string `json:"logo_color"`
}