作者 yangfu

权限认证

... ... @@ -11,6 +11,7 @@ info(
@server(
prefix: v1
group: department
middleware: LoginStatusCheck
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -11,6 +11,7 @@ info(
@server(
prefix: v1
group: role
middleware: LoginStatusCheck
jwt: SystemAuth
)
service Core {
... ...
... ... @@ -209,6 +209,7 @@ type (
@server(
prefix: v1
group: user
middleware: LoginStatusCheck
jwt: SystemAuth
)
service Core {
... ... @@ -249,7 +250,7 @@ service Core {
type(
SystemUserInfoRequest{
Token string `header:"x-mmm-accesstoken"`
}
SystemUserInfoResponse{
UserId int64 `json:"userId"`
... ... @@ -257,6 +258,7 @@ type(
Avatar string `json:"avatar"`
CompanyId int64 `json:"companyId"`
CompanyName string `json:"companyName"`
Code string `json:"code"`
}
UserStatisticsRequest{
UserId int64 `json:"userId"`
... ...
Name: discuss
Host: 0.0.0.0
Port: 8081
Verbose: true
Verbose: false
Migrate: false
Timeout: 30000
Log:
... ... @@ -12,7 +12,7 @@ Log:
TimeFormat: 2006-01-02 15:04:05.000
SystemAuth:
AccessSecret: discuss-secret
AccessSecret: digital-platform
AccessExpire: 360000
MiniAuth:
... ... @@ -25,3 +25,8 @@ Redis:
Pass:
DB:
DataSource: host=114.55.200.59 user=postgres password=eagle1010 dbname=sumifcc-discuss-dev port=31543 sslmode=disable TimeZone=Asia/Shanghai
ApiAuth:
Name: ApiAuth
Host: http://digital-platform-dev.fjmaimaimai.com
Timeout: 0s
\ No newline at end of file
... ...
... ... @@ -4,6 +4,7 @@ import (
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/config"
"time"
)
type Config struct {
... ... @@ -13,4 +14,11 @@ type Config struct {
SystemAuth config.Auth
MiniAuth config.Auth
Migrate bool `json:",optional,default=true"`
ApiAuth ApiService
}
type ApiService struct {
Name string
Host string
Timeout time.Duration
}
... ...
... ... @@ -173,58 +173,61 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/system/user/info",
Handler: user.SystemUserInfoHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/user/statistics",
Handler: user.SystemUserStatisticsHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/system/user/:id",
Handler: user.SystemUserGetHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/user/:id",
Handler: user.SystemUserUpdateHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/user/search",
Handler: user.SystemUserSearchHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/system/account/:id",
Handler: user.SystemUserAccountGetHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/account",
Handler: user.SystemUserAccountSaveHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/account/enable",
Handler: user.SystemUserAccountEnableHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/account/:id",
Handler: user.SystemUserAccountUpdateHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/account/search",
Handler: user.SystemUserAccountSearchHandler(serverCtx),
},
},
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/system/user/info",
Handler: user.SystemUserInfoHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/user/statistics",
Handler: user.SystemUserStatisticsHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/system/user/:id",
Handler: user.SystemUserGetHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/user/:id",
Handler: user.SystemUserUpdateHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/user/search",
Handler: user.SystemUserSearchHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/system/account/:id",
Handler: user.SystemUserAccountGetHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/account",
Handler: user.SystemUserAccountSaveHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/account/enable",
Handler: user.SystemUserAccountEnableHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/account/:id",
Handler: user.SystemUserAccountUpdateHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/account/search",
Handler: user.SystemUserAccountSearchHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
... ... @@ -354,60 +357,66 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/system/role/:id",
Handler: role.SystemGetRoleHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/role",
Handler: role.SystemSaveRoleHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/system/role/:id",
Handler: role.SystemDeleteRoleHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/role/:id",
Handler: role.SystemUpdateRoleHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/role/search",
Handler: role.SystemSearchRoleHandler(serverCtx),
},
},
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/system/role/:id",
Handler: role.SystemGetRoleHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/role",
Handler: role.SystemSaveRoleHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/system/role/:id",
Handler: role.SystemDeleteRoleHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/role/:id",
Handler: role.SystemUpdateRoleHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/role/search",
Handler: role.SystemSearchRoleHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/system/department/list",
Handler: department.SystemListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/department/add",
Handler: department.SystemAddHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/system/department/:id",
Handler: department.SystemGetHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/department/:id",
Handler: department.SystemUpdateHandler(serverCtx),
},
},
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LoginStatusCheck},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/system/department/list",
Handler: department.SystemListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/department/add",
Handler: department.SystemAddHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/system/department/:id",
Handler: department.SystemGetHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/department/:id",
Handler: department.SystemUpdateHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
... ...
... ... @@ -2,8 +2,15 @@ package user
import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"strconv"
"github.com/zeromicro/go-zero/core/logx"
)
... ... @@ -23,6 +30,67 @@ func NewSystemUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sy
}
func (l *SystemUserInfoLogic) SystemUserInfo(req *types.SystemUserInfoRequest) (resp *types.SystemUserInfoResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
userToken = contextdata.GetUserTokenFromCtx(l.ctx)
response *authlib.DataUserMe
company *domain.Company
companyId int64
code = tool.Krand(6, tool.KC_RAND_KIND_ALL)
)
if response, err = l.svcCtx.ApiAuthService.MeInfo(l.ctx, authlib.RequestUserMeQuery{
Token: req.Token,
}); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
companyId, _ = strconv.ParseInt(response.CurrentCompany.ID, 10, 64)
resp = &types.SystemUserInfoResponse{}
resp.UserName = response.User.NickName
resp.UserId, _ = strconv.ParseInt(response.User.ID, 10, 64)
resp.Avatar = response.User.Avatar
resp.CompanyName = response.CurrentCompany.Name
resp.CompanyId = companyId
resp.Code = code
if companyId != userToken.CompanyId {
return nil, xerr.NewErrMsgErr("获取用户资料失败", fmt.Errorf("当前登录公司信息不匹配"))
}
company, err = l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, userToken.CompanyId)
// 新建公司
if err == domain.ErrNotFound {
company = &domain.Company{
Id: companyId,
Name: response.CurrentCompany.Name,
Logo: response.CurrentCompany.Logo,
Code: code,
}
if company, err = l.svcCtx.CompanyRepository.Insert(l.ctx, conn, company); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
err = nil
return
}
if err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
resp.Code = company.Code
// 更新公司
if response.CurrentCompany != nil {
var changed bool
if response.CurrentCompany.Name != "" && response.CurrentCompany.Name != company.Name {
company.Name = response.CurrentCompany.Name
changed = true
}
if response.CurrentCompany.Logo != "" && response.CurrentCompany.Logo != company.Logo {
company.Logo = response.CurrentCompany.Logo
changed = true
}
if changed {
if company, err = l.svcCtx.CompanyRepository.UpdateWithVersion(l.ctx, conn, company); err != nil {
return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
}
}
}
return
}
... ...
package middleware
import (
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"net/http"
)
type LoginStatusCheckMiddleware struct {
apiAuth authlib.ApiAuthService
}
func NewLoginStatusCheckMiddleware(apiAuth authlib.ApiAuthService) *LoginStatusCheckMiddleware {
return &LoginStatusCheckMiddleware{
apiAuth: apiAuth,
}
}
func (m *LoginStatusCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("x-mmm-accesstoken")
if len(token) > 0 {
_, err := m.apiAuth.LoginCheck(r.Context(), authlib.RequestLoginCheck{
Token: token,
})
if err != nil {
gatewayError, ok := err.(gateway.HttpError)
if ok {
unAuthResponse(w, gatewayError.Base.Code, gatewayError.Base.Msg)
return
}
result.HttpResult(r, w, struct{}{}, xerr.NewErr(err))
return
}
}
next(w, r)
}
}
func unAuthResponse(w http.ResponseWriter, code int, msg string) {
data := map[string]interface{}{
"msg": msg,
"code": code,
"data": struct{}{},
}
httpx.WriteJson(w, http.StatusUnauthorized, data)
}
... ...
... ... @@ -2,10 +2,14 @@ package svc
import (
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/rest"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/config"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/middleware"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/repository"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/database"
"gorm.io/gorm"
... ... @@ -33,6 +37,10 @@ type ServiceContext struct {
UserLoveFlagRepository domain.UserLoveFlagRepository
UserReadArticleRepository domain.UserReadArticleRepository
UserRepository domain.UserRepository
ApiAuthService authlib.ApiAuthService
LoginStatusCheck rest.Middleware
}
func NewServiceContext(c config.Config) *ServiceContext {
... ... @@ -41,11 +49,16 @@ func NewServiceContext(c config.Config) *ServiceContext {
mlCache := cache.NewMultiLevelCache([]string{c.Redis.Host}, c.Redis.Pass)
redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"})
apiAuth := authlib.ApiAuthService{
Service: gateway.NewService(c.ApiAuth.Name, c.ApiAuth.Host, c.ApiAuth.Timeout),
}
return &ServiceContext{
Config: c,
DB: db,
Redis: redis,
Config: c,
DB: db,
Redis: redis,
ApiAuthService: apiAuth,
LoginStatusCheck: middleware.NewLoginStatusCheckMiddleware(apiAuth).Handle,
CommentRepository: repository.NewCommentRepository(cache.NewCachedRepository(mlCache)),
ArticleBackupRepository: repository.NewArticleBackupRepository(cache.NewCachedRepository(mlCache)),
ArticleCommentRepository: repository.NewArticleCommentRepository(cache.NewCachedRepository(mlCache)),
... ...
... ... @@ -392,6 +392,7 @@ type SimpleComment struct {
}
type SystemUserInfoRequest struct {
Token string `header:"x-mmm-accesstoken"`
}
type SystemUserInfoResponse struct {
... ... @@ -400,6 +401,7 @@ type SystemUserInfoResponse struct {
Avatar string `json:"avatar"`
CompanyId int64 `json:"companyId"`
CompanyName string `json:"companyName"`
Code string `json:"code"`
}
type UserStatisticsRequest struct {
... ...
... ... @@ -10,7 +10,7 @@ import (
type Company struct {
Id int64 // 唯一标识
Name string // 名称
Code string // 编码(搜索使用,4位字母数字)
Code string `gorm:"uniqueIndex:idx_company_code"` // 编码(搜索使用,4位字母数字)
Logo string // 公司LOGO
CreatedAt int64
... ...
package authlib
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway"
"net/http"
)
type ApiAuthService struct {
gateway.Service
}
func (svc *ApiAuthService) MeInfo(ctx context.Context, request RequestUserMeQuery) (*DataUserMe, error) {
var result DataUserMe
if err := svc.Do(ctx, "/v1/user/me", http.MethodGet, request, &result); err != nil {
return nil, err
}
return &result, nil
}
func (svc *ApiAuthService) MeAppInfo(ctx context.Context, request RequestUserMeQuery) (*DataUserAppInfo, error) {
var result DataUserAppInfo
if err := svc.Do(ctx, "/v1/user/me-app-info", http.MethodGet, request, &result); err != nil {
return nil, err
}
return &result, nil
}
func (svc *ApiAuthService) LoginCheck(ctx context.Context, request RequestLoginCheck) (*DataLoginCheck, error) {
var (
result DataLoginCheck
err error
)
if err = svc.Do(ctx, "/v1/login/check?token="+request.Token, http.MethodGet, request, &result); err != nil {
return nil, err
}
if errCodeMsg, ok := err.(gateway.HttpError); ok {
return &DataLoginCheck{
Code: errCodeMsg.Base.Code,
Msg: errCodeMsg.Base.Msg,
}, nil
}
return &result, nil
}
func (svc *ApiAuthService) AppLogin(ctx context.Context, request RequestAppLogin) (*DataAppLogin, error) {
var result DataAppLogin
if err := svc.Do(ctx, "/v1/login/check?token="+request.Token, http.MethodGet, request, &result); err != nil {
return nil, err
}
return &result, nil
}
... ...
package authlib
type RequestUserMeQuery struct {
Token string `header:"x-mmm-accesstoken"`
//UserId int
//CompanyId int
}
type DataUserMe struct {
User *struct {
ID string `json:"id"`
Phone string `json:"phone"`
NickName string `json:"nickName"`
Avatar string `json:"avatar"`
} `json:"user,optional"`
CompanyList []*struct {
ID string `json:"id"`
Name string `json:"name"`
Logo string `json:"logo"`
DefaultLogin int `json:"defaultLogin"`
Types int `json:"types"`
} `json:"companyList,optional"`
CurrentCompany *struct {
ID string `json:"id"`
Name string `json:"name"`
Logo string `json:"logo"`
DefaultLogin int `json:"defaultLogin"`
Types int `json:"types"`
} `json:"currentCompany,optional"`
Workbench []*struct {
ID int `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
CoverImage string `json:"coverImage"`
URL string `json:"url"`
} `json:"workbench,optional"`
Menus []*struct {
MenuID int `json:"menuId"`
ParentID int `json:"parentId"`
MenuName string `json:"menuName"`
Code string `json:"code"`
Types string `json:"types"`
} `json:"menus,optional"`
}
type RequestLoginCheck struct {
Token string
}
type DataLoginCheck struct {
Code int `json:"code,optional"`
Msg string `json:"msg,optional"`
}
type (
RequestAppLogin struct {
AppKey string `json:"appKey" valid:"Required"` // 应用键值
Token string `json:"token" valid:"Required"` // 凭证
}
DataAppLogin struct {
AppEnabled bool `json:"appEnabled"`
}
)
type (
DataUserAppInfo struct {
Apps []AppItem `json:"apps"`
}
AppItem struct {
AppId int64
AppKey string
AppName string
}
)
... ...
package gateway
import (
"encoding/json"
"fmt"
)
// Response 统一消息返回格式
type Response struct {
Code int `json:"code,optional"`
Msg string `json:"msg,optional"`
Data json.RawMessage `json:"data,optional"`
}
//
//type Request struct {
// Url string
// Method string
// Param interface{}
//}
type HttpError struct {
Base Response
}
func (e HttpError) Error() string {
return fmt.Sprintf("HttpError code:%d msg:%s", e.Base.Code, e.Base.Msg)
}
... ...
package gateway
import (
"context"
"encoding/json"
"fmt"
"github.com/zeromicro/go-zero/core/mapping"
"github.com/zeromicro/go-zero/rest/httpc"
"io/ioutil"
"net/http"
"strings"
"time"
)
type Service struct {
Timeout time.Duration
host string
Interceptor func(msg string)
ServiceName string
service httpc.Service
}
func NewService(name string, host string, timeout time.Duration, opts ...httpc.Option) Service {
client := &http.Client{}
//client.Timeout = timeout
service := Service{
host: host,
service: httpc.NewServiceWithClient(name, client, opts...),
}
return service
}
func (gateway Service) Do(ctx context.Context, url string, method string, val interface{}, result interface{}) error {
var (
baseResponse = Response{}
begin = time.Now()
body []byte
)
response, err := gateway.service.Do(ctx, method, gateway.host+url, val)
defer func() {
jsonParam, _ := json.Marshal(val)
jsonData, _ := json.Marshal(result)
if err != nil {
result = err.Error()
}
if gateway.Interceptor != nil {
gateway.Interceptor(fmt.Sprintf("【网关】%v | %v%v | %v : %v \n-->> %v \n<<-- %v", time.Since(begin), gateway.host, url, strings.ToUpper(method),
result,
string(jsonParam),
string(jsonData),
))
}
}()
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return HttpError{
Base: Response{
Code: response.StatusCode,
Msg: response.Status,
},
}
}
body, err = Bytes(response)
if err != nil {
return err
}
if err = json.Unmarshal(body, &baseResponse); err != nil {
return err
}
if baseResponse.Code != 0 {
return HttpError{
Base: Response{
Code: baseResponse.Code,
Msg: baseResponse.Msg,
},
}
}
if err = mapping.UnmarshalJsonBytes(baseResponse.Data, result); err != nil {
return err
}
return nil
}
func Bytes(resp *http.Response) ([]byte, error) {
var body []byte
if resp.Body == nil {
return nil, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
}
... ...
package gateway
import "net/http"
type RequestOptions struct {
Header http.Header
// key:form key value:path
FileMap map[string]string
}
type Option func(o *RequestOptions)
func WithHeader(header http.Header) Option {
return func(o *RequestOptions) {
o.Header = header
}
}
func WithFileMap(v map[string]string) Option {
return func(o *RequestOptions) {
o.FileMap = v
}
}
... ...
... ... @@ -9,8 +9,8 @@ import (
)
var (
CtxKeyJwtUserId = "UserId"
CtxKeyJwtCompanyId = "CompanyId"
CtxKeyJwtUserId = "userId"
CtxKeyJwtCompanyId = "companyId"
)
func GetInt64FromCtx(ctx context.Context, key string) int64 {
... ...
... ... @@ -37,7 +37,7 @@ func HttpResult(r *http.Request, w http.ResponseWriter, resp interface{}, err er
}
} else {
if grpcStatus, ok := status.FromError(causeErr); ok { // grpc err错误
grpcCode := uint32(grpcStatus.Code())
grpcCode := int(grpcStatus.Code())
if xerr.IsCodeErr(grpcCode) {
errCode = grpcCode
errMsg = grpcStatus.Message()
... ...
... ... @@ -12,11 +12,11 @@ func Success(data interface{}) *ResponseSuccessBean {
}
type ResponseErrorBean struct {
Code uint32 `json:"code"`
Code int `json:"code"`
Msg string `json:"msg"`
Error string `json:"err"`
}
func Error(errCode uint32, errMsg string) *ResponseErrorBean {
func Error(errCode int, errMsg string) *ResponseErrorBean {
return &ResponseErrorBean{Code: errCode, Msg: errMsg}
}
... ...
... ... @@ -16,10 +16,10 @@ func (tk UserToken) GenerateToken(secret string, expire int64) (string, error) {
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Unix() + expire
claims["iat"] = time.Now().Unix()
claims["UserId"] = tk.UserId
claims["AdminId"] = tk.AdminId
claims["CompanyId"] = tk.CompanyId
claims["ClientType"] = tk.ClientType
claims["userId"] = tk.UserId
claims["adminId"] = tk.AdminId
claims["companyId"] = tk.CompanyId
claims["clientType"] = tk.ClientType
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
... ...
... ... @@ -16,10 +16,10 @@ func NewErrMsgErr(errMsg string, internalError error) *CodeError {
/**指定错误码的错误**/
func NewCodeErr(errCode uint32, err error) *CodeError {
func NewCodeErr(errCode int, err error) *CodeError {
return &CodeError{errCode: errCode, errMsg: MapErrMsg(errCode), InternalError: err}
}
func NewCodeErrMsg(errCode uint32, err error, msg string) *CodeError {
func NewCodeErrMsg(errCode int, err error, msg string) *CodeError {
return &CodeError{errCode: errCode, errMsg: msg, InternalError: err}
}
... ...
... ... @@ -4,34 +4,34 @@ import "fmt"
const (
// OK 成功返回
OK uint32 = 200
OK int = 200
)
// 全局错误码
// 系统错误前3位代表业务,后三位代表具体功能
const (
ServerCommonError uint32 = 100001 // 系统错误
RequestParamError uint32 = 100002 // 参数请求错误
TokenExpireError uint32 = 100003 // token失效
TokenGenerateError uint32 = 100004 // 生成token失败
DbError uint32 = 100005 // 数据库错误
DbUpdateAffectedZeroError uint32 = 100006 // 数据库更新错误
ServerCommonError int = 100001 // 系统错误
RequestParamError int = 100002 // 参数请求错误
TokenExpireError int = 100003 // token失效
TokenGenerateError int = 100004 // 生成token失败
DbError int = 100005 // 数据库错误
DbUpdateAffectedZeroError int = 100006 // 数据库更新错误
)
/**微信模块**/
const (
ErrWxMiniAuthFailError uint32 = 500001
ErrUserNoAuth uint32 = 500002
ErrWxMiniAuthFailError int = 500001
ErrUserNoAuth int = 500002
)
type CodeError struct {
errCode uint32
errCode int
errMsg string
InternalError error
}
// GetErrCode 返回给前端的错误码
func (e *CodeError) GetErrCode() uint32 {
func (e *CodeError) GetErrCode() int {
return e.errCode
}
... ...
package xerr
var message map[uint32]string
var message map[int]string
func init() {
message = make(map[uint32]string)
message = make(map[int]string)
message[OK] = "SUCCESS"
message[ServerCommonError] = "服务器开小差啦,稍后再来试一试"
message[RequestParamError] = "参数错误"
... ... @@ -15,7 +15,7 @@ func init() {
message[ErrWxMiniAuthFailError] = "微信授权失败"
}
func MapErrMsg(errCode uint32) string {
func MapErrMsg(errCode int) string {
if msg, ok := message[errCode]; ok {
return msg
} else {
... ... @@ -23,7 +23,7 @@ func MapErrMsg(errCode uint32) string {
}
}
func IsCodeErr(errCode uint32) bool {
func IsCodeErr(errCode int) bool {
if _, ok := message[errCode]; ok {
return true
} else {
... ...