🔱: [server] sync upgrade with 21 commits [trident-sync]

Update README.md
This commit is contained in:
xiaojunnuo
2023-01-29 15:26:58 +08:00
parent 62e3945d30
commit fbde7cbd93
59 changed files with 2126 additions and 2 deletions
@@ -0,0 +1,48 @@
import { Config, Provide } from '@midwayjs/decorator';
import {
IWebMiddleware,
IMidwayKoaContext,
NextFunction
} from '@midwayjs/koa';
import * as _ from 'lodash';
import * as jwt from 'jsonwebtoken';
import { Constants } from '../basic/constants';
/**
* 权限校验
*/
@Provide()
export class AuthorityMiddleware implements IWebMiddleware {
@Config('biz.jwt.secret')
private secret: string;
@Config('biz.auth.ignoreUrls')
private ignoreUrls: string[];
resolve() {
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
const { url } = ctx;
const token = ctx.get('Authorization');
// 路由地址为 admin前缀的 需要权限校验
// console.log('ctx', ctx);
const queryIndex = url.indexOf('?');
let uri = url;
if (queryIndex >= 0) {
uri = url.substring(0, queryIndex);
}
const yes = this.ignoreUrls.includes(uri);
if (yes) {
await next();
return;
}
try {
ctx.user = jwt.verify(token, this.secret);
} catch (err) {
ctx.status = 401;
ctx.body = Constants.res.auth;
return;
}
await next();
};
}
}
@@ -0,0 +1,27 @@
import { Provide } from '@midwayjs/decorator';
import {
IWebMiddleware,
IMidwayKoaContext,
NextFunction,
} from '@midwayjs/koa';
import { logger } from '../utils/logger';
import { Result } from '../basic/result';
@Provide()
export class GlobalExceptionMiddleware implements IWebMiddleware {
resolve() {
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
const { url } = ctx;
const startTime = Date.now();
logger.info('请求开始:', url);
try {
await next();
logger.info('请求完成', url, Date.now() - startTime + 'ms');
} catch (err) {
logger.error('请求异常:', url, Date.now() - startTime + 'ms', err);
ctx.status = 200;
ctx.body = Result.error(err.code != null ? err.code : 1, err.message);
}
};
}
}
@@ -0,0 +1,50 @@
import { Config, Provide } from '@midwayjs/decorator';
import {
IMidwayKoaContext,
NextFunction,
IWebMiddleware,
} from '@midwayjs/koa';
import { PreviewException } from '../basic/exception/preview-exception';
/**
* 预览模式
*/
@Provide()
export class PreviewMiddleware implements IWebMiddleware {
@Config('preview.enabled')
private preview: boolean;
resolve() {
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
if (!this.preview) {
await next();
return;
}
let { url, request } = ctx;
const body: any = request.body;
let id = body.id || request.query.id;
const roleId = body.roleId;
if (id == null && roleId != null) {
id = roleId;
}
if (id != null && typeof id === 'string') {
id = parseInt(id);
}
if (url.indexOf('?') !== -1) {
url = url.substring(0, url.indexOf('?'));
}
const isModify =
url.endsWith('update') ||
url.endsWith('delete') ||
url.endsWith('authz');
const isPreviewId = id < 1000;
if (this.preview && isModify && isPreviewId) {
throw new PreviewException(
'对不起,预览环境不允许修改此数据,如需体验请添加新数据'
);
}
await next();
return;
};
}
}
@@ -0,0 +1,28 @@
import { Provide } from '@midwayjs/decorator';
import {
IWebMiddleware,
IMidwayKoaContext,
NextFunction,
} from '@midwayjs/koa';
import { logger } from '../utils/logger';
@Provide()
export class ReportMiddleware implements IWebMiddleware {
resolve() {
return async (ctx: IMidwayKoaContext, next: NextFunction) => {
const { url } = ctx;
logger.info('请求开始:', url);
const startTime = Date.now();
await next();
if (ctx.status !== 200) {
logger.error(
'请求失败:',
url,
ctx.status,
Date.now() - startTime + 'ms'
);
}
logger.info('请求完成:', url, ctx.status, Date.now() - startTime + 'ms');
};
}
}