From 6d8bdcb2a64f168f4f8743a021ed205e311371b6 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 26 Jun 2023 18:32:28 +0800 Subject: [PATCH] fix --- fs_gen_api.sh | 2 +- fs_gen_api_backend.sh | 2 +- .../internal/handler/getmodeldetailhandler.go | 34 ++++++++++++++++++- .../handler/getmodelotherinfohandler.go | 34 ++++++++++++++++++- .../handler/updateproductmodelhandler.go | 34 ++++++++++++++++++- .../internal/logic/getmodeldetaillogic.go | 18 ++-------- .../internal/logic/getmodelotherinfologic.go | 19 ++--------- .../internal/logic/updateproductmodellogic.go | 14 ++------ 8 files changed, 109 insertions(+), 48 deletions(-) diff --git a/fs_gen_api.sh b/fs_gen_api.sh index a699c2fc..d52d84bf 100755 --- a/fs_gen_api.sh +++ b/fs_gen_api.sh @@ -1,7 +1,7 @@ #! /bin/bash name=${1%%\\*} -options=("backend") +options=("backend" "product-template" "product-model") for option in "${options[@]}"; do if [ "$name" = "$option" ]; then echo "不能在"$name"里使用" diff --git a/fs_gen_api_backend.sh b/fs_gen_api_backend.sh index e2200b02..1acbf5fa 100755 --- a/fs_gen_api_backend.sh +++ b/fs_gen_api_backend.sh @@ -1,6 +1,6 @@ #! /bin/bash name=${1%%\\*} -options=("backend" "product-template" "backend2") +options=("backend" "product-template" "product-model") for option in "${options[@]}"; do if [ "$name" = "$option" ]; then echo $name diff --git a/server/product-model/internal/handler/getmodeldetailhandler.go b/server/product-model/internal/handler/getmodeldetailhandler.go index 4c9ec6fe..57e33649 100644 --- a/server/product-model/internal/handler/getmodeldetailhandler.go +++ b/server/product-model/internal/handler/getmodeldetailhandler.go @@ -7,6 +7,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" + "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/server/product-model/internal/logic" @@ -16,6 +17,37 @@ import ( func GetModelDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.BackendUserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil || claims == nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + // 从token中获取对应的用户信息 + userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + var req types.GetModelDetailReq // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 if err := httpx.Parse(r, &req); err != nil { @@ -28,7 +60,7 @@ func GetModelDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewGetModelDetailLogic(r.Context(), svcCtx) - resp := l.GetModelDetail(&req, r) + resp := l.GetModelDetail(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/product-model/internal/handler/getmodelotherinfohandler.go b/server/product-model/internal/handler/getmodelotherinfohandler.go index 15bd2554..e52d6a2f 100644 --- a/server/product-model/internal/handler/getmodelotherinfohandler.go +++ b/server/product-model/internal/handler/getmodelotherinfohandler.go @@ -7,6 +7,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" + "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/server/product-model/internal/logic" @@ -16,6 +17,37 @@ import ( func GetModelOtherInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.BackendUserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil || claims == nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + // 从token中获取对应的用户信息 + userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + var req types.GetModelOtherInfoReq // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 if err := httpx.Parse(r, &req); err != nil { @@ -28,7 +60,7 @@ func GetModelOtherInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewGetModelOtherInfoLogic(r.Context(), svcCtx) - resp := l.GetModelOtherInfo(&req, r) + resp := l.GetModelOtherInfo(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/product-model/internal/handler/updateproductmodelhandler.go b/server/product-model/internal/handler/updateproductmodelhandler.go index 173df20f..9bbe2298 100644 --- a/server/product-model/internal/handler/updateproductmodelhandler.go +++ b/server/product-model/internal/handler/updateproductmodelhandler.go @@ -7,6 +7,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" + "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/server/product-model/internal/logic" @@ -16,6 +17,37 @@ import ( func UpdateProductModelHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.BackendUserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil || claims == nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + // 从token中获取对应的用户信息 + userinfo, err = auth.GetBackendUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + var req types.UpdateProductModelReq // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 if err := httpx.Parse(r, &req); err != nil { @@ -28,7 +60,7 @@ func UpdateProductModelHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewUpdateProductModelLogic(r.Context(), svcCtx) - resp := l.UpdateProductModel(&req, r) + resp := l.UpdateProductModel(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/product-model/internal/logic/getmodeldetaillogic.go b/server/product-model/internal/logic/getmodeldetaillogic.go index c0a6760a..2e6e1d85 100644 --- a/server/product-model/internal/logic/getmodeldetaillogic.go +++ b/server/product-model/internal/logic/getmodeldetaillogic.go @@ -1,14 +1,12 @@ package logic import ( + "context" "encoding/json" "errors" - "fusenapi/model/gmodel" + "fusenapi/utils/auth" "fusenapi/utils/basic" "gorm.io/gorm" - "net/http" - - "context" "fusenapi/server/product-model/internal/svc" "fusenapi/server/product-model/internal/types" @@ -30,17 +28,7 @@ func NewGetModelDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge } } -func (l *GetModelDetailLogic) GetModelDetail(req *types.GetModelDetailReq, r *http.Request) (resp *basic.Response) { - authKey := r.Header.Get("Auth-Key") - genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn) - _, err := genentModel.Find(l.ctx, authKey) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..") - } - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info") - } +func (l *GetModelDetailLogic) GetModelDetail(req *types.GetModelDetailReq, userInfo *auth.BackendUserInfo) (resp *basic.Response) { if req.ModelId <= 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param model_id is required") } diff --git a/server/product-model/internal/logic/getmodelotherinfologic.go b/server/product-model/internal/logic/getmodelotherinfologic.go index 679d5705..2b9e583c 100644 --- a/server/product-model/internal/logic/getmodelotherinfologic.go +++ b/server/product-model/internal/logic/getmodelotherinfologic.go @@ -1,15 +1,12 @@ package logic import ( + "context" "encoding/json" - "errors" "fusenapi/constants" "fusenapi/model/gmodel" + "fusenapi/utils/auth" "fusenapi/utils/basic" - "gorm.io/gorm" - "net/http" - - "context" "fusenapi/server/product-model/internal/svc" "fusenapi/server/product-model/internal/types" @@ -31,17 +28,7 @@ func NewGetModelOtherInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) } } -func (l *GetModelOtherInfoLogic) GetModelOtherInfo(req *types.GetModelOtherInfoReq, r *http.Request) (resp *basic.Response) { - authKey := r.Header.Get("Auth-Key") - genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn) - _, err := genentModel.Find(l.ctx, authKey) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..") - } - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info") - } +func (l *GetModelOtherInfoLogic) GetModelOtherInfo(req *types.GetModelOtherInfoReq, userInfo *auth.BackendUserInfo) (resp *basic.Response) { //获取所有灯光列表 modelLightList, err := l.svcCtx.AllModels.FsProductModel3dLight.GetAll(l.ctx) if err != nil { diff --git a/server/product-model/internal/logic/updateproductmodellogic.go b/server/product-model/internal/logic/updateproductmodellogic.go index 870cc7da..06c67da0 100644 --- a/server/product-model/internal/logic/updateproductmodellogic.go +++ b/server/product-model/internal/logic/updateproductmodellogic.go @@ -5,9 +5,9 @@ import ( "errors" "fmt" "fusenapi/model/gmodel" + "fusenapi/utils/auth" "fusenapi/utils/basic" "gorm.io/gorm" - "net/http" "strings" "time" @@ -33,17 +33,7 @@ func NewUpdateProductModelLogic(ctx context.Context, svcCtx *svc.ServiceContext) } } -func (l *UpdateProductModelLogic) UpdateProductModel(req *types.UpdateProductModelReq, r *http.Request) (resp *basic.Response) { - authKey := r.Header.Get("Auth-Key") - genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn) - _, err := genentModel.Find(l.ctx, authKey) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..") - } - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info") - } +func (l *UpdateProductModelLogic) UpdateProductModel(req *types.UpdateProductModelReq, userInfo *auth.BackendUserInfo) (resp *basic.Response) { if req.ModelData.Id <= 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "modelData`s id is required") }