nsgm-cli 2.1.30 → 2.1.31

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.
@@ -32,8 +32,17 @@ interface MenuItem {
32
32
  subMenus?: SubMenuItem[];
33
33
  }
34
34
 
35
- // 从环境变量获取 prefix
36
- const prefix = process.env.NEXT_PUBLIC_PREFIX || "";
35
+ // 安全获取 prefix 的辅助函数
36
+ const getPrefix = () => {
37
+ try {
38
+ if (typeof process !== "undefined" && process.env && process.env.NEXT_PUBLIC_PREFIX) {
39
+ return process.env.NEXT_PUBLIC_PREFIX;
40
+ }
41
+ } catch {
42
+ // 浏览器环境使用空字符串
43
+ }
44
+ return "";
45
+ };
37
46
 
38
47
  const getLocationKey = () => {
39
48
  const result = {
@@ -53,6 +62,7 @@ const getLocationKey = () => {
53
62
  const locationIndex = locationStr.indexOf("/");
54
63
  locationStr = locationStr.substring(locationIndex);
55
64
 
65
+ const prefix = getPrefix();
56
66
  if (prefix && locationStr.indexOf(prefix) !== -1) {
57
67
  locationStr = locationStr.split(prefix)[1];
58
68
  }
@@ -92,6 +102,7 @@ const getLocationKey = () => {
92
102
 
93
103
  const routerPush = (router: any, url: string) => {
94
104
  if (router && url && typeof window !== "undefined") {
105
+ const prefix = getPrefix();
95
106
  if (prefix && url.indexOf(prefix) === -1) {
96
107
  url = prefix + url;
97
108
  }
@@ -29,11 +29,20 @@ const tempStore = configureStore({
29
29
 
30
30
  export type AppDispatch = typeof tempStore.dispatch;
31
31
 
32
+ // 安全获取 NODE_ENV
33
+ const isDevToolsEnabled = () => {
34
+ try {
35
+ return typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production";
36
+ } catch {
37
+ return false;
38
+ }
39
+ };
40
+
32
41
  function initStore(initialState?: any): EnhancedStore {
33
42
  return configureStore({
34
43
  reducer: combineReducer,
35
44
  preloadedState: initialState,
36
- devTools: process.env.NODE_ENV !== "production",
45
+ devTools: isDevToolsEnabled(),
37
46
  middleware: (getDefaultMiddleware) =>
38
47
  getDefaultMiddleware({
39
48
  serializableCheck: {
@@ -54,7 +54,9 @@ export const SideMenu = styled(Menu)`
54
54
  padding: 8px 0;
55
55
  `;
56
56
 
57
- export const ContentLayout = styled(Layout)<{ collapsed?: boolean }>`
57
+ export const ContentLayout = styled(Layout).withConfig({
58
+ shouldForwardProp: (prop) => prop !== "collapsed",
59
+ })<{ collapsed?: boolean }>`
58
60
  display: flex;
59
61
  flex-direction: column;
60
62
  flex: 1;
@@ -1,17 +1,37 @@
1
1
  import _ from "lodash";
2
2
 
3
3
  export const getLocalEnv = () => {
4
- let env = process.env.NEXT_PUBLIC_ENV || "uat";
5
- env = env.toLowerCase();
6
- return env;
4
+ // 安全地访问 process.env,避免在浏览器中直接访问 process 对象
5
+ let env = "uat";
6
+ try {
7
+ if (typeof process !== "undefined" && process.env && process.env.NEXT_PUBLIC_ENV) {
8
+ env = process.env.NEXT_PUBLIC_ENV;
9
+ }
10
+ } catch {
11
+ // 在浏览器环境中 process 可能未定义,使用默认值
12
+ }
13
+ return env.toLowerCase();
7
14
  };
8
15
 
9
16
  export const getLocalApiPrefix = () => {
10
- let protocol = process.env.NEXT_PUBLIC_PROTOCOL || "http";
11
- let host = process.env.NEXT_PUBLIC_HOST || "localhost";
12
- let port = process.env.NEXT_PUBLIC_PORT || "3000";
13
- const prefix = process.env.NEXT_PUBLIC_PREFIX || "";
14
- const isExport = process.env.NEXT_PUBLIC_IS_EXPORT === "true";
17
+ // 安全地访问 process.env
18
+ let protocol = "http";
19
+ let host = "localhost";
20
+ let port = "3000";
21
+ let prefix = "";
22
+ let isExport = false;
23
+
24
+ try {
25
+ if (typeof process !== "undefined" && process.env) {
26
+ protocol = process.env.NEXT_PUBLIC_PROTOCOL || protocol;
27
+ host = process.env.NEXT_PUBLIC_HOST || host;
28
+ port = process.env.NEXT_PUBLIC_PORT || port;
29
+ prefix = process.env.NEXT_PUBLIC_PREFIX || "";
30
+ isExport = process.env.NEXT_PUBLIC_IS_EXPORT === "true";
31
+ }
32
+ } catch {
33
+ // 在浏览器环境中 process 可能未定义,使用默认值
34
+ }
15
35
 
16
36
  let localApiPrefix = "";
17
37
 
@@ -32,9 +32,15 @@ export const GRAPHQL_CONFIG = {
32
32
  cookieName: "csrfToken",
33
33
  },
34
34
 
35
- // 开发模式配置
36
- development: {
37
- enableDebugLogs: process.env.NODE_ENV === "development",
35
+ // 开发模式配置 - 使用 getter 延迟访问 process
36
+ get development() {
37
+ let enableDebugLogs = false;
38
+ try {
39
+ enableDebugLogs = typeof process !== "undefined" && process.env && process.env.NODE_ENV === "development";
40
+ } catch {
41
+ // 浏览器环境使用默认值
42
+ }
43
+ return { enableDebugLogs };
38
44
  },
39
45
  };
40
46
 
@@ -3,10 +3,9 @@ import { setCookie, getCookie, delCookie } from "./cookie";
3
3
  import { getUrlParamByKey, getLocalApiPrefix, getLocalEnv, handleXSS } from "./common";
4
4
  import _ from "lodash";
5
5
 
6
- const env = getLocalEnv();
7
-
8
- const LOGIN_COOKIE_ID = `${env}_cas_nsgm`;
9
- const LOGIN_COOKIE_USER = `${env}_nsgm_user`;
6
+ // 延迟初始化 cookie 名称,避免在模块加载时访问 process
7
+ const getLoginCookieId = () => `${getLocalEnv()}_cas_nsgm`;
8
+ const getLoginCookieUser = () => `${getLocalEnv()}_nsgm_user`;
10
9
 
11
10
  const getPrincipalUrl = () => {
12
11
  const url = `${getLocalApiPrefix()}/rest/sso/sessionCheck`;
@@ -67,8 +66,8 @@ const handleLocationHref = () => {
67
66
  };
68
67
 
69
68
  const jumpToLogin = () => {
70
- delCookie(LOGIN_COOKIE_ID);
71
- delCookie(LOGIN_COOKIE_USER);
69
+ delCookie(getLoginCookieId());
70
+ delCookie(getLoginCookieUser());
72
71
 
73
72
  if (typeof window !== "undefined") {
74
73
  window.location.href = `${window.location.origin}/login`;
@@ -115,7 +114,7 @@ const storeLoginUser = (userAttr: any, callback: any) => {
115
114
  "name",
116
115
  "sn",
117
116
  ]);
118
- setCookie(LOGIN_COOKIE_USER, user, null);
117
+ setCookie(getLoginCookieUser(), user, null);
119
118
  callback?.(JSON.parse(user));
120
119
  } else {
121
120
  callback?.();
@@ -124,7 +123,7 @@ const storeLoginUser = (userAttr: any, callback: any) => {
124
123
 
125
124
  const storeLogin = (cookie: any, cookieExpire: any, userAttr: any, callback: any) => {
126
125
  if (cookie) {
127
- setCookie(LOGIN_COOKIE_ID, cookie, cookieExpire);
126
+ setCookie(getLoginCookieId(), cookie, cookieExpire);
128
127
  }
129
128
 
130
129
  storeLoginUser(userAttr, callback);
@@ -166,7 +165,7 @@ const validateLogin = (ticket: string, name = "", callback: any) => {
166
165
  };
167
166
 
168
167
  export const login = (callback: any) => {
169
- const cookieLoginValue = getCookie(LOGIN_COOKIE_ID);
168
+ const cookieLoginValue = getCookie(getLoginCookieId());
170
169
 
171
170
  if (typeof window !== "undefined") {
172
171
  const locationHref = window.location.href;
@@ -32,9 +32,13 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
35
38
  Object.defineProperty(exports, "__esModule", { value: true });
36
39
  exports.initConfigFiles = exports.initTestFiles = exports.initTypesFiles = exports.initRootFiles = exports.initScriptsFiles = exports.initPublicFiles = exports.initServerFiles = exports.initPagesFiles = exports.initClientFiles = void 0;
37
40
  const path_1 = __importStar(require("path"));
41
+ const shelljs_1 = __importDefault(require("shelljs"));
38
42
  const constants_1 = require("./constants");
39
43
  const utils_1 = require("./utils");
40
44
  const fs_1 = require("fs");
@@ -549,6 +553,15 @@ const initRootFiles = (dictionary, newDestFolder) => {
549
553
  ];
550
554
  // 3. 复制文件
551
555
  copyMultipleFiles(fileMappings);
556
+ // 4. 替换 mysql.config.js 中的数据库名称为项目名称(中横线替换为下划线)
557
+ const destMysqlConfigPath = (0, path_1.resolve)(baseDestPath + ROOT_FILES.mysqlConfig);
558
+ if ((0, fs_1.existsSync)(destMysqlConfigPath)) {
559
+ const projectName = path_1.default.basename(baseDestPath);
560
+ // 将中横线替换为下划线,生成数据库名称
561
+ const databaseName = projectName.replace(/-/g, "_").toLowerCase();
562
+ shelljs_1.default.sed("-i", /database: 'crm_demo'/, `database: '${databaseName}'`, destMysqlConfigPath);
563
+ console.log(`Database name set to: ${databaseName}`);
564
+ }
552
565
  console.log("Root files initialization completed");
553
566
  return {
554
567
  destPackagePath: (0, path_1.resolve)(baseDestPath + constants_1.packagePath),
@@ -556,13 +556,13 @@ export default Page`;
556
556
  */
557
557
  generateClientValidation() {
558
558
  const integerFields = this.getFormFields().filter((field) => field.type === "integer");
559
- if (integerFields.length === 0) {
560
- return "";
561
- }
562
- const validations = integerFields.map((field) => {
559
+ const decimalFields = this.getFormFields().filter((field) => field.type === "decimal");
560
+ const validations = [];
561
+ // 生成 integer 字段验证
562
+ integerFields.forEach((field) => {
563
563
  const fieldName = field.name;
564
564
  const capitalizedName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
565
- return ` // 验证${fieldName}
565
+ validations.push(` // 验证${fieldName}
566
566
  const ${fieldName}Value = modalObj.${fieldName}
567
567
  if (${fieldName}Value !== undefined && ${fieldName}Value !== null && ${fieldName}Value !== '') {
568
568
  const parsed${capitalizedName} = parseInt(${fieldName}Value, 10)
@@ -571,8 +571,26 @@ export default Page`;
571
571
  return
572
572
  }
573
573
  modalObj.${fieldName} = parsed${capitalizedName}
574
- }`;
574
+ }`);
575
575
  });
576
+ // 生成 decimal 字段验证
577
+ decimalFields.forEach((field) => {
578
+ const fieldName = field.name;
579
+ const capitalizedName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
580
+ validations.push(` // 验证${fieldName}
581
+ const ${fieldName}Value = modalObj.${fieldName}
582
+ if (${fieldName}Value !== undefined && ${fieldName}Value !== null && ${fieldName}Value !== '') {
583
+ const parsed${capitalizedName} = parseFloat(${fieldName}Value)
584
+ if (isNaN(parsed${capitalizedName})) {
585
+ message.error(\`${fieldName}必须是数字,当前值: "\${${fieldName}Value}"\`)
586
+ return
587
+ }
588
+ modalObj.${fieldName} = parsed${capitalizedName}
589
+ }`);
590
+ });
591
+ if (validations.length === 0) {
592
+ return "";
593
+ }
576
594
  return `${validations.join("\n\n")}\n\n`;
577
595
  }
578
596
  }