@hlw-midway/core 1.0.0
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 +4 -0
- package/dist/bin/check.d.ts +4 -0
- package/dist/bin/check.js +41 -0
- package/dist/bin/entity.d.ts +8 -0
- package/dist/bin/entity.js +50 -0
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.js +49 -0
- package/dist/bin/obfuscate.d.ts +1 -0
- package/dist/bin/obfuscate.js +69 -0
- package/dist/cache/store.d.ts +18 -0
- package/dist/cache/store.js +38 -0
- package/dist/config/config.default.d.ts +8 -0
- package/dist/config/config.default.js +22 -0
- package/dist/configuration.d.ts +17 -0
- package/dist/configuration.js +106 -0
- package/dist/constant/global.d.ts +59 -0
- package/dist/constant/global.js +85 -0
- package/dist/controller/base.d.ts +107 -0
- package/dist/controller/base.js +236 -0
- package/dist/decorator/cache.d.ts +2 -0
- package/dist/decorator/cache.js +10 -0
- package/dist/decorator/controller.d.ts +71 -0
- package/dist/decorator/controller.js +169 -0
- package/dist/decorator/event.d.ts +21 -0
- package/dist/decorator/event.js +39 -0
- package/dist/decorator/index.d.ts +22 -0
- package/dist/decorator/index.js +120 -0
- package/dist/decorator/tag.d.ts +22 -0
- package/dist/decorator/tag.js +40 -0
- package/dist/decorator/transaction.d.ts +8 -0
- package/dist/decorator/transaction.js +10 -0
- package/dist/entity/base.d.ts +10 -0
- package/dist/entity/base.js +41 -0
- package/dist/entity/mongo.d.ts +10 -0
- package/dist/entity/mongo.js +35 -0
- package/dist/entity/typeorm.d.ts +3 -0
- package/dist/entity/typeorm.js +7 -0
- package/dist/event/index.d.ts +53 -0
- package/dist/event/index.js +188 -0
- package/dist/exception/base.d.ts +8 -0
- package/dist/exception/base.js +15 -0
- package/dist/exception/comm.d.ts +7 -0
- package/dist/exception/comm.js +15 -0
- package/dist/exception/core.d.ts +7 -0
- package/dist/exception/core.js +15 -0
- package/dist/exception/filter.d.ts +11 -0
- package/dist/exception/filter.js +42 -0
- package/dist/exception/validate.d.ts +7 -0
- package/dist/exception/validate.js +15 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +60 -0
- package/dist/interface.d.ts +432 -0
- package/dist/interface.js +24 -0
- package/dist/module/config.d.ts +21 -0
- package/dist/module/config.js +109 -0
- package/dist/module/import.d.ts +64 -0
- package/dist/module/import.js +244 -0
- package/dist/module/menu.d.ts +54 -0
- package/dist/module/menu.js +178 -0
- package/dist/rest/eps.d.ts +51 -0
- package/dist/rest/eps.js +269 -0
- package/dist/service/base.d.ts +167 -0
- package/dist/service/base.js +281 -0
- package/dist/service/mysql.d.ts +143 -0
- package/dist/service/mysql.js +524 -0
- package/dist/service/postgres.d.ts +160 -0
- package/dist/service/postgres.js +639 -0
- package/dist/service/sqlite.d.ts +142 -0
- package/dist/service/sqlite.js +560 -0
- package/dist/tag/data.d.ts +25 -0
- package/dist/tag/data.js +85 -0
- package/dist/util/func.d.ts +8 -0
- package/dist/util/func.js +44 -0
- package/dist/util/location.d.ts +26 -0
- package/dist/util/location.js +113 -0
- package/index.d.ts +10 -0
- package/package.json +68 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HlwDecorator = void 0;
|
|
13
|
+
const cache_1 = require("./cache");
|
|
14
|
+
const cache_manager_1 = require("@midwayjs/cache-manager");
|
|
15
|
+
const core_1 = require("@midwayjs/core");
|
|
16
|
+
const typeorm_1 = require("@midwayjs/typeorm");
|
|
17
|
+
const comm_1 = require("../exception/comm");
|
|
18
|
+
const transaction_1 = require("./transaction");
|
|
19
|
+
const md5 = require("md5");
|
|
20
|
+
const data_1 = require("../tag/data");
|
|
21
|
+
/**
|
|
22
|
+
* 装饰器
|
|
23
|
+
*/
|
|
24
|
+
let HlwDecorator = class HlwDecorator {
|
|
25
|
+
async init() {
|
|
26
|
+
// 事务
|
|
27
|
+
await this.transaction();
|
|
28
|
+
// 缓存
|
|
29
|
+
await this.cache();
|
|
30
|
+
// URL标签
|
|
31
|
+
await this.hlwUrlTagData.init();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 缓存
|
|
35
|
+
*/
|
|
36
|
+
async cache() {
|
|
37
|
+
this.decoratorService.registerMethodHandler(cache_1.HLW_CACHE, options => {
|
|
38
|
+
return {
|
|
39
|
+
around: async (joinPoint) => {
|
|
40
|
+
const key = md5(joinPoint.target.constructor.name +
|
|
41
|
+
joinPoint.methodName +
|
|
42
|
+
JSON.stringify(joinPoint.args));
|
|
43
|
+
// 缓存有数据就返回
|
|
44
|
+
let data = await this.midwayCache.get(key);
|
|
45
|
+
if (data) {
|
|
46
|
+
return JSON.parse(data);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// 执行原始方法
|
|
50
|
+
data = await joinPoint.proceed(...joinPoint.args);
|
|
51
|
+
await this.midwayCache.set(key, JSON.stringify(data), options.metadata);
|
|
52
|
+
}
|
|
53
|
+
return data;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 事务
|
|
60
|
+
*/
|
|
61
|
+
async transaction() {
|
|
62
|
+
this.decoratorService.registerMethodHandler(transaction_1.HLW_TRANSACTION, options => {
|
|
63
|
+
return {
|
|
64
|
+
around: async (joinPoint) => {
|
|
65
|
+
const option = options.metadata;
|
|
66
|
+
const dataSource = this.typeORMDataSourceManager.getDataSource((option === null || option === void 0 ? void 0 : option.connectionName) || 'default');
|
|
67
|
+
const queryRunner = dataSource.createQueryRunner();
|
|
68
|
+
await queryRunner.connect();
|
|
69
|
+
if (option && option.isolation) {
|
|
70
|
+
await queryRunner.startTransaction(option.isolation);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
await queryRunner.startTransaction();
|
|
74
|
+
}
|
|
75
|
+
let data;
|
|
76
|
+
try {
|
|
77
|
+
joinPoint.args.push(queryRunner);
|
|
78
|
+
data = await joinPoint.proceed(...joinPoint.args);
|
|
79
|
+
await queryRunner.commitTransaction();
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
await queryRunner.rollbackTransaction();
|
|
83
|
+
throw new comm_1.HlwCommException(error.message);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
await queryRunner.release();
|
|
87
|
+
}
|
|
88
|
+
return data;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
exports.HlwDecorator = HlwDecorator;
|
|
95
|
+
__decorate([
|
|
96
|
+
(0, core_1.Inject)(),
|
|
97
|
+
__metadata("design:type", typeorm_1.TypeORMDataSourceManager)
|
|
98
|
+
], HlwDecorator.prototype, "typeORMDataSourceManager", void 0);
|
|
99
|
+
__decorate([
|
|
100
|
+
(0, core_1.Inject)(),
|
|
101
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
102
|
+
], HlwDecorator.prototype, "decoratorService", void 0);
|
|
103
|
+
__decorate([
|
|
104
|
+
(0, core_1.InjectClient)(cache_manager_1.CachingFactory, 'default'),
|
|
105
|
+
__metadata("design:type", Object)
|
|
106
|
+
], HlwDecorator.prototype, "midwayCache", void 0);
|
|
107
|
+
__decorate([
|
|
108
|
+
(0, core_1.Inject)(),
|
|
109
|
+
__metadata("design:type", data_1.HlwUrlTagData)
|
|
110
|
+
], HlwDecorator.prototype, "hlwUrlTagData", void 0);
|
|
111
|
+
__decorate([
|
|
112
|
+
(0, core_1.Init)(),
|
|
113
|
+
__metadata("design:type", Function),
|
|
114
|
+
__metadata("design:paramtypes", []),
|
|
115
|
+
__metadata("design:returntype", Promise)
|
|
116
|
+
], HlwDecorator.prototype, "init", null);
|
|
117
|
+
exports.HlwDecorator = HlwDecorator = __decorate([
|
|
118
|
+
(0, core_1.Provide)(),
|
|
119
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
|
|
120
|
+
], HlwDecorator);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const HLW_URL_TAG_KEY = "decorator:hlw:url:tag";
|
|
2
|
+
export declare const HLW_METHOD_TAG_KEY = "decorator:hlw:method:tag";
|
|
3
|
+
export declare enum TagTypes {
|
|
4
|
+
IGNORE_TOKEN = "ignoreToken",
|
|
5
|
+
IGNORE_SIGN = "ignoreSign"
|
|
6
|
+
}
|
|
7
|
+
export interface HlwUrlTagConfig {
|
|
8
|
+
key: TagTypes | string;
|
|
9
|
+
value?: string[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 打标记
|
|
13
|
+
* @param data
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
export declare function HlwUrlTag(data?: HlwUrlTagConfig): ClassDecorator;
|
|
17
|
+
/**
|
|
18
|
+
* 方法打标记
|
|
19
|
+
* @param data
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export declare function HlwTag(tag: TagTypes | string): MethodDecorator;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TagTypes = exports.HLW_METHOD_TAG_KEY = exports.HLW_URL_TAG_KEY = void 0;
|
|
4
|
+
exports.HlwUrlTag = HlwUrlTag;
|
|
5
|
+
exports.HlwTag = HlwTag;
|
|
6
|
+
const core_1 = require("@midwayjs/core");
|
|
7
|
+
exports.HLW_URL_TAG_KEY = 'decorator:hlw:url:tag';
|
|
8
|
+
exports.HLW_METHOD_TAG_KEY = 'decorator:hlw:method:tag';
|
|
9
|
+
var TagTypes;
|
|
10
|
+
(function (TagTypes) {
|
|
11
|
+
TagTypes["IGNORE_TOKEN"] = "ignoreToken";
|
|
12
|
+
TagTypes["IGNORE_SIGN"] = "ignoreSign";
|
|
13
|
+
})(TagTypes || (exports.TagTypes = TagTypes = {}));
|
|
14
|
+
/**
|
|
15
|
+
* 打标记
|
|
16
|
+
* @param data
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
function HlwUrlTag(data) {
|
|
20
|
+
return (target) => {
|
|
21
|
+
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
|
22
|
+
(0, core_1.saveModule)(exports.HLW_URL_TAG_KEY, target);
|
|
23
|
+
// 保存一些元数据信息,任意你希望存的东西
|
|
24
|
+
(0, core_1.saveClassMetadata)(exports.HLW_URL_TAG_KEY, data, target);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 方法打标记
|
|
29
|
+
* @param data
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
function HlwTag(tag) {
|
|
33
|
+
return (target, key, descriptor) => {
|
|
34
|
+
(0, core_1.savePropertyDataToClass)(exports.HLW_METHOD_TAG_KEY, {
|
|
35
|
+
key,
|
|
36
|
+
tag,
|
|
37
|
+
}, target, key);
|
|
38
|
+
return descriptor;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type IsolationLevel = 'READ UNCOMMITTED' | 'READ COMMITTED' | 'REPEATABLE READ' | 'SERIALIZABLE';
|
|
2
|
+
export interface TransactionOptions {
|
|
3
|
+
connectionName?: string;
|
|
4
|
+
isolation?: IsolationLevel;
|
|
5
|
+
}
|
|
6
|
+
export declare const HLW_TRANSACTION = "decorator:hlw_transaction";
|
|
7
|
+
export declare function HlwTransaction(option?: TransactionOptions): MethodDecorator;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HLW_TRANSACTION = void 0;
|
|
4
|
+
exports.HlwTransaction = HlwTransaction;
|
|
5
|
+
const core_1 = require("@midwayjs/core");
|
|
6
|
+
// 装饰器内部的唯一 id
|
|
7
|
+
exports.HLW_TRANSACTION = 'decorator:hlw_transaction';
|
|
8
|
+
function HlwTransaction(option) {
|
|
9
|
+
return (0, core_1.createCustomMethodDecorator)(exports.HLW_TRANSACTION, option);
|
|
10
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseEntity = void 0;
|
|
13
|
+
const typeorm_1 = require("typeorm");
|
|
14
|
+
const typeorm_2 = require("./typeorm");
|
|
15
|
+
/**
|
|
16
|
+
* 模型基类
|
|
17
|
+
*/
|
|
18
|
+
class BaseEntity extends typeorm_2.HlwBaseEntity {
|
|
19
|
+
}
|
|
20
|
+
exports.BaseEntity = BaseEntity;
|
|
21
|
+
__decorate([
|
|
22
|
+
(0, typeorm_1.PrimaryGeneratedColumn)('increment', {
|
|
23
|
+
comment: 'ID',
|
|
24
|
+
}),
|
|
25
|
+
__metadata("design:type", Number)
|
|
26
|
+
], BaseEntity.prototype, "id", void 0);
|
|
27
|
+
__decorate([
|
|
28
|
+
(0, typeorm_1.Index)(),
|
|
29
|
+
(0, typeorm_1.CreateDateColumn)({ comment: '创建时间' }),
|
|
30
|
+
__metadata("design:type", Date)
|
|
31
|
+
], BaseEntity.prototype, "createdAt", void 0);
|
|
32
|
+
__decorate([
|
|
33
|
+
(0, typeorm_1.Index)(),
|
|
34
|
+
(0, typeorm_1.UpdateDateColumn)({ comment: '更新时间' }),
|
|
35
|
+
__metadata("design:type", Date)
|
|
36
|
+
], BaseEntity.prototype, "updatedAt", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
(0, typeorm_1.Index)(),
|
|
39
|
+
(0, typeorm_1.Column)({ comment: '租户ID', nullable: true }),
|
|
40
|
+
__metadata("design:type", Number)
|
|
41
|
+
], BaseEntity.prototype, "tenantId", void 0);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var _a;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.BaseMongoEntity = void 0;
|
|
14
|
+
const typeorm_1 = require("typeorm");
|
|
15
|
+
const typeorm_2 = require("./typeorm");
|
|
16
|
+
/**
|
|
17
|
+
* 模型基类
|
|
18
|
+
*/
|
|
19
|
+
class BaseMongoEntity extends typeorm_2.HlwBaseEntity {
|
|
20
|
+
}
|
|
21
|
+
exports.BaseMongoEntity = BaseMongoEntity;
|
|
22
|
+
__decorate([
|
|
23
|
+
(0, typeorm_1.ObjectIdColumn)({ comment: 'id' }),
|
|
24
|
+
__metadata("design:type", typeof (_a = typeof typeorm_1.ObjectID !== "undefined" && typeorm_1.ObjectID) === "function" ? _a : Object)
|
|
25
|
+
], BaseMongoEntity.prototype, "id", void 0);
|
|
26
|
+
__decorate([
|
|
27
|
+
(0, typeorm_1.Index)(),
|
|
28
|
+
(0, typeorm_1.CreateDateColumn)({ comment: '创建时间' }),
|
|
29
|
+
__metadata("design:type", Date)
|
|
30
|
+
], BaseMongoEntity.prototype, "createdAt", void 0);
|
|
31
|
+
__decorate([
|
|
32
|
+
(0, typeorm_1.Index)(),
|
|
33
|
+
(0, typeorm_1.UpdateDateColumn)({ comment: '更新时间' }),
|
|
34
|
+
__metadata("design:type", Date)
|
|
35
|
+
], BaseMongoEntity.prototype, "updatedAt", void 0);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as Events from 'events';
|
|
2
|
+
import { IMidwayApplication } from '@midwayjs/core';
|
|
3
|
+
export declare const HLW_EVENT_MESSAGE = "hlw:event:message";
|
|
4
|
+
/**
|
|
5
|
+
* 事件
|
|
6
|
+
*/
|
|
7
|
+
export declare class HlwEventManager extends Events {
|
|
8
|
+
app: IMidwayApplication;
|
|
9
|
+
keys: string;
|
|
10
|
+
eventData: {
|
|
11
|
+
[key: string]: {
|
|
12
|
+
module: any;
|
|
13
|
+
method: string;
|
|
14
|
+
}[];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* 初始化
|
|
18
|
+
*/
|
|
19
|
+
init(): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* 发送事件
|
|
22
|
+
* @param event
|
|
23
|
+
* @param args
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 发送全局事件
|
|
29
|
+
* @param event 事件
|
|
30
|
+
* @param random 是否随机一个
|
|
31
|
+
* @param args 参数
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
globalEmit(event: string, random?: boolean, ...args: any[]): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* 处理事件
|
|
37
|
+
* @param module
|
|
38
|
+
*/
|
|
39
|
+
handlerEvent(module: any): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* 全局事件
|
|
42
|
+
*/
|
|
43
|
+
globalEvent(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* 普通事件
|
|
46
|
+
*/
|
|
47
|
+
commEvent(): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* 执行事件
|
|
50
|
+
* @param message
|
|
51
|
+
*/
|
|
52
|
+
doAction(message: any): Promise<void>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HlwEventManager = exports.HLW_EVENT_MESSAGE = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const Events = require("events");
|
|
15
|
+
const core_2 = require("@midwayjs/core");
|
|
16
|
+
const event_1 = require("../decorator/event");
|
|
17
|
+
const pm2 = require("pm2");
|
|
18
|
+
const _ = require("lodash");
|
|
19
|
+
exports.HLW_EVENT_MESSAGE = 'hlw:event:message';
|
|
20
|
+
/**
|
|
21
|
+
* 事件
|
|
22
|
+
*/
|
|
23
|
+
let HlwEventManager = class HlwEventManager extends Events {
|
|
24
|
+
constructor() {
|
|
25
|
+
super(...arguments);
|
|
26
|
+
// 事件数据 某个事件对应的模块对应的方法
|
|
27
|
+
this.eventData = {};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 初始化
|
|
31
|
+
*/
|
|
32
|
+
async init() {
|
|
33
|
+
const eventModules = (0, core_1.listModule)(event_1.HLW_CLS_EVENT_KEY);
|
|
34
|
+
for (const module of eventModules) {
|
|
35
|
+
await this.handlerEvent(module);
|
|
36
|
+
}
|
|
37
|
+
await this.commEvent();
|
|
38
|
+
await this.globalEvent();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 发送事件
|
|
42
|
+
* @param event
|
|
43
|
+
* @param args
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
emit(event, ...args) {
|
|
47
|
+
return super.emit(exports.HLW_EVENT_MESSAGE, {
|
|
48
|
+
type: exports.HLW_EVENT_MESSAGE,
|
|
49
|
+
data: {
|
|
50
|
+
event,
|
|
51
|
+
args,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 发送全局事件
|
|
57
|
+
* @param event 事件
|
|
58
|
+
* @param random 是否随机一个
|
|
59
|
+
* @param args 参数
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
async globalEmit(event, random = false, ...args) {
|
|
63
|
+
// 如果是本地运行还是转普通模式
|
|
64
|
+
if (this.app.getEnv() === 'local') {
|
|
65
|
+
this.emit(event, ...args);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
pm2.connect(() => {
|
|
69
|
+
pm2.list((err, list) => {
|
|
70
|
+
const ps = list.map(e => {
|
|
71
|
+
return {
|
|
72
|
+
id: e.pm_id,
|
|
73
|
+
name: e.name,
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
// random 为 true 时随机发给同名称的一个进程
|
|
77
|
+
if (random) {
|
|
78
|
+
// 按名称分组
|
|
79
|
+
const group = _.groupBy(ps, 'name');
|
|
80
|
+
const names = Object.keys(group);
|
|
81
|
+
// 遍历名称
|
|
82
|
+
names.forEach(name => {
|
|
83
|
+
const pss = group[name];
|
|
84
|
+
// 随机一个
|
|
85
|
+
const index = _.random(0, pss.length - 1);
|
|
86
|
+
const ps = pss[index];
|
|
87
|
+
// 发给这个进程
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
pm2.sendDataToProcessId({
|
|
90
|
+
type: 'process:msg',
|
|
91
|
+
data: {
|
|
92
|
+
type: `${exports.HLW_EVENT_MESSAGE}@${this.keys}`,
|
|
93
|
+
event,
|
|
94
|
+
args,
|
|
95
|
+
},
|
|
96
|
+
id: ps.id,
|
|
97
|
+
topic: 'hlw:event:topic',
|
|
98
|
+
}, (err, res) => { });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
// 发给所有进程
|
|
103
|
+
ps.forEach(e => {
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
pm2.sendDataToProcessId({
|
|
106
|
+
type: 'process:msg',
|
|
107
|
+
data: {
|
|
108
|
+
type: `${exports.HLW_EVENT_MESSAGE}@${this.keys}`,
|
|
109
|
+
event,
|
|
110
|
+
args,
|
|
111
|
+
},
|
|
112
|
+
id: e.id,
|
|
113
|
+
topic: 'hlw:event:topic',
|
|
114
|
+
}, (err, res) => { });
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 处理事件
|
|
122
|
+
* @param module
|
|
123
|
+
*/
|
|
124
|
+
async handlerEvent(module) {
|
|
125
|
+
const events = (0, core_1.getClassMetadata)(event_1.HLW_EVENT_KEY, module);
|
|
126
|
+
for (const event of events) {
|
|
127
|
+
const listen = event.eventName ? event.eventName : event.propertyKey;
|
|
128
|
+
if (!this.eventData[listen]) {
|
|
129
|
+
this.eventData[listen] = [];
|
|
130
|
+
}
|
|
131
|
+
this.eventData[listen].push({
|
|
132
|
+
module,
|
|
133
|
+
method: event.propertyKey,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 全局事件
|
|
139
|
+
*/
|
|
140
|
+
async globalEvent() {
|
|
141
|
+
process.on('message', async (message) => {
|
|
142
|
+
const data = message === null || message === void 0 ? void 0 : message.data;
|
|
143
|
+
if (!data)
|
|
144
|
+
return;
|
|
145
|
+
if (data.type != `${exports.HLW_EVENT_MESSAGE}@${this.keys}`)
|
|
146
|
+
return;
|
|
147
|
+
await this.doAction(message);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 普通事件
|
|
152
|
+
*/
|
|
153
|
+
async commEvent() {
|
|
154
|
+
this.on(exports.HLW_EVENT_MESSAGE, async (message) => {
|
|
155
|
+
await this.doAction(message);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* 执行事件
|
|
160
|
+
* @param message
|
|
161
|
+
*/
|
|
162
|
+
async doAction(message) {
|
|
163
|
+
const data = message.data;
|
|
164
|
+
const method = data.event;
|
|
165
|
+
const args = data.args;
|
|
166
|
+
if (this.eventData[method]) {
|
|
167
|
+
for (const event of this.eventData[method]) {
|
|
168
|
+
const moduleInstance = await this.app
|
|
169
|
+
.getApplicationContext()
|
|
170
|
+
.getAsync(event.module);
|
|
171
|
+
moduleInstance[event.method](...args);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
exports.HlwEventManager = HlwEventManager;
|
|
177
|
+
__decorate([
|
|
178
|
+
(0, core_1.App)(),
|
|
179
|
+
__metadata("design:type", Object)
|
|
180
|
+
], HlwEventManager.prototype, "app", void 0);
|
|
181
|
+
__decorate([
|
|
182
|
+
(0, core_2.Config)('keys'),
|
|
183
|
+
__metadata("design:type", String)
|
|
184
|
+
], HlwEventManager.prototype, "keys", void 0);
|
|
185
|
+
exports.HlwEventManager = HlwEventManager = __decorate([
|
|
186
|
+
(0, core_1.Provide)(),
|
|
187
|
+
(0, core_2.Scope)(core_2.ScopeEnum.Singleton)
|
|
188
|
+
], HlwEventManager);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 异常基类
|
|
6
|
+
*/
|
|
7
|
+
class BaseException extends Error {
|
|
8
|
+
constructor(name, code, message, statusCode) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = name;
|
|
11
|
+
this.status = code;
|
|
12
|
+
this.statusCode = statusCode;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.BaseException = BaseException;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HlwCommException = void 0;
|
|
4
|
+
const global_1 = require("../constant/global");
|
|
5
|
+
const base_1 = require("./base");
|
|
6
|
+
/**
|
|
7
|
+
* 通用异常
|
|
8
|
+
*/
|
|
9
|
+
class HlwCommException extends base_1.BaseException {
|
|
10
|
+
constructor(message, statusCode) {
|
|
11
|
+
const { RESCODE, RESMESSAGE } = global_1.GlobalConfig.getInstance();
|
|
12
|
+
super('HlwCommException', RESCODE.COMMFAIL, message ? message : RESMESSAGE.COMMFAIL, statusCode);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.HlwCommException = HlwCommException;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HlwCoreException = void 0;
|
|
4
|
+
const global_1 = require("../constant/global");
|
|
5
|
+
const base_1 = require("./base");
|
|
6
|
+
/**
|
|
7
|
+
* 核心异常
|
|
8
|
+
*/
|
|
9
|
+
class HlwCoreException extends base_1.BaseException {
|
|
10
|
+
constructor(message, statusCode) {
|
|
11
|
+
const { RESCODE, RESMESSAGE } = global_1.GlobalConfig.getInstance();
|
|
12
|
+
super('HlwCoreException', RESCODE.COREFAIL, message ? message : RESMESSAGE.COREFAIL, statusCode);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.HlwCoreException = HlwCoreException;
|