midway-fatcms 0.0.1-beta.25 → 0.0.1-beta.28

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 (171) hide show
  1. package/dist/controller/home.controller.js +2 -1
  2. package/dist/controller/render/AppRenderController.js +2 -1
  3. package/dist/models/bizmodels.d.ts +1 -0
  4. package/dist/service/asyncTask/handler/ExportExcelAsyncTaskHandler.js +1 -1
  5. package/dist/service/crudstd/CrudStdService.d.ts +0 -6
  6. package/dist/service/crudstd/CrudStdService.js +13 -33
  7. package/package.json +1 -5
  8. package/src/config/config.default.ts +0 -207
  9. package/src/config/seed/aeskey.txt +0 -1
  10. package/src/config/utils.ts +0 -22
  11. package/src/configuration.ts +0 -109
  12. package/src/controller/base/BaseApiController.ts +0 -170
  13. package/src/controller/gateway/AnyApiGatewayController.ts +0 -33
  14. package/src/controller/gateway/AsyncTaskController.ts +0 -157
  15. package/src/controller/gateway/CrudMtdGatewayController.ts +0 -111
  16. package/src/controller/gateway/CrudStdGatewayController.ts +0 -101
  17. package/src/controller/gateway/DocGatewayController.ts +0 -173
  18. package/src/controller/gateway/FileController.ts +0 -109
  19. package/src/controller/gateway/ProxyApiGatewayController.ts +0 -47
  20. package/src/controller/gateway/PublicApiController.ts +0 -142
  21. package/src/controller/gateway/StaticController.ts +0 -298
  22. package/src/controller/helpers.controller.ts +0 -161
  23. package/src/controller/home.controller.ts +0 -64
  24. package/src/controller/manage/AnyApiMangeApi.ts +0 -66
  25. package/src/controller/manage/AppLogMangeApi.ts +0 -53
  26. package/src/controller/manage/AppMangeApi.ts +0 -53
  27. package/src/controller/manage/AppPageMangeApi.ts +0 -52
  28. package/src/controller/manage/AppSchemaHistoryApi.ts +0 -49
  29. package/src/controller/manage/CrudMethodsMangeApi.ts +0 -49
  30. package/src/controller/manage/CrudStandardDesignApi.ts +0 -346
  31. package/src/controller/manage/DataDictManageApi.ts +0 -78
  32. package/src/controller/manage/DeployManageApi.ts +0 -175
  33. package/src/controller/manage/DocLibManageApi.ts +0 -69
  34. package/src/controller/manage/DocManageApi.ts +0 -99
  35. package/src/controller/manage/FileManageApi.ts +0 -45
  36. package/src/controller/manage/LowCodeTplManageApi.ts +0 -52
  37. package/src/controller/manage/MenuManageApi.ts +0 -58
  38. package/src/controller/manage/ProxyApiMangeApi.ts +0 -52
  39. package/src/controller/manage/SuperAdminManageApi.ts +0 -139
  40. package/src/controller/manage/SysConfigMangeApi.ts +0 -95
  41. package/src/controller/manage/SystemInfoManageApi.ts +0 -53
  42. package/src/controller/manage/UserAccountManageApi.ts +0 -94
  43. package/src/controller/manage/WorkbenchMangeApi.ts +0 -72
  44. package/src/controller/myinfo/AuthController.ts +0 -108
  45. package/src/controller/myinfo/MyInfoController.ts +0 -32
  46. package/src/controller/render/AppRenderController.ts +0 -79
  47. package/src/controller/test.controller.ts +0 -37
  48. package/src/filter/default.filter.ts +0 -13
  49. package/src/filter/notfound.filter.ts +0 -10
  50. package/src/index.ts +0 -106
  51. package/src/interface.ts +0 -31
  52. package/src/libs/crud-pro/CrudPro.ts +0 -165
  53. package/src/libs/crud-pro/defaultConfigs.ts +0 -15
  54. package/src/libs/crud-pro/exceptions.ts +0 -124
  55. package/src/libs/crud-pro/interfaces.ts +0 -190
  56. package/src/libs/crud-pro/models/ExecuteContext.ts +0 -120
  57. package/src/libs/crud-pro/models/ExecuteContextFunc.ts +0 -96
  58. package/src/libs/crud-pro/models/FuncContext.ts +0 -21
  59. package/src/libs/crud-pro/models/RequestCfgModel.ts +0 -141
  60. package/src/libs/crud-pro/models/RequestModel.ts +0 -141
  61. package/src/libs/crud-pro/models/ResModel.ts +0 -19
  62. package/src/libs/crud-pro/models/ServiceHub.ts +0 -32
  63. package/src/libs/crud-pro/models/SqlCfgModel.ts +0 -52
  64. package/src/libs/crud-pro/models/SqlSegArg.ts +0 -13
  65. package/src/libs/crud-pro/models/Transaction.ts +0 -73
  66. package/src/libs/crud-pro/models/TransactionMySQL.ts +0 -79
  67. package/src/libs/crud-pro/models/TransactionPostgres.ts +0 -91
  68. package/src/libs/crud-pro/models/TransactionSqlServer.ts +0 -102
  69. package/src/libs/crud-pro/models/keys.ts +0 -159
  70. package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +0 -83
  71. package/src/libs/crud-pro/services/CrudProExecuteFuncService.ts +0 -128
  72. package/src/libs/crud-pro/services/CrudProExecuteSqlService.ts +0 -262
  73. package/src/libs/crud-pro/services/CrudProFieldUpdateService.ts +0 -60
  74. package/src/libs/crud-pro/services/CrudProFieldValidateService.ts +0 -180
  75. package/src/libs/crud-pro/services/CrudProGenSqlCondition.ts +0 -354
  76. package/src/libs/crud-pro/services/CrudProGenSqlService.ts +0 -185
  77. package/src/libs/crud-pro/services/CrudProOriginToExecuteSql.ts +0 -393
  78. package/src/libs/crud-pro/services/CrudProServiceBase.ts +0 -94
  79. package/src/libs/crud-pro/services/CrudProTableMetaService.ts +0 -86
  80. package/src/libs/crud-pro/services/CurdProServiceHub.ts +0 -92
  81. package/src/libs/crud-pro/sql.txt +0 -120
  82. package/src/libs/crud-pro/utils/CompareUtils.ts +0 -23
  83. package/src/libs/crud-pro/utils/DatabaseName.ts +0 -60
  84. package/src/libs/crud-pro/utils/DateTimeUtils.ts +0 -20
  85. package/src/libs/crud-pro/utils/MemoryRefreshCache.ts +0 -64
  86. package/src/libs/crud-pro/utils/MessageParseUtils.ts +0 -33
  87. package/src/libs/crud-pro/utils/MixinUtils.ts +0 -285
  88. package/src/libs/crud-pro/utils/ModelUtils.ts +0 -55
  89. package/src/libs/crud-pro/utils/MultiKeyMap.ts +0 -72
  90. package/src/libs/crud-pro/utils/SqlFuncUtils.ts +0 -29
  91. package/src/libs/crud-pro/utils/TypeUtils.ts +0 -188
  92. package/src/libs/crud-pro/utils/ValidateUtils.ts +0 -165
  93. package/src/libs/crud-pro/utils/pool/MySQLUtils.ts +0 -20
  94. package/src/libs/crud-pro/utils/pool/PostgresUtils.ts +0 -22
  95. package/src/libs/crud-pro/utils/pool/SqlServerUtils.ts +0 -22
  96. package/src/libs/crud-pro/utils/sqlConvert/convertColumnName.ts +0 -26
  97. package/src/libs/crud-pro/utils/sqlConvert/convertMix.ts +0 -26
  98. package/src/libs/crud-pro/utils/sqlConvert/convertMsSql.ts +0 -11
  99. package/src/libs/crud-pro/utils/sqlConvert/convertPgSql.ts +0 -11
  100. package/src/libs/crud-pro/utils/sqlConvert/convertPgType.ts +0 -129
  101. package/src/libs/global-config/global-config.ts +0 -78
  102. package/src/libs/utils/common-dto.ts +0 -52
  103. package/src/libs/utils/crypto-utils.ts +0 -50
  104. package/src/libs/utils/errorToString.ts +0 -61
  105. package/src/libs/utils/fatcms-request.ts +0 -103
  106. package/src/libs/utils/functions.ts +0 -73
  107. package/src/libs/utils/ordernum-utils.ts +0 -14
  108. package/src/libs/utils/parseConfig.ts +0 -54
  109. package/src/libs/utils/parseCreateSql.ts +0 -91
  110. package/src/libs/utils/render-utils.ts +0 -184
  111. package/src/middleware/forbidden.middleware.ts +0 -52
  112. package/src/middleware/global.middleware.ts +0 -280
  113. package/src/middleware/permission.middleware.ts +0 -80
  114. package/src/middleware/tx.middleware.ts +0 -30
  115. package/src/models/AsyncTaskModel.ts +0 -82
  116. package/src/models/RedisKeys.ts +0 -13
  117. package/src/models/SystemEntities.ts +0 -115
  118. package/src/models/SystemPerm.ts +0 -105
  119. package/src/models/SystemTables.ts +0 -27
  120. package/src/models/bizmodels.ts +0 -120
  121. package/src/models/contextLogger.ts +0 -132
  122. package/src/models/devops.ts +0 -17
  123. package/src/models/userSession.ts +0 -216
  124. package/src/schedule/anonymousContext.ts +0 -73
  125. package/src/schedule/index.ts +0 -12
  126. package/src/schedule/runSchedule.ts +0 -82
  127. package/src/schedule/scheduleNames.ts +0 -21
  128. package/src/service/AuthService.ts +0 -272
  129. package/src/service/EnumInfoService.ts +0 -130
  130. package/src/service/FileCenterService.ts +0 -395
  131. package/src/service/SysConfigService.ts +0 -37
  132. package/src/service/UserAccountService.ts +0 -107
  133. package/src/service/UserSessionService.ts +0 -78
  134. package/src/service/VisitStatService.ts +0 -166
  135. package/src/service/WorkbenchService.ts +0 -165
  136. package/src/service/anyapi/AnyApiSandboxService.ts +0 -121
  137. package/src/service/anyapi/AnyApiService.ts +0 -186
  138. package/src/service/asyncTask/AsyncTaskRunnerService.ts +0 -266
  139. package/src/service/asyncTask/AsyncTaskService.ts +0 -21
  140. package/src/service/asyncTask/handler/ExcelInfoModel.ts +0 -11
  141. package/src/service/asyncTask/handler/ExportExcelAsyncTaskHandler.ts +0 -245
  142. package/src/service/asyncTask/handler/ExportExcelByInnerHttpHandler.ts +0 -147
  143. package/src/service/asyncTask/handler/ExportExcelByStdCrudHandler.ts +0 -138
  144. package/src/service/base/ApiBaseService.ts +0 -42
  145. package/src/service/base/ApiRateLimiter.ts +0 -59
  146. package/src/service/base/BaseService.ts +0 -100
  147. package/src/service/base/RedisCacheService.ts +0 -38
  148. package/src/service/crudstd/CrudStdActionService.ts +0 -27
  149. package/src/service/crudstd/CrudStdConstant.ts +0 -62
  150. package/src/service/crudstd/CrudStdRelationService.ts +0 -78
  151. package/src/service/crudstd/CrudStdService.ts +0 -283
  152. package/src/service/curd/CrudProQuick.ts +0 -131
  153. package/src/service/curd/CurdMixByAccountService.ts +0 -90
  154. package/src/service/curd/CurdMixByDictService.ts +0 -114
  155. package/src/service/curd/CurdMixByLinkToCustomService.ts +0 -219
  156. package/src/service/curd/CurdMixBySysConfigService.ts +0 -78
  157. package/src/service/curd/CurdMixByWorkbenchService.ts +0 -71
  158. package/src/service/curd/CurdMixService.ts +0 -97
  159. package/src/service/curd/CurdMixUtils.ts +0 -311
  160. package/src/service/curd/CurdProService.ts +0 -229
  161. package/src/service/curd/fixCfgModel.ts +0 -139
  162. package/src/service/proxyapi/ProxyApiLoadService.ts +0 -174
  163. package/src/service/proxyapi/ProxyApiService.ts +0 -262
  164. package/src/service/proxyapi/ProxyApiUtils.ts +0 -32
  165. package/src/service/proxyapi/RouteHandler.ts +0 -8
  166. package/src/service/proxyapi/RouteTrie.ts +0 -74
  167. package/src/service/proxyapi/WeightedRandom.ts +0 -37
  168. package/src/service/proxyapi/WeightedRoundRobin.ts +0 -44
  169. package/src/views/404_app.html +0 -31
  170. package/src/views/404_workbench.html +0 -34
  171. package/src/views/static/favicon.ico +0 -0
@@ -1,91 +0,0 @@
1
- function isChineseChar(char: string): boolean {
2
- const reg = /[\u4e00-\u9fff]/;
3
- return reg.test(char);
4
- }
5
-
6
- function isLetterOrNumber(char) {
7
- const reg = /^[a-zA-Z0-9]$/;
8
- return reg.test(char);
9
- }
10
- function isTitleValidChar(c: string) {
11
- return isChineseChar(c) || isLetterOrNumber(c) || c === ' ';
12
- }
13
-
14
- /**
15
- * 移除引号
16
- * @param s
17
- */
18
- function parseTableFieldTitleFromComment(s: string) {
19
- if (!s) {
20
- return '';
21
- }
22
- s = s.trim();
23
- s = s.replace(/'/gm, '');
24
- const arr = [];
25
- for (let i = 0; i < s.length; i++) {
26
- const c = s.charAt(i);
27
- if (isTitleValidChar(c)) {
28
- arr.push(c);
29
- } else {
30
- break;
31
- }
32
- }
33
- return arr.join('');
34
- }
35
-
36
- /**
37
- * 从最后一行中提取表格的标题
38
- * @param arr
39
- */
40
- function parseTableTitle(arr: string[]): string {
41
- if (arr.length === 0) {
42
- return '';
43
- }
44
-
45
- // 最后一行
46
- const key = ' COMMENT=';
47
- const lastStr = arr[arr.length - 1].toUpperCase();
48
- const s = lastStr.indexOf(key);
49
- if (s < 0) {
50
- return '';
51
- }
52
- const c = lastStr.substring(s + key.length).trim();
53
- return parseTableFieldTitleFromComment(c);
54
- }
55
-
56
- function parseCreateSqlToTitleMap(createSqlStr: string) {
57
- if (!createSqlStr) {
58
- return {
59
- tableTitle: '',
60
- fieldsTitleMap: {},
61
- };
62
- }
63
- const titleMap = {};
64
- const arr0 = createSqlStr.split('\n').map(s => {
65
- return s.trim();
66
- });
67
-
68
- const tableTitle = parseTableTitle(arr0);
69
-
70
- let arr = arr0.filter(s => {
71
- return !(s.endsWith('(') || s.startsWith('('));
72
- });
73
- arr = arr.filter(s => {
74
- return s.includes(' COMMENT ');
75
- });
76
-
77
- for (let i = 0; i < arr.length; i++) {
78
- const arrElement = arr[i].split(' COMMENT ');
79
- const e0 = arrElement[0];
80
- const e1 = arrElement[1];
81
- const keyArr = e0.split(' ');
82
- const field = keyArr[0].replace(/`/gm, '');
83
- titleMap[field] = parseTableFieldTitleFromComment(e1).trim();
84
- }
85
- return {
86
- tableTitle, // 表格标题
87
- fieldsTitleMap: titleMap, // 列标题
88
- };
89
- }
90
-
91
- export { parseCreateSqlToTitleMap };
@@ -1,184 +0,0 @@
1
- import * as _ from 'lodash';
2
- import * as moment from 'moment';
3
- import {Context} from '@midwayjs/koa';
4
- import {parseJsonObject} from "./functions";
5
- import {getExtLocalLoaderPort} from "./fatcms-request";
6
- import {TypeUtils} from "../crud-pro/utils/TypeUtils";
7
-
8
- // const demo = {"schema":[
9
- // {"title":"资源配置","name":"fileList","type":"array","properties":{"fileUrl":{"label":"文件URL","component":"Input","xProps":{"hasClear":true},"width":500}}}],
10
- // "data":{"fileList":[{"settingKey":"n1iqmu8qnc_1","fileUrl":"aa"},{"settingKey":"n1iqmu8rml_2","fileUrl":"aa"}]
11
- // }}
12
-
13
-
14
- interface IFileElement {
15
- fileUrl: string;
16
- fileType?: string;
17
- isModule?: boolean;
18
- }
19
-
20
- interface IRenderUtilsProps {
21
- ctx: Context;
22
- package_assets: any;
23
- workbenchInfo: any;
24
- userInfo?: any;
25
- appInfo?: any;
26
- fatcmscsrftoken: string;
27
- }
28
-
29
- function parseCookie(cookieStr:string) : any {
30
- const cookies = {};
31
-
32
- if (!cookieStr) {
33
- return cookies;
34
- }
35
-
36
- // 分割每个 cookie 项
37
- const cookieItems = cookieStr.split(';');
38
-
39
- for (const item of cookieItems) {
40
- // 去除空白字符
41
- const trimmedItem = item.trim();
42
- if (!trimmedItem) {
43
- continue;
44
- }
45
-
46
- // 找到第一个等号的位置
47
- const eqIndex = trimmedItem.indexOf('=');
48
- if (eqIndex === -1) {
49
- continue;
50
- }
51
-
52
- // 提取 key 和 value
53
- const key = trimmedItem.substring(0, eqIndex).trim();
54
- const value = trimmedItem.substring(eqIndex + 1).trim();
55
-
56
- // 处理可能的引号
57
- if (value.startsWith('"') && value.endsWith('"')) {
58
- cookies[key] = value.slice(1, -1);
59
- } else {
60
- cookies[key] = value;
61
- }
62
- }
63
-
64
- return cookies;
65
- }
66
-
67
-
68
- class RenderUtils {
69
- private readonly ctx: Context;
70
- private readonly fileList: IFileElement[];
71
- private readonly workbenchInfo: any;
72
- private readonly userInfo: any;
73
- private readonly appInfo: any;
74
- private readonly fatcmscsrftoken: string;
75
-
76
- constructor(props: IRenderUtilsProps) {
77
- this.ctx = props.ctx;
78
- this.workbenchInfo = props.workbenchInfo || {};
79
- this.userInfo = props.userInfo || {};
80
- this.appInfo = props.appInfo || {};
81
- this.fatcmscsrftoken = props.fatcmscsrftoken;
82
- const packageAssets = parseJsonObject(props.package_assets) || {};
83
- const fileList = _.get(packageAssets, 'data.fileList');
84
- if (Array.isArray(fileList)) {
85
- this.fileList = fileList.filter((f)=> {
86
- return f && f.fileUrl && typeof f.fileUrl === 'string' && f.fileUrl.length > 5; // 至少五个字符。
87
- });
88
- } else {
89
- this.fileList = [];
90
- const time = moment().format('YYYY-MM-DD HH:mm:ss.SSS');
91
- console.info(time + ' 解析fileList为空==>' + JSON.stringify({
92
- workbench_code: this.workbenchInfo?.workbench_code,
93
- app_code: this.appInfo?.app_code,
94
- }))
95
- }
96
- }
97
-
98
- renderCsrfToken() {
99
- return `<script>window.__fatcmscsrftoken = "${this.fatcmscsrftoken}";</script>`;
100
- }
101
-
102
- renderUserInfo() {
103
- return `<script>window.__user_info = ${JSON.stringify(this.userInfo)} </script>`;
104
- }
105
-
106
- renderWorkbenchInfo() {
107
- const infoPick = _.pick(this.workbenchInfo, [
108
- 'id', 'workbench_code', 'workbench_name', 'workbench_domain', 'workbench_desc', 'config_type', 'config_content'
109
- ]);
110
- return `<script>window.__workbench_info = ${JSON.stringify(infoPick)}</script>`;
111
- }
112
-
113
- renderAppInfo() {
114
- const infoPick = _.pick(this.appInfo, [
115
- 'id', 'app_code', 'app_name', 'app_type', 'app_desc', 'config_type', 'config_content'
116
- ]);
117
- return `<script>window.__app_info = ${JSON.stringify(infoPick)}</script>`;
118
- }
119
-
120
- renderCookieInfo(keys: string) {
121
- try {
122
- const cookies = parseCookie(this.ctx.headers?.cookie)
123
- const cookieObj = {};
124
- if (typeof keys === 'string') {
125
- const keyArr = keys.split(',');
126
- for (let i = 0; i < keyArr.length; i++) {
127
- const keyName = keyArr[i];
128
- cookieObj[keyName] = cookies[keyName];
129
- }
130
- }
131
- return `<script>window.__cookie_info = ${JSON.stringify(cookieObj)}</script>`;
132
- } catch (e) {
133
- return `<script>window.__cookie_info_error = ${e}; </script>`;
134
- }
135
-
136
- }
137
-
138
-
139
- renderJsAssets() {
140
- const fileList = this.fileList.filter((s) => {
141
- return s.fileType === 'js' || s.fileUrl.endsWith('.js');
142
- });
143
- const arr = fileList.map(f => {
144
- if (f.isModule) {
145
- return `<script type="module" crossorigin src="${f.fileUrl}" ></script>`
146
- }
147
- return `<script src="${f.fileUrl}" ></script>`
148
- });
149
- return arr.join('\n');
150
- }
151
-
152
- renderCssAssets() {
153
- const fileList = this.fileList.filter((s) => {
154
- return s.fileType === 'css' || s.fileUrl.endsWith('.css');
155
- });
156
- const fileUrlList = fileList.map(f => {
157
- return f.fileUrl;
158
- });
159
-
160
- const arr = fileUrlList.map((url) => {
161
- return `<link href="${url}" rel="stylesheet" />`;
162
- });
163
- return arr.join('\n');
164
- }
165
-
166
-
167
-
168
- renderExtLocalLoaderPortByDevHeader(){
169
- const loaderPort = getExtLocalLoaderPort(this.ctx);
170
- if (loaderPort && TypeUtils.isNumeric(loaderPort)) {
171
- return `<script>window.__local_loader_port_from_dev_header = ${loaderPort}</script>`;
172
- }
173
- return '';
174
- }
175
-
176
- }
177
-
178
- function createRenderUtils(props: IRenderUtilsProps) {
179
- return new RenderUtils(props)
180
- }
181
-
182
- export {
183
- createRenderUtils
184
- }
@@ -1,52 +0,0 @@
1
- import { Middleware, IMiddleware } from '@midwayjs/core';
2
- import { NextFunction, Context } from '@midwayjs/koa';
3
-
4
- // 一些爬虫/漏洞分析工具会来请求这玩意。
5
- const blackEqualList = ['/config.json', '/backend/.env', '/application.yml', '/db.ini', '/.well-known/security.txt'];
6
-
7
- const blackPrefixList = ['/.aws/', '/.git/', '/.svn/', '/.env/', '/src/', '/var/logs/', '/var/log/', '/cgi-bin/', '/php-cgi/'];
8
-
9
- /**
10
- * 针对一些路径禁止访问。避免一些爬虫抓取网站内容,实际上这里枚举的路径,本身就是不存在的。只是为了提前拦截,为了服务器性能考虑。
11
- */
12
- @Middleware()
13
- export class ForbiddenMiddleware implements IMiddleware<Context, NextFunction> {
14
- match(ctx: Context): boolean {
15
- const path = ctx.path;
16
- if (path === '/') {
17
- return false;
18
- }
19
-
20
- if (path.startsWith('/ns/')) {
21
- return false;
22
- }
23
-
24
- for (let i = 0; i < blackEqualList.length; i++) {
25
- const backlistElement = blackEqualList[i];
26
- if (path === backlistElement) {
27
- return true;
28
- }
29
- }
30
-
31
- for (let i = 0; i < blackPrefixList.length; i++) {
32
- const blackPrefix = blackPrefixList[i];
33
- if (path.startsWith(blackPrefix)) {
34
- return true;
35
- }
36
- }
37
-
38
- return !!(path.includes('/wp-includes/') || path.endsWith('.php') || path.endsWith('/.git/config'));
39
- }
40
-
41
- resolve() {
42
- return async (ctx: Context, next: NextFunction) => {
43
- ctx.set({ 'content-type': 'text/html; charset=utf-8' });
44
- ctx.status = 404;
45
- ctx.body = '404!404!404!重要的事情说三遍!';
46
- };
47
- }
48
-
49
- static getName(): string {
50
- return 'ForbiddenMiddleware';
51
- }
52
- }
@@ -1,280 +0,0 @@
1
- import { Middleware, IMiddleware } from '@midwayjs/core';
2
- import { NextFunction, Context } from '@midwayjs/koa';
3
- import * as _ from 'lodash';
4
- import { Transaction } from '@/libs/crud-pro/models/Transaction';
5
- import { UserSessionService } from '@/service/UserSessionService';
6
- import { WorkbenchService } from '@/service/WorkbenchService';
7
- import { ISessionInfo, UserSessionInfo } from '@/models/userSession';
8
- import { ICommonResult, CommonResult } from '@/libs/utils/common-dto';
9
- import { isEnableDebug, isEnableSuperAdmin } from '@/libs/utils/fatcms-request';
10
- import { ContextLogger } from '@/models/contextLogger';
11
- import { Stream } from 'node:stream';
12
- import { VisitStatService } from '@/service/VisitStatService';
13
-
14
- function isFunction(fun: any): boolean {
15
- return typeof fun === 'function';
16
- }
17
-
18
- function isObject(obj: any): boolean {
19
- return obj && typeof obj === 'object';
20
- }
21
-
22
- function isArrayBuffer(obj): boolean {
23
- if (!obj) {
24
- return false;
25
- }
26
- return obj instanceof ArrayBuffer || Object.prototype.toString.call(obj) === '[object ArrayBuffer]';
27
- }
28
-
29
- function isBlob(obj) {
30
- return obj?.constructor?.name === 'Blob';
31
- }
32
-
33
- function isStream(obj) {
34
- return obj && obj instanceof Stream;
35
- }
36
-
37
- /**
38
- * 专门对ExecuteSqlContext对象进行处理
39
- * @param res
40
- */
41
- function handleExecuteSqlContext(res: any): ICommonResult {
42
- // ExecuteSqlContext
43
- if (res && isFunction(res.getResMessage) && isFunction(res.getResModel)) {
44
- const message = res.getResMessage();
45
- return { success: true, message: message, data: res.getResModel() };
46
- }
47
- return res;
48
- }
49
-
50
- /**
51
- * 处理空数据
52
- * @param res
53
- */
54
- function handleNullRes(res: any): ICommonResult {
55
- if (res === null || typeof res === 'undefined') {
56
- return { success: false, message: '接口没有返回任何数据', data: null };
57
- }
58
- return res;
59
- }
60
-
61
- /**
62
- * 处理普通数组数据
63
- * @param res
64
- */
65
- function handleArrayRes(res: any): ICommonResult {
66
- if (Array.isArray(res) && res.length) {
67
- return { success: true, data: res };
68
- }
69
- return res;
70
- }
71
-
72
- function addDebugInfoFromContext(debugInfo: any, ctx: Context): void {
73
- const userSession: UserSessionInfo = ctx.userSession;
74
- const contextLogger: ContextLogger = ctx.contextLogger;
75
- if (isObject(userSession)) {
76
- const sessionInfo = userSession.getSessionInfo();
77
- _.set(debugInfo, 'sessionInfo', sessionInfo);
78
- }
79
- if (isObject(contextLogger)) {
80
- _.set(debugInfo, 'logList', contextLogger.getLogList());
81
- }
82
- }
83
-
84
- /**
85
- * 处理调试信息
86
- * @param beforeRes 上一个输出
87
- * @param ctx
88
- * @param originRes 最原始的输出
89
- */
90
- function handleDebugRes(beforeRes: any, ctx: Context, originRes: any): ICommonResult {
91
- const isDebug = isEnableDebug(ctx);
92
-
93
- if (!isDebug) {
94
- // 没有开启debug模式
95
- if (beforeRes && beforeRes.debugInfo) {
96
- delete beforeRes.debugInfo;
97
- }
98
- return beforeRes;
99
- }
100
-
101
- // 开启了debug模式
102
- const debugInfo = {};
103
-
104
- addDebugInfoFromContext(debugInfo, ctx);
105
-
106
- //debugInfo3. 响应中本身就有debugInfo
107
- if (isObject(originRes)) {
108
- if (typeof originRes.debugInfo === 'string') {
109
- _.set(debugInfo, 'debugInfo', originRes.debugInfo);
110
- }
111
- if (isObject(beforeRes.debugInfo)) {
112
- Object.assign(debugInfo, originRes.debugInfo);
113
- }
114
-
115
- if (isFunction(originRes.getReqModel)) {
116
- _.set(debugInfo, 'reqMode', originRes.getReqModel());
117
- }
118
- if (isObject(originRes.cfgModel)) {
119
- _.set(debugInfo, 'cfgMode', originRes.cfgModel);
120
- }
121
- }
122
-
123
- // debugInfo处理完毕
124
- const nextRes: any = {};
125
- if (isObject(beforeRes)) {
126
- Object.assign(nextRes, beforeRes);
127
- }
128
- nextRes.debugInfo = debugInfo;
129
- return nextRes as ICommonResult;
130
- }
131
-
132
- async function getUserSessionService(ctx: Context): Promise<UserSessionService> {
133
- return ctx.requestContext.getAsync(UserSessionService);
134
- }
135
-
136
- async function getWorkbenchService(ctx: Context): Promise<WorkbenchService> {
137
- return ctx.requestContext.getAsync(WorkbenchService);
138
- }
139
-
140
- async function getVisitStatService(ctx: Context): Promise<VisitStatService> {
141
- return ctx.requestContext.getAsync(VisitStatService);
142
- }
143
-
144
- /**
145
- * 检查是否是超级管理员
146
- * @param ctx
147
- * @param sessionInfo
148
- */
149
- function checkIsSuperAdmin(ctx: Context, sessionInfo: ISessionInfo): boolean {
150
- if (!sessionInfo || !sessionInfo.loginName) {
151
- return false;
152
- }
153
-
154
- if (!isEnableSuperAdmin(ctx)) {
155
- return false;
156
- }
157
-
158
- const currentLoginName = sessionInfo.loginName;
159
- const superAdminList = ctx.app.getConfig('superAdminList');
160
-
161
- if (Array.isArray(superAdminList)) {
162
- for (let i = 0; i < superAdminList.length; i++) {
163
- const element = superAdminList[i];
164
- const login_name = _.get(element, 'login_name');
165
- if (login_name && login_name === currentLoginName) {
166
- return true;
167
- }
168
- }
169
- }
170
- return false;
171
- }
172
-
173
- function handleDebugError(e: any, ctx: Context): ICommonResult {
174
- ctx.logger.error('GlobalMiddleware', e);
175
-
176
- let errorStack = null;
177
- let errorCode = null;
178
- let errorMessage = '' + e;
179
-
180
- if (isObject(e)) {
181
- if (e.message) {
182
- errorMessage = e.message;
183
- }
184
- if (e.code) {
185
- errorCode = e.code;
186
- }
187
- errorStack = e.stack;
188
- }
189
-
190
- const res = CommonResult.errorRes(errorMessage, errorCode);
191
- if (isEnableDebug(ctx)) {
192
- const debugInfo = { errorStack };
193
- addDebugInfoFromContext(debugInfo, ctx);
194
- res.debugInfo = debugInfo;
195
- }
196
-
197
- return res;
198
- }
199
-
200
- async function trackRequest(ctx: Context) {
201
- try {
202
- const visitStatService = await getVisitStatService(ctx);
203
- await visitStatService.trackRequest(ctx.path, 'path');
204
- } catch (e) {
205
- console.error('trackRequestError', e);
206
- }
207
- }
208
-
209
- const excludePathPrefix = ['/ns/static/', '/ns/api/helpers'];
210
-
211
- /**
212
- * 全局中间件
213
- */
214
- @Middleware()
215
- export class GlobalMiddleware implements IMiddleware<Context, NextFunction> {
216
- match(ctx: Context): boolean {
217
- const path: string = ctx.path;
218
- for (let i = 0; i < excludePathPrefix.length; i++) {
219
- const excludePathPrefix1 = excludePathPrefix[i];
220
- if (path.startsWith(excludePathPrefix1)) {
221
- return false;
222
- }
223
- }
224
- return true;
225
- }
226
-
227
- resolve() {
228
- return async (ctx: Context, next: NextFunction) => {
229
- ctx.contextLogger = new ContextLogger(ctx);
230
- ctx.transaction = new Transaction();
231
-
232
- try {
233
- // 支持的站点
234
- const workbenchService = await getWorkbenchService(ctx);
235
- const isSupportTheHost = await workbenchService.isSupportCurrentHostForForMiddleware();
236
- if (isSupportTheHost !== true) {
237
- return isSupportTheHost;
238
- }
239
-
240
- const sessionService = await getUserSessionService(ctx);
241
- const sessionInfo = await sessionService.getCurrentUserSessionForMiddleware();
242
- const isSuperAdmin = checkIsSuperAdmin(ctx, sessionInfo);
243
-
244
- ctx.userSession = new UserSessionInfo(sessionInfo, isSuperAdmin);
245
- ctx.workbenchInfo = await workbenchService.getCurrentHostWorkbenchInfo();
246
-
247
- // 数据埋点
248
- await trackRequest(ctx);
249
-
250
- const res0 = await next();
251
-
252
- // 基本数据类型:需要搞成字符串
253
- if (['bigint', 'symbol'].includes(typeof res0)) {
254
- return res0.toString();
255
- }
256
-
257
- // 基本数据类型: 交给KOA处理的
258
- if (!res0 || ['string', 'number', 'boolean'].includes(typeof res0) || Buffer.isBuffer(res0) || isArrayBuffer(res0) || isStream(res0) || isBlob(res0)) {
259
- return res0;
260
- }
261
-
262
- // 其他:普通的JSON数据
263
- let res = handleExecuteSqlContext(res0);
264
- res = handleNullRes(res);
265
- res = handleArrayRes(res);
266
- res = handleDebugRes(res, ctx, res0);
267
-
268
- return res;
269
- } catch (e) {
270
- return handleDebugError(e, ctx);
271
- } finally {
272
- await ctx.transaction.releaseTx();
273
- }
274
- };
275
- }
276
-
277
- static getName(): string {
278
- return 'GlobalMiddleware';
279
- }
280
- }
@@ -1,80 +0,0 @@
1
- import { NextFunction, Context } from '@midwayjs/koa';
2
- import { Exceptions } from '../libs/crud-pro/exceptions';
3
-
4
- function checkPermission(codeList: string | string[]) {
5
- return async (ctx: Context, next: NextFunction) => {
6
- const userSession = ctx.userSession;
7
-
8
- // 只有登录用户才会有功能点权限,所以必须登录
9
- if (!userSession.isLogin()) {
10
- return {
11
- success: false,
12
- message: '用户未登录, 请先登录',
13
- code: Exceptions.NOT_LOGIN,
14
- };
15
- }
16
-
17
- // 查询当前用户是否用此功能点的权限
18
- const hasPermission = userSession.hasAnyPermission(codeList);
19
- if (!hasPermission) {
20
- let message: string;
21
- const loginName = userSession.getSessionInfo().loginName;
22
- if (loginName === 'devopsviewer') {
23
- message = '开放体验账号不支持这些操作:新增、修改、删除、上传';
24
- } else {
25
- message = '缺少权限: ' + JSON.stringify(codeList);
26
- }
27
-
28
- return {
29
- success: false,
30
- message: message,
31
- code: Exceptions.NO_AUTH,
32
- };
33
- }
34
-
35
- await next();
36
- };
37
- }
38
-
39
- function checkRole(codeList: string | string[]) {
40
- return async (ctx: Context, next: NextFunction) => {
41
- const userSession = ctx.userSession;
42
-
43
- // 只有登录用户才会有功能点权限,所以必须登录
44
- if (!userSession.isLogin()) {
45
- return {
46
- success: false,
47
- message: '用户未登录, 请先登录',
48
- code: Exceptions.NOT_LOGIN,
49
- };
50
- }
51
-
52
- // 查询当前用户是否用此功能点的权限
53
- const hasRole = userSession.hasAnyRole(codeList);
54
- if (!hasRole) {
55
- return {
56
- success: false,
57
- message: '缺少角色权限: ' + JSON.stringify(codeList),
58
- code: Exceptions.NO_AUTH,
59
- };
60
- }
61
-
62
- await next();
63
- };
64
- }
65
-
66
- function checkLogin() {
67
- return async (ctx: Context, next: NextFunction) => {
68
- const userSession = ctx.userSession;
69
- if (!userSession.isLogin()) {
70
- return {
71
- success: false,
72
- message: '用户未登录, 请先登录',
73
- code: Exceptions.NOT_LOGIN,
74
- };
75
- }
76
- await next();
77
- };
78
- }
79
-
80
- export { checkPermission, checkLogin, checkRole };