fix
This commit is contained in:
parent
2eb8bf936f
commit
eedce6c2d7
78
server/product/internal/handler/getlightbypidhandler.go
Normal file
78
server/product/internal/handler/getlightbypidhandler.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 GetLightByPidHandler(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.GetLightByPidReq
|
||||||
|
// 如果端点有请求结构体,则使用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.NewGetLightByPidLogic(r.Context(), svcCtx)
|
||||||
|
resp := l.GetLightByPid(&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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -92,6 +92,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||||
Path: "/api/product/get_fitting_by_pid",
|
Path: "/api/product/get_fitting_by_pid",
|
||||||
Handler: GetFittingByPidHandler(serverCtx),
|
Handler: GetFittingByPidHandler(serverCtx),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/api/product/get_light_by_pid",
|
||||||
|
Handler: GetLightByPidHandler(serverCtx),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
95
server/product/internal/logic/getlightbypidlogic.go
Normal file
95
server/product/internal/logic/getlightbypidlogic.go
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
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 GetLightByPidLogic struct {
|
||||||
|
logx.Logger
|
||||||
|
ctx context.Context
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGetLightByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLightByPidLogic {
|
||||||
|
return &GetLightByPidLogic{
|
||||||
|
Logger: logx.WithContext(ctx),
|
||||||
|
ctx: ctx,
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *GetLightByPidLogic) GetLightByPid(req *types.GetLightByPidReq, 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")
|
||||||
|
}
|
||||||
|
//获取尺寸ids
|
||||||
|
sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.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)
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
lightIds := make([]int64, 0, len(modelList))
|
||||||
|
for _, v := range modelList {
|
||||||
|
lightIds = append(lightIds, *v.Light)
|
||||||
|
}
|
||||||
|
//获取光源数据
|
||||||
|
lightList, err := l.svcCtx.AllModels.FsProductModel3dLight.GetAllByIds(l.ctx, lightIds)
|
||||||
|
if err != nil {
|
||||||
|
logx.Error(err)
|
||||||
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get light list")
|
||||||
|
}
|
||||||
|
//组装数据
|
||||||
|
listRsp := make([]interface{}, 0, len(lightList))
|
||||||
|
for _, v := range lightList {
|
||||||
|
var lightInfo map[string]interface{}
|
||||||
|
if v.Info == nil || *v.Info == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal([]byte(*v.Info), &lightInfo); err != nil {
|
||||||
|
logx.Error(err)
|
||||||
|
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse light info")
|
||||||
|
}
|
||||||
|
lightInfo["id"] = v.Id
|
||||||
|
listRsp = append(listRsp, lightInfo)
|
||||||
|
}
|
||||||
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)
|
||||||
|
}
|
|
@ -343,6 +343,10 @@ type GetFittingByPidRsp struct {
|
||||||
ModelInfo interface{} `json:"model_info"`
|
ModelInfo interface{} `json:"model_info"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetLightByPidReq struct {
|
||||||
|
Pid string `form:"pid"`
|
||||||
|
}
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,9 @@ service product {
|
||||||
//获取产品配件数据
|
//获取产品配件数据
|
||||||
@handler GetFittingByPidHandler
|
@handler GetFittingByPidHandler
|
||||||
get /api/product/get_fitting_by_pid(GetFittingByPidReq) returns (response);
|
get /api/product/get_fitting_by_pid(GetFittingByPidReq) returns (response);
|
||||||
|
//获取产品灯光数据
|
||||||
|
@handler GetLightByPidHandler
|
||||||
|
get /api/product/get_light_by_pid(GetLightByPidReq) returns (response);
|
||||||
//*********************产品详情分解接口结束***********************
|
//*********************产品详情分解接口结束***********************
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,3 +375,7 @@ type GetFittingByPidRsp {
|
||||||
Price int64 `json:"price"`
|
Price int64 `json:"price"`
|
||||||
ModelInfo interface{} `json:"model_info"`
|
ModelInfo interface{} `json:"model_info"`
|
||||||
}
|
}
|
||||||
|
//获取产品灯光数据
|
||||||
|
type GetLightByPidReq {
|
||||||
|
Pid string `form:"pid"`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user