diff --git a/server/canteen/internal/handler/getcanteendetailhandler.go b/server/canteen/internal/handler/getcanteendetailhandler.go index f5c8d44f..97b4889c 100644 --- a/server/canteen/internal/handler/getcanteendetailhandler.go +++ b/server/canteen/internal/handler/getcanteendetailhandler.go @@ -17,28 +17,40 @@ import ( func GetCanteenDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.GetCanteenDetailReq @@ -55,7 +67,6 @@ func GetCanteenDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewGetCanteenDetailLogic(r.Context(), svcCtx) resp := l.GetCanteenDetail(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/canteen/internal/handler/savecanteentypeproducthandler.go b/server/canteen/internal/handler/savecanteentypeproducthandler.go index 7e5ee156..770c77b1 100644 --- a/server/canteen/internal/handler/savecanteentypeproducthandler.go +++ b/server/canteen/internal/handler/savecanteentypeproducthandler.go @@ -17,28 +17,40 @@ import ( func SaveCanteenTypeProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.SaveCanteenTypeProductReq @@ -55,7 +67,6 @@ func SaveCanteenTypeProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc l := logic.NewSaveCanteenTypeProductLogic(r.Context(), svcCtx) resp := l.SaveCanteenTypeProduct(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/canteen/internal/logic/getcanteendetaillogic.go b/server/canteen/internal/logic/getcanteendetaillogic.go index 11fd92ce..c87b0730 100644 --- a/server/canteen/internal/logic/getcanteendetaillogic.go +++ b/server/canteen/internal/logic/getcanteendetaillogic.go @@ -27,7 +27,10 @@ func NewGetCanteenDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) * } // 获取餐厅详情 -func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq, loginInfo *auth.UserInfo) (resp *basic.Response) { +func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first") + } //获取餐厅类型数据 canteenTypeModel := gmodel.NewFsCanteenTypeModel(l.svcCtx.MysqlConn) canteenTypeInfo, err := canteenTypeModel.FindOne(l.ctx, req.Id) diff --git a/server/canteen/internal/logic/savecanteentypeproductlogic.go b/server/canteen/internal/logic/savecanteentypeproductlogic.go index 69117664..f1dbd00b 100644 --- a/server/canteen/internal/logic/savecanteentypeproductlogic.go +++ b/server/canteen/internal/logic/savecanteentypeproductlogic.go @@ -29,7 +29,10 @@ func NewSaveCanteenTypeProductLogic(ctx context.Context, svcCtx *svc.ServiceCont } // 保存餐厅类型的关联产品 -func (l *SaveCanteenTypeProductLogic) SaveCanteenTypeProduct(req *types.SaveCanteenTypeProductReq, loginInfo *auth.UserInfo) (resp *basic.Response) { +func (l *SaveCanteenTypeProductLogic) SaveCanteenTypeProduct(req *types.SaveCanteenTypeProductReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first") + } if len(req.ProductList) == 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "product list can`t be empty") } diff --git a/server/canteen/internal/types/types.go b/server/canteen/internal/types/types.go index 34a65a59..e2774873 100644 --- a/server/canteen/internal/types/types.go +++ b/server/canteen/internal/types/types.go @@ -35,9 +35,6 @@ type SaveCanteenProduct struct { SId string `json:"s_id"` } -type Request struct { -} - type Response struct { Code int `json:"code"` Message string `json:"msg"` diff --git a/server/data-transfer/internal/handler/getqrcodesetlisthandler.go b/server/data-transfer/internal/handler/getqrcodesetlisthandler.go index 190b2864..8b6d101b 100644 --- a/server/data-transfer/internal/handler/getqrcodesetlisthandler.go +++ b/server/data-transfer/internal/handler/getqrcodesetlisthandler.go @@ -16,34 +16,45 @@ import ( func GetQrCodeSetListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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} } l := logic.NewGetQrCodeSetListLogic(r.Context(), svcCtx) resp := l.GetQrCodeSetList(userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/data-transfer/internal/handler/getstandardlogolisthandler.go b/server/data-transfer/internal/handler/getstandardlogolisthandler.go index 4c9d27f9..ae5e1316 100644 --- a/server/data-transfer/internal/handler/getstandardlogolisthandler.go +++ b/server/data-transfer/internal/handler/getstandardlogolisthandler.go @@ -16,34 +16,45 @@ import ( func GetStandardLogoListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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} } l := logic.NewGetStandardLogoListLogic(r.Context(), svcCtx) resp := l.GetStandardLogoList(userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/data-transfer/internal/handler/uploadqrcodehandler.go b/server/data-transfer/internal/handler/uploadqrcodehandler.go index 2b6afa80..79bf19bb 100644 --- a/server/data-transfer/internal/handler/uploadqrcodehandler.go +++ b/server/data-transfer/internal/handler/uploadqrcodehandler.go @@ -17,28 +17,40 @@ import ( func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.UploadQrcodeReq @@ -55,7 +67,6 @@ func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewUploadQrcodeLogic(r.Context(), svcCtx) resp := l.UploadQrcode(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/data-transfer/internal/logic/getqrcodesetlistlogic.go b/server/data-transfer/internal/logic/getqrcodesetlistlogic.go index b17db08f..46ef0d22 100644 --- a/server/data-transfer/internal/logic/getqrcodesetlistlogic.go +++ b/server/data-transfer/internal/logic/getqrcodesetlistlogic.go @@ -27,7 +27,10 @@ func NewGetQrCodeSetListLogic(ctx context.Context, svcCtx *svc.ServiceContext) * } // 获取二维码配置列表 -func (l *GetQrCodeSetListLogic) GetQrCodeSetList(loginInfo *auth.UserInfo) (resp *basic.Response) { +func (l *GetQrCodeSetListLogic) GetQrCodeSetList(userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first") + } qrCodeModel := gmodel.NewFsQrcodeSetModel(l.svcCtx.MysqlConn) qrCodeList, err := qrCodeModel.GetAll(l.ctx) if err != nil { diff --git a/server/data-transfer/internal/logic/getstandardlogolistlogic.go b/server/data-transfer/internal/logic/getstandardlogolistlogic.go index 61785c8f..3deb42c5 100644 --- a/server/data-transfer/internal/logic/getstandardlogolistlogic.go +++ b/server/data-transfer/internal/logic/getstandardlogolistlogic.go @@ -26,7 +26,10 @@ func NewGetStandardLogoListLogic(ctx context.Context, svcCtx *svc2.ServiceContex } // 获取标准logo列表 -func (l *GetStandardLogoListLogic) GetStandardLogoList(loginInfo *auth.UserInfo) (resp *basic.Response) { +func (l *GetStandardLogoListLogic) GetStandardLogoList(userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first") + } standardLogoModel := gmodel.NewFsStandardLogoModel(l.svcCtx.MysqlConn) logoList, err := standardLogoModel.GetAll(l.ctx) if err != nil { diff --git a/server/data-transfer/internal/logic/uploadqrcodelogic.go b/server/data-transfer/internal/logic/uploadqrcodelogic.go index 8e7b6f0a..d8749dac 100644 --- a/server/data-transfer/internal/logic/uploadqrcodelogic.go +++ b/server/data-transfer/internal/logic/uploadqrcodelogic.go @@ -27,7 +27,10 @@ func NewUploadQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Uplo } // 生成base64二维码 -func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, loginInfo *auth.UserInfo) (resp *basic.Response) { +func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first") + } if req.Url == "" { resp.SetStatus(basic.CodeApiErr, "param url is empty") } diff --git a/server/map_library/etc/map-library.yaml b/server/map-library/etc/map-library.yaml similarity index 100% rename from server/map_library/etc/map-library.yaml rename to server/map-library/etc/map-library.yaml diff --git a/server/map_library/internal/config/config.go b/server/map-library/internal/config/config.go similarity index 76% rename from server/map_library/internal/config/config.go rename to server/map-library/internal/config/config.go index f9c9e9fd..cac8b83d 100644 --- a/server/map_library/internal/config/config.go +++ b/server/map-library/internal/config/config.go @@ -1,7 +1,7 @@ package config import ( - "fusenapi/server/map_library/internal/types" + "fusenapi/server/map-library/internal/types" "github.com/zeromicro/go-zero/rest" ) diff --git a/server/map-library/internal/handler/getmaplibrarylisthandler.go b/server/map-library/internal/handler/getmaplibrarylisthandler.go new file mode 100644 index 00000000..d717c6da --- /dev/null +++ b/server/map-library/internal/handler/getmaplibrarylisthandler.go @@ -0,0 +1,66 @@ +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/map-library/internal/logic" + "fusenapi/server/map-library/internal/svc" +) + +func GetMapLibraryListHandler(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} + } + + l := logic.NewGetMapLibraryListLogic(r.Context(), svcCtx) + resp := l.GetMapLibraryList(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) + } + } +} diff --git a/server/map_library/internal/handler/routes.go b/server/map-library/internal/handler/routes.go similarity index 89% rename from server/map_library/internal/handler/routes.go rename to server/map-library/internal/handler/routes.go index 2e9984d4..c45b9beb 100644 --- a/server/map_library/internal/handler/routes.go +++ b/server/map-library/internal/handler/routes.go @@ -4,7 +4,7 @@ package handler import ( "net/http" - "fusenapi/server/map_library/internal/svc" + "fusenapi/server/map-library/internal/svc" "github.com/zeromicro/go-zero/rest" ) diff --git a/server/map_library/internal/logic/getmaplibrarylistlogic.go b/server/map-library/internal/logic/getmaplibrarylistlogic.go similarity index 91% rename from server/map_library/internal/logic/getmaplibrarylistlogic.go rename to server/map-library/internal/logic/getmaplibrarylistlogic.go index 0cae21dc..762a5451 100644 --- a/server/map_library/internal/logic/getmaplibrarylistlogic.go +++ b/server/map-library/internal/logic/getmaplibrarylistlogic.go @@ -3,14 +3,13 @@ package logic import ( "encoding/json" "fusenapi/model/gmodel" - "fusenapi/server/map_library/internal/types" + "fusenapi/server/map-library/internal/svc" + "fusenapi/server/map-library/internal/types" "fusenapi/utils/auth" "fusenapi/utils/basic" "time" "context" - - "fusenapi/server/map_library/internal/svc" "github.com/zeromicro/go-zero/core/logx" ) @@ -29,6 +28,9 @@ func NewGetMapLibraryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *GetMapLibraryListLogic) GetMapLibraryList(userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first") + } mapLibraryModel := gmodel.NewFsMapLibraryModel(l.svcCtx.MysqlConn) mapLibraryList, err := mapLibraryModel.GetAllEnabledList(l.ctx) if err != nil { diff --git a/server/map_library/internal/svc/servicecontext.go b/server/map-library/internal/svc/servicecontext.go similarity index 96% rename from server/map_library/internal/svc/servicecontext.go rename to server/map-library/internal/svc/servicecontext.go index 3d6acee1..3f4cf673 100644 --- a/server/map_library/internal/svc/servicecontext.go +++ b/server/map-library/internal/svc/servicecontext.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "fusenapi/initalize" - "fusenapi/server/map_library/internal/config" + "fusenapi/server/map-library/internal/config" "github.com/golang-jwt/jwt" "gorm.io/gorm" "net/http" diff --git a/server/map_library/internal/types/types.go b/server/map-library/internal/types/types.go similarity index 100% rename from server/map_library/internal/types/types.go rename to server/map-library/internal/types/types.go diff --git a/server/map_library/map-library.go b/server/map-library/map-library.go similarity index 87% rename from server/map_library/map-library.go rename to server/map-library/map-library.go index d0622347..824d1f6c 100644 --- a/server/map_library/map-library.go +++ b/server/map-library/map-library.go @@ -4,9 +4,9 @@ import ( "flag" "fmt" - "fusenapi/server/map_library/internal/config" - "fusenapi/server/map_library/internal/handler" - "fusenapi/server/map_library/internal/svc" + "fusenapi/server/map-library/internal/config" + "fusenapi/server/map-library/internal/handler" + "fusenapi/server/map-library/internal/svc" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/rest" diff --git a/server/map_library/internal/handler/getmaplibrarylisthandler.go b/server/map_library/internal/handler/getmaplibrarylisthandler.go deleted file mode 100644 index f81221e0..00000000 --- a/server/map_library/internal/handler/getmaplibrarylisthandler.go +++ /dev/null @@ -1,55 +0,0 @@ -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/map_library/internal/logic" - "fusenapi/server/map_library/internal/svc" -) - -func GetMapLibraryListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken - claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 - if err != nil { - httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", - }) - logx.Info("unauthorized:", err.Error()) - return - } - - // 从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 - } - - l := logic.NewGetMapLibraryListLogic(r.Context(), svcCtx) - resp := l.GetMapLibraryList(userinfo) - // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 - 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) - } - } -} diff --git a/server/product/internal/handler/getproductlisthandler.go b/server/product/internal/handler/getproductlisthandler.go index 35a1d3d6..5f1154c2 100644 --- a/server/product/internal/handler/getproductlisthandler.go +++ b/server/product/internal/handler/getproductlisthandler.go @@ -17,28 +17,40 @@ import ( func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.GetProductListReq @@ -55,7 +67,6 @@ func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewGetProductListLogic(r.Context(), svcCtx) resp := l.GetProductList(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/product/internal/handler/getsizebyproducthandler.go b/server/product/internal/handler/getsizebyproducthandler.go index 13bcf0bb..ffb4fbe6 100644 --- a/server/product/internal/handler/getsizebyproducthandler.go +++ b/server/product/internal/handler/getsizebyproducthandler.go @@ -16,34 +16,45 @@ import ( func GetSizeByProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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} } l := logic.NewGetSizeByProductLogic(r.Context(), svcCtx) resp := l.GetSizeByProduct(userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/product/internal/handler/getsuccessrecommandhandler.go b/server/product/internal/handler/getsuccessrecommandhandler.go index a3196ad5..c779fd4f 100644 --- a/server/product/internal/handler/getsuccessrecommandhandler.go +++ b/server/product/internal/handler/getsuccessrecommandhandler.go @@ -17,28 +17,40 @@ import ( func GetSuccessRecommandHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.GetSuccessRecommandReq @@ -55,7 +67,6 @@ func GetSuccessRecommandHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewGetSuccessRecommandLogic(r.Context(), svcCtx) resp := l.GetSuccessRecommand(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/product/internal/logic/getproductlistlogic.go b/server/product/internal/logic/getproductlistlogic.go index 86e7021d..e7fbf8d5 100644 --- a/server/product/internal/logic/getproductlistlogic.go +++ b/server/product/internal/logic/getproductlistlogic.go @@ -33,7 +33,10 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge } // 获取产品列表 -func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo *auth.UserInfo) (resp *basic.Response) { +func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } //如果是demo if req.IsDemo == 1 { var demo types.GetProductListRsp @@ -52,7 +55,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login } //查询用户信息 userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn) - userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId) + userInfo, err := userModel.FindOne(l.ctx, userinfo.UserId) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err") diff --git a/server/product/internal/logic/getsizebyproductlogic.go b/server/product/internal/logic/getsizebyproductlogic.go index 213f5792..8d1127a9 100644 --- a/server/product/internal/logic/getsizebyproductlogic.go +++ b/server/product/internal/logic/getsizebyproductlogic.go @@ -33,7 +33,10 @@ func NewGetSizeByProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) * } // 获取分类下的产品以及尺寸 -func (l *GetSizeByProductLogic) GetSizeByProduct(loginInfo *auth.UserInfo) (resp *basic.Response) { +func (l *GetSizeByProductLogic) GetSizeByProduct(userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } //获取所有网站目录 tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn) tagsList, err := tagsModel.GetAllByLevel(l.ctx, constants.TYPE_WEBSITE) diff --git a/server/product/internal/logic/getsuccessrecommandlogic.go b/server/product/internal/logic/getsuccessrecommandlogic.go index f13ccba7..7e833544 100644 --- a/server/product/internal/logic/getsuccessrecommandlogic.go +++ b/server/product/internal/logic/getsuccessrecommandlogic.go @@ -27,6 +27,9 @@ func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext // 获取推荐的产品列表 func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, userInfo *auth.UserInfo) (resp *basic.Response) { + if userInfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } //获取用户信息 userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn) user, err := userModel.FindOne(l.ctx, userInfo.UserId) diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index ff2759e8..1b74bab6 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -96,9 +96,6 @@ type PriceObj struct { Price float64 `json:"price"` } -type Request struct { -} - type Response struct { Code int `json:"code"` Message string `json:"msg"` diff --git a/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go b/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go index 6b470929..6a94336a 100644 --- a/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go +++ b/server/shopping-cart-confirmation/internal/handler/cartaddhandler.go @@ -17,28 +17,40 @@ import ( func CartAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.CartAddReq @@ -55,7 +67,6 @@ func CartAddHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewCartAddLogic(r.Context(), svcCtx) resp := l.CartAdd(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go b/server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go index 31c0ca7b..dbf617c7 100644 --- a/server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go +++ b/server/shopping-cart-confirmation/internal/handler/cartdeletehandler.go @@ -17,28 +17,40 @@ import ( func CartDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.CartDeleteReq @@ -55,7 +67,6 @@ func CartDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewCartDeleteLogic(r.Context(), svcCtx) resp := l.CartDelete(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/shopping-cart-confirmation/internal/handler/cartlisthandler.go b/server/shopping-cart-confirmation/internal/handler/cartlisthandler.go index 7232a4b9..b7ccbbc2 100644 --- a/server/shopping-cart-confirmation/internal/handler/cartlisthandler.go +++ b/server/shopping-cart-confirmation/internal/handler/cartlisthandler.go @@ -17,28 +17,40 @@ import ( func CartListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.CartListReq @@ -55,7 +67,6 @@ func CartListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewCartListLogic(r.Context(), svcCtx) resp := l.CartList(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/shopping-cart-confirmation/internal/handler/cartnumberhandler.go b/server/shopping-cart-confirmation/internal/handler/cartnumberhandler.go index eb820553..40070f45 100644 --- a/server/shopping-cart-confirmation/internal/handler/cartnumberhandler.go +++ b/server/shopping-cart-confirmation/internal/handler/cartnumberhandler.go @@ -16,34 +16,45 @@ import ( func CartNumberHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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} } l := logic.NewCartNumberLogic(r.Context(), svcCtx) resp := l.CartNumber(userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/shopping-cart-confirmation/internal/handler/cartorderdetailhandler.go b/server/shopping-cart-confirmation/internal/handler/cartorderdetailhandler.go index f2f23523..1bda9ec4 100644 --- a/server/shopping-cart-confirmation/internal/handler/cartorderdetailhandler.go +++ b/server/shopping-cart-confirmation/internal/handler/cartorderdetailhandler.go @@ -17,28 +17,40 @@ import ( func CartOrderDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.CartOrderDetailReq @@ -55,7 +67,6 @@ func CartOrderDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewCartOrderDetailLogic(r.Context(), svcCtx) resp := l.CartOrderDetail(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go b/server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go index 0516e923..bc861fad 100644 --- a/server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go +++ b/server/shopping-cart-confirmation/internal/handler/changeordermethodhandler.go @@ -17,28 +17,40 @@ import ( func ChangeOrderMethodHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // 解析jwtToken + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 claims, err := svcCtx.ParseJwtToken(r) - // 如果解析出错,则返回未授权的JSON响应并记录错误消息 + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - // 从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 + 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.ChangeOrderMethodReq @@ -55,7 +67,6 @@ func ChangeOrderMethodHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := logic.NewChangeOrderMethodLogic(r.Context(), svcCtx) resp := l.ChangeOrderMethod(&req, userinfo) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; - // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) } else { diff --git a/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go b/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go index b224a166..c4114978 100644 --- a/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go +++ b/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go @@ -31,6 +31,9 @@ func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLo // 添加入购物车 func (l *CartAddLogic) CartAdd(req *types.CartAddReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } if req.BuyNum == 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param buy_num can`t be 0") } diff --git a/server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go b/server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go index a58f7b63..c8136c9f 100644 --- a/server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go +++ b/server/shopping-cart-confirmation/internal/logic/cartdeletelogic.go @@ -28,6 +28,9 @@ func NewCartDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartDe } func (l *CartDeleteLogic) CartDelete(req *types.CartDeleteReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } if req.Id <= 0 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param id") } diff --git a/server/shopping-cart-confirmation/internal/logic/cartlistlogic.go b/server/shopping-cart-confirmation/internal/logic/cartlistlogic.go index 4f54248b..ba4cb95c 100644 --- a/server/shopping-cart-confirmation/internal/logic/cartlistlogic.go +++ b/server/shopping-cart-confirmation/internal/logic/cartlistlogic.go @@ -38,6 +38,9 @@ func NewCartListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartList // 获取用户购物车列表 func (l *CartListLogic) CartList(req *types.CartListReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } //获取当前图片应该返回的尺寸大小 if req.Size > 0 { req.Size = image.GetCurrentSize(req.Size) diff --git a/server/shopping-cart-confirmation/internal/logic/cartnumberlogic.go b/server/shopping-cart-confirmation/internal/logic/cartnumberlogic.go index eb4d6f48..44c35d32 100644 --- a/server/shopping-cart-confirmation/internal/logic/cartnumberlogic.go +++ b/server/shopping-cart-confirmation/internal/logic/cartnumberlogic.go @@ -27,6 +27,9 @@ func NewCartNumberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartNu } func (l *CartNumberLogic) CartNumber(userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } cartModel := gmodel.NewFsCartModel(l.svcCtx.MysqlConn) total, err := cartModel.CountUserCart(l.ctx, userinfo.UserId) if err != nil { diff --git a/server/shopping-cart-confirmation/internal/logic/cartorderdetaillogic.go b/server/shopping-cart-confirmation/internal/logic/cartorderdetaillogic.go index a699c2f1..de8e4dc2 100644 --- a/server/shopping-cart-confirmation/internal/logic/cartorderdetaillogic.go +++ b/server/shopping-cart-confirmation/internal/logic/cartorderdetaillogic.go @@ -31,6 +31,9 @@ func NewCartOrderDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *C } func (l *CartOrderDetailLogic) CartOrderDetail(req *types.CartOrderDetailReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } req.Sn = strings.Trim(req.Sn, " ") if req.Sn == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param sn is required") diff --git a/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go b/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go index ddf9301f..d6fffd18 100644 --- a/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go +++ b/server/shopping-cart-confirmation/internal/logic/changeordermethodlogic.go @@ -30,6 +30,9 @@ func NewChangeOrderMethodLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *ChangeOrderMethodLogic) ChangeOrderMethod(req *types.ChangeOrderMethodReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if userinfo.GetIdType() != auth.IDTYPE_User { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first") + } req.Sn = strings.Trim(req.Sn, " ") if req.Sn == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param sn is required") diff --git a/server_api/map_library.api b/server_api/map-library.api similarity index 100% rename from server_api/map_library.api rename to server_api/map-library.api