midway-fatcms 0.0.1-beta.64 → 0.0.1-beta.66

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 (108) hide show
  1. package/dist/config/config.default.js +7 -0
  2. package/dist/controller/base/BaseApiController.d.ts +5 -0
  3. package/dist/controller/base/BaseApiController.js +25 -0
  4. package/dist/controller/gateway/CrudStdGatewayController.d.ts +1 -1
  5. package/dist/controller/gateway/CrudStdGatewayController.js +36 -10
  6. package/dist/controller/gateway/FileController.js +2 -1
  7. package/dist/controller/gateway/PublicApiController.d.ts +1 -0
  8. package/dist/controller/gateway/PublicApiController.js +64 -28
  9. package/dist/index.d.ts +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/interface.d.ts +2 -0
  12. package/dist/libs/crud-pro/exceptions.d.ts +2 -0
  13. package/dist/libs/crud-pro/exceptions.js +2 -0
  14. package/dist/libs/crud-pro/models/keys.d.ts +1 -1
  15. package/dist/libs/crud-pro/models/keys.js +1 -1
  16. package/dist/libs/crud-pro/services/CrudProOriginToExecuteSql.js +6 -1
  17. package/dist/libs/crud-pro/utils/sqlConvert/convertColumnName.js +14 -0
  18. package/dist/libs/utils/AsymmetricCrypto.d.ts +76 -0
  19. package/dist/libs/utils/AsymmetricCrypto.js +261 -0
  20. package/dist/libs/utils/format-url.d.ts +2 -0
  21. package/dist/libs/utils/format-url.js +13 -0
  22. package/dist/libs/utils/render-utils.d.ts +16 -7
  23. package/dist/libs/utils/render-utils.js +27 -6
  24. package/dist/middleware/global.middleware.js +7 -0
  25. package/dist/models/SystemEntities.d.ts +34 -0
  26. package/dist/models/SystemEntities.js +22 -1
  27. package/dist/models/WorkbenchInfoTools.d.ts +7 -0
  28. package/dist/models/WorkbenchInfoTools.js +20 -0
  29. package/dist/models/bizmodels.d.ts +9 -0
  30. package/dist/models/contextLogger.d.ts +2 -0
  31. package/dist/models/contextLogger.js +8 -1
  32. package/dist/models/userSession.d.ts +2 -0
  33. package/dist/models/userSession.js +2 -0
  34. package/dist/schedule/anonymousContext.js +2 -0
  35. package/dist/service/AuthService.js +7 -0
  36. package/dist/service/EnumInfoService.d.ts +1 -1
  37. package/dist/service/EnumInfoService.js +32 -26
  38. package/dist/service/UserSessionService.d.ts +6 -1
  39. package/dist/service/UserSessionService.js +26 -17
  40. package/dist/service/WorkbenchService.d.ts +1 -0
  41. package/dist/service/WorkbenchService.js +27 -1
  42. package/dist/service/base/cache/CacheServiceFactory.d.ts +20 -0
  43. package/dist/service/base/cache/CacheServiceFactory.js +67 -0
  44. package/dist/service/base/cache/FatcmsBaseCtxCache.d.ts +19 -0
  45. package/dist/service/base/cache/FatcmsBaseCtxCache.js +38 -0
  46. package/dist/service/base/cache/FatcmsBaseDiskCache.d.ts +24 -0
  47. package/dist/service/base/cache/FatcmsBaseDiskCache.js +81 -0
  48. package/dist/service/base/cache/FatcmsBaseMemoryCache.d.ts +18 -0
  49. package/dist/service/base/cache/FatcmsBaseMemoryCache.js +66 -0
  50. package/dist/service/base/cache/FatcmsBaseNoneCache.d.ts +10 -0
  51. package/dist/service/base/cache/FatcmsBaseNoneCache.js +19 -0
  52. package/dist/service/base/{RedisCacheService.d.ts → cache/FatcmsBaseRedisCache.d.ts} +6 -6
  53. package/dist/service/base/cache/FatcmsBaseRedisCache.js +39 -0
  54. package/dist/service/crudstd/CrudStdService.d.ts +6 -4
  55. package/dist/service/crudstd/CrudStdService.js +24 -10
  56. package/dist/service/curd/CurdMixByDictService.d.ts +1 -2
  57. package/dist/service/curd/CurdMixByDictService.js +12 -8
  58. package/dist/service/curd/CurdMixByLinkToCustomService.d.ts +1 -2
  59. package/dist/service/curd/CurdMixByLinkToCustomService.js +17 -13
  60. package/dist/service/curd/CurdMixBySysConfigService.d.ts +4 -0
  61. package/dist/service/curd/CurdMixBySysConfigService.js +50 -10
  62. package/dist/service/curd/CurdMixByWorkbenchService.d.ts +2 -1
  63. package/dist/service/curd/CurdMixByWorkbenchService.js +30 -22
  64. package/package.json +1 -1
  65. package/src/config/config.default.ts +8 -0
  66. package/src/controller/base/BaseApiController.ts +39 -0
  67. package/src/controller/gateway/CrudStdGatewayController.ts +42 -10
  68. package/src/controller/gateway/FileController.ts +2 -2
  69. package/src/controller/gateway/PublicApiController.ts +72 -28
  70. package/src/controller/manage/DocLibManageApi.ts +5 -5
  71. package/src/controller/manage/FileManageApi.ts +5 -5
  72. package/src/controller/manage/MenuManageApi.ts +4 -4
  73. package/src/index.ts +1 -1
  74. package/src/interface.ts +2 -0
  75. package/src/libs/crud-pro/exceptions.ts +2 -0
  76. package/src/libs/crud-pro/models/keys.ts +6 -6
  77. package/src/libs/crud-pro/services/CrudProOriginToExecuteSql.ts +7 -1
  78. package/src/libs/crud-pro/utils/sqlConvert/convertColumnName.ts +18 -0
  79. package/src/libs/utils/AsymmetricCrypto.ts +310 -0
  80. package/src/libs/utils/format-url.ts +16 -0
  81. package/src/libs/utils/render-utils.ts +56 -15
  82. package/src/middleware/global.middleware.ts +8 -0
  83. package/src/models/SystemEntities.ts +43 -0
  84. package/src/models/WorkbenchInfoTools.ts +18 -0
  85. package/src/models/bizmodels.ts +12 -0
  86. package/src/models/contextLogger.ts +10 -1
  87. package/src/models/userSession.ts +4 -0
  88. package/src/schedule/anonymousContext.ts +2 -0
  89. package/src/service/AuthService.ts +11 -0
  90. package/src/service/EnumInfoService.ts +35 -22
  91. package/src/service/UserSessionService.ts +29 -18
  92. package/src/service/WorkbenchService.ts +42 -2
  93. package/src/service/base/cache/CacheServiceFactory.ts +66 -0
  94. package/src/service/base/cache/FatcmsBaseCtxCache.ts +47 -0
  95. package/src/service/base/cache/FatcmsBaseDiskCache.ts +87 -0
  96. package/src/service/base/cache/FatcmsBaseMemoryCache.ts +74 -0
  97. package/src/service/base/cache/FatcmsBaseNoneCache.ts +24 -0
  98. package/src/service/base/cache/FatcmsBaseRedisCache.ts +48 -0
  99. package/src/service/crudstd/CrudStdService.ts +28 -12
  100. package/src/service/curd/CurdMixByAccountService.ts +1 -0
  101. package/src/service/curd/CurdMixByDictService.ts +14 -8
  102. package/src/service/curd/CurdMixByLinkToCustomService.ts +21 -12
  103. package/src/service/curd/CurdMixBySysConfigService.ts +60 -11
  104. package/src/service/curd/CurdMixByWorkbenchService.ts +31 -24
  105. package/src/service/proxyapi/WeightedRandom.ts +1 -1
  106. package/src/service/proxyapi/WeightedRoundRobin.ts +1 -1
  107. package/dist/service/base/RedisCacheService.js +0 -57
  108. package/src/service/base/RedisCacheService.ts +0 -42
@@ -4,6 +4,7 @@ import { Context } from '@midwayjs/koa';
4
4
  import { parseJsonObject } from './functions';
5
5
  import { getExtLocalLoaderPort } from './fatcms-request';
6
6
  import { TypeUtils } from '../crud-pro/utils/TypeUtils';
7
+ import { ISessionInfo } from '@/models/userSession';
7
8
 
8
9
  // const demo = {"schema":[
9
10
  // {"title":"资源配置","name":"fileList","type":"array","properties":{"fileUrl":{"label":"文件URL","component":"Input","xProps":{"hasClear":true},"width":500}}}],
@@ -20,7 +21,7 @@ interface IRenderUtilsProps {
20
21
  ctx: Context;
21
22
  package_assets: any;
22
23
  workbenchInfo: any;
23
- userInfo?: any;
24
+ userInfo?: ISessionInfo;
24
25
  appInfo?: any;
25
26
  fatcmscsrftoken: string;
26
27
  }
@@ -63,22 +64,53 @@ function parseCookie(cookieStr: string): any {
63
64
  return cookies;
64
65
  }
65
66
 
67
+
68
+ function isMobileUserAgent(userAgent = '') {
69
+ // 空值处理
70
+ if (!userAgent) return false;
71
+
72
+ // 转为小写统一匹配
73
+ const ua = userAgent.toLowerCase();
74
+
75
+ // 【核心】移动端关键词(覆盖主流系统)
76
+ const mobileKeywords = /android|iphone|ipod|ios|mobile|blackberry|iemobile|opera mini|windows phone|harmonyos/i;
77
+ // 【排除项】平板/PC关键词(避免误判)
78
+ const excludeKeywords = /ipad|tablet|playbook|kindle|pc|windows nt|macintosh|linux x86_64/i;
79
+
80
+ // 判断规则:包含移动端关键词 + 不包含排除项关键词
81
+ const isMatchMobile = mobileKeywords.test(ua);
82
+ const isExclude = excludeKeywords.test(ua);
83
+
84
+ return isMatchMobile && !isExclude;
85
+ }
86
+
66
87
  class RenderUtils {
67
- private readonly ctx: Context;
68
- private readonly fileList: IFileElement[];
69
- private readonly workbenchInfo: any;
70
- private readonly userInfo: any;
71
- private readonly appInfo: any;
72
- private readonly fatcmscsrftoken: string;
88
+ public readonly ctx: Context;
89
+ public readonly fileList: IFileElement[];
90
+ public readonly workbenchInfo: any;
91
+ public readonly userInfo: ISessionInfo;
92
+ public readonly appInfo: any;
93
+ public readonly cookieInfo: any;
94
+ public readonly fatcmscsrftoken: string;
95
+ public readonly isMobileUserAgent: boolean;
96
+ public readonly isMobileByQuery: boolean;
73
97
 
74
98
  constructor(props: IRenderUtilsProps) {
75
99
  this.ctx = props.ctx;
100
+ const query = props.ctx?.query || {};
101
+ const headers = props.ctx?.headers || {};
76
102
  this.workbenchInfo = props.workbenchInfo || {};
77
- this.userInfo = props.userInfo || {};
103
+ this.userInfo = props.userInfo || ({} as ISessionInfo);
78
104
  this.appInfo = props.appInfo || {};
79
105
  this.fatcmscsrftoken = props.fatcmscsrftoken;
106
+ this.cookieInfo = parseCookie(headers?.cookie);
107
+ this.isMobileUserAgent = isMobileUserAgent(headers['user-agent'] || '');
108
+ this.isMobileByQuery = `${query['__is_mobile_request__']}` === 'true'
109
+
80
110
  const packageAssets = parseJsonObject(props.package_assets) || {};
81
111
  const fileList = _.get(packageAssets, 'data.fileList');
112
+
113
+
82
114
  if (Array.isArray(fileList)) {
83
115
  this.fileList = fileList.filter(f => {
84
116
  return f && f.fileUrl && typeof f.fileUrl === 'string' && f.fileUrl.length > 5; // 至少五个字符。
@@ -88,11 +120,11 @@ class RenderUtils {
88
120
  const time = moment().format('YYYY-MM-DD HH:mm:ss.SSS');
89
121
  console.info(
90
122
  time +
91
- ' 解析fileList为空==>' +
92
- JSON.stringify({
93
- workbench_code: this.workbenchInfo?.workbench_code,
94
- app_code: this.appInfo?.app_code,
95
- })
123
+ ' 解析fileList为空==>' +
124
+ JSON.stringify({
125
+ workbench_code: this.workbenchInfo?.workbench_code,
126
+ app_code: this.appInfo?.app_code,
127
+ })
96
128
  );
97
129
  }
98
130
  }
@@ -102,7 +134,9 @@ class RenderUtils {
102
134
  }
103
135
 
104
136
  renderUserInfo() {
105
- return `<script>window.__user_info = ${JSON.stringify(this.userInfo)} </script>`;
137
+ const userInfoClone = { ...this.userInfo };
138
+ delete userInfoClone.privateKey;
139
+ return `<script>window.__user_info = ${JSON.stringify(userInfoClone)} </script>`;
106
140
  }
107
141
 
108
142
  renderWorkbenchInfo() {
@@ -115,9 +149,13 @@ class RenderUtils {
115
149
  return `<script>window.__app_info = ${JSON.stringify(infoPick)}</script>`;
116
150
  }
117
151
 
152
+
153
+
154
+
155
+
118
156
  renderCookieInfo(keys: string) {
119
157
  try {
120
- const cookies = parseCookie(this.ctx.headers?.cookie);
158
+ const cookies = this.cookieInfo;
121
159
  const cookieObj = {};
122
160
  if (typeof keys === 'string') {
123
161
  const keyArr = keys.split(',');
@@ -132,6 +170,9 @@ class RenderUtils {
132
170
  }
133
171
  }
134
172
 
173
+
174
+
175
+
135
176
  renderJsAssets() {
136
177
  const fileList = this.fileList.filter(s => {
137
178
  return s.fileType === 'js' || s.fileUrl.endsWith('.js');
@@ -10,6 +10,7 @@ import { isEnableDebug, isEnableSuperAdmin } from '@/libs/utils/fatcms-request';
10
10
  import { ContextLogger } from '@/models/contextLogger';
11
11
  import { Stream } from 'node:stream';
12
12
  import { VisitStatService } from '@/service/VisitStatService';
13
+ import { WorkbenchInfoTools } from '@/models/WorkbenchInfoTools';
13
14
 
14
15
  function isFunction(fun: any): boolean {
15
16
  return typeof fun === 'function';
@@ -256,6 +257,13 @@ export class GlobalMiddleware implements IMiddleware<Context, NextFunction> {
256
257
 
257
258
  ctx.userSession = new UserSessionInfo(sessionInfo, isSuperAdmin);
258
259
  ctx.workbenchInfo = await workbenchService.getCurrentHostWorkbenchInfo();
260
+ ctx.workbenchInfoTools = new WorkbenchInfoTools(ctx.workbenchInfo);
261
+
262
+ // 校验是否支持该路径
263
+ const isSupportTheNodePath = workbenchService.isSupportTheNodePathByWorkbenchInfo(ctx.workbenchInfo, ctx.path);
264
+ if (!isSupportTheNodePath) {
265
+ return CommonResult.errorRes('非法访问!此站点不支持该路径!', 'NOT_SUPPORT_PATH');
266
+ }
259
267
 
260
268
  // 数据埋点
261
269
  await trackRequest(ctx);
@@ -121,3 +121,46 @@ export interface IWorkbenchEntity {
121
121
  workbench_type: number;
122
122
  package_assets?: string;
123
123
  }
124
+
125
+ export enum CacheLevelEnum {
126
+ NONE = 'NONE', // 不缓存
127
+ CONTEXT = 'CONTEXT', // 请求上下文缓存
128
+ MEMORY = 'MEMORY', // 内存缓存
129
+ REDIS = 'REDIS', // Redis缓存
130
+ DISK = 'DISK', // 磁盘缓存
131
+ }
132
+
133
+ export enum CacheNameEnum {
134
+ CurdMixByDict = 'CurdMixByDict',
135
+ CurdMixByLinkToCustom = 'CurdMixByLinkToCustom',
136
+ CurdMixBySysConfig = 'CurdMixBySysConfig',
137
+ CurdMixByWorkbench = 'CurdMixByWorkbench',
138
+ GetParsedCrudStdAppInfo = 'GetParsedCrudStdAppInfo',
139
+ GetWorkbenchMenu = 'GetWorkbenchMenu',
140
+ GetNavPageInfo = 'GetNavPageInfo',
141
+ GetEnumInfoByCode = 'GetEnumInfoByCode',
142
+ UserSessionBySessionId = 'UserSessionBySessionId',
143
+ UserSessionByAsyncTaskId = 'UserSessionByAsyncTaskId',
144
+ }
145
+
146
+
147
+ export interface IWorkbenchConfig {
148
+ nodeApiWhiteEnable?: boolean; // 接口白名单开关
149
+ nodeApiWhiteList?: string[]; // 接口白名单,完整匹配
150
+ nodeApiWhitePrefix?: string[]; // 接口白名单,根据前缀匹配
151
+
152
+ crudStdAppInfoCacheLevel?: CacheLevelEnum; // 零代码CRUD接口白名单开关. 默认: MEMORY
153
+ crudStdAppInfoCacheSecond?: number; // 零代码CRUD接口白名单缓存时间,单位:秒。 默认 5分钟
154
+
155
+ publicApiNavPageInfoCacheLevel?: CacheLevelEnum; // 公开API导航页缓存级别. 默认: MEMORY
156
+ publicApiNavPageInfoCacheSecond?: number; // 公开API导航页缓存时间,单位:秒。 默认 5分钟
157
+
158
+ publicApiMenuCacheLevel?: CacheLevelEnum; // 公开API菜单缓存级别. 默认: MEMORY
159
+ publicApiMenuCacheSecond?: number; // 公开API菜单缓存时间,单位:秒。 默认 5分钟
160
+
161
+ queryEnumInfoCacheLevel?: CacheLevelEnum; // 查询枚举缓存级别. 默认: MEMORY
162
+ queryEnumInfoCacheSecond?: number; // 查询枚举缓存时间,单位:秒。 默认 5分钟
163
+
164
+ curdMixByCommonCacheLevel?: CacheLevelEnum; // CrudMixService 缓存级别. 默认: MEMORY
165
+ curdMixByCommonCacheSecond?: number; // CrudMixService 缓存时间,单位:秒。 默认 5分钟
166
+ }
@@ -0,0 +1,18 @@
1
+ import { parseJsonObject } from "@/libs/utils/functions";
2
+ import { IWorkbenchConfig, IWorkbenchEntity } from "./SystemEntities";
3
+
4
+
5
+ export class WorkbenchInfoTools {
6
+ private parsedConfigContent: IWorkbenchConfig = null;
7
+ constructor(private readonly workbenchInfo: IWorkbenchEntity) {
8
+ }
9
+
10
+ public getWorkbenchConfig(): IWorkbenchConfig {
11
+ if (this.parsedConfigContent) {
12
+ return this.parsedConfigContent;
13
+ }
14
+ const configObj: IWorkbenchConfig = parseJsonObject(this.workbenchInfo?.config_content);
15
+ this.parsedConfigContent = configObj || {};
16
+ return this.parsedConfigContent;
17
+ }
18
+ }
@@ -125,3 +125,15 @@ export interface IStdCrudExportInputParams {
125
125
  primaryKey: any; // 公共
126
126
  pageSize: number; // 公共
127
127
  }
128
+
129
+ export interface ICacheServiceBase {
130
+ getJsonObject(key: string): Promise<any>;
131
+ setJsonObject(key: string, obj: any, cacheSecond?: number): Promise<any>;
132
+ removeItem(key: string): Promise<any>;
133
+ }
134
+
135
+
136
+ export interface IfatcmsCacheConfig {
137
+ diskCacheDir: string;
138
+ keyPrefix: string;
139
+ }
@@ -54,17 +54,25 @@ class ContextLogger implements ILoggerContext {
54
54
  private readonly ctx: Context;
55
55
  private readonly app: koa.Application;
56
56
  private readonly logList: any = [];
57
+ private currentTime: number;
57
58
 
58
59
  constructor(ctx: Context) {
59
60
  this.app = ctx.app;
60
61
  this.ctx = ctx;
61
62
  this.logList = [];
63
+ this.currentTime = Date.now();
62
64
  }
63
65
 
64
66
  getLogList() {
65
67
  return this.logList;
66
68
  }
67
69
 
70
+ private getDiffTime(): number {
71
+ const diffTime = Date.now() - this.currentTime;
72
+ this.currentTime = Date.now();
73
+ return diffTime;
74
+ }
75
+
68
76
  debug(msg: any, ...args: any[]): void {
69
77
  this.getLogger().debug(msg, ...args);
70
78
  this.appendLogList('debug', msg, ...args);
@@ -97,7 +105,8 @@ class ContextLogger implements ILoggerContext {
97
105
  }
98
106
  const time = moment().format('YYYY-MM-DD HH:mm:ss.SSS');
99
107
  const caller = getCallerInfo();
100
- this.logList.push({ level, msg, args, caller, time });
108
+ const diffTime = this.getDiffTime();
109
+ this.logList.push({ level, msg, args, caller, time, diffTime });
101
110
  }
102
111
 
103
112
  private getLogger(): ILogger {
@@ -24,6 +24,8 @@ interface ISessionInfo {
24
24
  accountId: string;
25
25
  workbenchCode: string;
26
26
  accountType: string;
27
+ publicKey: string; // 公钥
28
+ privateKey: string; // 私钥
27
29
  bizExt?: any;
28
30
  }
29
31
 
@@ -57,6 +59,8 @@ function createEmptySessionInfo(superAdmin: boolean): ISessionInfo {
57
59
  accountId: '',
58
60
  workbenchCode: '',
59
61
  accountType: '',
62
+ publicKey: '',
63
+ privateKey: '',
60
64
  };
61
65
  }
62
66
 
@@ -14,6 +14,8 @@ const anonymousUserInfo: ISessionInfo = {
14
14
  accountId: '',
15
15
  workbenchCode: '',
16
16
  accountType: SYS_ACCOUNT_TYPE,
17
+ publicKey: '',
18
+ privateKey: '',
17
19
  };
18
20
 
19
21
  const anonymousWorkbenchInfo: IWorkbenchEntity = {
@@ -12,6 +12,7 @@ import { CommonResult } from '@/libs/utils/common-dto';
12
12
  import { isEnableSuperAdmin } from '@/libs/utils/fatcms-request';
13
13
  import { CommonException } from '@/libs/crud-pro/exceptions';
14
14
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
15
+ import { AsymmetricCrypto } from '@/libs/utils/AsymmetricCrypto';
15
16
 
16
17
  @Provide()
17
18
  export class AuthService {
@@ -100,6 +101,10 @@ export class AuthService {
100
101
  functionCodes = await this.queryFunctionCodeList(roleCodes);
101
102
  }
102
103
 
104
+
105
+ const { publicKey, privateKey } = await AsymmetricCrypto.generateKeyPair();
106
+
107
+
103
108
  const sessionInfo: ISessionInfo = {
104
109
  nickName: consumerUserInfo.nickName,
105
110
  avatar: consumerUserInfo.avatar,
@@ -110,6 +115,8 @@ export class AuthService {
110
115
  accountId: consumerUserInfo.accountId,
111
116
  workbenchCode: consumerUserInfo.workbenchCode,
112
117
  accountType: consumerUserInfo.accountType,
118
+ publicKey,
119
+ privateKey,
113
120
  };
114
121
 
115
122
  await this.userSessionService.saveUserSession(sessionInfo);
@@ -142,6 +149,8 @@ export class AuthService {
142
149
  const roleCodes = await this.queryUserRoleCodeList(accountId);
143
150
  const functionCodes = await this.queryFunctionCodeList(roleCodes);
144
151
 
152
+ const { publicKey, privateKey } = await AsymmetricCrypto.generateKeyPair();
153
+
145
154
  const sessionInfo: ISessionInfo = {
146
155
  nickName: userAccount.nick_name,
147
156
  avatar: userAccount.avatar,
@@ -153,6 +162,8 @@ export class AuthService {
153
162
  workbenchCode,
154
163
  accountType: SYS_ACCOUNT_TYPE,
155
164
  bizExt: {},
165
+ publicKey,
166
+ privateKey,
156
167
  };
157
168
 
158
169
  if (bizExt && typeof bizExt === 'object') {
@@ -5,10 +5,10 @@ import { CurdProService } from './curd/CurdProService';
5
5
  import { RelatedType } from './curd/CurdMixUtils';
6
6
  import { SystemTables } from '@/models/SystemTables';
7
7
  import { parseConfigContentToEnumInfo, IEnumInfo } from '@/libs/utils/parseConfig';
8
- import { RedisCacheService } from './base/RedisCacheService';
9
8
  import { CurdMixByLinkToCustomService } from './curd/CurdMixByLinkToCustomService';
10
9
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
11
- import { RedisKeys } from '@/models/RedisKeys';
10
+ import { CacheServiceFactory } from './base/cache/CacheServiceFactory';
11
+ import { CacheLevelEnum, CacheNameEnum } from '@/models/SystemEntities';
12
12
 
13
13
  interface IQueryEnumInfo {
14
14
  type: RelatedType;
@@ -24,6 +24,10 @@ function filterEmpty(e: IEnumInfo): boolean {
24
24
  return e.label && typeof e.value !== 'undefined';
25
25
  }
26
26
 
27
+ const DEFAULT_CACHE_LEVEL = CacheLevelEnum.MEMORY;
28
+ const DEFAULT_CACHE_SECOND = 60 * 5;
29
+
30
+
27
31
  @Provide()
28
32
  export class EnumInfoService {
29
33
  @Inject()
@@ -35,33 +39,42 @@ export class EnumInfoService {
35
39
  private curdMixByLinkToCustomService: CurdMixByLinkToCustomService;
36
40
 
37
41
  @Inject()
38
- private redisCacheService: RedisCacheService;
42
+ private cacheServiceFactory: CacheServiceFactory;
39
43
 
40
44
  async queryEnumInfo(codeList: IQueryEnumInfo[], refreshCache: boolean): Promise<IQueryEnumResult[]> {
41
- const expireSecond = 2 * 60;
45
+ const { queryEnumInfoCacheLevel, queryEnumInfoCacheSecond } = this.ctx.workbenchInfoTools.getWorkbenchConfig() || {};
46
+
47
+ const getEnumInfoByCode = async (type: RelatedType, code: string) => {
48
+ if (type === RelatedType.linkToCustom) {
49
+ return await this.queryEnumInfoByLinkToCustom(code);
50
+ }
51
+
52
+ const cacheKey = `${type}@@${code}`;
53
+ const emums = refreshCache ? null : await this.cacheServiceFactory.getJsonObjectCache({
54
+ cacheLevel: queryEnumInfoCacheLevel || DEFAULT_CACHE_LEVEL,
55
+ cacheSecond: queryEnumInfoCacheSecond || DEFAULT_CACHE_SECOND,
56
+ cacheKey,
57
+ refreshCache,
58
+ cacheName: CacheNameEnum.GetEnumInfoByCode,
59
+ getter: async () => {
60
+ if (type === RelatedType.sysCfgEnum) {
61
+ return await this.queryEnumInfoBySysCfgEnum(code);
62
+ } else if (type === RelatedType.dict) {
63
+ return await this.queryEnumInfoItemByDict(code);
64
+ }
65
+ return [];
66
+ },
67
+ });
68
+ return emums;
69
+ };
42
70
 
43
71
  const result = [];
44
72
  for (let i = 0; i < codeList.length; i++) {
45
73
  const { code, type } = codeList[i];
46
- const cacheKey = `${RedisKeys.QUERY_ENUM_INFO_PREFIX}${type}@@${code}`;
47
- const emums = refreshCache ? null : await this.redisCacheService.getJsonObject(cacheKey);
48
- if (!Array.isArray(emums) || emums.length === 0) {
49
- let values;
50
- if (type === RelatedType.sysCfgEnum) {
51
- values = await this.queryEnumInfoBySysCfgEnum(code);
52
- await this.redisCacheService.setJsonObject(cacheKey, values, expireSecond);
53
- } else if (type === RelatedType.dict) {
54
- values = await this.queryEnumInfoItemByDict(code);
55
- await this.redisCacheService.setJsonObject(cacheKey, values, expireSecond);
56
- } else if (type === RelatedType.linkToCustom) {
57
- values = await this.queryEnumInfoByLinkToCustom(code);
58
- // await this.redisCacheService.setJsonObject(cacheKey, values, expireSecond);
59
- }
60
- result.push({ type, code, values });
61
- } else {
62
- result.push({ type, code, values: emums });
63
- }
74
+ const emums = await getEnumInfoByCode(type, code);
75
+ result.push({ type, code, values: emums });
64
76
  }
77
+
65
78
  return result;
66
79
  }
67
80
 
@@ -2,13 +2,14 @@ import { Config, Inject, Provide } from '@midwayjs/core';
2
2
  import { Context } from '@midwayjs/koa';
3
3
  import { parseJsonObject } from '@/libs/utils/functions';
4
4
  import { ISessionInfo, SESSION_ID_KEY, sessionCookieCfg } from '@/models/userSession';
5
- import { RedisCacheService } from './base/RedisCacheService';
6
- import { RedisKeys } from '@/models/RedisKeys';
7
5
  import { HEADER_KEY_RUN_BY_ASYNC_TASK_ID } from '@/models/AsyncTaskModel';
8
6
  import { CurdProService } from './curd/CurdProService';
9
7
  import { KeysOfSimpleSQL } from '@/libs/crud-pro/models/keys';
10
8
  import { SystemTables } from '@/models/SystemTables';
11
9
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
10
+ import { CacheServiceFactory } from './base/cache/CacheServiceFactory';
11
+ import { CacheLevelEnum, CacheNameEnum } from '@/models/SystemEntities';
12
+ import { ICacheServiceBase } from '@/models/bizmodels';
12
13
 
13
14
  function pickUserAvatar(avatar: any): string {
14
15
  if (!avatar) {
@@ -37,11 +38,7 @@ function pickUserAvatar(avatar: any): string {
37
38
  return null;
38
39
  }
39
40
 
40
- const SESSION_KEEP_TIME_SECOND: number = 3600 * 24 * 30;
41
-
42
- const toCacheKey = (sessionId: string): string => {
43
- return `${RedisKeys.USER_SESSION_PREFIX}${sessionId}`;
44
- };
41
+ const SESSION_KEEP_TIME_SECOND: number = 3600 * 24 * 7;
45
42
 
46
43
  @Provide()
47
44
  export class UserSessionService {
@@ -49,7 +46,7 @@ export class UserSessionService {
49
46
  private ctx: Context;
50
47
 
51
48
  @Inject()
52
- private redisCacheService: RedisCacheService;
49
+ private cacheServiceFactory: CacheServiceFactory;
53
50
 
54
51
  @Inject()
55
52
  private curdProService: CurdProService;
@@ -68,6 +65,15 @@ export class UserSessionService {
68
65
  return SESSION_KEEP_TIME_SECOND;
69
66
  }
70
67
 
68
+
69
+ /**
70
+ * 获取用户会话缓存服务
71
+ * @returns
72
+ */
73
+ private getUserSessionCacheService(): ICacheServiceBase {
74
+ return this.cacheServiceFactory.getCacheService(CacheLevelEnum.REDIS, CacheNameEnum.UserSessionBySessionId);
75
+ }
76
+
71
77
  /**
72
78
  * 保存用户会话信息。
73
79
  * 用户登录后,在业务的项目中,可以调用此函数,更新bizExt字段
@@ -77,7 +83,8 @@ export class UserSessionService {
77
83
  sessionInfo.avatar = pickUserAvatar(sessionInfo.avatar);
78
84
  const sessionId = sessionInfo.sessionId;
79
85
  const keepTime = this.getSessionKeepTimeSecond();
80
- await this.redisCacheService.setJsonObject(toCacheKey(sessionId), sessionInfo, keepTime);
86
+ const userSessionCache = this.getUserSessionCacheService();
87
+ await userSessionCache.setJsonObject(sessionId, sessionInfo, keepTime);
81
88
  }
82
89
 
83
90
  /**
@@ -85,7 +92,8 @@ export class UserSessionService {
85
92
  * @param sessionId
86
93
  */
87
94
  public async removeUserSession(sessionId: string) {
88
- return await this.redisCacheService.removeItem(toCacheKey(sessionId));
95
+ const userSessionCache = this.getUserSessionCacheService();
96
+ return await userSessionCache.removeItem(sessionId);
89
97
  }
90
98
 
91
99
  /**
@@ -97,7 +105,8 @@ export class UserSessionService {
97
105
  if (!sessionId) {
98
106
  return null;
99
107
  }
100
- return await this.redisCacheService.getJsonObject(toCacheKey(sessionId));
108
+ const userSessionCache = this.getUserSessionCacheService();
109
+ return await userSessionCache.getJsonObject(sessionId);
101
110
  }
102
111
 
103
112
  /**
@@ -105,7 +114,7 @@ export class UserSessionService {
105
114
  * @param asyncTaskId
106
115
  */
107
116
  private async getCurrentUserSessionByAsyncTaskId(asyncTaskId: string): Promise<ISessionInfo> {
108
- const taskSessionCacheKey = `${RedisKeys.USER_SESSION_ASYNC_TASK_ID_PREFIX}${asyncTaskId}`;
117
+ const taskSessionCacheKey = `${asyncTaskId}`;
109
118
 
110
119
  const loadSessionInfo = async (): Promise<ISessionInfo> => {
111
120
  const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
@@ -124,7 +133,6 @@ export class UserSessionService {
124
133
  sqlDbType: SystemDbType,
125
134
  }
126
135
  );
127
-
128
136
  const asyncTaskObj = asyncTaskRes.getOneObj();
129
137
  if (!asyncTaskObj) {
130
138
  return null;
@@ -132,11 +140,14 @@ export class UserSessionService {
132
140
  return parseJsonObject(asyncTaskObj.created_user_session);
133
141
  };
134
142
 
135
- let sessionInfo1 = await this.redisCacheService.getJsonObject(taskSessionCacheKey);
136
- if (!sessionInfo1) {
137
- sessionInfo1 = await loadSessionInfo();
138
- await this.redisCacheService.setJsonObject(taskSessionCacheKey, sessionInfo1, 3600); // 只缓存一小时即可
139
- }
143
+ const sessionInfo1 = await this.cacheServiceFactory.getJsonObjectCache({
144
+ cacheLevel: CacheLevelEnum.REDIS,
145
+ cacheName: CacheNameEnum.UserSessionByAsyncTaskId,
146
+ cacheKey: taskSessionCacheKey,
147
+ cacheSecond: 3600,
148
+ getter: loadSessionInfo,
149
+ });
150
+
140
151
  return sessionInfo1;
141
152
  }
142
153
 
@@ -7,12 +7,13 @@ import { BaseService } from './base/BaseService';
7
7
  import { CurdProService } from './curd/CurdProService';
8
8
  import { CommonException } from '@/libs/crud-pro/exceptions';
9
9
  import { parseJsonObject } from '@/libs/utils/functions';
10
- import { IWorkbenchEntity } from '@/models/SystemEntities';
10
+ import { IWorkbenchConfig, IWorkbenchEntity } from '@/models/SystemEntities';
11
11
  import { IScheduleService } from '@/interface';
12
12
  import { getDebugWorkbenchCode } from '@/libs/utils/fatcms-request';
13
13
  import { MixinUtils } from '@/libs/crud-pro/utils/MixinUtils';
14
14
  import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
15
15
  import { HEADER_KEY_RUN_BY_ASYNC_TASK_WORKBENCH_CODE } from '@/models/AsyncTaskModel';
16
+ import { formatHost } from '@/libs/utils/format-url';
16
17
  //
17
18
  // const lruCache = new LRUCache<string, any>({
18
19
  // max: 500,
@@ -170,10 +171,12 @@ export class WorkbenchService extends BaseService implements IScheduleService {
170
171
  * @returns
171
172
  */
172
173
  private getCurrentHost(): string {
173
- const host = this.ctx.request.host;
174
+ let host = this.ctx.request.host;
174
175
  if (!host) {
175
176
  throw new CommonException('GET_CURRENT_HOST_ERROR', '获取当前域名失败');
176
177
  }
178
+
179
+ host = formatHost(host);
177
180
  return host;
178
181
  }
179
182
 
@@ -217,4 +220,41 @@ export class WorkbenchService extends BaseService implements IScheduleService {
217
220
  }
218
221
  return true;
219
222
  }
223
+
224
+
225
+
226
+ public isSupportTheNodePathByWorkbenchInfo(workbenchInfo: IWorkbenchEntity, path: string): boolean {
227
+
228
+ // 非node api路径,直接返回true
229
+ if(!path.startsWith('/ns/')) {
230
+ return true;
231
+ }
232
+
233
+ const configObj: IWorkbenchConfig = parseJsonObject(workbenchInfo?.config_content);
234
+ // 为开启校验开关。开关为true时,才校验。
235
+ if (!configObj || !configObj.nodeApiWhiteEnable) {
236
+ return true;
237
+ }
238
+ let isOkByList = false;
239
+ const { nodeApiWhiteList, nodeApiWhitePrefix } = configObj;
240
+ if (Array.isArray(nodeApiWhiteList) && nodeApiWhiteList.length > 0) {
241
+ isOkByList = nodeApiWhiteList.includes(path);
242
+ }
243
+
244
+ if (isOkByList) {
245
+ return true;
246
+ }
247
+
248
+ let isOkByPrefix = false;
249
+ if (Array.isArray(nodeApiWhitePrefix) && nodeApiWhitePrefix.length > 0) {
250
+ isOkByPrefix = nodeApiWhitePrefix.some((prefix) => path.startsWith(prefix));
251
+ }
252
+ return isOkByPrefix;
253
+
254
+ }
255
+
256
+
257
+
258
+
259
+
220
260
  }