@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,107 @@
|
|
|
1
|
+
import { CurdOption } from '../decorator/controller';
|
|
2
|
+
import { IMidwayApplication } from '@midwayjs/core';
|
|
3
|
+
import { Context } from '@midwayjs/koa';
|
|
4
|
+
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
|
5
|
+
/**
|
|
6
|
+
* 控制器基类
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class BaseController {
|
|
9
|
+
baseCtx: Context;
|
|
10
|
+
service: any;
|
|
11
|
+
baseApp: IMidwayApplication;
|
|
12
|
+
curdOption: CurdOption;
|
|
13
|
+
typeORMDataSourceManager: TypeORMDataSourceManager;
|
|
14
|
+
connectionName: any;
|
|
15
|
+
init(): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* 获取用户ID
|
|
18
|
+
* @param type 类型
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
protected getUserId(type?: 'admin' | 'app'): any;
|
|
22
|
+
/**
|
|
23
|
+
* 创建动态方法
|
|
24
|
+
* @param curdOption 配置
|
|
25
|
+
*/
|
|
26
|
+
private createDynamicMethods;
|
|
27
|
+
private before;
|
|
28
|
+
/**
|
|
29
|
+
* 插入参数值
|
|
30
|
+
* @param curdOption 配置
|
|
31
|
+
*/
|
|
32
|
+
private insertParam;
|
|
33
|
+
/**
|
|
34
|
+
* 设置实体
|
|
35
|
+
* @param curdOption 配置
|
|
36
|
+
*/
|
|
37
|
+
private setEntity;
|
|
38
|
+
/**
|
|
39
|
+
* 设置service
|
|
40
|
+
* @param curdOption
|
|
41
|
+
*/
|
|
42
|
+
private setService;
|
|
43
|
+
/**
|
|
44
|
+
* 新增
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
add(): Promise<{
|
|
48
|
+
code: number;
|
|
49
|
+
message: string;
|
|
50
|
+
}>;
|
|
51
|
+
/**
|
|
52
|
+
* 删除
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
delete(): Promise<{
|
|
56
|
+
code: number;
|
|
57
|
+
message: string;
|
|
58
|
+
}>;
|
|
59
|
+
/**
|
|
60
|
+
* 更新
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
63
|
+
update(): Promise<{
|
|
64
|
+
code: number;
|
|
65
|
+
message: string;
|
|
66
|
+
}>;
|
|
67
|
+
/**
|
|
68
|
+
* 分页查询
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
71
|
+
page(): Promise<{
|
|
72
|
+
code: number;
|
|
73
|
+
message: string;
|
|
74
|
+
}>;
|
|
75
|
+
/**
|
|
76
|
+
* 列表查询
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
list(): Promise<{
|
|
80
|
+
code: number;
|
|
81
|
+
message: string;
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* 根据ID查询信息
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
info(): Promise<{
|
|
88
|
+
code: number;
|
|
89
|
+
message: string;
|
|
90
|
+
}>;
|
|
91
|
+
/**
|
|
92
|
+
* 成功返回
|
|
93
|
+
* @param data 返回数据
|
|
94
|
+
*/
|
|
95
|
+
ok(data?: any): {
|
|
96
|
+
code: number;
|
|
97
|
+
message: string;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* 失败返回
|
|
101
|
+
* @param message
|
|
102
|
+
*/
|
|
103
|
+
fail(message?: string, code?: number): {
|
|
104
|
+
code: number;
|
|
105
|
+
message: string;
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
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.BaseController = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const global_1 = require("../constant/global");
|
|
15
|
+
const typeorm_1 = require("@midwayjs/typeorm");
|
|
16
|
+
const validate_1 = require("../exception/validate");
|
|
17
|
+
/**
|
|
18
|
+
* 控制器基类
|
|
19
|
+
*/
|
|
20
|
+
let BaseController = class BaseController {
|
|
21
|
+
async init() {
|
|
22
|
+
const option = (0, core_1.getClassMetadata)(core_1.CONTROLLER_KEY, this);
|
|
23
|
+
this.service = await this.baseCtx.requestContext.getAsync('baseService');
|
|
24
|
+
const curdOption = option.curdOption;
|
|
25
|
+
this.curdOption = curdOption;
|
|
26
|
+
if (!this.curdOption) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
// 操作之前
|
|
30
|
+
await this.before(curdOption);
|
|
31
|
+
// 设置service
|
|
32
|
+
await this.setService(curdOption);
|
|
33
|
+
// 设置实体
|
|
34
|
+
await this.setEntity(curdOption);
|
|
35
|
+
// 创建动态方法
|
|
36
|
+
await this.createDynamicMethods(curdOption);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 获取用户ID
|
|
40
|
+
* @param type 类型
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
getUserId(type = 'admin') {
|
|
44
|
+
var _a, _b;
|
|
45
|
+
return type === 'admin'
|
|
46
|
+
? (_a = this.baseCtx.admin) === null || _a === void 0 ? void 0 : _a.userId
|
|
47
|
+
: (_b = this.baseCtx.user) === null || _b === void 0 ? void 0 : _b.id;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 创建动态方法
|
|
51
|
+
* @param curdOption 配置
|
|
52
|
+
*/
|
|
53
|
+
async createDynamicMethods(curdOption) {
|
|
54
|
+
if (!curdOption.serviceApis) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// 过滤出非标准方法
|
|
58
|
+
const customMethods = curdOption.serviceApis;
|
|
59
|
+
// 为每个自定义方法创建对应的控制器方法
|
|
60
|
+
for (const api of customMethods) {
|
|
61
|
+
const methodName = typeof api === 'string' ? api : api.method;
|
|
62
|
+
if (this[methodName]) {
|
|
63
|
+
continue; // 如果方法已存在则跳过
|
|
64
|
+
}
|
|
65
|
+
this[methodName] = async function () {
|
|
66
|
+
const { body } = this.baseCtx.request;
|
|
67
|
+
const serviceMethod = this.service[methodName];
|
|
68
|
+
if (typeof serviceMethod !== 'function') {
|
|
69
|
+
throw new validate_1.HlwValidateException(`Service method ${methodName} not found`);
|
|
70
|
+
}
|
|
71
|
+
return this.ok(await serviceMethod.call(this.service, body));
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async before(curdOption) {
|
|
76
|
+
if (!(curdOption === null || curdOption === void 0 ? void 0 : curdOption.before)) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
await curdOption.before(this.baseCtx, this.baseApp);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 插入参数值
|
|
83
|
+
* @param curdOption 配置
|
|
84
|
+
*/
|
|
85
|
+
async insertParam(curdOption) {
|
|
86
|
+
if (!(curdOption === null || curdOption === void 0 ? void 0 : curdOption.insertParam)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const body = this.baseCtx.request.body;
|
|
90
|
+
if (body) {
|
|
91
|
+
// 判断body是否是数组
|
|
92
|
+
if (Array.isArray(body)) {
|
|
93
|
+
for (let i = 0; i < body.length; i++) {
|
|
94
|
+
body[i] = {
|
|
95
|
+
...body[i],
|
|
96
|
+
...(await curdOption.insertParam(this.baseCtx, this.baseApp)),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
this.baseCtx.request.body = body;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.baseCtx.request.body = {
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
...this.baseCtx.request.body,
|
|
105
|
+
...(await curdOption.insertParam(this.baseCtx, this.baseApp)),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 设置实体
|
|
111
|
+
* @param curdOption 配置
|
|
112
|
+
*/
|
|
113
|
+
async setEntity(curdOption) {
|
|
114
|
+
const entity = curdOption === null || curdOption === void 0 ? void 0 : curdOption.entity;
|
|
115
|
+
if (entity) {
|
|
116
|
+
const dataSourceName = this.typeORMDataSourceManager.getDataSourceNameByModel(entity);
|
|
117
|
+
this.connectionName = dataSourceName;
|
|
118
|
+
const entityModel = this.typeORMDataSourceManager
|
|
119
|
+
.getDataSource(dataSourceName)
|
|
120
|
+
.getRepository(entity);
|
|
121
|
+
this.service.setEntity(entityModel);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* 设置service
|
|
126
|
+
* @param curdOption
|
|
127
|
+
*/
|
|
128
|
+
async setService(curdOption) {
|
|
129
|
+
if (curdOption.service) {
|
|
130
|
+
this.service = await this.baseCtx.requestContext.getAsync(curdOption.service);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 新增
|
|
135
|
+
* @returns
|
|
136
|
+
*/
|
|
137
|
+
async add() {
|
|
138
|
+
// 插入参数
|
|
139
|
+
await this.insertParam(this.curdOption);
|
|
140
|
+
const { body } = this.baseCtx.request;
|
|
141
|
+
return this.ok(await this.service.add(body));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 删除
|
|
145
|
+
* @returns
|
|
146
|
+
*/
|
|
147
|
+
async delete() {
|
|
148
|
+
// @ts-ignore
|
|
149
|
+
const { ids } = this.baseCtx.request.body;
|
|
150
|
+
return this.ok(await this.service.delete(ids));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* 更新
|
|
154
|
+
* @returns
|
|
155
|
+
*/
|
|
156
|
+
async update() {
|
|
157
|
+
const { body } = this.baseCtx.request;
|
|
158
|
+
return this.ok(await this.service.update(body));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 分页查询
|
|
162
|
+
* @returns
|
|
163
|
+
*/
|
|
164
|
+
async page() {
|
|
165
|
+
const { body } = this.baseCtx.request;
|
|
166
|
+
return this.ok(await this.service.page(body, this.curdOption.pageQueryOp, this.connectionName));
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 列表查询
|
|
170
|
+
* @returns
|
|
171
|
+
*/
|
|
172
|
+
async list() {
|
|
173
|
+
const { body } = this.baseCtx.request;
|
|
174
|
+
return this.ok(await this.service.list(body, this.curdOption.listQueryOp, this.connectionName));
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 根据ID查询信息
|
|
178
|
+
* @returns
|
|
179
|
+
*/
|
|
180
|
+
async info() {
|
|
181
|
+
const { id } = this.baseCtx.query;
|
|
182
|
+
return this.ok(await this.service.info(id, this.curdOption.infoIgnoreProperty));
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 成功返回
|
|
186
|
+
* @param data 返回数据
|
|
187
|
+
*/
|
|
188
|
+
ok(data) {
|
|
189
|
+
const { RESCODE, RESMESSAGE } = global_1.GlobalConfig.getInstance();
|
|
190
|
+
const res = {
|
|
191
|
+
code: RESCODE.SUCCESS,
|
|
192
|
+
message: RESMESSAGE.SUCCESS,
|
|
193
|
+
};
|
|
194
|
+
if (data || data == 0) {
|
|
195
|
+
res['data'] = data;
|
|
196
|
+
}
|
|
197
|
+
return res;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 失败返回
|
|
201
|
+
* @param message
|
|
202
|
+
*/
|
|
203
|
+
fail(message, code) {
|
|
204
|
+
const { RESCODE, RESMESSAGE } = global_1.GlobalConfig.getInstance();
|
|
205
|
+
return {
|
|
206
|
+
code: code ? code : RESCODE.COMMFAIL,
|
|
207
|
+
message: message
|
|
208
|
+
? message
|
|
209
|
+
: code == RESCODE.VALIDATEFAIL
|
|
210
|
+
? RESMESSAGE.VALIDATEFAIL
|
|
211
|
+
: RESMESSAGE.COMMFAIL,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
exports.BaseController = BaseController;
|
|
216
|
+
__decorate([
|
|
217
|
+
(0, core_1.Inject)('ctx'),
|
|
218
|
+
__metadata("design:type", Object)
|
|
219
|
+
], BaseController.prototype, "baseCtx", void 0);
|
|
220
|
+
__decorate([
|
|
221
|
+
(0, core_1.App)(),
|
|
222
|
+
__metadata("design:type", Object)
|
|
223
|
+
], BaseController.prototype, "baseApp", void 0);
|
|
224
|
+
__decorate([
|
|
225
|
+
(0, core_1.Inject)(),
|
|
226
|
+
__metadata("design:type", typeorm_1.TypeORMDataSourceManager)
|
|
227
|
+
], BaseController.prototype, "typeORMDataSourceManager", void 0);
|
|
228
|
+
__decorate([
|
|
229
|
+
(0, core_1.Init)(),
|
|
230
|
+
__metadata("design:type", Function),
|
|
231
|
+
__metadata("design:paramtypes", []),
|
|
232
|
+
__metadata("design:returntype", Promise)
|
|
233
|
+
], BaseController.prototype, "init", null);
|
|
234
|
+
exports.BaseController = BaseController = __decorate([
|
|
235
|
+
(0, core_1.Provide)()
|
|
236
|
+
], BaseController);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HLW_CACHE = void 0;
|
|
4
|
+
exports.HlwCache = HlwCache;
|
|
5
|
+
const core_1 = require("@midwayjs/core");
|
|
6
|
+
// 装饰器内部的唯一 id
|
|
7
|
+
exports.HLW_CACHE = 'decorator:hlw_cache';
|
|
8
|
+
function HlwCache(ttl) {
|
|
9
|
+
return (0, core_1.createCustomMethodDecorator)(exports.HLW_CACHE, ttl);
|
|
10
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { MiddlewareParamArray } from '@midwayjs/core';
|
|
2
|
+
export type ApiTypes = 'add' | 'delete' | 'update' | 'page' | 'info' | 'list';
|
|
3
|
+
/** 服务映射接口 */
|
|
4
|
+
export type ServiceApis = {
|
|
5
|
+
/** 方法 */
|
|
6
|
+
method: string;
|
|
7
|
+
/** 描述 */
|
|
8
|
+
summary: string;
|
|
9
|
+
};
|
|
10
|
+
export interface CurdOption {
|
|
11
|
+
prefix?: string;
|
|
12
|
+
api?: ApiTypes[];
|
|
13
|
+
serviceApis?: (ServiceApis | string)[];
|
|
14
|
+
pageQueryOp?: QueryOp | Function;
|
|
15
|
+
listQueryOp?: QueryOp | Function;
|
|
16
|
+
insertParam?: Function;
|
|
17
|
+
before?: Function;
|
|
18
|
+
infoIgnoreProperty?: string[];
|
|
19
|
+
entity?: any;
|
|
20
|
+
service?: any;
|
|
21
|
+
urlTag?: {
|
|
22
|
+
name: 'ignoreToken' | string;
|
|
23
|
+
url: ApiTypes[];
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface JoinOp {
|
|
27
|
+
entity: any;
|
|
28
|
+
alias: string;
|
|
29
|
+
condition: string;
|
|
30
|
+
type?: 'innerJoin' | 'leftJoin';
|
|
31
|
+
}
|
|
32
|
+
export interface FieldEq {
|
|
33
|
+
column: string;
|
|
34
|
+
requestParam: string;
|
|
35
|
+
}
|
|
36
|
+
export interface QueryOp {
|
|
37
|
+
keyWordLikeFields?: string[];
|
|
38
|
+
where?: Function | any[][];
|
|
39
|
+
select?: string[];
|
|
40
|
+
fieldLike?: string[] | FieldEq[] | (string | FieldEq)[];
|
|
41
|
+
fieldEq?: string[] | FieldEq[] | (string | FieldEq)[];
|
|
42
|
+
addOrderBy?: {};
|
|
43
|
+
join?: JoinOp[];
|
|
44
|
+
extend?: Function;
|
|
45
|
+
}
|
|
46
|
+
export interface ControllerOption {
|
|
47
|
+
curdOption?: CurdOption & string;
|
|
48
|
+
routerOptions?: {
|
|
49
|
+
sensitive?: boolean;
|
|
50
|
+
middleware?: MiddlewareParamArray;
|
|
51
|
+
alias?: string[];
|
|
52
|
+
description?: string;
|
|
53
|
+
tagName?: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export interface RouterOptions {
|
|
57
|
+
sensitive?: boolean;
|
|
58
|
+
middleware?: MiddlewareParamArray;
|
|
59
|
+
description?: string;
|
|
60
|
+
tagName?: string;
|
|
61
|
+
ignoreGlobalPrefix?: boolean;
|
|
62
|
+
}
|
|
63
|
+
export declare function HlwController(curdOption?: CurdOption | string | RouterOptions, routerOptions?: RouterOptions): ClassDecorator;
|
|
64
|
+
export declare const apiDesc: {
|
|
65
|
+
add: string;
|
|
66
|
+
delete: string;
|
|
67
|
+
update: string;
|
|
68
|
+
page: string;
|
|
69
|
+
list: string;
|
|
70
|
+
info: string;
|
|
71
|
+
};
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.apiDesc = void 0;
|
|
4
|
+
exports.HlwController = HlwController;
|
|
5
|
+
const core_1 = require("@midwayjs/core");
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const _ = require("lodash");
|
|
8
|
+
const location_1 = require("../util/location");
|
|
9
|
+
// HLW 的装饰器
|
|
10
|
+
function HlwController(curdOption, routerOptions = { middleware: [], sensitive: true }) {
|
|
11
|
+
return (target) => {
|
|
12
|
+
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
|
13
|
+
(0, core_1.saveModule)(core_1.CONTROLLER_KEY, target);
|
|
14
|
+
let prefix;
|
|
15
|
+
if (curdOption) {
|
|
16
|
+
// 判断 curdOption 的类型
|
|
17
|
+
if (typeof curdOption === 'string') {
|
|
18
|
+
prefix = curdOption;
|
|
19
|
+
}
|
|
20
|
+
else if (curdOption && 'api' in curdOption) {
|
|
21
|
+
// curdOption 是 CurdOption 类型
|
|
22
|
+
prefix = curdOption.prefix || '';
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// curdOption 是 RouterOptions 类型 合并到 routerOptions
|
|
26
|
+
routerOptions = { ...curdOption, ...routerOptions };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// 如果已有明确的 prefix,同步加载模块中间件并注册
|
|
30
|
+
if (prefix) {
|
|
31
|
+
location_1.default.scriptPath(target).then(async (res) => {
|
|
32
|
+
if (res === null || res === void 0 ? void 0 : res.path) {
|
|
33
|
+
const pathSps = res.path.split('.');
|
|
34
|
+
const paths = pathSps[pathSps.length - 2].split(/[/\\]/);
|
|
35
|
+
let module = null;
|
|
36
|
+
for (const p of paths.reverse()) {
|
|
37
|
+
if (p == 'controller' && paths.includes('modules')) {
|
|
38
|
+
module = 'ready';
|
|
39
|
+
}
|
|
40
|
+
else if (module === 'ready' && p != 'controller') {
|
|
41
|
+
module = p;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (module) {
|
|
46
|
+
const configPath = `${res.path.split(new RegExp(`modules[/\\\\]${module}`))[0]}modules/${module}/config.${_.endsWith(res.path, 'ts') ? 'ts' : 'js'}`;
|
|
47
|
+
if (fs.existsSync(configPath)) {
|
|
48
|
+
const config = require(configPath).default();
|
|
49
|
+
routerOptions.middleware = (config.middlewares || []).concat(routerOptions.middleware || []);
|
|
50
|
+
}
|
|
51
|
+
saveMetadata(prefix, routerOptions, target, curdOption, module);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
saveMetadata(prefix, routerOptions, target, curdOption, null);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
saveMetadata(prefix, routerOptions, target, curdOption, null);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
// 同步先保存 prefix,确保 HlwUrlTagData 能及时读到
|
|
62
|
+
saveMetadata(prefix, routerOptions, target, curdOption, null);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// 如果不存在路由前缀,那么自动根据当前文件夹路径
|
|
66
|
+
location_1.default.scriptPath(target).then(async (res) => {
|
|
67
|
+
if (!(res === null || res === void 0 ? void 0 : res.path))
|
|
68
|
+
return;
|
|
69
|
+
const pathSps = res.path.split('.');
|
|
70
|
+
const paths = pathSps[pathSps.length - 2].split(/[/\\]/);
|
|
71
|
+
const pathArr = [];
|
|
72
|
+
let module = null;
|
|
73
|
+
for (const path of paths.reverse()) {
|
|
74
|
+
if (path != 'controller' && !module) {
|
|
75
|
+
pathArr.push(path);
|
|
76
|
+
}
|
|
77
|
+
if (path == 'controller' && !paths.includes('modules')) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
if (path == 'controller' && paths.includes('modules')) {
|
|
81
|
+
module = 'ready';
|
|
82
|
+
}
|
|
83
|
+
if (module && path != 'controller') {
|
|
84
|
+
module = `${path}`;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (module) {
|
|
89
|
+
pathArr.reverse();
|
|
90
|
+
pathArr.splice(1, 0, module);
|
|
91
|
+
if (pathArr[pathArr.length - 1] === 'index') {
|
|
92
|
+
pathArr.pop();
|
|
93
|
+
}
|
|
94
|
+
// 追加模块中间件
|
|
95
|
+
const path = `${res.path.split(new RegExp(`modules[/\\\\]${module}`))[0]}modules/${module}/config.${_.endsWith(res.path, 'ts') ? 'ts' : 'js'}`;
|
|
96
|
+
if (fs.existsSync(path)) {
|
|
97
|
+
const config = require(path).default();
|
|
98
|
+
routerOptions.middleware = (config.middlewares || []).concat(routerOptions.middleware || []);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!prefix) {
|
|
102
|
+
prefix = `/${pathArr.map(p => p.replace(/-/g, '/')).join('/')}`;
|
|
103
|
+
}
|
|
104
|
+
saveMetadata(prefix, routerOptions, target, curdOption, module);
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
exports.apiDesc = {
|
|
109
|
+
add: '新增',
|
|
110
|
+
delete: '删除',
|
|
111
|
+
update: '修改',
|
|
112
|
+
page: '分页查询',
|
|
113
|
+
list: '列表查询',
|
|
114
|
+
info: '单个信息',
|
|
115
|
+
};
|
|
116
|
+
// 保存一些元数据信息,任意你希望存的东西
|
|
117
|
+
function saveMetadata(prefix, routerOptions, target, curdOption, module) {
|
|
118
|
+
// 统一转小写,避免文件名大小写导致路由不一致
|
|
119
|
+
prefix = (prefix || '').toLowerCase();
|
|
120
|
+
if (module && !routerOptions.tagName) {
|
|
121
|
+
routerOptions = routerOptions || {};
|
|
122
|
+
routerOptions.tagName = module;
|
|
123
|
+
}
|
|
124
|
+
(0, core_1.saveClassMetadata)(core_1.CONTROLLER_KEY, {
|
|
125
|
+
prefix,
|
|
126
|
+
routerOptions,
|
|
127
|
+
curdOption,
|
|
128
|
+
module,
|
|
129
|
+
}, target);
|
|
130
|
+
// 追加CRUD路由
|
|
131
|
+
if (!_.isEmpty(curdOption === null || curdOption === void 0 ? void 0 : curdOption.api)) {
|
|
132
|
+
// 获取已存在的路由
|
|
133
|
+
const existingRoutes = (0, core_1.getClassMetadata)(core_1.WEB_ROUTER_KEY, target) || [];
|
|
134
|
+
const existingPaths = existingRoutes.map(route => route.path);
|
|
135
|
+
curdOption === null || curdOption === void 0 ? void 0 : curdOption.api.forEach(path => {
|
|
136
|
+
const routePath = `/${path}`;
|
|
137
|
+
// 检查路由是否已存在
|
|
138
|
+
if (!existingPaths.includes(routePath)) {
|
|
139
|
+
(0, core_1.attachClassMetadata)(core_1.WEB_ROUTER_KEY, {
|
|
140
|
+
path: routePath,
|
|
141
|
+
requestMethod: path == 'info' ? 'get' : 'post',
|
|
142
|
+
method: path,
|
|
143
|
+
summary: exports.apiDesc[path] || path,
|
|
144
|
+
description: '',
|
|
145
|
+
}, target);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (!_.isEmpty(curdOption === null || curdOption === void 0 ? void 0 : curdOption.serviceApis)) {
|
|
150
|
+
// 获取已存在的路由
|
|
151
|
+
const existingRoutes = (0, core_1.getClassMetadata)(core_1.WEB_ROUTER_KEY, target) || [];
|
|
152
|
+
const existingPaths = existingRoutes.map(route => route.path);
|
|
153
|
+
curdOption.serviceApis.forEach(api => {
|
|
154
|
+
const methodName = typeof api === 'string' ? api : api.method;
|
|
155
|
+
const routePath = `/${methodName}`;
|
|
156
|
+
// 检查路由是否已存在
|
|
157
|
+
if (!existingPaths.includes(routePath)) {
|
|
158
|
+
(0, core_1.attachClassMetadata)(core_1.WEB_ROUTER_KEY, {
|
|
159
|
+
path: routePath,
|
|
160
|
+
requestMethod: 'post',
|
|
161
|
+
method: methodName,
|
|
162
|
+
summary: typeof api === 'string' ? api : api.summary,
|
|
163
|
+
description: '',
|
|
164
|
+
}, target);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Request)(target);
|
|
169
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const HLW_CLS_EVENT_KEY = "decorator:hlw:cls:event";
|
|
2
|
+
/**
|
|
3
|
+
* 事件配置
|
|
4
|
+
*/
|
|
5
|
+
export interface HlwEventOptions {
|
|
6
|
+
/** 是否全局 */
|
|
7
|
+
isGlobal: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 事件
|
|
11
|
+
* @param options
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export declare function HlwEvent(options?: HlwEventOptions): ClassDecorator;
|
|
15
|
+
export declare const HLW_EVENT_KEY = "decorator:hlw:event";
|
|
16
|
+
/**
|
|
17
|
+
* 事件
|
|
18
|
+
* @param eventName
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export declare function Event(eventName?: string): MethodDecorator;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HLW_EVENT_KEY = exports.HLW_CLS_EVENT_KEY = void 0;
|
|
4
|
+
exports.HlwEvent = HlwEvent;
|
|
5
|
+
exports.Event = Event;
|
|
6
|
+
const core_1 = require("@midwayjs/core");
|
|
7
|
+
const core_2 = require("@midwayjs/core");
|
|
8
|
+
exports.HLW_CLS_EVENT_KEY = 'decorator:hlw:cls:event';
|
|
9
|
+
/**
|
|
10
|
+
* 事件
|
|
11
|
+
* @param options
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
function HlwEvent(options = {}) {
|
|
15
|
+
return (target) => {
|
|
16
|
+
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
|
17
|
+
(0, core_1.saveModule)(exports.HLW_CLS_EVENT_KEY, target);
|
|
18
|
+
// 保存一些元数据信息,任意你希望存的东西
|
|
19
|
+
(0, core_1.saveClassMetadata)(exports.HLW_CLS_EVENT_KEY, options, target);
|
|
20
|
+
// 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx
|
|
21
|
+
(0, core_2.Scope)(core_2.ScopeEnum.Singleton)(target);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
exports.HLW_EVENT_KEY = 'decorator:hlw:event';
|
|
25
|
+
/**
|
|
26
|
+
* 事件
|
|
27
|
+
* @param eventName
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
function Event(eventName) {
|
|
31
|
+
return (target, propertyKey, descriptor) => {
|
|
32
|
+
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
|
33
|
+
(0, core_1.attachClassMetadata)(exports.HLW_EVENT_KEY, {
|
|
34
|
+
eventName,
|
|
35
|
+
propertyKey,
|
|
36
|
+
descriptor,
|
|
37
|
+
}, target);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MidwayCache } from '@midwayjs/cache-manager';
|
|
2
|
+
import { MidwayDecoratorService } from '@midwayjs/core';
|
|
3
|
+
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
|
4
|
+
import { HlwUrlTagData } from '../tag/data';
|
|
5
|
+
/**
|
|
6
|
+
* 装饰器
|
|
7
|
+
*/
|
|
8
|
+
export declare class HlwDecorator {
|
|
9
|
+
typeORMDataSourceManager: TypeORMDataSourceManager;
|
|
10
|
+
decoratorService: MidwayDecoratorService;
|
|
11
|
+
midwayCache: MidwayCache;
|
|
12
|
+
hlwUrlTagData: HlwUrlTagData;
|
|
13
|
+
init(): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* 缓存
|
|
16
|
+
*/
|
|
17
|
+
cache(): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* 事务
|
|
20
|
+
*/
|
|
21
|
+
transaction(): Promise<void>;
|
|
22
|
+
}
|