@cicctencent/midwayjs-base 1.0.38 → 1.0.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +582 -347
- package/dist/config/env.js +6 -3
- package/dist/constants/error-code.d.ts +27 -0
- package/dist/constants/error-code.js +53 -0
- package/dist/filter/default.filter.d.ts +4 -3
- package/dist/filter/default.filter.js +12 -2
- package/dist/guard/api.js +12 -6
- package/dist/guard/auth.d.ts +7 -3
- package/dist/guard/auth.js +46 -53
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/lib/decorator.d.ts +1 -1
- package/dist/lib/decorator.js +2 -3
- package/dist/middleware/apiResultFormatter.js +6 -6
- package/dist/types/base.model.service.d.ts +1 -1
- package/dist/types/base.model.service.js +11 -5
- package/dist/types/base.service.js +1 -1
- package/package.json +30 -30
- package/typings/midway.d.ts +54 -54
package/dist/config/env.js
CHANGED
|
@@ -39,13 +39,16 @@ const path = __importStar(require("path"));
|
|
|
39
39
|
const fs = __importStar(require("fs"));
|
|
40
40
|
function loadEnv(envPath) {
|
|
41
41
|
envPath = envPath || path.join(process.cwd(), '.env');
|
|
42
|
-
console.log('env path', envPath);
|
|
43
42
|
if (fs.existsSync(envPath)) {
|
|
44
|
-
const
|
|
43
|
+
const result = dotenv.config({
|
|
45
44
|
path: envPath,
|
|
46
45
|
encoding: 'utf-8',
|
|
47
46
|
});
|
|
48
|
-
|
|
47
|
+
if (result.error) {
|
|
48
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
49
|
+
console.warn('[midwayjs-base] Failed to load .env file:', result.error.message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
49
52
|
}
|
|
50
53
|
return process.env;
|
|
51
54
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare enum ErrorCode {
|
|
2
|
+
SUCCESS = 0,
|
|
3
|
+
SYSTEM_ERROR = 1001,
|
|
4
|
+
PARAM_ERROR = 1002,
|
|
5
|
+
TIMEOUT = 1003,
|
|
6
|
+
TOKEN_INVALID = 50001,
|
|
7
|
+
TOKEN_EXPIRED = 50002,
|
|
8
|
+
AUTH_REQUIRED = 50003,
|
|
9
|
+
IP_NOT_ALLOWED = 50004,
|
|
10
|
+
TIMESTAMP_EXPIRED = 50005,
|
|
11
|
+
TIMESTAMP_INVALID = 50006,
|
|
12
|
+
TOKEN_EMPTY = 50007
|
|
13
|
+
}
|
|
14
|
+
export declare const ErrorMessages: Record<ErrorCode, string>;
|
|
15
|
+
export declare function createError(code: ErrorCode, msg?: string): {
|
|
16
|
+
ret: ErrorCode;
|
|
17
|
+
msg: string;
|
|
18
|
+
};
|
|
19
|
+
export declare class BizError extends Error {
|
|
20
|
+
ret: ErrorCode;
|
|
21
|
+
msg: string;
|
|
22
|
+
constructor(code: ErrorCode, msg?: string);
|
|
23
|
+
toJSON(): {
|
|
24
|
+
ret: ErrorCode;
|
|
25
|
+
msg: string;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BizError = exports.ErrorMessages = exports.ErrorCode = void 0;
|
|
4
|
+
exports.createError = createError;
|
|
5
|
+
var ErrorCode;
|
|
6
|
+
(function (ErrorCode) {
|
|
7
|
+
ErrorCode[ErrorCode["SUCCESS"] = 0] = "SUCCESS";
|
|
8
|
+
ErrorCode[ErrorCode["SYSTEM_ERROR"] = 1001] = "SYSTEM_ERROR";
|
|
9
|
+
ErrorCode[ErrorCode["PARAM_ERROR"] = 1002] = "PARAM_ERROR";
|
|
10
|
+
ErrorCode[ErrorCode["TIMEOUT"] = 1003] = "TIMEOUT";
|
|
11
|
+
ErrorCode[ErrorCode["TOKEN_INVALID"] = 50001] = "TOKEN_INVALID";
|
|
12
|
+
ErrorCode[ErrorCode["TOKEN_EXPIRED"] = 50002] = "TOKEN_EXPIRED";
|
|
13
|
+
ErrorCode[ErrorCode["AUTH_REQUIRED"] = 50003] = "AUTH_REQUIRED";
|
|
14
|
+
ErrorCode[ErrorCode["IP_NOT_ALLOWED"] = 50004] = "IP_NOT_ALLOWED";
|
|
15
|
+
ErrorCode[ErrorCode["TIMESTAMP_EXPIRED"] = 50005] = "TIMESTAMP_EXPIRED";
|
|
16
|
+
ErrorCode[ErrorCode["TIMESTAMP_INVALID"] = 50006] = "TIMESTAMP_INVALID";
|
|
17
|
+
ErrorCode[ErrorCode["TOKEN_EMPTY"] = 50007] = "TOKEN_EMPTY";
|
|
18
|
+
})(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
|
|
19
|
+
exports.ErrorMessages = {
|
|
20
|
+
[ErrorCode.SUCCESS]: 'success',
|
|
21
|
+
[ErrorCode.SYSTEM_ERROR]: '系统错误',
|
|
22
|
+
[ErrorCode.PARAM_ERROR]: '参数错误',
|
|
23
|
+
[ErrorCode.TIMEOUT]: '请求超时',
|
|
24
|
+
[ErrorCode.TOKEN_INVALID]: 'token输入错误',
|
|
25
|
+
[ErrorCode.TOKEN_EXPIRED]: 'token不存在或已过期',
|
|
26
|
+
[ErrorCode.AUTH_REQUIRED]: '需要登陆态',
|
|
27
|
+
[ErrorCode.IP_NOT_ALLOWED]: 'IP不在白名单内',
|
|
28
|
+
[ErrorCode.TIMESTAMP_EXPIRED]: 'timestamp已超过有效时间',
|
|
29
|
+
[ErrorCode.TIMESTAMP_INVALID]: 'timestamp时间异常',
|
|
30
|
+
[ErrorCode.TOKEN_EMPTY]: 'token为空,非法请求',
|
|
31
|
+
};
|
|
32
|
+
function createError(code, msg) {
|
|
33
|
+
return {
|
|
34
|
+
ret: code,
|
|
35
|
+
msg: msg || exports.ErrorMessages[code] || '未知错误',
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
class BizError extends Error {
|
|
39
|
+
constructor(code, msg) {
|
|
40
|
+
super(msg || exports.ErrorMessages[code] || '未知错误');
|
|
41
|
+
this.ret = code;
|
|
42
|
+
this.msg = this.message;
|
|
43
|
+
this.name = 'BizError';
|
|
44
|
+
}
|
|
45
|
+
toJSON() {
|
|
46
|
+
return {
|
|
47
|
+
ret: this.ret,
|
|
48
|
+
msg: this.msg,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.BizError = BizError;
|
|
53
|
+
//# sourceMappingURL=error-code.js.map
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Context } from '@midwayjs/koa';
|
|
2
|
+
import { ErrorCode, BizError } from '../constants/error-code';
|
|
2
3
|
export declare class DefaultErrorFilter {
|
|
3
|
-
catch(err: Error, ctx: Context): Promise<{
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
catch(err: Error | BizError, ctx: Context): Promise<{
|
|
5
|
+
ret: ErrorCode;
|
|
6
|
+
msg: string;
|
|
6
7
|
}>;
|
|
7
8
|
}
|
|
@@ -8,11 +8,21 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.DefaultErrorFilter = void 0;
|
|
10
10
|
const core_1 = require("@midwayjs/core");
|
|
11
|
+
const error_code_1 = require("../constants/error-code");
|
|
11
12
|
let DefaultErrorFilter = class DefaultErrorFilter {
|
|
12
13
|
async catch(err, ctx) {
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
if (err instanceof error_code_1.BizError) {
|
|
15
|
+
ctx.logger?.error('Business error:', err.toJSON());
|
|
16
|
+
return err.toJSON();
|
|
17
|
+
}
|
|
18
|
+
ctx.logger?.error('Unhandled error:', {
|
|
19
|
+
name: err.name,
|
|
15
20
|
message: err.message,
|
|
21
|
+
stack: err.stack,
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
ret: error_code_1.ErrorCode.SYSTEM_ERROR,
|
|
25
|
+
msg: err.message || '系统错误',
|
|
16
26
|
};
|
|
17
27
|
}
|
|
18
28
|
};
|
package/dist/guard/api.js
CHANGED
|
@@ -16,6 +16,7 @@ exports.ApiGuard = void 0;
|
|
|
16
16
|
const core_1 = require("@midwayjs/core");
|
|
17
17
|
const decorator_1 = require("../lib/decorator");
|
|
18
18
|
const api_1 = __importDefault(require("@fefeding/common/dist/utils/api"));
|
|
19
|
+
const error_code_1 = require("../constants/error-code");
|
|
19
20
|
let ApiGuard = class ApiGuard {
|
|
20
21
|
async canActivate(context, supplierClz, methodName) {
|
|
21
22
|
const needIP = (0, decorator_1.getCheckIP)(supplierClz, methodName);
|
|
@@ -40,16 +41,21 @@ let ApiGuard = class ApiGuard {
|
|
|
40
41
|
context.cookies.get('timestamp'));
|
|
41
42
|
if (!reqToken || !timestamp) {
|
|
42
43
|
context.logger.error(`token为空,非法请求, token: ${reqToken}, timestamp: ${timestamp}`);
|
|
43
|
-
throw
|
|
44
|
+
throw new error_code_1.BizError(error_code_1.ErrorCode.TOKEN_EMPTY);
|
|
44
45
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
const now = Date.now();
|
|
47
|
+
const validWindow = 5 * 60 * 1000;
|
|
48
|
+
if (timestamp < now - validWindow) {
|
|
49
|
+
throw new error_code_1.BizError(error_code_1.ErrorCode.TIMESTAMP_EXPIRED);
|
|
50
|
+
}
|
|
51
|
+
if (timestamp > now + validWindow) {
|
|
52
|
+
context.logger.error(`timestamp时间异常,客户端时间超前: ${timestamp}, 当前时间: ${now}`);
|
|
53
|
+
throw new error_code_1.BizError(error_code_1.ErrorCode.TIMESTAMP_INVALID);
|
|
48
54
|
}
|
|
49
55
|
const thisToken = api_1.default.createApiToken(this.apiOption.key, timestamp);
|
|
50
56
|
if (reqToken !== thisToken.sign) {
|
|
51
57
|
context.logger.error(`输入token:${reqToken} 本地: ${thisToken.sign} 不一致`);
|
|
52
|
-
throw
|
|
58
|
+
throw new error_code_1.BizError(error_code_1.ErrorCode.TOKEN_INVALID);
|
|
53
59
|
}
|
|
54
60
|
}
|
|
55
61
|
checkIPInWhiteList(context) {
|
|
@@ -57,7 +63,7 @@ let ApiGuard = class ApiGuard {
|
|
|
57
63
|
if (this.ipOption?.whiteList && !this.ipOption.whiteList.includes('*')) {
|
|
58
64
|
if (!this.ipOption.whiteList.includes(requestIp)) {
|
|
59
65
|
context.logger.error(`IP:${requestIp} 不在白名单内`);
|
|
60
|
-
throw
|
|
66
|
+
throw new error_code_1.BizError(error_code_1.ErrorCode.IP_NOT_ALLOWED, `IP:${requestIp} 不在白名单内`);
|
|
61
67
|
}
|
|
62
68
|
}
|
|
63
69
|
}
|
package/dist/guard/auth.d.ts
CHANGED
|
@@ -11,8 +11,12 @@ export declare class AuthGuard implements IGuard<Context> {
|
|
|
11
11
|
apiOption: any;
|
|
12
12
|
koa: any;
|
|
13
13
|
init(): Promise<void>;
|
|
14
|
-
|
|
14
|
+
private isPathIgnored;
|
|
15
|
+
checkNeedAuth(context: Context, supplierClz: any, methodName: string): boolean;
|
|
15
16
|
canActivate(context: Context, supplierClz: any, methodName: string): Promise<boolean>;
|
|
16
|
-
checkAuth(context: Context, token: string, needAuth?: boolean): Promise<
|
|
17
|
-
authError(context: Context, error
|
|
17
|
+
checkAuth(context: Context, token: string, needAuth?: boolean): Promise<void>;
|
|
18
|
+
authError(context: Context, error: {
|
|
19
|
+
ret: number;
|
|
20
|
+
msg: string;
|
|
21
|
+
}): void;
|
|
18
22
|
}
|
package/dist/guard/auth.js
CHANGED
|
@@ -14,59 +14,53 @@ const core_1 = require("@midwayjs/core");
|
|
|
14
14
|
const decorator_1 = require("../lib/decorator");
|
|
15
15
|
const utils_1 = require("../lib/utils");
|
|
16
16
|
const session_service_interface_1 = require("../types/session.service.interface");
|
|
17
|
+
const error_code_1 = require("../constants/error-code");
|
|
17
18
|
let AuthGuard = class AuthGuard {
|
|
18
19
|
async init() {
|
|
19
20
|
const appContext = this.app.getApplicationContext();
|
|
20
|
-
console.log('init auth guard');
|
|
21
21
|
this.sessionService = await appContext.getAsync(session_service_interface_1.DEFAULT_SESSION_SERVICE_KEY);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
console.log('use custom session service for auth guard', this.sessionService.sessionOption.serviceKey);
|
|
28
|
-
this.sessionService = sessionServiceObj;
|
|
22
|
+
const customKey = this.sessionService?.sessionOption?.serviceKey;
|
|
23
|
+
if (customKey && customKey !== session_service_interface_1.DEFAULT_SESSION_SERVICE_KEY) {
|
|
24
|
+
const customService = await appContext.getAsync(customKey);
|
|
25
|
+
if (customService) {
|
|
26
|
+
this.sessionService = customService;
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
29
|
}
|
|
30
|
+
isPathIgnored(path) {
|
|
31
|
+
const ignores = this.authOption?.ignores;
|
|
32
|
+
if (!ignores)
|
|
33
|
+
return false;
|
|
34
|
+
if (ignores === '*')
|
|
35
|
+
return true;
|
|
36
|
+
if (typeof ignores === 'string') {
|
|
37
|
+
return ignores === path;
|
|
38
|
+
}
|
|
39
|
+
if (Array.isArray(ignores)) {
|
|
40
|
+
return ignores.some(rule => rule === '*' ||
|
|
41
|
+
(rule instanceof RegExp ? rule.test(path) : rule === path));
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
32
45
|
checkNeedAuth(context, supplierClz, methodName) {
|
|
33
|
-
if (this.
|
|
34
|
-
|
|
35
|
-
this.authOption.ignores === context.path) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
if (Array.isArray(this.authOption.ignores)) {
|
|
39
|
-
if (this.authOption.ignores.includes('*'))
|
|
40
|
-
return false;
|
|
41
|
-
for (const t of this.authOption.ignores) {
|
|
42
|
-
if (t instanceof RegExp) {
|
|
43
|
-
if (t.test(context.path))
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
else if (typeof t === 'string') {
|
|
47
|
-
if (t === context.path)
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
46
|
+
if (this.isPathIgnored(context.path)) {
|
|
47
|
+
return false;
|
|
52
48
|
}
|
|
53
|
-
|
|
54
|
-
return needAuth;
|
|
49
|
+
return (0, decorator_1.getCheckLogin)(supplierClz, methodName) === true;
|
|
55
50
|
}
|
|
56
51
|
async canActivate(context, supplierClz, methodName) {
|
|
57
52
|
const needAuth = this.checkNeedAuth(context, supplierClz, methodName);
|
|
58
53
|
const token = (0, utils_1.getAuthToken)(context);
|
|
59
54
|
if (token)
|
|
60
55
|
context.auth_token = token;
|
|
61
|
-
|
|
62
|
-
return
|
|
56
|
+
await this.checkAuth(context, token, needAuth);
|
|
57
|
+
return true;
|
|
63
58
|
}
|
|
64
59
|
async checkAuth(context, token, needAuth) {
|
|
65
60
|
this.sessionService.ctx = context;
|
|
66
|
-
const authRet = 50001;
|
|
67
61
|
let errRes = null;
|
|
62
|
+
let session = null;
|
|
68
63
|
try {
|
|
69
|
-
let session = null;
|
|
70
64
|
const authCode = context.URL.searchParams.get('auth_code');
|
|
71
65
|
if (authCode) {
|
|
72
66
|
session = await this.sessionService.loginByCode(authCode);
|
|
@@ -75,11 +69,10 @@ let AuthGuard = class AuthGuard {
|
|
|
75
69
|
}
|
|
76
70
|
context.URL.searchParams.delete('auth_code');
|
|
77
71
|
context.URL.hostname = context.detect?.HOSTNAME || context.hostname;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return true;
|
|
72
|
+
context.redirect(context.URL.toString());
|
|
73
|
+
return;
|
|
81
74
|
}
|
|
82
|
-
if (token
|
|
75
|
+
if (token) {
|
|
83
76
|
if (token.startsWith('Bearer ')) {
|
|
84
77
|
const jwtData = (0, utils_1.decodeJWT)(token.replace('Bearer ', ''), this.sessionService?.sessionOption?.secretKey || 'fefeding');
|
|
85
78
|
if (jwtData) {
|
|
@@ -89,58 +82,58 @@ let AuthGuard = class AuthGuard {
|
|
|
89
82
|
else {
|
|
90
83
|
session = await this.sessionService.getLoginSession(token);
|
|
91
84
|
}
|
|
92
|
-
if (!session
|
|
85
|
+
if (!session?.id) {
|
|
93
86
|
(0, utils_1.setAuthToken)(context, '');
|
|
94
87
|
errRes = {
|
|
95
|
-
ret:
|
|
88
|
+
ret: error_code_1.ErrorCode.TOKEN_EXPIRED,
|
|
96
89
|
msg: 'token不存在或已过期',
|
|
97
90
|
};
|
|
98
91
|
}
|
|
99
92
|
}
|
|
100
93
|
if (!session) {
|
|
101
94
|
errRes = {
|
|
102
|
-
ret:
|
|
95
|
+
ret: error_code_1.ErrorCode.AUTH_REQUIRED,
|
|
103
96
|
msg: '需要登陆态',
|
|
104
97
|
};
|
|
105
98
|
}
|
|
106
99
|
context.currentSession = session;
|
|
107
|
-
if (session && context.logger
|
|
108
|
-
context.logger.setOptions
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
100
|
+
if (session && context.logger) {
|
|
101
|
+
if (typeof context.logger.setOptions === 'function') {
|
|
102
|
+
context.logger.setOptions({
|
|
103
|
+
loginId: session.loginId || '',
|
|
104
|
+
userId: (session.userId || '').toString(),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
112
107
|
}
|
|
113
108
|
}
|
|
114
109
|
catch (e) {
|
|
115
110
|
errRes = {
|
|
116
|
-
ret: e?.ret ||
|
|
111
|
+
ret: e?.ret || error_code_1.ErrorCode.SYSTEM_ERROR,
|
|
117
112
|
msg: e?.msg || e?.message || 'auth error',
|
|
118
113
|
};
|
|
119
|
-
|
|
114
|
+
context.logger?.error('Auth check error:', e);
|
|
120
115
|
}
|
|
121
116
|
if (errRes && needAuth) {
|
|
122
117
|
this.authError(context, errRes);
|
|
123
118
|
}
|
|
124
|
-
return true;
|
|
125
119
|
}
|
|
126
120
|
authError(context, error) {
|
|
127
121
|
const isApi = this.apiOption?.reg?.test(context.path);
|
|
128
122
|
if (isApi) {
|
|
129
123
|
context.status = 200;
|
|
130
|
-
|
|
131
|
-
throw error;
|
|
124
|
+
throw new error_code_1.BizError(error.ret, error.msg);
|
|
132
125
|
}
|
|
133
126
|
else {
|
|
134
127
|
const loginUrl = `${this.ssoOption?.baseUrl || this.koa?.globalPrefix || ''}/login`;
|
|
135
128
|
if (!/\/login\/?(\?|$|#)/.test(context.URL.pathname)) {
|
|
136
129
|
const URL = context.URL;
|
|
137
|
-
if (context.detect?.HOSTNAME &&
|
|
138
|
-
URL.hostname !== context.detect?.HOSTNAME)
|
|
130
|
+
if (context.detect?.HOSTNAME && URL.hostname !== context.detect.HOSTNAME) {
|
|
139
131
|
URL.hostname = context.detect.HOSTNAME;
|
|
132
|
+
}
|
|
140
133
|
URL.searchParams.delete('jv_auth_code');
|
|
141
134
|
URL.searchParams.delete('token');
|
|
142
135
|
URL.searchParams.delete('auth_code');
|
|
143
|
-
const redirectUrl =
|
|
136
|
+
const redirectUrl = URL.toString();
|
|
144
137
|
context.redirect(`${loginUrl}?url=${encodeURIComponent(redirectUrl)}&appid=${this.ssoOption?.appId || 0}`);
|
|
145
138
|
}
|
|
146
139
|
else {
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * as jwt from './lib/jwt';
|
|
|
5
5
|
export * as decorators from './lib/decorator';
|
|
6
6
|
export * from '@fefeding/common/dist/models/base/enumType';
|
|
7
7
|
export { BaseORM as BaseEntity } from '@fefeding/common/dist/models/base/baseORM';
|
|
8
|
+
export * from './constants/error-code';
|
|
8
9
|
export * from './middleware/apiResultFormatter';
|
|
9
10
|
export * from './middleware/requestInit';
|
|
10
11
|
export * from './middleware/detect.middleware';
|
|
@@ -16,3 +17,4 @@ export * from './types/base.controller';
|
|
|
16
17
|
export * from './types/session.service.interface';
|
|
17
18
|
export * from './service/session.service';
|
|
18
19
|
export * from './service/tencent.service';
|
|
20
|
+
export * from './filter/default.filter';
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,7 @@ exports.decorators = __importStar(require("./lib/decorator"));
|
|
|
46
46
|
__exportStar(require("@fefeding/common/dist/models/base/enumType"), exports);
|
|
47
47
|
var baseORM_1 = require("@fefeding/common/dist/models/base/baseORM");
|
|
48
48
|
Object.defineProperty(exports, "BaseEntity", { enumerable: true, get: function () { return baseORM_1.BaseORM; } });
|
|
49
|
+
__exportStar(require("./constants/error-code"), exports);
|
|
49
50
|
__exportStar(require("./middleware/apiResultFormatter"), exports);
|
|
50
51
|
__exportStar(require("./middleware/requestInit"), exports);
|
|
51
52
|
__exportStar(require("./middleware/detect.middleware"), exports);
|
|
@@ -57,4 +58,5 @@ __exportStar(require("./types/base.controller"), exports);
|
|
|
57
58
|
__exportStar(require("./types/session.service.interface"), exports);
|
|
58
59
|
__exportStar(require("./service/session.service"), exports);
|
|
59
60
|
__exportStar(require("./service/tencent.service"), exports);
|
|
61
|
+
__exportStar(require("./filter/default.filter"), exports);
|
|
60
62
|
//# sourceMappingURL=index.js.map
|
package/dist/lib/decorator.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare function checkApiToken(need?: boolean): MethodDecorator;
|
|
2
|
-
export declare function getApiToken(target: any, propertyKey: string | symbol):
|
|
2
|
+
export declare function getApiToken(target: any, propertyKey: string | symbol): boolean;
|
|
3
3
|
export declare function checkLogin(need?: boolean): MethodDecorator;
|
|
4
4
|
export declare function getCheckLogin(target: any, propertyKey: string | symbol): any;
|
|
5
5
|
export declare function checkIP(need?: boolean): MethodDecorator;
|
package/dist/lib/decorator.js
CHANGED
|
@@ -20,10 +20,9 @@ function checkApiToken(need = true) {
|
|
|
20
20
|
function getApiToken(target, propertyKey) {
|
|
21
21
|
try {
|
|
22
22
|
const value = (0, core_1.getPropertyMetadata)(decorator_1.default.apiTokenMetadataKey, target, propertyKey);
|
|
23
|
-
return value;
|
|
23
|
+
return value === true;
|
|
24
24
|
}
|
|
25
|
-
catch
|
|
26
|
-
console.log(e);
|
|
25
|
+
catch {
|
|
27
26
|
return false;
|
|
28
27
|
}
|
|
29
28
|
}
|
|
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.ApiResultFormatterMiddleware = void 0;
|
|
13
13
|
const core_1 = require("@midwayjs/core");
|
|
14
14
|
const stream_1 = require("stream");
|
|
15
|
+
const error_code_1 = require("../constants/error-code");
|
|
15
16
|
let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
16
17
|
ignore(ctx) {
|
|
17
18
|
const koaCtx = ctx;
|
|
@@ -42,17 +43,16 @@ let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
catch (err) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
koaCtx.body = err;
|
|
46
|
+
if (err instanceof error_code_1.BizError || typeof err?.ret === 'number') {
|
|
47
|
+
koaCtx.body = err instanceof error_code_1.BizError ? err.toJSON() : err;
|
|
48
48
|
}
|
|
49
49
|
else {
|
|
50
50
|
koaCtx.body = {
|
|
51
|
-
ret:
|
|
52
|
-
msg: err?.message || '
|
|
51
|
+
ret: error_code_1.ErrorCode.SYSTEM_ERROR,
|
|
52
|
+
msg: err?.message || '系统错误',
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
|
-
koaCtx.logger.error(koaCtx.request.rawBody, koaCtx.body);
|
|
55
|
+
koaCtx.logger.error('API error:', koaCtx.request.rawBody, koaCtx.body);
|
|
56
56
|
}
|
|
57
57
|
finally {
|
|
58
58
|
if (koaCtx.status !== 302)
|
|
@@ -13,7 +13,7 @@ export declare abstract class BaseModelService<K extends BaseEntity> extends Bas
|
|
|
13
13
|
save(entity: K, qryRunner?: QueryRunner): Promise<K>;
|
|
14
14
|
insert(data: DeepPartial<K> | DeepPartial<K>[]): Promise<K | K[]>;
|
|
15
15
|
update(data: DeepPartial<K> | DeepPartial<K>[], repo?: Repository<K>): Promise<K | K[]>;
|
|
16
|
-
delete(data: DeepPartial<K> | DeepPartial<K>[], repo?: Repository<K>): Promise<K |
|
|
16
|
+
delete(data: DeepPartial<K> | DeepPartial<K>[], repo?: Repository<K>): Promise<K | K[]>;
|
|
17
17
|
findAndCount(where: FindOptionsWhere<K> | FindOptionsWhere<K>[], options?: Options<K>): Promise<{
|
|
18
18
|
rows: K[];
|
|
19
19
|
count: number;
|
|
@@ -47,14 +47,21 @@ class BaseModelService extends base_service_1.BaseService {
|
|
|
47
47
|
}
|
|
48
48
|
async delete(data, repo) {
|
|
49
49
|
repo = repo || this.model;
|
|
50
|
+
const updater = this.ctx.currentSession?.loginId;
|
|
50
51
|
if (Array.isArray(data)) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
const entities = data.map(item => {
|
|
53
|
+
return this.model.create({
|
|
54
|
+
...item,
|
|
55
|
+
valid: enumType_1.EValid.Unvalid,
|
|
56
|
+
updater,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
return await repo.save(entities);
|
|
55
60
|
}
|
|
56
61
|
else {
|
|
57
62
|
data.valid = enumType_1.EValid.Unvalid;
|
|
63
|
+
if (updater)
|
|
64
|
+
data.updater = updater;
|
|
58
65
|
return await repo.save(data);
|
|
59
66
|
}
|
|
60
67
|
}
|
|
@@ -64,7 +71,6 @@ class BaseModelService extends base_service_1.BaseService {
|
|
|
64
71
|
let take = Number(size);
|
|
65
72
|
if (take > 10000)
|
|
66
73
|
take = 10000;
|
|
67
|
-
console.log('find count', where, skip, take);
|
|
68
74
|
const [rows, count] = await Promise.all([
|
|
69
75
|
this.model.find({ where, select, order, skip, take, relations }),
|
|
70
76
|
this.model.countBy(where),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cicctencent/midwayjs-base",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.39",
|
|
4
4
|
"description": "基础框架,服务",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -24,47 +24,47 @@
|
|
|
24
24
|
],
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@jest/globals": "^30.
|
|
28
|
-
"@midwayjs/mock": "^3.20.
|
|
27
|
+
"@jest/globals": "^30.4.1",
|
|
28
|
+
"@midwayjs/mock": "^3.20.24",
|
|
29
29
|
"@types/ip": "^1.1.3",
|
|
30
30
|
"@types/jest": "^30.0.0",
|
|
31
31
|
"@types/jsonwebtoken": "^9.0.10",
|
|
32
|
-
"@types/koa": "^3.0.
|
|
33
|
-
"@types/lodash": "^4.17.
|
|
32
|
+
"@types/koa": "^3.0.3",
|
|
33
|
+
"@types/lodash": "^4.17.24",
|
|
34
34
|
"@types/lodash-es": "^4.17.12",
|
|
35
|
-
"@types/node": "^24.
|
|
36
|
-
"cross-env": "^10.
|
|
37
|
-
"jest": "^30.
|
|
35
|
+
"@types/node": "^24.12.4",
|
|
36
|
+
"cross-env": "^10.1.0",
|
|
37
|
+
"jest": "^30.4.2",
|
|
38
38
|
"mwts": "^1.3.0",
|
|
39
|
-
"mwtsc": "^1.
|
|
39
|
+
"mwtsc": "^1.16.0",
|
|
40
40
|
"npm-run-all": "^4.1.5",
|
|
41
|
-
"ts-jest": "^29.4.
|
|
42
|
-
"typescript": "^5.9.
|
|
41
|
+
"ts-jest": "^29.4.10",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@fefeding/common": "^1.0.
|
|
46
|
-
"@midwayjs/axios": "^3.20.
|
|
47
|
-
"@midwayjs/bootstrap": "^3.20.
|
|
48
|
-
"@midwayjs/cache-manager": "^3.20.
|
|
45
|
+
"@fefeding/common": "^1.0.59",
|
|
46
|
+
"@midwayjs/axios": "^3.20.24",
|
|
47
|
+
"@midwayjs/bootstrap": "^3.20.24",
|
|
48
|
+
"@midwayjs/cache-manager": "^3.20.24",
|
|
49
49
|
"@midwayjs/cli": "^2.1.1",
|
|
50
50
|
"@midwayjs/cookies": "^1.3.0",
|
|
51
|
-
"@midwayjs/core": "^3.20.
|
|
52
|
-
"@midwayjs/decorator": "^3.20.
|
|
53
|
-
"@midwayjs/info": "^3.20.
|
|
54
|
-
"@midwayjs/koa": "^3.20.
|
|
51
|
+
"@midwayjs/core": "^3.20.24",
|
|
52
|
+
"@midwayjs/decorator": "^3.20.24",
|
|
53
|
+
"@midwayjs/info": "^3.20.24",
|
|
54
|
+
"@midwayjs/koa": "^3.20.24",
|
|
55
55
|
"@midwayjs/logger": "^4.0.1",
|
|
56
|
-
"@midwayjs/static-file": "^3.20.
|
|
57
|
-
"@midwayjs/typeorm": "^3.20.
|
|
58
|
-
"@midwayjs/view-nunjucks": "^3.20.
|
|
59
|
-
"axios": "^1.
|
|
60
|
-
"dayjs": "^1.11.
|
|
61
|
-
"dotenv": "^17.2
|
|
56
|
+
"@midwayjs/static-file": "^3.20.24",
|
|
57
|
+
"@midwayjs/typeorm": "^3.20.24",
|
|
58
|
+
"@midwayjs/view-nunjucks": "^3.20.24",
|
|
59
|
+
"axios": "^1.16.1",
|
|
60
|
+
"dayjs": "^1.11.20",
|
|
61
|
+
"dotenv": "^17.4.2",
|
|
62
62
|
"ip": "^2.0.1",
|
|
63
|
-
"jsonwebtoken": "^9.0.
|
|
64
|
-
"lodash": "^4.
|
|
65
|
-
"mysql2": "^3.
|
|
63
|
+
"jsonwebtoken": "^9.0.3",
|
|
64
|
+
"lodash": "^4.18.1",
|
|
65
|
+
"mysql2": "^3.22.3",
|
|
66
66
|
"reflect-metadata": "^0.2.2",
|
|
67
|
-
"typeorm": "^0.3.
|
|
68
|
-
"uuid": "^13.0.
|
|
67
|
+
"typeorm": "^0.3.30",
|
|
68
|
+
"uuid": "^13.0.2"
|
|
69
69
|
}
|
|
70
70
|
}
|