diff --git a/src/modules/base/controller/admin/open.ts b/src/modules/base/controller/admin/open.ts index a1b7f7d..b42785b 100644 --- a/src/modules/base/controller/admin/open.ts +++ b/src/modules/base/controller/admin/open.ts @@ -6,6 +6,7 @@ import { CoolUrlTag, CoolTag, TagTypes, + RESCODE, } from '@cool-midway/core'; import { LoginDTO } from '../../dto/login'; import { BaseSysLoginService } from '../../service/sys/login'; @@ -84,6 +85,15 @@ export class BaseOpenController extends BaseController { @CoolTag(TagTypes.IGNORE_TOKEN) @Get('/refreshToken', { summary: '刷新token' }) async refreshToken(@Query('refreshToken') refreshToken: string) { - return this.ok(await this.baseSysLoginService.refreshToken(refreshToken)); + try { + const token = await this.baseSysLoginService.refreshToken(refreshToken); + return this.ok(token); + } catch (e) { + this.ctx.status = 401; + this.ctx.body = { + code: RESCODE.COMMFAIL, + message: '登录失效~', + }; + } } } diff --git a/src/modules/base/middleware/authority.ts b/src/modules/base/middleware/authority.ts index 1c861a4..3eaee42 100644 --- a/src/modules/base/middleware/authority.ts +++ b/src/modules/base/middleware/authority.ts @@ -39,7 +39,7 @@ export class BaseAuthorityMiddleware return async (ctx: Context, next: NextFunction) => { let statusCode = 200; let { url } = ctx; - url = url.replace(this.prefix, ''); + url = url.replace(this.prefix, '').split('?')[0]; const token = ctx.get('Authorization'); const adminUrl = '/admin/'; // 路由地址为 admin前缀的 需要权限校验 diff --git a/src/modules/base/service/sys/login.ts b/src/modules/base/service/sys/login.ts index b102a98..15309af 100644 --- a/src/modules/base/service/sys/login.ts +++ b/src/modules/base/service/sys/login.ts @@ -1,5 +1,5 @@ import { Inject, Provide, Config } from '@midwayjs/decorator'; -import { BaseService, CoolCommException, RESCODE } from '@cool-midway/core'; +import { BaseService, CoolCommException } from '@cool-midway/core'; import { LoginDTO } from '../../dto/login'; import * as svgCaptcha from 'svg-captcha'; import { v1 as uuid } from 'uuid'; @@ -217,43 +217,34 @@ export class BaseSysLoginService extends BaseService { * @param token */ async refreshToken(token: string) { - try { - const decoded = jwt.verify(token, this.coolConfig.jwt.secret); - if (decoded && decoded['isRefresh']) { - delete decoded['exp']; - delete decoded['iat']; + const decoded = jwt.verify(token, this.coolConfig.jwt.secret); + if (decoded && decoded['isRefresh']) { + delete decoded['exp']; + delete decoded['iat']; - const { expire, refreshExpire } = this.coolConfig.jwt.token; - decoded['isRefresh'] = false; - const result = { - expire, - token: jwt.sign(decoded, this.coolConfig.jwt.secret, { - expiresIn: expire, - }), - refreshExpire, - refreshToken: '', - }; - decoded['isRefresh'] = true; - result.refreshToken = jwt.sign(decoded, this.coolConfig.jwt.secret, { - expiresIn: refreshExpire, - }); - await this.cacheManager.set( - `admin:passwordVersion:${decoded['userId']}`, - decoded['passwordVersion'] - ); - await this.cacheManager.set( - `admin:token:${decoded['userId']}`, - result.token - ); - return result; - } - } catch (err) { - this.ctx.status = 401; - this.ctx.body = { - code: RESCODE.COMMFAIL, - message: '登录失效~', + const { expire, refreshExpire } = this.coolConfig.jwt.token; + decoded['isRefresh'] = false; + const result = { + expire, + token: jwt.sign(decoded, this.coolConfig.jwt.secret, { + expiresIn: expire, + }), + refreshExpire, + refreshToken: '', }; - return; + decoded['isRefresh'] = true; + result.refreshToken = jwt.sign(decoded, this.coolConfig.jwt.secret, { + expiresIn: refreshExpire, + }); + await this.cacheManager.set( + `admin:passwordVersion:${decoded['userId']}`, + decoded['passwordVersion'] + ); + await this.cacheManager.set( + `admin:token:${decoded['userId']}`, + result.token + ); + return result; } } }