midway-fatcms 0.0.1-beta.26 → 0.0.1-beta.29

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.
Files changed (188) hide show
  1. package/dist/config/config.default.js +6 -4
  2. package/dist/controller/home.controller.js +2 -1
  3. package/dist/controller/manage/CrudStandardDesignApi.js +8 -8
  4. package/dist/controller/render/AppRenderController.js +2 -1
  5. package/dist/middleware/global.middleware.js +22 -9
  6. package/dist/models/AsyncTaskModel.d.ts +2 -0
  7. package/dist/models/AsyncTaskModel.js +3 -1
  8. package/dist/models/RedisKeys.d.ts +2 -0
  9. package/dist/models/RedisKeys.js +2 -0
  10. package/dist/models/bizmodels.d.ts +1 -0
  11. package/dist/service/UserSessionService.d.ts +21 -0
  12. package/dist/service/UserSessionService.js +71 -1
  13. package/dist/service/WorkbenchService.d.ts +32 -0
  14. package/dist/service/WorkbenchService.js +54 -11
  15. package/dist/service/anyapi/AnyApiSandboxService.js +12 -12
  16. package/dist/service/asyncTask/AsyncTaskRunnerService.d.ts +14 -1
  17. package/dist/service/asyncTask/AsyncTaskRunnerService.js +70 -25
  18. package/dist/service/asyncTask/handler/ExportExcelAsyncTaskHandler.js +1 -1
  19. package/dist/service/asyncTask/handler/ExportExcelByInnerHttpHandler.js +11 -2
  20. package/dist/service/base/RedisCacheService.d.ts +7 -0
  21. package/dist/service/base/RedisCacheService.js +7 -0
  22. package/dist/service/crudstd/CrudStdService.d.ts +0 -6
  23. package/dist/service/crudstd/CrudStdService.js +13 -33
  24. package/package.json +1 -5
  25. package/src/config/config.default.ts +0 -207
  26. package/src/config/seed/aeskey.txt +0 -1
  27. package/src/config/utils.ts +0 -22
  28. package/src/configuration.ts +0 -109
  29. package/src/controller/base/BaseApiController.ts +0 -170
  30. package/src/controller/gateway/AnyApiGatewayController.ts +0 -33
  31. package/src/controller/gateway/AsyncTaskController.ts +0 -157
  32. package/src/controller/gateway/CrudMtdGatewayController.ts +0 -111
  33. package/src/controller/gateway/CrudStdGatewayController.ts +0 -101
  34. package/src/controller/gateway/DocGatewayController.ts +0 -173
  35. package/src/controller/gateway/FileController.ts +0 -109
  36. package/src/controller/gateway/ProxyApiGatewayController.ts +0 -47
  37. package/src/controller/gateway/PublicApiController.ts +0 -142
  38. package/src/controller/gateway/StaticController.ts +0 -298
  39. package/src/controller/helpers.controller.ts +0 -161
  40. package/src/controller/home.controller.ts +0 -64
  41. package/src/controller/manage/AnyApiMangeApi.ts +0 -66
  42. package/src/controller/manage/AppLogMangeApi.ts +0 -53
  43. package/src/controller/manage/AppMangeApi.ts +0 -53
  44. package/src/controller/manage/AppPageMangeApi.ts +0 -52
  45. package/src/controller/manage/AppSchemaHistoryApi.ts +0 -49
  46. package/src/controller/manage/CrudMethodsMangeApi.ts +0 -49
  47. package/src/controller/manage/CrudStandardDesignApi.ts +0 -346
  48. package/src/controller/manage/DataDictManageApi.ts +0 -78
  49. package/src/controller/manage/DeployManageApi.ts +0 -175
  50. package/src/controller/manage/DocLibManageApi.ts +0 -69
  51. package/src/controller/manage/DocManageApi.ts +0 -99
  52. package/src/controller/manage/FileManageApi.ts +0 -45
  53. package/src/controller/manage/LowCodeTplManageApi.ts +0 -52
  54. package/src/controller/manage/MenuManageApi.ts +0 -58
  55. package/src/controller/manage/ProxyApiMangeApi.ts +0 -52
  56. package/src/controller/manage/SuperAdminManageApi.ts +0 -139
  57. package/src/controller/manage/SysConfigMangeApi.ts +0 -95
  58. package/src/controller/manage/SystemInfoManageApi.ts +0 -53
  59. package/src/controller/manage/UserAccountManageApi.ts +0 -94
  60. package/src/controller/manage/WorkbenchMangeApi.ts +0 -72
  61. package/src/controller/myinfo/AuthController.ts +0 -108
  62. package/src/controller/myinfo/MyInfoController.ts +0 -32
  63. package/src/controller/render/AppRenderController.ts +0 -79
  64. package/src/controller/test.controller.ts +0 -37
  65. package/src/filter/default.filter.ts +0 -13
  66. package/src/filter/notfound.filter.ts +0 -10
  67. package/src/index.ts +0 -106
  68. package/src/interface.ts +0 -31
  69. package/src/libs/crud-pro/CrudPro.ts +0 -165
  70. package/src/libs/crud-pro/defaultConfigs.ts +0 -15
  71. package/src/libs/crud-pro/exceptions.ts +0 -124
  72. package/src/libs/crud-pro/interfaces.ts +0 -190
  73. package/src/libs/crud-pro/models/ExecuteContext.ts +0 -120
  74. package/src/libs/crud-pro/models/ExecuteContextFunc.ts +0 -96
  75. package/src/libs/crud-pro/models/FuncContext.ts +0 -21
  76. package/src/libs/crud-pro/models/RequestCfgModel.ts +0 -141
  77. package/src/libs/crud-pro/models/RequestModel.ts +0 -141
  78. package/src/libs/crud-pro/models/ResModel.ts +0 -19
  79. package/src/libs/crud-pro/models/ServiceHub.ts +0 -32
  80. package/src/libs/crud-pro/models/SqlCfgModel.ts +0 -52
  81. package/src/libs/crud-pro/models/SqlSegArg.ts +0 -13
  82. package/src/libs/crud-pro/models/Transaction.ts +0 -73
  83. package/src/libs/crud-pro/models/TransactionMySQL.ts +0 -79
  84. package/src/libs/crud-pro/models/TransactionPostgres.ts +0 -91
  85. package/src/libs/crud-pro/models/TransactionSqlServer.ts +0 -102
  86. package/src/libs/crud-pro/models/keys.ts +0 -159
  87. package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +0 -83
  88. package/src/libs/crud-pro/services/CrudProExecuteFuncService.ts +0 -128
  89. package/src/libs/crud-pro/services/CrudProExecuteSqlService.ts +0 -262
  90. package/src/libs/crud-pro/services/CrudProFieldUpdateService.ts +0 -60
  91. package/src/libs/crud-pro/services/CrudProFieldValidateService.ts +0 -180
  92. package/src/libs/crud-pro/services/CrudProGenSqlCondition.ts +0 -354
  93. package/src/libs/crud-pro/services/CrudProGenSqlService.ts +0 -185
  94. package/src/libs/crud-pro/services/CrudProOriginToExecuteSql.ts +0 -393
  95. package/src/libs/crud-pro/services/CrudProServiceBase.ts +0 -94
  96. package/src/libs/crud-pro/services/CrudProTableMetaService.ts +0 -86
  97. package/src/libs/crud-pro/services/CurdProServiceHub.ts +0 -92
  98. package/src/libs/crud-pro/sql.txt +0 -120
  99. package/src/libs/crud-pro/utils/CompareUtils.ts +0 -23
  100. package/src/libs/crud-pro/utils/DatabaseName.ts +0 -60
  101. package/src/libs/crud-pro/utils/DateTimeUtils.ts +0 -20
  102. package/src/libs/crud-pro/utils/MemoryRefreshCache.ts +0 -64
  103. package/src/libs/crud-pro/utils/MessageParseUtils.ts +0 -33
  104. package/src/libs/crud-pro/utils/MixinUtils.ts +0 -285
  105. package/src/libs/crud-pro/utils/ModelUtils.ts +0 -55
  106. package/src/libs/crud-pro/utils/MultiKeyMap.ts +0 -72
  107. package/src/libs/crud-pro/utils/SqlFuncUtils.ts +0 -29
  108. package/src/libs/crud-pro/utils/TypeUtils.ts +0 -188
  109. package/src/libs/crud-pro/utils/ValidateUtils.ts +0 -165
  110. package/src/libs/crud-pro/utils/pool/MySQLUtils.ts +0 -20
  111. package/src/libs/crud-pro/utils/pool/PostgresUtils.ts +0 -22
  112. package/src/libs/crud-pro/utils/pool/SqlServerUtils.ts +0 -22
  113. package/src/libs/crud-pro/utils/sqlConvert/convertColumnName.ts +0 -26
  114. package/src/libs/crud-pro/utils/sqlConvert/convertMix.ts +0 -26
  115. package/src/libs/crud-pro/utils/sqlConvert/convertMsSql.ts +0 -11
  116. package/src/libs/crud-pro/utils/sqlConvert/convertPgSql.ts +0 -11
  117. package/src/libs/crud-pro/utils/sqlConvert/convertPgType.ts +0 -129
  118. package/src/libs/global-config/global-config.ts +0 -78
  119. package/src/libs/utils/common-dto.ts +0 -52
  120. package/src/libs/utils/crypto-utils.ts +0 -50
  121. package/src/libs/utils/errorToString.ts +0 -61
  122. package/src/libs/utils/fatcms-request.ts +0 -103
  123. package/src/libs/utils/functions.ts +0 -73
  124. package/src/libs/utils/ordernum-utils.ts +0 -14
  125. package/src/libs/utils/parseConfig.ts +0 -54
  126. package/src/libs/utils/parseCreateSql.ts +0 -91
  127. package/src/libs/utils/render-utils.ts +0 -184
  128. package/src/middleware/forbidden.middleware.ts +0 -52
  129. package/src/middleware/global.middleware.ts +0 -280
  130. package/src/middleware/permission.middleware.ts +0 -80
  131. package/src/middleware/tx.middleware.ts +0 -30
  132. package/src/models/AsyncTaskModel.ts +0 -82
  133. package/src/models/RedisKeys.ts +0 -13
  134. package/src/models/SystemEntities.ts +0 -115
  135. package/src/models/SystemPerm.ts +0 -105
  136. package/src/models/SystemTables.ts +0 -27
  137. package/src/models/bizmodels.ts +0 -120
  138. package/src/models/contextLogger.ts +0 -132
  139. package/src/models/devops.ts +0 -17
  140. package/src/models/userSession.ts +0 -216
  141. package/src/schedule/anonymousContext.ts +0 -73
  142. package/src/schedule/index.ts +0 -12
  143. package/src/schedule/runSchedule.ts +0 -82
  144. package/src/schedule/scheduleNames.ts +0 -21
  145. package/src/service/AuthService.ts +0 -272
  146. package/src/service/EnumInfoService.ts +0 -130
  147. package/src/service/FileCenterService.ts +0 -395
  148. package/src/service/SysConfigService.ts +0 -37
  149. package/src/service/UserAccountService.ts +0 -107
  150. package/src/service/UserSessionService.ts +0 -78
  151. package/src/service/VisitStatService.ts +0 -166
  152. package/src/service/WorkbenchService.ts +0 -165
  153. package/src/service/anyapi/AnyApiSandboxService.ts +0 -121
  154. package/src/service/anyapi/AnyApiService.ts +0 -186
  155. package/src/service/asyncTask/AsyncTaskRunnerService.ts +0 -266
  156. package/src/service/asyncTask/AsyncTaskService.ts +0 -21
  157. package/src/service/asyncTask/handler/ExcelInfoModel.ts +0 -11
  158. package/src/service/asyncTask/handler/ExportExcelAsyncTaskHandler.ts +0 -245
  159. package/src/service/asyncTask/handler/ExportExcelByInnerHttpHandler.ts +0 -147
  160. package/src/service/asyncTask/handler/ExportExcelByStdCrudHandler.ts +0 -138
  161. package/src/service/base/ApiBaseService.ts +0 -42
  162. package/src/service/base/ApiRateLimiter.ts +0 -59
  163. package/src/service/base/BaseService.ts +0 -100
  164. package/src/service/base/RedisCacheService.ts +0 -38
  165. package/src/service/crudstd/CrudStdActionService.ts +0 -27
  166. package/src/service/crudstd/CrudStdConstant.ts +0 -62
  167. package/src/service/crudstd/CrudStdRelationService.ts +0 -78
  168. package/src/service/crudstd/CrudStdService.ts +0 -283
  169. package/src/service/curd/CrudProQuick.ts +0 -131
  170. package/src/service/curd/CurdMixByAccountService.ts +0 -90
  171. package/src/service/curd/CurdMixByDictService.ts +0 -114
  172. package/src/service/curd/CurdMixByLinkToCustomService.ts +0 -219
  173. package/src/service/curd/CurdMixBySysConfigService.ts +0 -78
  174. package/src/service/curd/CurdMixByWorkbenchService.ts +0 -71
  175. package/src/service/curd/CurdMixService.ts +0 -97
  176. package/src/service/curd/CurdMixUtils.ts +0 -311
  177. package/src/service/curd/CurdProService.ts +0 -229
  178. package/src/service/curd/fixCfgModel.ts +0 -139
  179. package/src/service/proxyapi/ProxyApiLoadService.ts +0 -174
  180. package/src/service/proxyapi/ProxyApiService.ts +0 -262
  181. package/src/service/proxyapi/ProxyApiUtils.ts +0 -32
  182. package/src/service/proxyapi/RouteHandler.ts +0 -8
  183. package/src/service/proxyapi/RouteTrie.ts +0 -74
  184. package/src/service/proxyapi/WeightedRandom.ts +0 -37
  185. package/src/service/proxyapi/WeightedRoundRobin.ts +0 -44
  186. package/src/views/404_app.html +0 -31
  187. package/src/views/404_workbench.html +0 -34
  188. package/src/views/static/favicon.ico +0 -0
@@ -1,245 +0,0 @@
1
- import { IMidwayKoaContext } from '@midwayjs/koa';
2
- import { ISysAsyncTaskHandler, SysAsyncTaskContext, SysAsyncTaskStatus } from '@/models/AsyncTaskModel';
3
- import { ANONYMOUS_CONTEXT } from '@/schedule';
4
- import { IExcelHeaderInfo, IExcelAsyncTaskHandler } from './ExcelInfoModel';
5
- import { SysAsyncFileFormat } from '@/models/AsyncTaskModel';
6
- import { ExportExcelByStdCrudHandler } from './ExportExcelByStdCrudHandler';
7
- import { ExportExcelByInnerHttpHandler } from './ExportExcelByInnerHttpHandler';
8
- import { IStdCrudExportInputParams, StdCrudExportInputParamsAppType } from '@/models/bizmodels';
9
-
10
- import * as _ from 'lodash';
11
- import * as fastcsv from 'fast-csv';
12
- import * as fs from 'node:fs';
13
- import * as fs2 from 'node:fs/promises';
14
- import * as path from 'node:path';
15
- import { parseJsonObject } from '@/libs/utils/functions';
16
- import { UserSessionInfo } from '@/models/userSession';
17
- import { errorToString } from '@/libs/utils/errorToString';
18
-
19
- const indexRef = { current: 0 };
20
-
21
- async function createFileFolder(distFileFolder: string) {
22
- try {
23
- await fs2.access(distFileFolder);
24
- } catch (e) {
25
- await fs2.mkdir(distFileFolder, { recursive: true });
26
- }
27
- }
28
-
29
- function getExcelAsyncTaskHandler(asyncTaskContext: SysAsyncTaskContext, ctx: IMidwayKoaContext): IExcelAsyncTaskHandler {
30
- const inputParams: IStdCrudExportInputParams = parseJsonObject(asyncTaskContext?.task?.input_params);
31
- if (inputParams && inputParams.appType === StdCrudExportInputParamsAppType.STD_CRUD) {
32
- return new ExportExcelByStdCrudHandler(asyncTaskContext, inputParams, ctx);
33
- }
34
- if (inputParams && inputParams.appType === StdCrudExportInputParamsAppType.INNER_HTTP) {
35
- return new ExportExcelByInnerHttpHandler(asyncTaskContext, inputParams, ctx);
36
- }
37
- throw new Error('[getExcelAsyncTaskHandler]appType不支持;appType = ' + inputParams.appType);
38
- }
39
-
40
- /**
41
- * 多实例,每个任务一个实例。
42
- * 导出EXCEL异步任务:
43
- */
44
- class ExportExcelAsyncTask {
45
- protected asyncTaskContext: SysAsyncTaskContext;
46
- protected ctx: IMidwayKoaContext;
47
- protected headerColumns: IExcelHeaderInfo[];
48
- protected excelAsyncTaskHandler: IExcelAsyncTaskHandler;
49
-
50
- constructor(asyncTaskContext: SysAsyncTaskContext, ctx: IMidwayKoaContext) {
51
- this.asyncTaskContext = asyncTaskContext;
52
- this.ctx = ctx;
53
- this.excelAsyncTaskHandler = getExcelAsyncTaskHandler(asyncTaskContext, ctx);
54
- }
55
-
56
- private getDistFileFolder() {
57
- const app = ANONYMOUS_CONTEXT.getApp();
58
- const config = app.getConfig();
59
- const fatcmsExportExcelTmpFilePath = config.fatcmsExportExcelTmpFilePath;
60
- if (!fatcmsExportExcelTmpFilePath) {
61
- return path.join(__dirname, '../../tmp');
62
- }
63
- return fatcmsExportExcelTmpFilePath;
64
- }
65
-
66
- async executeWithContext() {
67
- const taskElement = this.asyncTaskContext.task;
68
- const taskId = _.get(this.asyncTaskContext, 'task.id');
69
-
70
- this.ctx.logger.info('[ExportExcelAsyncTask] executeWithContext start, taskId=' + taskId);
71
-
72
- const pageSize = await this.excelAsyncTaskHandler.getPageSize();
73
-
74
- const fileName = await this.getExcelFileName(); // 文件名
75
- const distFileFolder = this.getDistFileFolder();
76
- await createFileFolder(distFileFolder);
77
- const diskFilePath = path.resolve(distFileFolder, fileName); // 磁盘文件路径
78
-
79
- const totalCount = await this.getTotalCount();
80
-
81
- await this.asyncTaskContext.updateTaskStatus({
82
- output_total_records: totalCount,
83
- output_file_format: SysAsyncFileFormat.CSV,
84
- output_file_path: diskFilePath,
85
- });
86
-
87
- this.headerColumns = await this.getExcelHeader();
88
-
89
- const ws = fs.createWriteStream(diskFilePath, {
90
- encoding: 'utf-8',
91
- });
92
-
93
- const csvStream = fastcsv.format({ writeHeaders: false, headers: false, delimiter: ',' });
94
- csvStream.pipe(ws);
95
- csvStream.write('\ufeff'); // 写入BOM头,解决中文乱码问题
96
-
97
- csvStream.write(this.formatToCsvHeader(this.headerColumns));
98
-
99
- let processed_records = 0;
100
-
101
- try {
102
- if (typeof totalCount === 'number' && totalCount > 0) {
103
- const batchCount = Math.ceil(totalCount / pageSize);
104
- for (let i = 0; i < batchCount; i++) {
105
- const num = await this.writeExcelBatchData(csvStream, i + 1, pageSize);
106
- processed_records = processed_records + num;
107
- const progress = Math.round((processed_records / totalCount) * 100);
108
- await this.asyncTaskContext.updateTaskStatus({
109
- processed_records,
110
- progress,
111
- });
112
- }
113
- }
114
-
115
- taskElement.task_status = SysAsyncTaskStatus.SUCCEEDED;
116
- } catch (error) {
117
- taskElement.task_status = SysAsyncTaskStatus.FAILED;
118
- taskElement.error_message = errorToString(error);
119
- const progress = Math.round((processed_records / totalCount) * 100);
120
- await this.asyncTaskContext.updateTaskStatus({
121
- processed_records,
122
- progress,
123
- task_status: taskElement.task_status,
124
- error_message: taskElement.error_message,
125
- });
126
- }
127
-
128
- csvStream.end();
129
- ws.on('finish', async () => {
130
- this.ctx.logger.info('[ExportExcelAsyncTask] executeWithContext finish, taskId=' + taskId);
131
- const fsStat = await fs2.stat(diskFilePath);
132
- const output_file_size = fsStat.size;
133
- this.asyncTaskContext.updateTaskStatus({
134
- output_file_size,
135
- });
136
- });
137
- this.ctx.logger.info('[ExportExcelAsyncTask] executeWithContext end, taskId=' + taskId);
138
- }
139
-
140
- private formatToCsvHeader(headerColumns: IExcelHeaderInfo[]): string[] {
141
- return headerColumns.map(item => {
142
- return item.title;
143
- });
144
- }
145
-
146
- private async writeExcelBatchData(csvStream: fastcsv.CsvFormatterStream<any, any>, pageNo: number, pageSize: number): Promise<number> {
147
- const dataList: any[] = await this.getExcelDataList(pageNo, pageSize);
148
- for (let i = 0; i < dataList.length; i++) {
149
- const csvRow = this.formatToCsvRow(dataList[i]);
150
- csvStream.write(csvRow);
151
- }
152
- return dataList.length;
153
- }
154
-
155
- private formatToCsvRow(rowObj: any): string[] {
156
- return this.headerColumns.map(obj => {
157
- const dataIndex = obj.dataIndex;
158
- const value = _.get(rowObj, dataIndex);
159
- if (typeof value === 'undefined' || value === null) {
160
- return '';
161
- }
162
- if (typeof value !== 'string') {
163
- // boolean , number
164
- return '' + value;
165
- }
166
- return value;
167
- });
168
- }
169
-
170
- /**
171
- * 获取文件名
172
- * @returns 文件名
173
- */
174
- private async getExcelFileName() {
175
- indexRef.current = indexRef.current + 1;
176
- if (indexRef.current > 100000) {
177
- indexRef.current = 1;
178
- }
179
- const num = `${indexRef.current}`.padStart(10);
180
- return `export_${Date.now()}_${num}.csv`;
181
- }
182
-
183
- /**
184
- * 获取Excel的表头
185
- * @returns
186
- */
187
- private async getExcelHeader(): Promise<IExcelHeaderInfo[]> {
188
- return this.excelAsyncTaskHandler.getExcelHeader();
189
- // const inputParams = parseJsonObject(this.asyncTaskContext?.task?.input_params);
190
- // if (inputParams && inputParams.appType === 'STD_CRUD') {
191
- // // const appCode = inputParams.appCode;
192
- // console.log(this)
193
- // }
194
- // return [
195
- // { title: 'ID', dataIndex: 'id' },
196
- // { title: '姓名', dataIndex: 'name' },
197
- // { title: '邮箱', dataIndex: 'email' }
198
- // ]
199
- }
200
-
201
- /**
202
- * 获取文件总数
203
- */
204
- private async getTotalCount(): Promise<number> {
205
- const totalCount = await this.excelAsyncTaskHandler.getTotalCount();
206
- // 最多导出10万条数据
207
- return Math.min(totalCount, 10 * 10000);
208
- }
209
-
210
- /**
211
- * 获取数据内容数组
212
- * @param pageNo 当前页号,从1开始
213
- * @param pageSize 每页获取的数量。
214
- */
215
- private async getExcelDataList(pageNo: number, pageSize: number): Promise<any[]> {
216
- return this.excelAsyncTaskHandler.getExcelDataList(pageNo, pageSize);
217
- // const dataList = [];
218
- // for (var i = 0; i < pageSize; i++) {
219
- // dataList.push({ id: i, name: 'nmae' + i, email: 'www.dss@dd.dom' })
220
- // }
221
- // return dataList;
222
- }
223
- }
224
-
225
- /**
226
- * 单例
227
- */
228
- export class ExportExcelAsyncTaskHandler implements ISysAsyncTaskHandler {
229
- public async execute(asyncTaskContext: SysAsyncTaskContext): Promise<any> {
230
- const taskId = _.get(asyncTaskContext, 'task.id');
231
-
232
- console.log('[ExportExcelAsyncTaskHandler] execute, taskId=>' + taskId);
233
-
234
- const res = await ANONYMOUS_CONTEXT.runServiceAtAnonymousContext(async (ctx: IMidwayKoaContext) => {
235
- const createdUserSession = parseJsonObject(asyncTaskContext?.task?.created_user_session);
236
- ctx.userSession = new UserSessionInfo(createdUserSession, false);
237
- const task = new ExportExcelAsyncTask(asyncTaskContext, ctx);
238
- return task.executeWithContext();
239
- });
240
- if (res.error) {
241
- throw res.error;
242
- }
243
- return res.result;
244
- }
245
- }
@@ -1,147 +0,0 @@
1
- import * as _ from 'lodash';
2
- import axios from 'axios';
3
- import { IMidwayKoaContext } from '@midwayjs/koa';
4
- import { IExcelAsyncTaskHandler, IExcelHeaderInfo } from './ExcelInfoModel';
5
- import { SysAsyncTaskContext } from '@/models/AsyncTaskModel';
6
- import { IStdCrudExportInputParams, ITableProQueryParams } from '@/models/bizmodels';
7
- import { IRequestModel } from '@/libs/crud-pro/interfaces';
8
- import { ANONYMOUS_CONTEXT } from '@/schedule';
9
-
10
- function pickData(a: any, key: string) {
11
- if (a && typeof a.getResModel === 'function') {
12
- const s = a.getResModel();
13
- return s[key];
14
- }
15
- if (!a) {
16
- throw 'ExportExcelByInnerHttpHandler返回数据不存在';
17
- }
18
- return _.get(a, `data.${key}`);
19
- }
20
-
21
- /**
22
- * 多实例,每个任务一个实例。
23
- * 导出Excel。请求任务级别的。
24
- * 使用内部HTTP接口导出
25
- */
26
- class ExportExcelByInnerHttpHandler implements IExcelAsyncTaskHandler {
27
- protected asyncTaskContext: SysAsyncTaskContext;
28
- protected ctx: IMidwayKoaContext;
29
- protected inputParams: IStdCrudExportInputParams;
30
- protected appCode: string;
31
- public constructor(asyncTaskContext: SysAsyncTaskContext, inputParams: IStdCrudExportInputParams, ctx: IMidwayKoaContext) {
32
- this.ctx = ctx;
33
- this.inputParams = inputParams;
34
- this.asyncTaskContext = asyncTaskContext;
35
- this.appCode = this.inputParams.appCode;
36
- }
37
-
38
- async getPageSize(): Promise<number> {
39
- if (typeof this.inputParams.pageSize === 'number') {
40
- return Math.min(this.inputParams.pageSize, 1000);
41
- }
42
- return 1000;
43
- }
44
-
45
- /**
46
- * 获取Excel表的表头
47
- * @returns 表头信息
48
- */
49
- async getExcelHeader(): Promise<IExcelHeaderInfo[]> {
50
- const columns = this.inputParams.columns;
51
- if (!columns) {
52
- throw new Error('导出Excel的表头信息不存在');
53
- }
54
- if (!Array.isArray(columns) || columns.length === 0) {
55
- throw new Error('导出Excel的表头信息不是数组');
56
- }
57
- return columns
58
- .map(cc => {
59
- let { title, dataIndex } = cc;
60
- if (!dataIndex) {
61
- return null;
62
- }
63
- if (!title) {
64
- title = '字段_' + dataIndex;
65
- }
66
- return { title, dataIndex };
67
- })
68
- .filter(Boolean);
69
- }
70
-
71
- private async sendRequest(params: any): Promise<any> {
72
- const port = ANONYMOUS_CONTEXT.getApp().getConfig().koa.port;
73
- let queryApi = this.inputParams.innerHttpUrl; // 以斜杠开头
74
- if (!queryApi.startsWith('/')) {
75
- queryApi = '/' + queryApi;
76
- }
77
-
78
- params.runByExportExcel = true;
79
-
80
- const response = await axios({
81
- method: 'POST',
82
- url: `http://127.0.0.1:${port}${queryApi}`,
83
- data: params,
84
- headers: this.inputParams.headers || {},
85
- responseType: 'json',
86
- validateStatus: () => true,
87
- });
88
- if (response.status !== 200) {
89
- throw new Error('导出Excel请求错误失败,状态码:' + response.status);
90
- }
91
- return response.data;
92
- }
93
-
94
- /**
95
- * 获取要导出的数量总数
96
- * @returns 数据总量
97
- */
98
- async getTotalCount(): Promise<number> {
99
- const selectedRowKeys: string[] | number[] = this.inputParams?.selectedRowKeys || [];
100
- if (Array.isArray(selectedRowKeys) && selectedRowKeys.length > 0) {
101
- return selectedRowKeys.length;
102
- }
103
-
104
- const lastQueryParams: ITableProQueryParams = this.inputParams?.lastQueryParams;
105
- const res = await this.sendRequest({
106
- condition: lastQueryParams?.condition || {},
107
- orderBy: lastQueryParams?.orderBy,
108
- pageSize: 1,
109
- pageNo: 1,
110
- });
111
- return pickData(res, 'total_count');
112
- }
113
-
114
- /**
115
- * 获取要导出Excel的数据的内容
116
- * @param pageNo 页号,从1开始
117
- * @param pageSize 每页数量
118
- * @returns
119
- */
120
- async getExcelDataList(pageNo: number, pageSize: number): Promise<any[]> {
121
- const selectedRowKeys: string[] | number[] = this.inputParams?.selectedRowKeys || [];
122
- const lastQueryParams: ITableProQueryParams = this.inputParams?.lastQueryParams;
123
- const primaryKey: string = this.inputParams.primaryKey || 'id';
124
-
125
- const params: IRequestModel = {
126
- condition: lastQueryParams?.condition || {},
127
- pageSize: pageSize,
128
- pageNo: pageNo,
129
- orderBy: lastQueryParams?.orderBy,
130
- };
131
-
132
- if (Array.isArray(selectedRowKeys) && selectedRowKeys.length > 0) {
133
- params.condition[primaryKey] = {
134
- $in: selectedRowKeys,
135
- };
136
- }
137
- const res = await this.sendRequest({
138
- condition: lastQueryParams?.condition || {},
139
- orderBy: lastQueryParams?.orderBy,
140
- pageSize: pageSize,
141
- pageNo: pageNo,
142
- });
143
- return pickData(res, 'rows');
144
- }
145
- }
146
-
147
- export { ExportExcelByInnerHttpHandler };
@@ -1,138 +0,0 @@
1
- import * as _ from 'lodash';
2
- import { IMidwayKoaContext } from '@midwayjs/koa';
3
- import { IExcelAsyncTaskHandler, IExcelHeaderInfo } from './ExcelInfoModel';
4
- import { SysAsyncTaskContext } from '@/models/AsyncTaskModel';
5
- import { CrudStdService, SPECIAL_SETTING_KEY } from '@/service/crudstd/CrudStdService';
6
- import { ICrudStdAppInfo, IStdCrudExportInputParams, ITableProQueryParams } from '@/models/bizmodels';
7
- import { IRequestModel } from '@/libs/crud-pro/interfaces';
8
- import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
9
- import { ExecuteContext } from '@/libs/crud-pro/models/ExecuteContext';
10
-
11
- function pickData(a: any, key: string) {
12
- if (a && typeof a.getResModel === 'function') {
13
- const s = a.getResModel();
14
- return s[key];
15
- }
16
- if (!a) {
17
- throw 'executeStdQuery返回数据不存在';
18
- }
19
- return _.get(a, `data.${key}`);
20
- }
21
-
22
- /**
23
- * 多实例,每个任务一个实例。
24
- * 导出Excel。请求任务级别的。
25
- * 使用标准CtdCrudApp导出
26
- */
27
- class ExportExcelByStdCrudHandler implements IExcelAsyncTaskHandler {
28
- protected asyncTaskContext: SysAsyncTaskContext;
29
- protected ctx: IMidwayKoaContext;
30
- protected inputParams: IStdCrudExportInputParams;
31
- protected appCode: string;
32
- protected crudStdAppInfo: ICrudStdAppInfo;
33
- public constructor(asyncTaskContext: SysAsyncTaskContext, inputParams: IStdCrudExportInputParams, ctx: IMidwayKoaContext) {
34
- this.ctx = ctx;
35
- this.inputParams = inputParams;
36
- this.asyncTaskContext = asyncTaskContext;
37
- this.appCode = this.inputParams.appCode;
38
- }
39
-
40
- async getPageSize(): Promise<number> {
41
- if (typeof this.inputParams.pageSize === 'number') {
42
- return Math.min(this.inputParams.pageSize, 1000);
43
- }
44
- return 1000;
45
- }
46
-
47
- /**
48
- * 获取应用信息
49
- * @returns CrudStdService服务实例
50
- */
51
- private async getCrudStdService(): Promise<CrudStdService> {
52
- return await this.ctx.requestContext.getAsync(CrudStdService);
53
- }
54
-
55
- /**
56
- * 获取应用的配置信息
57
- * @returns 应用的配置信息
58
- */
59
- private async getCrudStdAppInfo(): Promise<ICrudStdAppInfo> {
60
- if (!this.crudStdAppInfo) {
61
- const appCode = this.inputParams.appCode;
62
- if (!appCode) {
63
- throw new Error('inputParams appCode is required');
64
- }
65
- const service = await this.getCrudStdService();
66
- this.crudStdAppInfo = await service.getParsedCrudStdAppInfo(appCode);
67
- }
68
- return this.crudStdAppInfo;
69
- }
70
-
71
- /**
72
- * 获取Excel表的表头
73
- * @returns 表头信息
74
- */
75
- async getExcelHeader(): Promise<IExcelHeaderInfo[]> {
76
- const crudStdAppInfo = await this.getCrudStdAppInfo();
77
- const stdCrudCfgObj = crudStdAppInfo.stdCrudCfgObj;
78
- const tableColumns = stdCrudCfgObj.tableColumns || [];
79
- return tableColumns.map(cc => {
80
- return cc;
81
- });
82
- }
83
-
84
- /**
85
- * 获取要导出的数量总数
86
- * @returns 数据总量
87
- */
88
- async getTotalCount(): Promise<number> {
89
- const selectedRowKeys: string[] | number[] = this.inputParams?.selectedRowKeys || [];
90
- if (Array.isArray(selectedRowKeys) && selectedRowKeys.length > 0) {
91
- return selectedRowKeys.length;
92
- }
93
-
94
- const lastQueryParams: ITableProQueryParams = this.inputParams?.lastQueryParams;
95
- const crudStdService = await this.getCrudStdService();
96
- const appCode = this.appCode;
97
- const params: IRequestModel = {
98
- condition: lastQueryParams?.condition || {},
99
- pageSize: 1,
100
- pageNo: 1,
101
- };
102
- if (!this.ctx.innerHeaders) {
103
- this.ctx.innerHeaders = {};
104
- }
105
- Object.assign(this.ctx.innerHeaders, this.inputParams.headers || {});
106
- const a: ExecuteContext = await crudStdService.executeStdQuery(appCode, SPECIAL_SETTING_KEY.QUERY_LIST, params, KeysOfSimpleSQL.SIMPLE_QUERY_PAGE);
107
- return pickData(a, 'total_count');
108
- }
109
-
110
- /**
111
- * 获取要导出Excel的数据的内容
112
- * @param pageNo 页号,从1开始
113
- * @param pageSize 每页数量
114
- * @returns
115
- */
116
- async getExcelDataList(pageNo: number, pageSize: number): Promise<any[]> {
117
- const selectedRowKeys: string[] | number[] = this.inputParams.selectedRowKeys || [];
118
- const primaryKey: string = this.inputParams.primaryKey || 'id';
119
- const lastQueryParams: ITableProQueryParams = this.inputParams.lastQueryParams;
120
- const crudStdService = await this.getCrudStdService();
121
- const appCode = this.appCode;
122
- const params: IRequestModel = {
123
- condition: lastQueryParams?.condition || {},
124
- pageSize: pageSize,
125
- pageNo: pageNo,
126
- orderBy: lastQueryParams?.orderBy,
127
- };
128
- if (Array.isArray(selectedRowKeys) && selectedRowKeys.length > 0) {
129
- params.condition[primaryKey] = {
130
- $in: selectedRowKeys,
131
- };
132
- }
133
- const a: ExecuteContext = await crudStdService.executeStdQuery(appCode, SPECIAL_SETTING_KEY.QUERY_LIST, params, KeysOfSimpleSQL.SIMPLE_QUERY_PAGE);
134
- return pickData(a, 'rows');
135
- }
136
- }
137
-
138
- export { ExportExcelByStdCrudHandler };
@@ -1,42 +0,0 @@
1
- import { Provide } from '@midwayjs/core';
2
- import { BaseService } from './BaseService';
3
- import { IApiBaseEntity } from '@/models/SystemEntities';
4
- import { BizException } from '@/models/devops';
5
- import { Exceptions } from '@/libs/crud-pro/exceptions';
6
- import { tryAcquireToken } from './ApiRateLimiter';
7
-
8
- export const API_BASE_TYPE = {
9
- PROXY_API: 'PROXY_API',
10
- ANY_API: 'ANY_API',
11
- CRUD_METHOD: 'CRUD_METHOD',
12
- };
13
-
14
- @Provide()
15
- export class ApiBaseService extends BaseService {
16
- /**
17
- * 前置校验
18
- * @param apiBaseType
19
- * @param entity
20
- */
21
- public async beforeCheckApiAccessibility(apiBaseType: string, entity: IApiBaseEntity) {
22
- // 权限校验
23
- const isPermSuccess = this.ctx.userSession.isAuthPass(entity.auth_type as any, entity.auth_config);
24
- if (!isPermSuccess) {
25
- throw new BizException('权限校验失败', Exceptions.NO_AUTH);
26
- }
27
- //服务降级
28
- if (entity.fallback_enable === 1) {
29
- throw new BizException(entity.fallback_message || '此服务暂时不支持访问', 'FALLBACK_ENABLE');
30
- }
31
-
32
- // 限流令牌桶
33
- if (entity.limit_enabled === 1) {
34
- const accountId = this.ctx.userSession.getSessionInfo().accountId;
35
- const apiId = apiBaseType + '_' + entity.id;
36
- const isAcquire = tryAcquireToken(apiId, accountId, entity.limit_max, entity.limit_rate);
37
- if (!isAcquire) {
38
- throw new BizException('频繁访问此接口,被限流', 'LIMIT_ENABLED');
39
- }
40
- }
41
- }
42
- }
@@ -1,59 +0,0 @@
1
- import { LRUCache } from 'lru-cache';
2
-
3
- // 定义 TokenBucket 类
4
- class TokenBucket {
5
- private capacity: number;
6
- private fillRate: number;
7
- private tokens: number;
8
- private lastRefillTime: number;
9
-
10
- constructor(capacity: number, fillRate: number) {
11
- this.capacity = capacity;
12
- this.fillRate = fillRate;
13
- this.tokens = capacity;
14
- this.lastRefillTime = Date.now();
15
- }
16
-
17
- private refill(): void {
18
- const now = Date.now();
19
- const elapsedTime = (now - this.lastRefillTime) / 1000;
20
- const newTokens = elapsedTime * this.fillRate;
21
- this.tokens = Math.min(this.capacity, this.tokens + newTokens);
22
- this.lastRefillTime = now;
23
- }
24
-
25
- public tryAcquire(tokensNeeded = 1): boolean {
26
- this.refill();
27
- if (this.tokens >= tokensNeeded) {
28
- this.tokens -= tokensNeeded;
29
- return true;
30
- }
31
- return false;
32
- }
33
- }
34
-
35
- // 定义一个全局的 Map 来存储不同接口和用户组合的令牌桶
36
- const apiUserBuckets = new LRUCache<string, TokenBucket>({
37
- max: 500,
38
- ttl: 1000 * 60 * 5,
39
- ttlAutopurge: true,
40
- });
41
-
42
- /**
43
- * 对接口ID , 每个用户ID 进行限流。
44
- * @param apiId 接口ID
45
- * @param userId 用户ID
46
- * @param limit_max 限流令牌桶最大值,最大容量
47
- * @param limit_rate 每秒产生的令牌数量
48
- * @return {Boolean} 返回token是否可以用。不能被处理就返回false
49
- */
50
- function tryAcquireToken(apiId: string, userId: string, limit_max: number, limit_rate: number): boolean {
51
- const key = `${apiId}-${userId}`;
52
- if (!apiUserBuckets.has(key)) {
53
- apiUserBuckets.set(key, new TokenBucket(limit_max, limit_rate));
54
- }
55
- const bucket = apiUserBuckets.get(key)!;
56
- return bucket.tryAcquire();
57
- }
58
-
59
- export { tryAcquireToken };