正在显示
10 个修改的文件
包含
116 行增加
和
220 行删除
pkg/contextdata/tenant.go
已删除
100644 → 0
@@ -5,7 +5,6 @@ import ( | @@ -5,7 +5,6 @@ import ( | ||
5 | "encoding/json" | 5 | "encoding/json" |
6 | "github.com/golang-jwt/jwt/v4" | 6 | "github.com/golang-jwt/jwt/v4" |
7 | "github.com/zeromicro/go-zero/core/logx" | 7 | "github.com/zeromicro/go-zero/core/logx" |
8 | - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/config" | ||
9 | "time" | 8 | "time" |
10 | ) | 9 | ) |
11 | 10 | ||
@@ -63,22 +62,13 @@ type UserToken struct { | @@ -63,22 +62,13 @@ type UserToken struct { | ||
63 | CompanyId int64 `json:"companyId"` | 62 | CompanyId int64 `json:"companyId"` |
64 | } | 63 | } |
65 | 64 | ||
66 | -func (tk UserToken) GenerateToken(jwtConfig config.JwtAuth) (string, error) { | 65 | +func (tk UserToken) GenerateToken(secret string, expire int64) (string, error) { |
67 | claims := make(jwt.MapClaims) | 66 | claims := make(jwt.MapClaims) |
68 | - claims["exp"] = time.Now().Unix() + jwtConfig.Expire | 67 | + claims["exp"] = time.Now().Unix() + expire |
69 | claims["iat"] = time.Now().Unix() | 68 | claims["iat"] = time.Now().Unix() |
70 | claims["UserId"] = tk.UserId | 69 | claims["UserId"] = tk.UserId |
71 | token := jwt.New(jwt.SigningMethodHS256) | 70 | token := jwt.New(jwt.SigningMethodHS256) |
72 | token.Claims = claims | 71 | token.Claims = claims |
73 | 72 | ||
74 | - return token.SignedString([]byte(jwtConfig.AccessSecret)) | ||
75 | -} | ||
76 | - | ||
77 | -func (tk *UserToken) ParseToken(jwtConfig config.JWT, str string) error { | ||
78 | - return nil | ||
79 | -} | ||
80 | - | ||
81 | -// CheckUserInfo 如果UserToken有效 返回:true 否则返回false | ||
82 | -func (tk *UserToken) CheckUserInfo() bool { | ||
83 | - return !(tk.UserId > 100000000 || tk.UserId <= 0) | 73 | + return token.SignedString([]byte(secret)) |
84 | } | 74 | } |
pkg/result/httpResult.go
已删除
100644 → 0
1 | -package result | ||
2 | - | ||
3 | -import ( | ||
4 | - "fmt" | ||
5 | - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" | ||
6 | - "net/http" | ||
7 | - | ||
8 | - "github.com/pkg/errors" | ||
9 | - "github.com/zeromicro/go-zero/core/logx" | ||
10 | - "github.com/zeromicro/go-zero/rest/httpx" | ||
11 | - "google.golang.org/grpc/status" | ||
12 | -) | ||
13 | - | ||
14 | -// http返回 | ||
15 | -func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) { | ||
16 | - | ||
17 | - if err == nil { | ||
18 | - //成功返回 | ||
19 | - r := Success(resp) | ||
20 | - httpx.WriteJson(w, http.StatusOK, r) | ||
21 | - } else { | ||
22 | - //错误返回 | ||
23 | - errcode := xerr.SERVER_COMMON_ERROR | ||
24 | - errmsg := "服务器开小差啦,稍后再来试一试" | ||
25 | - internalErr := "" | ||
26 | - causeErr := errors.Cause(err) // err类型 | ||
27 | - if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型 | ||
28 | - //自定义CodeError | ||
29 | - errcode = e.GetErrCode() | ||
30 | - errmsg = e.GetErrMsg() | ||
31 | - if e.InternalError != nil { | ||
32 | - internalErr = e.InternalError.Error() | ||
33 | - } | ||
34 | - } else { | ||
35 | - if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误 | ||
36 | - grpcCode := uint32(gstatus.Code()) | ||
37 | - if xerr.IsCodeErr(grpcCode) { //区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端 | ||
38 | - errcode = grpcCode | ||
39 | - errmsg = gstatus.Message() | ||
40 | - } | ||
41 | - } | ||
42 | - } | ||
43 | - | ||
44 | - logx.WithContext(r.Context()).Errorf("【API-ERR】 : %+v ", err) | ||
45 | - response := Error(errcode, errmsg) | ||
46 | - response.Error = internalErr | ||
47 | - httpx.WriteJson(w, http.StatusOK, response) | ||
48 | - } | ||
49 | -} | ||
50 | - | ||
51 | -// 授权的http方法 | ||
52 | -func AuthHttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) { | ||
53 | - | ||
54 | - if err == nil { | ||
55 | - //成功返回 | ||
56 | - r := Success(resp) | ||
57 | - httpx.WriteJson(w, http.StatusOK, r) | ||
58 | - } else { | ||
59 | - //错误返回 | ||
60 | - errcode := xerr.SERVER_COMMON_ERROR | ||
61 | - errmsg := "服务器开小差啦,稍后再来试一试" | ||
62 | - | ||
63 | - causeErr := errors.Cause(err) // err类型 | ||
64 | - if e, ok := causeErr.(*xerr.CodeError); ok { //自定义错误类型 | ||
65 | - //自定义CodeError | ||
66 | - errcode = e.GetErrCode() | ||
67 | - errmsg = e.GetErrMsg() | ||
68 | - } else { | ||
69 | - if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误 | ||
70 | - grpcCode := uint32(gstatus.Code()) | ||
71 | - if xerr.IsCodeErr(grpcCode) { //区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端 | ||
72 | - errcode = grpcCode | ||
73 | - errmsg = gstatus.Message() | ||
74 | - } | ||
75 | - } | ||
76 | - } | ||
77 | - | ||
78 | - logx.WithContext(r.Context()).Errorf("【GATEWAY-ERR】 : %+v ", err) | ||
79 | - | ||
80 | - httpx.WriteJson(w, http.StatusUnauthorized, Error(errcode, errmsg)) | ||
81 | - } | ||
82 | -} | ||
83 | - | ||
84 | -// http 参数错误返回 | ||
85 | -func ParamErrorResult(r *http.Request, w http.ResponseWriter, err error) { | ||
86 | - errMsg := fmt.Sprintf("%s ,%s", xerr.MapErrMsg(xerr.REUQEST_PARAM_ERROR), err.Error()) | ||
87 | - httpx.WriteJson(w, http.StatusBadRequest, Error(xerr.REUQEST_PARAM_ERROR, errMsg)) | ||
88 | -} |
pkg/result/http_result.go
0 → 100644
1 | +package result | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" | ||
6 | + "net/http" | ||
7 | + | ||
8 | + "github.com/pkg/errors" | ||
9 | + "github.com/zeromicro/go-zero/core/logx" | ||
10 | + "github.com/zeromicro/go-zero/rest/httpx" | ||
11 | + "google.golang.org/grpc/status" | ||
12 | +) | ||
13 | + | ||
14 | +// HttpResult http响应结果返回 | ||
15 | +func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err error) { | ||
16 | + // 成功返回 | ||
17 | + if err == nil { | ||
18 | + r := Success(resp) | ||
19 | + httpx.WriteJson(w, http.StatusOK, r) | ||
20 | + return | ||
21 | + } | ||
22 | + | ||
23 | + //错误返回 | ||
24 | + errCode := xerr.ServerCommonError | ||
25 | + errMsg := "服务器开小差啦,稍后再来试一试" | ||
26 | + internalErr := "" | ||
27 | + causeErr := errors.Cause(err) | ||
28 | + | ||
29 | + codeError := &xerr.CodeError{} | ||
30 | + if ok := errors.As(causeErr, codeError); ok { // 自定义错误类型 | ||
31 | + errCode = codeError.GetErrCode() | ||
32 | + errMsg = codeError.GetErrMsg() | ||
33 | + if codeError.InternalError != nil { | ||
34 | + internalErr = codeError.InternalError.Error() | ||
35 | + } | ||
36 | + } else { | ||
37 | + if grpcStatus, ok := status.FromError(causeErr); ok { // grpc err错误 | ||
38 | + grpcCode := uint32(grpcStatus.Code()) | ||
39 | + if xerr.IsCodeErr(grpcCode) { | ||
40 | + errCode = grpcCode | ||
41 | + errMsg = grpcStatus.Message() | ||
42 | + } | ||
43 | + } | ||
44 | + } | ||
45 | + // TODO:区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端 | ||
46 | + logx.WithContext(r.Context()).Errorf("【API-ERR】 : %+v ", err) | ||
47 | + response := Error(errCode, errMsg) | ||
48 | + response.Error = internalErr | ||
49 | + httpx.WriteJson(w, http.StatusOK, response) | ||
50 | + | ||
51 | +} | ||
52 | + | ||
53 | +// ParamErrorResult http参数错误返回 | ||
54 | +func ParamErrorResult(r *http.Request, w http.ResponseWriter, err error) { | ||
55 | + errMsg := fmt.Sprintf("%s ,%s", xerr.MapErrMsg(xerr.RequestParamError), err.Error()) | ||
56 | + httpx.WriteJson(w, http.StatusBadRequest, Error(xerr.RequestParamError, errMsg)) | ||
57 | +} |
pkg/result/jobResult.go
已删除
100644 → 0
1 | -package result | ||
2 | - | ||
3 | -import ( | ||
4 | - "context" | ||
5 | - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" | ||
6 | - | ||
7 | - "github.com/pkg/errors" | ||
8 | - "github.com/zeromicro/go-zero/core/logx" | ||
9 | - "google.golang.org/grpc/status" | ||
10 | -) | ||
11 | - | ||
12 | -// job返回 | ||
13 | -func JobResult(ctx context.Context, resp interface{}, err error) { | ||
14 | - if err == nil { | ||
15 | - // 成功返回 ,只有dev环境下才会打印info,线上不显示 | ||
16 | - if resp != nil { | ||
17 | - logx.Infof("resp: %+v", resp) | ||
18 | - } | ||
19 | - return | ||
20 | - } else { | ||
21 | - errCode := xerr.SERVER_COMMON_ERROR | ||
22 | - errMsg := "服务器开小差啦,稍后再来试一试" | ||
23 | - | ||
24 | - // 错误返回 | ||
25 | - causeErr := errors.Cause(err) // err类型 | ||
26 | - if e, ok := causeErr.(*xerr.CodeError); ok { // 自定义错误类型 | ||
27 | - // 自定义CodeError | ||
28 | - errCode = e.GetErrCode() | ||
29 | - errMsg = e.GetErrMsg() | ||
30 | - } else { | ||
31 | - if gstatus, ok := status.FromError(causeErr); ok { // grpc err错误 | ||
32 | - grpcCode := uint32(gstatus.Code()) | ||
33 | - if xerr.IsCodeErr(grpcCode) { // 区分自定义错误跟系统底层、db等错误,底层、db错误不能返回给前端 | ||
34 | - errCode = grpcCode | ||
35 | - errMsg = gstatus.Message() | ||
36 | - } | ||
37 | - } | ||
38 | - } | ||
39 | - | ||
40 | - logx.WithContext(ctx).Errorf("【JOB-ERR】 : %+v ,errCode:%d , errMsg:%s ", err, errCode, errMsg) | ||
41 | - return | ||
42 | - } | ||
43 | -} |
pkg/xerr/err.go
0 → 100644
1 | +package xerr | ||
2 | + | ||
3 | +/**默认的服务错误**/ | ||
4 | + | ||
5 | +func NewErr(err error) *CodeError { | ||
6 | + return &CodeError{errCode: ServerCommonError, InternalError: err} | ||
7 | +} | ||
8 | + | ||
9 | +func NewErrMsg(errMsg string) *CodeError { | ||
10 | + return &CodeError{errCode: ServerCommonError, errMsg: errMsg} | ||
11 | +} | ||
12 | + | ||
13 | +func NewErrMsgErr(errMsg string, internalError error) *CodeError { | ||
14 | + return &CodeError{errCode: ServerCommonError, errMsg: errMsg, InternalError: internalError} | ||
15 | +} | ||
16 | + | ||
17 | +/**指定错误码的错误**/ | ||
18 | + | ||
19 | +func NewCodeErr(errCode uint32, err error) *CodeError { | ||
20 | + return &CodeError{errCode: errCode, errMsg: MapErrMsg(errCode), InternalError: err} | ||
21 | +} | ||
22 | + | ||
23 | +func NewCodeErrMsg(errCode uint32, err error, msg string) *CodeError { | ||
24 | + return &CodeError{errCode: errCode, errMsg: msg, InternalError: err} | ||
25 | +} |
pkg/xerr/errCode.go
已删除
100644 → 0
1 | -package xerr | ||
2 | - | ||
3 | -// 成功返回 | ||
4 | -const OK uint32 = 200 | ||
5 | - | ||
6 | -/**(前3位代表业务,后三位代表具体功能)**/ | ||
7 | - | ||
8 | -// 全局错误码 | ||
9 | -const SERVER_COMMON_ERROR uint32 = 100001 | ||
10 | -const REUQEST_PARAM_ERROR uint32 = 100002 | ||
11 | -const TOKEN_EXPIRE_ERROR uint32 = 100003 | ||
12 | -const TOKEN_GENERATE_ERROR uint32 = 100004 | ||
13 | -const DB_ERROR uint32 = 100005 | ||
14 | -const DB_UPDATE_AFFECTED_ZERO_ERROR uint32 = 100006 | ||
15 | - | ||
16 | -const REQUEST_ARGS_ERROR = 200001 | ||
17 | - | ||
18 | -// 微信模块 | ||
19 | -const ErrWxMiniAuthFailError uint32 = 500001 | ||
20 | -const ErrUserNoAuth uint32 = 500002 |
1 | package xerr | 1 | package xerr |
2 | 2 | ||
3 | -import ( | ||
4 | - "fmt" | 3 | +import "fmt" |
4 | + | ||
5 | +const ( | ||
6 | + // OK 成功返回 | ||
7 | + OK uint32 = 200 | ||
8 | +) | ||
9 | + | ||
10 | +// 全局错误码 | ||
11 | +// 系统错误前3位代表业务,后三位代表具体功能 | ||
12 | +const ( | ||
13 | + ServerCommonError uint32 = 100001 // 系统错误 | ||
14 | + RequestParamError uint32 = 100002 // 参数请求错误 | ||
15 | + TokenExpireError uint32 = 100003 // token失效 | ||
16 | + TokenGenerateError uint32 = 100004 // 生成token失败 | ||
17 | + DbError uint32 = 100005 // 数据库错误 | ||
18 | + DbUpdateAffectedZeroError uint32 = 100006 // 数据库更新错误 | ||
5 | ) | 19 | ) |
6 | 20 | ||
7 | -/** | ||
8 | -常用通用固定错误 | ||
9 | -*/ | 21 | +/**微信模块**/ |
22 | +const ( | ||
23 | + ErrWxMiniAuthFailError uint32 = 500001 | ||
24 | + ErrUserNoAuth uint32 = 500002 | ||
25 | +) | ||
10 | 26 | ||
11 | type CodeError struct { | 27 | type CodeError struct { |
12 | errCode uint32 | 28 | errCode uint32 |
@@ -33,31 +49,3 @@ func (e *CodeError) Error() string { | @@ -33,31 +49,3 @@ func (e *CodeError) Error() string { | ||
33 | } | 49 | } |
34 | return fmt.Sprintf("ErrCode:%d,ErrMsg:%s", e.errCode, e.errMsg) | 50 | return fmt.Sprintf("ErrCode:%d,ErrMsg:%s", e.errCode, e.errMsg) |
35 | } | 51 | } |
36 | - | ||
37 | -/* | ||
38 | - 指定错误码的错误 | ||
39 | -*/ | ||
40 | - | ||
41 | -func NewCodeErr(errCode uint32, err error) *CodeError { | ||
42 | - return &CodeError{errCode: errCode, errMsg: MapErrMsg(errCode), InternalError: err} | ||
43 | -} | ||
44 | - | ||
45 | -func NewCodeErrMsg(errCode uint32, err error, msg string) *CodeError { | ||
46 | - return &CodeError{errCode: errCode, errMsg: msg, InternalError: err} | ||
47 | -} | ||
48 | - | ||
49 | -/* | ||
50 | - 默认的服务错误 | ||
51 | -*/ | ||
52 | - | ||
53 | -func NewErr(err error) *CodeError { | ||
54 | - return &CodeError{errCode: SERVER_COMMON_ERROR, InternalError: err} | ||
55 | -} | ||
56 | - | ||
57 | -func NewErrMsg(errMsg string) *CodeError { | ||
58 | - return &CodeError{errCode: SERVER_COMMON_ERROR, errMsg: errMsg} | ||
59 | -} | ||
60 | - | ||
61 | -func NewErrMsgErr(errMsg string, err error) *CodeError { | ||
62 | - return &CodeError{errCode: SERVER_COMMON_ERROR, errMsg: errMsg, InternalError: err} | ||
63 | -} |
@@ -5,26 +5,26 @@ var message map[uint32]string | @@ -5,26 +5,26 @@ var message map[uint32]string | ||
5 | func init() { | 5 | func init() { |
6 | message = make(map[uint32]string) | 6 | message = make(map[uint32]string) |
7 | message[OK] = "SUCCESS" | 7 | message[OK] = "SUCCESS" |
8 | - message[SERVER_COMMON_ERROR] = "服务器开小差啦,稍后再来试一试" | ||
9 | - message[REUQEST_PARAM_ERROR] = "参数错误" | ||
10 | - message[TOKEN_EXPIRE_ERROR] = "token失效,请重新登陆" | ||
11 | - message[TOKEN_GENERATE_ERROR] = "生成token失败" | ||
12 | - message[DB_ERROR] = "数据库繁忙,请稍后再试" | ||
13 | - message[DB_UPDATE_AFFECTED_ZERO_ERROR] = "更新数据影响行数为0" | 8 | + message[ServerCommonError] = "服务器开小差啦,稍后再来试一试" |
9 | + message[RequestParamError] = "参数错误" | ||
10 | + message[TokenExpireError] = "token失效,请重新登陆" | ||
11 | + message[TokenGenerateError] = "生成token失败" | ||
12 | + message[DbError] = "数据库繁忙,请稍后再试" | ||
13 | + message[DbUpdateAffectedZeroError] = "更新数据影响行数为0" | ||
14 | message[ErrUserNoAuth] = "无权限" | 14 | message[ErrUserNoAuth] = "无权限" |
15 | message[ErrWxMiniAuthFailError] = "微信授权失败" | 15 | message[ErrWxMiniAuthFailError] = "微信授权失败" |
16 | } | 16 | } |
17 | 17 | ||
18 | -func MapErrMsg(errcode uint32) string { | ||
19 | - if msg, ok := message[errcode]; ok { | 18 | +func MapErrMsg(errCode uint32) string { |
19 | + if msg, ok := message[errCode]; ok { | ||
20 | return msg | 20 | return msg |
21 | } else { | 21 | } else { |
22 | return "服务器开小差啦,稍后再来试一试" | 22 | return "服务器开小差啦,稍后再来试一试" |
23 | } | 23 | } |
24 | } | 24 | } |
25 | 25 | ||
26 | -func IsCodeErr(errcode uint32) bool { | ||
27 | - if _, ok := message[errcode]; ok { | 26 | +func IsCodeErr(errCode uint32) bool { |
27 | + if _, ok := message[errCode]; ok { | ||
28 | return true | 28 | return true |
29 | } else { | 29 | } else { |
30 | return false | 30 | return false |
-
请 注册 或 登录 后发表评论