结构修改
This commit is contained in:
78
code.go
78
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)
|
||||
}
|
||||
|
||||
12
response.go
12
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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user