fix
This commit is contained in:
78
server/product/internal/handler/designgatherhandler.go
Normal file
78
server/product/internal/handler/designgatherhandler.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 DesignGatherHandler(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.DesignGatherReq
|
||||
// 如果端点有请求结构体,则使用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.NewDesignGatherLogic(r.Context(), svcCtx)
|
||||
resp := l.DesignGather(&req, userinfo, r)
|
||||
// 如果响应不为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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/product/design",
|
||||
Handler: GetProductDesignHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/product/design-gather",
|
||||
Handler: DesignGatherHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
142
server/product/internal/logic/designgatherlogic.go
Normal file
142
server/product/internal/logic/designgatherlogic.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/server/product/internal/svc"
|
||||
"fusenapi/server/product/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/encryption_decryption"
|
||||
"fusenapi/utils/id_generator"
|
||||
"fusenapi/utils/ip"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DesignGatherLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDesignGatherLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DesignGatherLogic {
|
||||
return &DesignGatherLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DesignGatherLogic) DesignGather(req *types.DesignGatherReq, userinfo *auth.UserInfo, r *http.Request) (resp *basic.Response) {
|
||||
encryptWebsetting, err := l.svcCtx.AllModels.FsWebSet.FindValueByKey(l.ctx, "is_encrypt")
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "web setting is_encrypt is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get web setting")
|
||||
}
|
||||
var postInfo map[string]interface{}
|
||||
//不加密
|
||||
if encryptWebsetting.Value == nil || *encryptWebsetting.Value == "0" {
|
||||
if err = json.Unmarshal([]byte(req.Data), &postInfo); err != nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json data,format may be invalid")
|
||||
}
|
||||
} else { //加密的
|
||||
//解密数据
|
||||
desData, err := encryption_decryption.CBCDecrypt(req.Data)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "failed to decryption data")
|
||||
}
|
||||
if err = json.Unmarshal([]byte(desData), &postInfo); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse json data,format may be invalid")
|
||||
}
|
||||
}
|
||||
//获取ip地址
|
||||
ipAddr, err := ip.GetClientIP(r)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "client ip is not available")
|
||||
}
|
||||
postInfo["client_ip"] = ipAddr
|
||||
var (
|
||||
optionalId int64
|
||||
sizeId int64
|
||||
productId int64
|
||||
templateId int64
|
||||
materialId int64
|
||||
cover string
|
||||
info string
|
||||
clientNo string
|
||||
)
|
||||
//校验数据
|
||||
if _, ok := postInfo["optional_id"].(float64); !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param optional_id")
|
||||
}
|
||||
optionalId = int64(postInfo["optional_id"].(float64))
|
||||
if _, ok := postInfo["size_id"].(float64); !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param size_id")
|
||||
}
|
||||
sizeId = int64(postInfo["size_id"].(float64))
|
||||
if _, ok := postInfo["product_id"].(float64); !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param product_id")
|
||||
}
|
||||
productId = int64(postInfo["product_id"].(float64))
|
||||
if _, ok := postInfo["template_id"].(float64); !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param template_id")
|
||||
}
|
||||
templateId = int64(postInfo["template_id"].(float64))
|
||||
if _, ok := postInfo["material_id"].(float64); !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param material_id")
|
||||
}
|
||||
materialId = int64(postInfo["material_id"].(float64))
|
||||
if _, ok := postInfo["client_no"].(string); !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param client_no")
|
||||
}
|
||||
clientNo = postInfo["client_no"].(string)
|
||||
if postInfo["info"] == nil {
|
||||
info = ""
|
||||
} else {
|
||||
jsonInfoBytes, _ := json.Marshal(postInfo["info"])
|
||||
info = string(jsonInfoBytes)
|
||||
}
|
||||
if postInfo["cover"] != nil {
|
||||
cover = postInfo["cover"].(string)
|
||||
}
|
||||
//保存数据
|
||||
designSn, err := id_generator.GenSnowFlakeId()
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate design sn")
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
err = l.svcCtx.AllModels.FsProductDesignGather.Create(l.ctx, &gmodel.FsProductDesignGather{
|
||||
Sn: &designSn,
|
||||
UserId: &userinfo.UserId,
|
||||
ProductId: &productId,
|
||||
TemplateId: &templateId,
|
||||
MaterialId: &materialId,
|
||||
SizeId: &sizeId,
|
||||
OptionalId: &optionalId,
|
||||
Cover: &cover,
|
||||
Info: &info,
|
||||
Utime: &now,
|
||||
ClientIp: &ipAddr,
|
||||
ClientNo: &clientNo,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to save data")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.DesignGatherRsp{
|
||||
Sn: designSn,
|
||||
})
|
||||
}
|
||||
@@ -95,6 +95,14 @@ type GetProductDesignRsp struct {
|
||||
Info string `json:"info"`
|
||||
}
|
||||
|
||||
type DesignGatherReq struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
type DesignGatherRsp struct {
|
||||
Sn string `json:"sn"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user