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,216 +0,0 @@
1
- import * as _ from 'lodash';
2
- import { KeysOfAuthType } from '../libs/crud-pro/models/keys';
3
- import { MixinUtils } from '../libs/crud-pro/utils/MixinUtils';
4
- import { CommonException, Exceptions } from '../libs/crud-pro/exceptions';
5
- import { parseStringTrimArray } from '../libs/utils/functions';
6
-
7
- const sessionCookieCfg = {
8
- signed: true,
9
- encrypt: true,
10
- httpOnly: true,
11
- maxAge: 1000 * 3600 * 24 * 365,
12
- };
13
-
14
- const SESSION_ID_KEY = 'fatcmssessionid';
15
- const SYS_ACCOUNT_TYPE = 'fatcms';
16
-
17
- interface ISessionInfo {
18
- nickName: string;
19
- avatar: string;
20
- roleCodes: string[];
21
- functionCodes: string[];
22
- loginName: string;
23
- sessionId: string;
24
- accountId: string;
25
- workbenchCode: string;
26
- accountType: string;
27
- }
28
-
29
- interface IConsumerUserInfo {
30
- workbenchCode: string;
31
- accountType: string;
32
- accountId: string;
33
- nickName: string;
34
- loginName: string;
35
- avatar: string;
36
- }
37
-
38
- interface IBatchCheckProps {
39
- permissionCodeList?: string[];
40
- roleCodeList?: string[];
41
- }
42
-
43
- interface IBatchCheckRes {
44
- permissionCodeResult: Record<string, boolean>;
45
- roleCodeResult: Record<string, boolean>;
46
- }
47
-
48
- function createEmptySessionInfo(superAdmin: boolean): ISessionInfo {
49
- return {
50
- nickName: superAdmin ? 'SA' : '',
51
- avatar: '',
52
- roleCodes: [],
53
- functionCodes: [],
54
- loginName: '',
55
- sessionId: '',
56
- accountId: '',
57
- workbenchCode: '',
58
- accountType: '',
59
- };
60
- }
61
-
62
- class UserSessionInfo {
63
- private readonly sessionInfo: ISessionInfo;
64
-
65
- // 是否是超级管理员;拥有一切权限。紧急情况下使用
66
- private readonly superAdmin: boolean;
67
- constructor(sessionInfo: ISessionInfo, superAdmin?: boolean) {
68
- this.superAdmin = superAdmin === true;
69
- if (sessionInfo) {
70
- this.sessionInfo = sessionInfo;
71
- } else {
72
- this.sessionInfo = createEmptySessionInfo(superAdmin);
73
- }
74
- }
75
-
76
- getSessionInfo(): ISessionInfo {
77
- return this.sessionInfo;
78
- }
79
-
80
- getSessionValue(key: string): any {
81
- return _.get(this.sessionInfo, key);
82
- }
83
-
84
- isSuperAdmin(): boolean {
85
- return this.superAdmin;
86
- }
87
-
88
- /**
89
- * 是否是我自己的账号
90
- * @param accountId
91
- */
92
- isMySelf(accountId: string): boolean {
93
- const s = this.sessionInfo;
94
- return s && accountId && s.accountId === accountId;
95
- }
96
-
97
- isLogin(): boolean {
98
- const s = this.sessionInfo;
99
- return !!(s && s.sessionId && s.accountId && s.sessionId.length > 1);
100
- }
101
-
102
- hasAnyPermission(funcCodeList: string | string[]): boolean {
103
- if (this.isSuperAdmin()) {
104
- return true;
105
- }
106
- if (typeof funcCodeList === 'string') {
107
- return this.hasPermission(funcCodeList);
108
- }
109
-
110
- if (Array.isArray(funcCodeList)) {
111
- for (let i = 0; i < funcCodeList.length; i++) {
112
- const funcCode = funcCodeList[i];
113
- if (this.hasPermission(funcCode)) {
114
- return true;
115
- }
116
- }
117
- return false;
118
- }
119
- }
120
-
121
- hasPermission(funcCode: string): boolean {
122
- if (this.isSuperAdmin()) {
123
- return true;
124
- }
125
- const ss = this.sessionInfo.functionCodes || [];
126
- return ss.includes(funcCode);
127
- }
128
-
129
- hasAnyRole(roleCodeList: string | string[]): boolean {
130
- if (this.isSuperAdmin()) {
131
- return true;
132
- }
133
- if (typeof roleCodeList === 'string') {
134
- return this.hasRole(roleCodeList);
135
- }
136
-
137
- if (Array.isArray(roleCodeList)) {
138
- for (let i = 0; i < roleCodeList.length; i++) {
139
- const funcCode = roleCodeList[i];
140
- if (this.hasRole(funcCode)) {
141
- return true;
142
- }
143
- }
144
- return false;
145
- }
146
- }
147
-
148
- hasRole(roleCode: string): boolean {
149
- if (this.isSuperAdmin()) {
150
- return true;
151
- }
152
- const ss = this.sessionInfo.roleCodes || [];
153
- return ss.includes(roleCode);
154
- }
155
-
156
- isAuthPass(authType: KeysOfAuthType, authConfig: string | string[]): boolean {
157
- if (authType === KeysOfAuthType.free || !authType) {
158
- return true; // 无需校验
159
- }
160
- if (!this.isLogin()) {
161
- return false;
162
- }
163
- if (this.isSuperAdmin()) {
164
- return true;
165
- }
166
-
167
- if (authType === KeysOfAuthType.byFuncCode) {
168
- const funcCodes = parseStringTrimArray(authConfig);
169
- if (MixinUtils.isEmpty(funcCodes)) {
170
- throw new CommonException(Exceptions.CFG_AUTH_CODE_EMPTY, '缺少权限编码配置');
171
- }
172
- return this.hasAnyPermission(funcCodes);
173
- }
174
-
175
- if (authType === KeysOfAuthType.byRoleCode) {
176
- const roleCodes = parseStringTrimArray(authConfig);
177
- if (MixinUtils.isEmpty(roleCodes)) {
178
- throw new CommonException(Exceptions.CFG_AUTH_CODE_EMPTY, '缺少角色编码配置');
179
- }
180
- return this.hasAnyRole(roleCodes);
181
- }
182
-
183
- return true;
184
- }
185
-
186
- /**
187
- * 批量检查权限
188
- * @param params
189
- */
190
- batchCheckPermission(params: IBatchCheckProps): IBatchCheckRes {
191
- const { permissionCodeList, roleCodeList } = params;
192
- const permissionCodeResult: Record<string, boolean> = {};
193
- const roleCodeResult: Record<string, boolean> = {};
194
-
195
- if (Array.isArray(permissionCodeList)) {
196
- for (let i = 0; i < permissionCodeList.length; i++) {
197
- const permissionCode = permissionCodeList[i];
198
- if (permissionCode && typeof permissionCode === 'string') {
199
- permissionCodeResult[permissionCode] = this.hasPermission(permissionCode);
200
- }
201
- }
202
- }
203
-
204
- if (Array.isArray(roleCodeList)) {
205
- for (let i = 0; i < roleCodeList.length; i++) {
206
- const roleCode = roleCodeList[i];
207
- if (roleCode && typeof roleCode === 'string') {
208
- roleCodeResult[roleCode] = this.hasRole(roleCode);
209
- }
210
- }
211
- }
212
- return { permissionCodeResult, roleCodeResult };
213
- }
214
- }
215
-
216
- export { ISessionInfo, UserSessionInfo, SESSION_ID_KEY, SYS_ACCOUNT_TYPE, IConsumerUserInfo, sessionCookieCfg };
@@ -1,73 +0,0 @@
1
- import * as koa from '@midwayjs/koa';
2
- import { ContextLogger } from '@/models/contextLogger';
3
- import { Transaction } from '@/libs/crud-pro/models/Transaction';
4
- import { ISessionInfo, SYS_ACCOUNT_TYPE, UserSessionInfo } from '@/models/userSession';
5
- import { IWorkbenchEntity } from '@/models/SystemEntities';
6
-
7
- const anonymousUserInfo: ISessionInfo = {
8
- nickName: 'anonymous_user',
9
- avatar: '',
10
- roleCodes: [],
11
- functionCodes: [],
12
- loginName: 'anonymous_user',
13
- sessionId: '',
14
- accountId: '',
15
- workbenchCode: '',
16
- accountType: SYS_ACCOUNT_TYPE,
17
- };
18
-
19
- const anonymousWorkbenchInfo: IWorkbenchEntity = {
20
- id: 0,
21
- workbench_code: 'anonymous_workbench',
22
- workbench_name: 'anonymous_workbench',
23
- workbench_domain: 'anonymous_workbench',
24
- workbench_desc: 'anonymous_workbench',
25
- html_content: 'anonymous_workbench',
26
- status: 1,
27
- workbench_type: 0,
28
- };
29
-
30
- export type RunServiceAtAnonymousContextRunner = (ctx: koa.IMidwayKoaContext) => any;
31
- export interface RunServiceAtAnonymousContextRes {
32
- result: any;
33
- error: any;
34
- }
35
-
36
- class AnonymousContext {
37
- private app: koa.Application;
38
-
39
- setApp(app: koa.Application) {
40
- this.app = app;
41
- }
42
-
43
- getApp() {
44
- return this.app;
45
- }
46
-
47
- async runServiceAtAnonymousContext(runner: RunServiceAtAnonymousContextRunner): Promise<RunServiceAtAnonymousContextRes> {
48
- const app = this.app;
49
- const ctx: koa.IMidwayKoaContext = app.createAnonymousContext();
50
-
51
- ctx.app = app;
52
- ctx.contextLogger = new ContextLogger(ctx as any);
53
- ctx.transaction = new Transaction();
54
- ctx.userSession = new UserSessionInfo(anonymousUserInfo, true);
55
- ctx.workbenchInfo = anonymousWorkbenchInfo;
56
-
57
- let result: any = null;
58
- let error: any = null;
59
- try {
60
- result = await runner(ctx);
61
- } catch (e) {
62
- error = e;
63
- ctx.logger.error('runServiceAtAnonymousContext error ', e);
64
- } finally {
65
- await ctx.transaction.releaseTx();
66
- }
67
- return { result: result, error: error };
68
- }
69
- }
70
-
71
- const ANONYMOUS_CONTEXT = new AnonymousContext();
72
-
73
- export { ANONYMOUS_CONTEXT };
@@ -1,12 +0,0 @@
1
- import {SCHEDULE_QUEUE, runScheduleTaskOnce} from './runSchedule'
2
- import {ANONYMOUS_CONTEXT} from './anonymousContext'
3
- import {
4
- INNER_SCHEDULE_INTERVAL
5
- } from './scheduleNames'
6
-
7
- export {
8
- INNER_SCHEDULE_INTERVAL,
9
- ANONYMOUS_CONTEXT,
10
- SCHEDULE_QUEUE,
11
- runScheduleTaskOnce
12
- }
@@ -1,82 +0,0 @@
1
- import * as koa from '@midwayjs/koa';
2
- import {IScheduleService} from '@/interface';
3
- import {ANONYMOUS_CONTEXT} from "./anonymousContext";
4
-
5
-
6
- // 上次执行时间
7
- const lastExecuteTimeMap = {};
8
-
9
- const DEFAULT_SCHEDULE_INTERVAL = 2 * 60 * 1000;
10
-
11
-
12
- async function runScheduleTaskOnce(serviceName: string) {
13
- return await ANONYMOUS_CONTEXT.runServiceAtAnonymousContext(async (ctx: koa.IMidwayKoaContext) => {
14
- const serviceObject: IScheduleService = await ctx.requestContext.getAsync(serviceName);
15
- if (serviceObject && serviceObject.runBySchedule) {
16
- await serviceObject.runBySchedule();
17
- } else {
18
- ctx.logger.error(`runSchedule error , serviceName = ${serviceName}, 没有找到服务`);
19
- }
20
- });
21
- }
22
-
23
-
24
- class ScheduleQueue {
25
- private scheduleIntervalMap: Map<string, number> = new Map();
26
-
27
- public setScheduleTask(serviceName: string, intervalTime: number) {
28
- this.scheduleIntervalMap.set(serviceName, intervalTime);
29
- }
30
-
31
- public removeScheduleTask(serviceName: string) {
32
- this.scheduleIntervalMap.delete(serviceName);
33
- }
34
-
35
- private isAlreadyTimeToExecute(serviceName: string): boolean {
36
- if (!lastExecuteTimeMap[serviceName]) {
37
- return true; // 可以执行
38
- }
39
- const intervalTime1 = this.scheduleIntervalMap.get(serviceName);
40
- const intervalTime = intervalTime1 || DEFAULT_SCHEDULE_INTERVAL; // 时间间隔
41
- const lastExecuteTime = lastExecuteTimeMap[serviceName];
42
- return (Date.now() - lastExecuteTime) >= intervalTime;
43
- }
44
-
45
-
46
- private async runScheduleTaskOnceCheckTime(serviceName: string) {
47
- // 还没有到可以执行时间
48
- if (!this.isAlreadyTimeToExecute(serviceName)) {
49
- return;
50
- }
51
- lastExecuteTimeMap[serviceName] = Date.now();
52
- await runScheduleTaskOnce(serviceName);
53
- }
54
-
55
- private async runScheduleServiceList() {
56
- const serviceListKeys = this.scheduleIntervalMap.keys();
57
- const serviceList = [...serviceListKeys];
58
- for (let i = 0; i < serviceList.length; i++) {
59
- const serviceName = serviceList[i];
60
- try {
61
- await this.runScheduleTaskOnceCheckTime(serviceName);
62
- } catch (e) {
63
- ANONYMOUS_CONTEXT.getApp().getCoreLogger().error(`runSchedule error , serviceName = ${serviceName}`, e);
64
- }
65
- }
66
- }
67
-
68
-
69
- public async startScheduleLoop() {
70
- await this.runScheduleServiceList();
71
- setInterval(() => {
72
- this.runScheduleServiceList();
73
- }, 1000); // 最少1秒执行一次
74
- }
75
-
76
-
77
- }
78
-
79
-
80
- const SCHEDULE_QUEUE = new ScheduleQueue();
81
-
82
- export {SCHEDULE_QUEUE, runScheduleTaskOnce};
@@ -1,21 +0,0 @@
1
-
2
- const INNER_SCHEDULE_INTERVAL = {
3
- proxyApiLoadService: 2 * 60 * 1000, // 2分钟重新加载一次,
4
- workbenchService: 60 * 1000, // 1分钟重新加载一次,
5
- visitStatService: 10 * 60 * 1000, // 10分钟重新加载一次,
6
- asyncTaskRunnerService: 1000, // 1s可以执行
7
- }
8
-
9
- const INNER_SCHEDULE_NAMES = {
10
- proxyApiLoadService: 'proxyApiLoadService',
11
- workbenchService: 'workbenchService',
12
- visitStatService: "visitStatService",
13
- asyncTaskRunnerService: "asyncTaskRunnerService",
14
- }
15
-
16
-
17
-
18
- export {
19
- INNER_SCHEDULE_INTERVAL,
20
- INNER_SCHEDULE_NAMES
21
- }
@@ -1,272 +0,0 @@
1
- import { Inject, Provide, Config } from '@midwayjs/core';
2
- import { Context } from '@midwayjs/koa';
3
- import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
4
- import { CurdMixService } from './curd/CurdMixService';
5
- import * as md5 from 'md5';
6
- import { UserAccountService } from './UserAccountService';
7
- import { createUniqueId } from '@/libs/utils/functions';
8
- import { IConsumerUserInfo, ISessionInfo, SYS_ACCOUNT_TYPE } from '@/models/userSession';
9
- import { SystemTables } from '@/models/SystemTables';
10
- import { UserSessionService } from './UserSessionService';
11
- import { CommonResult } from '@/libs/utils/common-dto';
12
- import { isEnableSuperAdmin } from '@/libs/utils/fatcms-request';
13
- import { CommonException } from '@/libs/crud-pro/exceptions';
14
- import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
15
-
16
- @Provide()
17
- export class AuthService {
18
- @Inject()
19
- private ctx: Context;
20
-
21
- @Config('superAdminList')
22
- private superAdminList: any[];
23
-
24
- @Inject()
25
- private curdMixService: CurdMixService;
26
-
27
- @Inject()
28
- private userService: UserAccountService;
29
-
30
- @Inject()
31
- private userSessionService: UserSessionService;
32
-
33
- /**
34
- * 明文密码转unsaltedPwd密码
35
- * @param plainPwd
36
- * @param loginName
37
- */
38
- public toUnsaltedPwd(plainPwd: string, loginName: string) {
39
- const pw2 = md5(md5(plainPwd) + '_' + loginName);
40
- return pw2;
41
- }
42
-
43
- /**
44
- * front: pw1 = md5(md5(input_pw) + "_" + loginName)
45
- * db: pw2 = md5(pwd_salt + pw1)
46
- * 检查用户名密码是否正确
47
- * @param loginName
48
- * @param unsaltedPwd
49
- */
50
- public async checkLoginPassword(loginName: string, unsaltedPwd: string): Promise<CommonResult> {
51
- const message = '用户名或密码错误';
52
- const errorCode = 'USERNAME_OR_PASSWORD_IS_INCORRECT';
53
-
54
- if (!loginName) {
55
- return CommonResult.errorRes(message, errorCode, '!loginName');
56
- }
57
-
58
- if (!unsaltedPwd) {
59
- return CommonResult.errorRes(message, errorCode, '!unsaltedPwd');
60
- }
61
-
62
- const userAccount = await this.queryUserAccountByLoginName(loginName);
63
- if (!userAccount) {
64
- return CommonResult.errorRes(message, errorCode, '!userAccount');
65
- }
66
-
67
- if (userAccount.status !== 1) {
68
- return CommonResult.errorRes(message, errorCode, 'userAccount.status !== 1');
69
- }
70
-
71
- if (!userAccount.pwd_md5) {
72
- return CommonResult.errorRes(message, errorCode, '!userAccount.pwd_md5');
73
- }
74
-
75
- const pwd_salt = userAccount.pwd_salt;
76
- const pwd_md5 = md5(unsaltedPwd + pwd_salt);
77
-
78
- if (userAccount['pwd_md5'] !== pwd_md5) {
79
- return CommonResult.errorRes(message, errorCode, "userAccount['pwd_md5'] !== pwd_md5");
80
- }
81
-
82
- return CommonResult.successRes();
83
- }
84
-
85
- /**
86
- * 创建一个toC端的UserSession
87
- * @param consumerUserInfo
88
- */
89
- public async createUserSessionForConsumer(consumerUserInfo: IConsumerUserInfo) {
90
- const sessionId = createUniqueId();
91
-
92
- const sessionInfo: ISessionInfo = {
93
- nickName: consumerUserInfo.nickName,
94
- avatar: consumerUserInfo.avatar,
95
- roleCodes: [],
96
- functionCodes: [],
97
- loginName: consumerUserInfo.loginName,
98
- sessionId,
99
- accountId: consumerUserInfo.accountId,
100
- workbenchCode: consumerUserInfo.workbenchCode,
101
- accountType: consumerUserInfo.accountType,
102
- };
103
-
104
- await this.userSessionService.saveUserSession(sessionInfo);
105
-
106
- return {
107
- sessionId,
108
- loginName: sessionInfo.loginName,
109
- accountId: sessionInfo.accountId,
110
- nickName: sessionInfo.nickName,
111
- avatar: sessionInfo.avatar,
112
- workbenchCode: sessionInfo.workbenchCode,
113
- accountType: sessionInfo.accountType,
114
- };
115
- }
116
-
117
- /**
118
- * 创建一个Session
119
- * @param loginName
120
- * @param workbenchCode
121
- */
122
- public async createUserSession(loginName: string, workbenchCode: string) {
123
- const userAccount = await this.queryUserAccountByLoginName(loginName);
124
-
125
- if (!userAccount) {
126
- throw new CommonException('USER_ACCOUNT_NOT_EXIST', '用户账号不存在');
127
- }
128
-
129
- const accountId = userAccount.account_id;
130
- const sessionId = createUniqueId();
131
- const roleCodes = await this.queryUserRoleCodeList(accountId);
132
- const functionCodes = await this.queryFunctionCodeList(roleCodes);
133
-
134
- const sessionInfo: ISessionInfo = {
135
- nickName: userAccount.nick_name,
136
- avatar: userAccount.avatar,
137
- roleCodes,
138
- functionCodes,
139
- loginName,
140
- sessionId,
141
- accountId,
142
- workbenchCode,
143
- accountType: SYS_ACCOUNT_TYPE,
144
- };
145
-
146
- await this.userSessionService.saveUserSession(sessionInfo);
147
-
148
- return {
149
- sessionId,
150
- loginName,
151
- accountId,
152
- nickName: userAccount.nick_name,
153
- avatar: userAccount.avatar,
154
- workbenchCode: sessionInfo.workbenchCode,
155
- accountType: sessionInfo.accountType,
156
- };
157
- }
158
-
159
- /**
160
- * 获取用户
161
- * @param loginName
162
- * @private
163
- */
164
- private async queryUserAccountByLoginName(loginName: string) {
165
- const superAdminList = this.superAdminList;
166
- const findFromSuperAdmin = () => {
167
- if (!isEnableSuperAdmin(this.ctx)) {
168
- return null;
169
- }
170
-
171
- if (!Array.isArray(superAdminList)) {
172
- return null;
173
- }
174
-
175
- for (let i = 0; i < superAdminList.length; i++) {
176
- const element = superAdminList[i];
177
- if (element && element.login_name === loginName) {
178
- const nick_name = 'SA' + i;
179
- return {
180
- nick_name: element.nick_name || nick_name,
181
- avatar: '',
182
- account_id: nick_name,
183
- status: 1,
184
- login_name: element.login_name,
185
- pwd_salt: element.pwd_salt,
186
- pwd_md5: element.pwd_md5,
187
- };
188
- }
189
- }
190
- };
191
- const superAdmin = findFromSuperAdmin();
192
- if (superAdmin) {
193
- return superAdmin;
194
- }
195
- return this.userService.queryUserAccountByLoginName(loginName);
196
- }
197
-
198
- /**
199
- * 查询此accountId下关联的角色code列表
200
- * @param accountId
201
- * @private
202
- */
203
- private async queryUserRoleCodeList(accountId: string): Promise<string[]> {
204
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
205
-
206
- const res = await this.curdMixService.executeCrudByCfg(
207
- {
208
- condition: { account_id: accountId },
209
- limit: 10000,
210
- },
211
- {
212
- sqlTable: SystemTables.sys_perm_user_role,
213
- sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
214
- sqlDatabase: SystemDbName,
215
- sqlDbType: SystemDbType,
216
- }
217
- );
218
- const rows = res.getResModel().rows || [];
219
- const roleCodes: string[] = rows.map(({ role_code }) => {
220
- return role_code as string;
221
- });
222
- return [...new Set(roleCodes)];
223
- }
224
-
225
- /**
226
- * 查询此roleCodeList下面关联的所有功能点列表
227
- * @param roleCodeList
228
- * @private
229
- */
230
- private async queryFunctionCodeList(roleCodeList: string[]): Promise<string[]> {
231
- if (!roleCodeList || roleCodeList.length === 0) {
232
- return [];
233
- }
234
-
235
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
236
-
237
- const res = await this.curdMixService.executeCrudByCfg(
238
- {
239
- condition: { role_code: { $in: roleCodeList } },
240
- limit: 10000,
241
- },
242
- {
243
- sqlTable: SystemTables.sys_perm_role_func,
244
- sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
245
- sqlDatabase: SystemDbName,
246
- sqlDbType: SystemDbType,
247
- }
248
- );
249
- const rows = res.getResModel().rows || [];
250
- const funcCodes: string[] = rows.map(({ func_code }) => {
251
- return func_code;
252
- });
253
- return [...new Set(funcCodes)];
254
- }
255
-
256
- async removeUserSession(sessionId: string) {
257
- return this.userSessionService.removeUserSession(sessionId);
258
- }
259
-
260
- async refreshSession(sessionInfo: ISessionInfo): Promise<ISessionInfo> {
261
- const oldSessionId = sessionInfo.sessionId;
262
-
263
- const sessionId = createUniqueId();
264
- const newSessionInfo: ISessionInfo = { ...sessionInfo, sessionId };
265
-
266
- await this.userSessionService.saveUserSession(newSessionInfo);
267
-
268
- await this.removeUserSession(oldSessionId);
269
-
270
- return newSessionInfo;
271
- }
272
- }