fix
This commit is contained in:
parent
46418f2338
commit
ae1b3e1e64
78
server/product/internal/handler/getsizebypidhandler.go
Normal file
78
server/product/internal/handler/getsizebypidhandler.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 GetSizeByPidHandler(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.GetSizeByPidReq
|
||||
// 如果端点有请求结构体,则使用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.NewGetSizeByPidLogic(r.Context(), svcCtx)
|
||||
resp := l.GetSizeByPid(&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)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -77,6 +77,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Path: "/api/product/get_price_by_pid",
|
||||
Handler: GetPriceByPidHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/product/get_size_by_pid",
|
||||
Handler: GetSizeByPidHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
88
server/product/internal/logic/getsizebypidlogic.go
Normal file
88
server/product/internal/logic/getsizebypidlogic.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/constants"
|
||||
"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 GetSizeByPidLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetSizeByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSizeByPidLogic {
|
||||
return &GetSizeByPidLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetSizeByPidLogic) GetSizeByPid(req *types.GetSizeByPidReq, 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")
|
||||
}
|
||||
//获取产品尺寸列表(需要正序排序)
|
||||
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "sort ASC")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
|
||||
}
|
||||
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")
|
||||
}
|
||||
mapSizeModel := make(map[int64]int) //size id为key
|
||||
for k, v := range modelList {
|
||||
mapSizeModel[*v.SizeId] = k
|
||||
}
|
||||
//处理
|
||||
listRsp := make([]types.GetSizeByPidRsp, 0, len(sizeList))
|
||||
for _, sizeInfo := range sizeList {
|
||||
//没有模型的不能使用
|
||||
modelIndex, ok := mapSizeModel[sizeInfo.Id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
var title interface{}
|
||||
_ = json.Unmarshal([]byte(*sizeInfo.Title), &title)
|
||||
listRsp = append(listRsp, types.GetSizeByPidRsp{
|
||||
Id: sizeInfo.Id,
|
||||
Title: title,
|
||||
Capacity: *sizeInfo.Capacity,
|
||||
Cover: *sizeInfo.Cover,
|
||||
PartsCanDeleted: *sizeInfo.PartsCanDeleted > 0,
|
||||
ModelId: modelList[modelIndex].Id,
|
||||
})
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)
|
||||
}
|
|
@ -313,6 +313,19 @@ type PriceItem struct {
|
|||
Price int64 `json:"price"`
|
||||
}
|
||||
|
||||
type GetSizeByPidReq struct {
|
||||
Pid string `form:"pid"`
|
||||
}
|
||||
|
||||
type GetSizeByPidRsp struct {
|
||||
Id int64 `json:"id"` //尺寸id
|
||||
Title interface{} `json:"title"`
|
||||
Capacity string `json:"capacity"` //容量、尺寸、尺码描述
|
||||
Cover string `json:"cover"` //缩略图
|
||||
PartsCanDeleted bool `json:"parts_can_deleted"` //用户可否删除配件
|
||||
ModelId int64 `json:"model_id"` //产品主模型id
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,9 @@ service product {
|
|||
//获取产品阶梯价格列表
|
||||
@handler GetPriceByPidHandler
|
||||
get /api/product/get_price_by_pid(GetPriceByPidReq) returns (response);
|
||||
//获取产品尺寸列表
|
||||
@handler GetSizeByPidHandler
|
||||
get /api/product/get_size_by_pid(GetSizeByPidReq) returns (response);
|
||||
//*********************产品详情分解接口结束***********************
|
||||
}
|
||||
|
||||
|
@ -334,4 +337,16 @@ type PriceItem {
|
|||
Num int64 `json:"num"`
|
||||
TotalNum int64 `json:"total_num"`
|
||||
Price int64 `json:"price"`
|
||||
}
|
||||
//获取产品尺寸列表
|
||||
type GetSizeByPidReq {
|
||||
Pid string `form:"pid"`
|
||||
}
|
||||
type GetSizeByPidRsp {
|
||||
Id int64 `json:"id"` //尺寸id
|
||||
Title interface{} `json:"title"`
|
||||
Capacity string `json:"capacity"` //容量、尺寸、尺码描述
|
||||
Cover string `json:"cover"` //缩略图
|
||||
PartsCanDeleted bool `json:"parts_can_deleted"` //用户可否删除配件
|
||||
ModelId int64 `json:"model_id"` //产品主模型id
|
||||
}
|
Loading…
Reference in New Issue
Block a user