fix
This commit is contained in:
parent
fffdf448c4
commit
73ce1e6db3
|
@ -52,15 +52,19 @@ func (t *FsProductTemplateV2Model) FindByParam(ctx context.Context, id int64, mo
|
|||
func (t *FsProductTemplateV2Model) Update(ctx context.Context, id int64, data *FsProductTemplateV2) error {
|
||||
return t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).Where("`id` = ? ", id).Updates(&data).Error
|
||||
}
|
||||
func (t *FsProductTemplateV2Model) FindAllByModelIds(ctx context.Context, modelIds []int64) (resp []FsProductTemplateV2, err error) {
|
||||
func (t *FsProductTemplateV2Model) FindAllByModelIds(ctx context.Context, modelIds []int64, orderBy string, fields ...string) (resp []FsProductTemplateV2, err error) {
|
||||
if len(modelIds) == 0 {
|
||||
return
|
||||
}
|
||||
err = t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).Where("`model_id` in (?) and `is_del` = ? and `status` = ?", modelIds, 0, 1).Find(&resp).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
db := t.db.WithContext(ctx).Model(&FsProductTemplateV2{}).Where("`model_id` in (?) and `is_del` = ? and `status` = ?", modelIds, 0, 1)
|
||||
switch orderBy {
|
||||
case "":
|
||||
db = db.Order("id DESC")
|
||||
default:
|
||||
db = db.Order(orderBy)
|
||||
}
|
||||
return
|
||||
err = db.Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
func (t *FsProductTemplateV2Model) FindAllByProductIdModelIds(ctx context.Context, modelIds []int64, productId int64) (resp []FsProductTemplateV2, err error) {
|
||||
if len(modelIds) == 0 {
|
||||
|
|
|
@ -70,7 +70,7 @@ func (l *GetModelOtherInfoLogic) GetModelOtherInfo(req *types.GetModelOtherInfoR
|
|||
map3dModel[v.Id] = k
|
||||
}
|
||||
//根据模型ids获取产品模板
|
||||
productTemplateV2List, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIds(l.ctx, modelIds)
|
||||
productTemplateV2List, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIds(l.ctx, modelIds, "")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product v2 template list")
|
||||
|
|
78
server/product/internal/handler/gettemplatebypidhandler.go
Normal file
78
server/product/internal/handler/gettemplatebypidhandler.go
Normal 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 GetTemplateByPidHandler(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.GetTemplateByPidReq
|
||||
// 如果端点有请求结构体,则使用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.NewGetTemplateByPidLogic(r.Context(), svcCtx)
|
||||
resp := l.GetTemplateByPid(&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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -82,6 +82,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Path: "/api/product/get_size_by_pid",
|
||||
Handler: GetSizeByPidHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/product/get_template_by_pid",
|
||||
Handler: GetTemplateByPidHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -52,6 +52,9 @@ func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo
|
|||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list")
|
||||
}
|
||||
if len(priceList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success:price list is empty")
|
||||
}
|
||||
//处理价格信息
|
||||
mapRsp := make(map[string]*types.GetPriceByPidRsp)
|
||||
for _, price := range priceList {
|
||||
|
|
149
server/product/internal/logic/gettemplatebypidlogic.go
Normal file
149
server/product/internal/logic/gettemplatebypidlogic.go
Normal file
|
@ -0,0 +1,149 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/image"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetTemplateByPidLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTemplateByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTemplateByPidLogic {
|
||||
return &GetTemplateByPidLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTemplateByPidLogic) GetTemplateByPid(req *types.GetTemplateByPidReq, 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")
|
||||
}
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
//获取产品信息(只是获取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")
|
||||
}
|
||||
//获取尺寸ids(只获取)
|
||||
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "id")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
|
||||
}
|
||||
if len(sizeList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success:size list is empty")
|
||||
}
|
||||
sizeIds := make([]int64, 0, len(sizeList))
|
||||
for _, v := range sizeList {
|
||||
sizeIds = append(sizeIds, v.Id)
|
||||
}
|
||||
//获取模型数据
|
||||
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllBySizeIdsTag(l.ctx, sizeIds, constants.TAG_MODEL, "id,size_id")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
|
||||
}
|
||||
if len(modelList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success:model list is empty")
|
||||
}
|
||||
modelIds := make([]int64, 0, len(modelList))
|
||||
mapModel := make(map[int64]int)
|
||||
for k, v := range modelList {
|
||||
modelIds = append(modelIds, v.Id)
|
||||
mapModel[v.Id] = k
|
||||
}
|
||||
//获取模板数据
|
||||
productTemplateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByModelIds(l.ctx, modelIds, "sort DESC")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product templates")
|
||||
}
|
||||
if len(productTemplateList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success:product template list is empty")
|
||||
}
|
||||
tagIds := make([]int64, 0, len(productTemplateList))
|
||||
for _, v := range productTemplateList {
|
||||
if *v.Tag == "" {
|
||||
continue
|
||||
}
|
||||
tag, err := strconv.ParseInt(*v.Tag, 10, 64)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "template tag is not a number:"+*v.Tag)
|
||||
}
|
||||
tagIds = append(tagIds, tag)
|
||||
}
|
||||
//获取模板标签列表
|
||||
templateTagList, err := l.svcCtx.AllModels.FsProductTemplateTags.GetListByIds(l.ctx, tagIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tag list")
|
||||
}
|
||||
mapTemplateTag := make(map[string]int)
|
||||
for k, v := range templateTagList {
|
||||
mapTemplateTag[fmt.Sprintf("%d", v.Id)] = k
|
||||
}
|
||||
mapRsp := make(map[string][]interface{})
|
||||
for _, v := range productTemplateList {
|
||||
//过滤没有设置详细数据的模板
|
||||
if v.TemplateInfo == nil || *v.TemplateInfo == "" {
|
||||
continue
|
||||
}
|
||||
var templateInfo map[string]interface{}
|
||||
if err = json.Unmarshal([]byte(*v.TemplateInfo), &templateInfo); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json:template info")
|
||||
}
|
||||
if templateInfo["cover"] != nil && templateInfo["cover"].(string) != "" {
|
||||
cover := strings.Split(templateInfo["cover"].(string), ".")
|
||||
if req.Size >= 200 && len(cover) >= 2 {
|
||||
templateInfo["cover"] = fmt.Sprintf("%s_%d.%s", cover[0], req.Size, cover[1])
|
||||
}
|
||||
} else {
|
||||
templateInfo["cover"] = ""
|
||||
}
|
||||
templateInfo["tag_name"] = ""
|
||||
if tagIndex, ok := mapTemplateTag[*v.Tag]; ok {
|
||||
templateInfo["tag_name"] = *templateTagList[tagIndex].Title
|
||||
}
|
||||
templateInfo["title"] = *v.Title
|
||||
modelIndex, ok := mapModel[*v.ModelId]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key := fmt.Sprintf("_%d", *modelList[modelIndex].SizeId)
|
||||
if _, ok = mapRsp[key]; ok {
|
||||
mapRsp[key] = append(mapRsp[key], templateInfo)
|
||||
} else {
|
||||
mapRsp[key] = []interface{}{templateInfo}
|
||||
}
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp)
|
||||
}
|
|
@ -326,6 +326,11 @@ type GetSizeByPidRsp struct {
|
|||
ModelId int64 `json:"model_id"` //产品主模型id
|
||||
}
|
||||
|
||||
type GetTemplateByPidReq struct {
|
||||
Pid string `form:"pid"`
|
||||
Size uint32 `form:"size"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
|
|
|
@ -53,6 +53,9 @@ service product {
|
|||
//获取产品尺寸列表
|
||||
@handler GetSizeByPidHandler
|
||||
get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response);
|
||||
//获取产品模板列表
|
||||
@handler GetTemplateByPidHandler
|
||||
get /api/product/get_template_by_pid(GetTemplateByPidReq) returns (response);
|
||||
//*********************产品详情分解接口结束***********************
|
||||
}
|
||||
|
||||
|
@ -350,3 +353,8 @@ type GetSizeByPidRsp {
|
|||
PartsCanDeleted bool `json:"parts_can_deleted"` //用户可否删除配件
|
||||
ModelId int64 `json:"model_id"` //产品主模型id
|
||||
}
|
||||
//获取产品模板列表
|
||||
type GetTemplateByPidReq {
|
||||
Pid string `form:"pid"`
|
||||
Size uint32 `form:"size"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user