v1.2.0 修改jwt有效期和获取token接口
This commit is contained in:
@@ -246,6 +246,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/user/detail",
|
||||
Handler: user.DetailHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// get new token
|
||||
Method: http.MethodPost,
|
||||
Path: "/user/getToken",
|
||||
Handler: user.GetTokenHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithJwt(serverCtx.Config.JwtAuth.AccessSecret),
|
||||
rest.WithPrefix("/api/v1"),
|
||||
|
||||
17
app/user/cmd/api/internal/handler/user/gettokenhandler.go
Normal file
17
app/user/cmd/api/internal/handler/user/gettokenhandler.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"qnc-server/app/user/cmd/api/internal/logic/user"
|
||||
"qnc-server/app/user/cmd/api/internal/svc"
|
||||
"qnc-server/common/result"
|
||||
)
|
||||
|
||||
func GetTokenHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
l := user.NewGetTokenLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetToken()
|
||||
result.HttpResult(r, w, resp, err)
|
||||
}
|
||||
}
|
||||
48
app/user/cmd/api/internal/logic/user/gettokenlogic.go
Normal file
48
app/user/cmd/api/internal/logic/user/gettokenlogic.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"qnc-server/common/ctxdata"
|
||||
jwtx "qnc-server/common/jwt"
|
||||
"qnc-server/common/xerr"
|
||||
"time"
|
||||
|
||||
"qnc-server/app/user/cmd/api/internal/svc"
|
||||
"qnc-server/app/user/cmd/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetTokenLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTokenLogic {
|
||||
return &GetTokenLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTokenLogic) GetToken() (resp *types.MobileCodeLoginResp, err error) {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %+v", err)
|
||||
}
|
||||
token, generaErr := jwtx.GenerateJwtToken(userID, l.svcCtx.Config.JwtAuth.AccessSecret, l.svcCtx.Config.JwtAuth.AccessExpire)
|
||||
if generaErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "更新token, 生成token失败 : %d", userID)
|
||||
}
|
||||
// 获取当前时间戳
|
||||
now := time.Now().Unix()
|
||||
return &types.MobileCodeLoginResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"qnc-server/app/user/model"
|
||||
jwtx "qnc-server/common/jwt"
|
||||
"qnc-server/common/xerr"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -82,10 +83,12 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d", user.Id)
|
||||
}
|
||||
|
||||
// 获取当前时间戳
|
||||
now := time.Now().Unix()
|
||||
return &types.MobileCodeLoginResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
func (l *MobileCodeLoginLogic) MobileCodeLoginInside(req *types.MobileCodeLoginReq) (pass bool) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"qnc-server/common/tool"
|
||||
"qnc-server/common/xerr"
|
||||
"qnc-server/pkg/lzkit/lzUtils"
|
||||
"time"
|
||||
|
||||
"qnc-server/app/user/cmd/api/internal/svc"
|
||||
"qnc-server/app/user/cmd/api/internal/types"
|
||||
@@ -46,9 +47,11 @@ func (l *MobileLoginLogic) MobileLogin(req *types.MobileLoginReq) (resp *types.M
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d", user.Id)
|
||||
}
|
||||
|
||||
// 获取当前时间戳
|
||||
now := time.Now().Unix()
|
||||
return &types.MobileCodeLoginResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"qnc-server/common/tool"
|
||||
"qnc-server/common/xerr"
|
||||
"qnc-server/pkg/lzkit/lzUtils"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -87,9 +88,11 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
|
||||
if generaErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机注册, 生成jwt token失败, userid: %d, err:%+v", userId, generaErr)
|
||||
}
|
||||
// 获取当前时间戳
|
||||
now := time.Now().Unix()
|
||||
return &types.RegisterResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"qnc-server/app/user/model"
|
||||
jwtx "qnc-server/common/jwt"
|
||||
"time"
|
||||
|
||||
"qnc-server/app/user/cmd/api/internal/svc"
|
||||
"qnc-server/app/user/cmd/api/internal/types"
|
||||
@@ -90,11 +91,11 @@ func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthRe
|
||||
return nil, errors.Wrap(genErr, "生成JWT token失败")
|
||||
}
|
||||
|
||||
// 返回登录信息
|
||||
now := time.Now().Unix()
|
||||
return &types.WXH5AuthResp{
|
||||
AccessToken: token,
|
||||
AccessExpire: l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -170,8 +170,9 @@ func (w *WestDexService) G05HZ01CallAPI(code string, reqData map[string]interfac
|
||||
if westDexResp.Code != "0000" {
|
||||
if westDexResp.Data == nil {
|
||||
return nil, errors.New(westDexResp.Message)
|
||||
} else {
|
||||
return westDexResp.Data, errors.New(string(westDexResp.Data))
|
||||
}
|
||||
return westDexResp.Data, errors.New(westDexResp.Message)
|
||||
}
|
||||
if westDexResp.Data == nil {
|
||||
return nil, errors.New(westDexResp.Message)
|
||||
|
||||
Reference in New Issue
Block a user