From fd03cc08a79280c25a3729d0020a246a049310ed Mon Sep 17 00:00:00 2001 From: Fuyao Date: Tue, 9 Dec 2025 16:17:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code.go | 78 +++++++++++++++-------------------------------------- response.go | 12 +-------- 2 files changed, 22 insertions(+), 68 deletions(-) diff --git a/code.go b/code.go index 0f28b8c..e17ff9b 100644 --- a/code.go +++ b/code.go @@ -1,66 +1,30 @@ package response -import ( - "google.golang.org/grpc/status" +import "google.golang.org/grpc/status" + +const ( + CodeSuccess = 0 // 成功 + CodeFaild = 1 // 失败 + CodeInfo = 2 // 消息 + CodeWarn = 3 // 警告 ) const ( - CodeSuccess = 0 // 成功 - CodeInternalServerError = 1 // 内部服务错误 - CodeUserNotExist = 2 // 用户不存在 - CodeUserExist = 3 // 用户存在 - CodeWrongPassword = 4 // 密码错误 - CodeNoLogIn = 5 // 未登录或非法访问 - CodeTokenExpired = 6 // token 过期 - CodeInvalidParam = 7 // 无效或未填参数 - CodePermissionAccess = 8 // 权限通过 - CodePermissionDenied = 9 // 权限拒绝 - CodeEmailExist = 10 // 邮箱已存在 - CodeAccountExist = 11 // 账号已存在 - CodePhoneExist = 12 // 电话号码已存在 - CodeEmailAccountPhoneNone = 13 // 邮箱、账号、电话不能都为空 - CodeParentIDShouldNotEqualID = 14 // 父节点的点不能等于自己的id - CodeWrongOldPassword = 15 // 旧密码错误 - CodeRecordExistError = 40001 // 记录已存在 + MsgSuccess = "成功" ) -var ( - MsgSuccess = "成功" - MsgInternalServerError = "内部服务错误" - MsgUserNotExist = "用户不存在" - MsgUserExist = "用户存在" - MsgWrongPassword = "密码错误" - MsgNoLogIn = "未登录或非法访问" - MsgTokenExpired = "过期" - MsgInvalidParam = "无效或未填参数" - MsgPermissionAccess = "权限通过" - MsgPermissionDenied = "权限拒绝" - MsgEmailExist = "邮箱已存在" - MsgAccountExist = "账号已存在" - MsgPhoneExist = "电话号码已存在" - MsgEmailAccountPhoneNone = "邮箱、账号、电话不能都为空" - MsgParentIDShouldNotEqualID = "父节点的点不能等于自己的id" - MsgWrongOldPassword = "旧密码错误" - MsgRecordExistError = "记录已存在" -) +func Success(msg string) error { + return status.Error(CodeSuccess, msg) +} -var ( - ErrSuccess = status.Error(CodeSuccess, MsgSuccess) - ErrInternalServerError = status.Error(CodeInternalServerError, MsgInternalServerError) - ErrUserNotExist = status.Error(CodeUserNotExist, MsgUserNotExist) - ErrUserExist = status.Error(CodeUserExist, MsgUserExist) - ErrWrongPassword = status.Error(CodeWrongPassword, MsgWrongPassword) - ErrNoLogIn = status.Error(CodeNoLogIn, MsgNoLogIn) - ErrTokenExpired = status.Error(CodeTokenExpired, MsgTokenExpired) - ErrInvalidParam = status.Error(CodeInvalidParam, MsgInvalidParam) - ErrPermissionAccess = status.Error(CodePermissionAccess, MsgPermissionAccess) - ErrPermissionDenied = status.Error(CodePermissionDenied, MsgPermissionDenied) - ErrEmailExist = status.Error(CodeEmailExist, MsgEmailExist) - ErrAccountExist = status.Error(CodeAccountExist, MsgAccountExist) - ErrPhoneExist = status.Error(CodePhoneExist, MsgPhoneExist) - ErrEmailAccountPhoneNone = status.Error(CodeEmailAccountPhoneNone, MsgEmailAccountPhoneNone) - ErrParentIDShouldNotEqualID = status.Error(CodeParentIDShouldNotEqualID, MsgParentIDShouldNotEqualID) - ErrWrongOldPassword = status.Error(CodeWrongOldPassword, MsgWrongOldPassword) - ErrRecordExistError = status.Error(CodeRecordExistError, MsgRecordExistError) -) +func Faild(msg string) error { + return status.Error(CodeSuccess, msg) +} +func Info(msg string) error { + return status.Error(CodeSuccess, msg) +} + +func Warn(msg string) error { + return status.Error(CodeSuccess, msg) +} diff --git a/response.go b/response.go index 11e2f22..e204fe2 100644 --- a/response.go +++ b/response.go @@ -1,7 +1,6 @@ package response import ( - "errors" "net/http" "github.com/gin-gonic/gin" @@ -19,7 +18,7 @@ func getErrorCodeAndMessage(err error) (int, string) { if st, ok := status.FromError(err); ok { return int(st.Code()), st.Message() } - return CodeInternalServerError, "未知错误类型或内部服务错误" + return CodeFaild, "未知错误类型或内部服务错误" } func Result(c *gin.Context, err error, data any) { @@ -32,10 +31,6 @@ func Result(c *gin.Context, err error, data any) { httpStatus := http.StatusOK - if code == CodeInternalServerError || !errors.Is(err, ErrSuccess) && code == 0 { - httpStatus = http.StatusInternalServerError - } - c.JSON(httpStatus, Response{ Code: code, Message: message, @@ -53,10 +48,6 @@ func PageResult(c *gin.Context, err error, total int64, data any) { httpStatus := http.StatusOK - if code == CodeInternalServerError || !errors.Is(err, ErrSuccess) && code == 0 { - httpStatus = http.StatusInternalServerError - } - c.JSON(httpStatus, Response{ Code: code, Message: message, @@ -64,4 +55,3 @@ func PageResult(c *gin.Context, err error, total int64, data any) { Data: data, }) } -