mirror of
https://github.com/certd/certd.git
synced 2026-04-24 12:27:25 +08:00
feat: 升级midway,支持esm
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
import { Config, Inject, Provide } from '@midwayjs/decorator';
|
||||
import { Config, Inject, Provide } from '@midwayjs/core';
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from '@midwayjs/koa';
|
||||
import * as jwt from 'jsonwebtoken';
|
||||
import { Constants } from '../basic/constants';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { Constants } from '../basic/constants.js';
|
||||
import { MidwayWebRouterService } from '@midwayjs/core';
|
||||
import { RoleService } from '../modules/authority/service/role-service';
|
||||
import { logger } from '../utils/logger';
|
||||
import { RoleService } from '../modules/authority/service/role-service.js';
|
||||
import { logger } from '../utils/logger.js';
|
||||
|
||||
/**
|
||||
* 权限校验
|
||||
*/
|
||||
@Provide()
|
||||
export class AuthorityMiddleware implements IWebMiddleware {
|
||||
@Config('keys')
|
||||
@Config('auth.jwt.secret')
|
||||
private secret: string;
|
||||
@Inject()
|
||||
webRouterService: MidwayWebRouterService;
|
||||
@@ -21,10 +21,7 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
resolve() {
|
||||
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
|
||||
// 查询当前路由是否在路由表中注册
|
||||
const routeInfo = await this.webRouterService.getMatchedRouterInfo(
|
||||
ctx.path,
|
||||
ctx.method
|
||||
);
|
||||
const routeInfo = await this.webRouterService.getMatchedRouterInfo(ctx.path, ctx.method);
|
||||
if (routeInfo == null) {
|
||||
// 404
|
||||
await next();
|
||||
@@ -33,9 +30,7 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
const permission = routeInfo.summary;
|
||||
if (permission == null || permission === '') {
|
||||
ctx.status = 500;
|
||||
ctx.body = Constants.res.serverError(
|
||||
'该路由未配置权限控制:' + ctx.path
|
||||
);
|
||||
ctx.body = Constants.res.serverError('该路由未配置权限控制:' + ctx.path);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -57,6 +52,7 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
try {
|
||||
ctx.user = jwt.verify(token, this.secret);
|
||||
} catch (err) {
|
||||
logger.error('token verify error: ', err);
|
||||
ctx.status = 401;
|
||||
ctx.body = Constants.res.auth;
|
||||
return;
|
||||
@@ -65,8 +61,7 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
if (permission !== Constants.per.authOnly) {
|
||||
//如果不是仅校验登录,还需要校验是否拥有权限
|
||||
const roleIds: number[] = ctx.user.roles;
|
||||
const permissions =
|
||||
await this.roleService.getCachedPermissionSetByRoleIds(roleIds);
|
||||
const permissions = await this.roleService.getCachedPermissionSetByRoleIds(roleIds);
|
||||
|
||||
if (!permissions.has(permission)) {
|
||||
logger.info('not permission: ', ctx.req.url);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Provide } from '@midwayjs/decorator';
|
||||
import { Provide } from '@midwayjs/core';
|
||||
import { IWebMiddleware, IMidwayKoaContext, NextFunction } from '@midwayjs/koa';
|
||||
import { logger } from '../utils/logger';
|
||||
import { Result } from '../basic/result';
|
||||
import { logger } from '../utils/logger.js';
|
||||
import { Result } from '../basic/result.js';
|
||||
|
||||
@Provide()
|
||||
export class GlobalExceptionMiddleware implements IWebMiddleware {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Config, Provide } from '@midwayjs/decorator';
|
||||
import { Config, Provide } from '@midwayjs/core';
|
||||
import { IMidwayKoaContext, NextFunction, IWebMiddleware } from '@midwayjs/koa';
|
||||
import { PreviewException } from '../basic/exception/preview-exception';
|
||||
import { PreviewException } from '../basic/exception/preview-exception.js';
|
||||
|
||||
/**
|
||||
* 预览模式
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Middleware, IMiddleware } from '@midwayjs/core';
|
||||
import { NextFunction, Context } from '@midwayjs/koa';
|
||||
|
||||
@Middleware()
|
||||
export class ReportMiddleware implements IMiddleware<Context, NextFunction> {
|
||||
resolve() {
|
||||
return async (ctx: Context, next: NextFunction) => {
|
||||
// 控制器前执行的逻辑
|
||||
const startTime = Date.now();
|
||||
// 执行下一个 Web 中间件,最后执行到控制器
|
||||
// 这里可以拿到下一个中间件或者控制器的返回值
|
||||
const result = await next();
|
||||
// 控制器之后执行的逻辑
|
||||
ctx.logger.info(
|
||||
`Report in "src/middleware/report.middleware.ts", rt = ${
|
||||
Date.now() - startTime
|
||||
}ms`
|
||||
);
|
||||
// 返回给上一个中间件的结果
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
static getName(): string {
|
||||
return 'report';
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Provide } from '@midwayjs/decorator';
|
||||
import { Provide } from '@midwayjs/core';
|
||||
import { IWebMiddleware, IMidwayKoaContext, NextFunction } from '@midwayjs/koa';
|
||||
import { logger } from '../utils/logger';
|
||||
import { logger } from '../utils/logger.js';
|
||||
|
||||
@Provide()
|
||||
export class ReportMiddleware implements IWebMiddleware {
|
||||
|
||||
@@ -6,11 +6,11 @@ import {
|
||||
Provide,
|
||||
Scope,
|
||||
ScopeEnum,
|
||||
} from '@midwayjs/decorator';
|
||||
} from '@midwayjs/core';
|
||||
import { IMidwayKoaContext, IWebMiddleware, NextFunction } from '@midwayjs/koa';
|
||||
import { CommonException } from '../../basic/exception/common-exception';
|
||||
import { UserService } from '../../modules/authority/service/user-service';
|
||||
import { logger } from '../../utils/logger';
|
||||
import { CommonException } from '../../basic/exception/common-exception.js';
|
||||
import { UserService } from '../../modules/authority/service/user-service.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
|
||||
/**
|
||||
* 重置密码模式
|
||||
|
||||
Reference in New Issue
Block a user