nsgm-cli 2.1.19 → 2.1.21
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.
- package/client/components/Button.tsx +3 -3
- package/client/components/ClientProviders.tsx +12 -12
- package/client/components/LanguageSwitcher.tsx +26 -26
- package/client/components/SSRSafeAntdProvider.tsx +7 -7
- package/client/components/SuppressHydrationWarnings.tsx +30 -30
- package/client/components/__tests__/Button.test.tsx +12 -12
- package/client/layout/index.tsx +124 -124
- package/client/redux/reducers.ts +2 -2
- package/client/redux/store.ts +24 -24
- package/client/redux/template/manage/actions.ts +40 -40
- package/client/redux/template/manage/reducers.ts +32 -32
- package/client/redux/template/manage/types.ts +19 -19
- package/client/service/template/manage.ts +29 -29
- package/client/styled/common.ts +6 -6
- package/client/styled/layout/index.ts +17 -17
- package/client/styled/template/manage.ts +19 -19
- package/client/utils/common.ts +54 -54
- package/client/utils/cookie.ts +30 -30
- package/client/utils/fetch.ts +111 -111
- package/client/utils/i18n.ts +41 -41
- package/client/utils/menu.tsx +12 -12
- package/client/utils/navigation.ts +22 -22
- package/client/utils/sso.ts +124 -124
- package/client/utils/suppressWarnings.ts +17 -17
- package/generation/prettierrc +6 -0
- package/lib/args.js +19 -19
- package/lib/cli/app.d.ts +1 -1
- package/lib/cli/app.js +2 -2
- package/lib/cli/commands/build.d.ts +1 -1
- package/lib/cli/commands/build.js +9 -9
- package/lib/cli/commands/create.d.ts +1 -1
- package/lib/cli/commands/create.js +36 -36
- package/lib/cli/commands/delete.d.ts +1 -1
- package/lib/cli/commands/delete.js +55 -55
- package/lib/cli/commands/export.d.ts +1 -1
- package/lib/cli/commands/export.js +12 -12
- package/lib/cli/commands/help.d.ts +1 -1
- package/lib/cli/commands/help.js +29 -29
- package/lib/cli/commands/init.d.ts +1 -1
- package/lib/cli/commands/init.js +31 -31
- package/lib/cli/commands/server.d.ts +1 -1
- package/lib/cli/commands/server.js +12 -12
- package/lib/cli/commands/upgrade.d.ts +1 -1
- package/lib/cli/commands/upgrade.js +13 -13
- package/lib/cli/commands/version.d.ts +1 -1
- package/lib/cli/commands/version.js +7 -7
- package/lib/cli/index.d.ts +13 -13
- package/lib/cli/parser.d.ts +1 -1
- package/lib/cli/parser.js +12 -12
- package/lib/cli/registry.d.ts +1 -1
- package/lib/cli/types.d.ts +2 -2
- package/lib/cli/utils/console.d.ts +2 -2
- package/lib/cli/utils/console.js +22 -22
- package/lib/cli/utils/index.d.ts +2 -2
- package/lib/cli/utils/prompt.d.ts +1 -1
- package/lib/cli/utils/prompt.js +98 -98
- package/lib/constants.js +28 -28
- package/lib/generate.d.ts +2 -2
- package/lib/generate.js +19 -19
- package/lib/generate_create.d.ts +1 -1
- package/lib/generate_create.js +38 -38
- package/lib/generate_delete.js +63 -63
- package/lib/generate_init.js +94 -94
- package/lib/generators/base-generator.d.ts +1 -1
- package/lib/generators/base-generator.js +23 -23
- package/lib/generators/file-generator.js +15 -15
- package/lib/generators/generator-factory.d.ts +5 -5
- package/lib/generators/i18n-generator.d.ts +1 -1
- package/lib/generators/i18n-generator.js +127 -127
- package/lib/generators/page-generator.d.ts +1 -1
- package/lib/generators/page-generator.js +25 -25
- package/lib/generators/resolver-generator.d.ts +1 -1
- package/lib/generators/resolver-generator.js +27 -27
- package/lib/generators/schema-generator.d.ts +1 -1
- package/lib/generators/schema-generator.js +4 -4
- package/lib/generators/service-generator.d.ts +1 -1
- package/lib/generators/service-generator.js +29 -29
- package/lib/generators/sql-generator.d.ts +1 -1
- package/lib/generators/sql-generator.js +10 -10
- package/lib/index.js +23 -23
- package/lib/server/csrf.d.ts +3 -3
- package/lib/server/csrf.js +20 -20
- package/lib/server/db.d.ts +1 -1
- package/lib/server/db.js +21 -21
- package/lib/server/graphql.js +26 -26
- package/lib/server/plugins/date.d.ts +1 -1
- package/lib/server/plugins/date.js +6 -6
- package/lib/server/utils/graphql-cache.js +5 -5
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/lib/utils/project-config.d.ts +1 -1
- package/lib/utils/project-config.js +20 -20
- package/lib/utils.js +3 -3
- package/package.json +1 -1
- package/pages/_app.tsx +62 -62
- package/pages/_document.tsx +15 -15
- package/pages/_error.tsx +26 -26
- package/pages/index.tsx +48 -48
- package/pages/login.tsx +64 -64
- package/pages/template/manage.tsx +175 -175
|
@@ -52,17 +52,17 @@ function applyProjectConfig(projectPath, config) {
|
|
|
52
52
|
*/
|
|
53
53
|
function ensureDefaultFeatures(config) {
|
|
54
54
|
console.log(`✅ 项目已配置默认功能栈: Next.js + Styled Components + GraphQL + MySQL + TypeScript + ESLint`);
|
|
55
|
-
console.log(`✅ 数据库配置: ${config.database ?
|
|
56
|
-
console.log(`✅ 包含功能: ${config.features.join(
|
|
55
|
+
console.log(`✅ 数据库配置: ${config.database ? "已启用" : "未启用"}`);
|
|
56
|
+
console.log(`✅ 包含功能: ${config.features.join(", ")}`);
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
59
|
* 更新 package.json 文件
|
|
60
60
|
*/
|
|
61
61
|
function updatePackageJson(projectPath, config) {
|
|
62
|
-
const packageJsonPath = path.join(projectPath,
|
|
62
|
+
const packageJsonPath = path.join(projectPath, "package.json");
|
|
63
63
|
if (fs.existsSync(packageJsonPath)) {
|
|
64
64
|
try {
|
|
65
|
-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath,
|
|
65
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
66
66
|
// 更新基本信息
|
|
67
67
|
packageJson.name = config.projectName;
|
|
68
68
|
packageJson.description = config.description;
|
|
@@ -90,49 +90,49 @@ function updatePackageJson(projectPath, config) {
|
|
|
90
90
|
* 更新 README.md 文件
|
|
91
91
|
*/
|
|
92
92
|
function updateReadme(projectPath, config) {
|
|
93
|
-
const readmePath = path.join(projectPath,
|
|
93
|
+
const readmePath = path.join(projectPath, "README.md");
|
|
94
94
|
if (fs.existsSync(readmePath)) {
|
|
95
95
|
try {
|
|
96
|
-
let readmeContent = fs.readFileSync(readmePath,
|
|
96
|
+
let readmeContent = fs.readFileSync(readmePath, "utf8");
|
|
97
97
|
// 替换项目名称
|
|
98
98
|
readmeContent = readmeContent.replace(/nsgm-cli-project/g, config.projectName);
|
|
99
99
|
// 如果有描述,添加到 README 开头
|
|
100
|
-
if (config.description && config.description !==
|
|
100
|
+
if (config.description && config.description !== "A NSGM fullstack project") {
|
|
101
101
|
const descriptionSection = `# ${config.projectName}\n\n${config.description}\n\n`;
|
|
102
102
|
// 检查是否已经有项目名称标题
|
|
103
|
-
if (readmeContent.startsWith(
|
|
103
|
+
if (readmeContent.startsWith("# ")) {
|
|
104
104
|
// 替换第一行
|
|
105
|
-
const lines = readmeContent.split(
|
|
105
|
+
const lines = readmeContent.split("\n");
|
|
106
106
|
lines[0] = `# ${config.projectName}`;
|
|
107
|
-
if (lines[1] ===
|
|
107
|
+
if (lines[1] === "" && lines[2] && !lines[2].startsWith("#")) {
|
|
108
108
|
lines[2] = config.description;
|
|
109
109
|
}
|
|
110
110
|
else {
|
|
111
|
-
lines.splice(2, 0,
|
|
111
|
+
lines.splice(2, 0, "", config.description);
|
|
112
112
|
}
|
|
113
|
-
readmeContent = lines.join(
|
|
113
|
+
readmeContent = lines.join("\n");
|
|
114
114
|
}
|
|
115
115
|
else {
|
|
116
116
|
readmeContent = descriptionSection + readmeContent;
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
119
|
// 添加作者信息
|
|
120
|
-
if (config.author && config.author !==
|
|
120
|
+
if (config.author && config.author !== "Your Name") {
|
|
121
121
|
const authorSection = `\n## 作者\n\n${config.author}\n`;
|
|
122
122
|
readmeContent += authorSection;
|
|
123
123
|
}
|
|
124
124
|
// 添加功能说明
|
|
125
125
|
if (config.features.length > 0) {
|
|
126
126
|
const featureMap = {
|
|
127
|
-
nextjs:
|
|
128
|
-
styled:
|
|
129
|
-
graphql:
|
|
130
|
-
mysql:
|
|
131
|
-
typescript:
|
|
132
|
-
eslint:
|
|
127
|
+
nextjs: "Next.js 全栈框架",
|
|
128
|
+
styled: "Styled Components CSS-in-JS",
|
|
129
|
+
graphql: "GraphQL API",
|
|
130
|
+
mysql: "MySQL 数据库",
|
|
131
|
+
typescript: "TypeScript 强类型支持",
|
|
132
|
+
eslint: "ESLint 代码质量检查",
|
|
133
133
|
};
|
|
134
134
|
const featureNames = config.features.map((feature) => featureMap[feature] || feature);
|
|
135
|
-
const featuresSection = `\n## 技术栈\n\n${featureNames.map((feature) => `- ${feature}`).join(
|
|
135
|
+
const featuresSection = `\n## 技术栈\n\n${featureNames.map((feature) => `- ${feature}`).join("\n")}\n`;
|
|
136
136
|
readmeContent += featuresSection;
|
|
137
137
|
}
|
|
138
138
|
fs.writeFileSync(readmePath, readmeContent);
|
package/lib/utils.js
CHANGED
|
@@ -84,18 +84,18 @@ const handleReplace = ({ regex, replacement, paths }) => {
|
|
|
84
84
|
exports.handleReplace = handleReplace;
|
|
85
85
|
const replaceInFileAll = async (array, index = 0, callback) => {
|
|
86
86
|
if (replaceInFileFlag) {
|
|
87
|
-
console.log(
|
|
87
|
+
console.log("replaceInFileAll", index);
|
|
88
88
|
const arrayLen = array.length;
|
|
89
89
|
if (index < arrayLen) {
|
|
90
90
|
const item = array[index];
|
|
91
91
|
(0, replace_in_file_1.replaceInFile)(item)
|
|
92
92
|
.then((changedFiles) => {
|
|
93
|
-
console.log(
|
|
93
|
+
console.log("Modified files:", changedFiles);
|
|
94
94
|
(0, exports.replaceInFileAll)(array, ++index, callback);
|
|
95
95
|
})
|
|
96
96
|
.catch((error) => {
|
|
97
97
|
if (error) {
|
|
98
|
-
console.error(
|
|
98
|
+
console.error("Error occurred:", error);
|
|
99
99
|
}
|
|
100
100
|
});
|
|
101
101
|
}
|
package/package.json
CHANGED
package/pages/_app.tsx
CHANGED
|
@@ -1,104 +1,104 @@
|
|
|
1
1
|
// 必须在所有其他导入之前执行
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import React, { useEffect, useState } from
|
|
5
|
-
import { Provider } from
|
|
6
|
-
import { Spin } from
|
|
7
|
-
import { appWithTranslation } from
|
|
8
|
-
import { useRouter } from
|
|
9
|
-
import { useStore } from
|
|
10
|
-
import { Loading } from
|
|
11
|
-
import LayoutComponent from
|
|
12
|
-
import ClientProviders from
|
|
13
|
-
import SuppressHydrationWarnings from
|
|
14
|
-
import SSRSafeAntdProvider from
|
|
15
|
-
import { login } from
|
|
16
|
-
import { getAntdLocale } from
|
|
17
|
-
import { navigateToLogin } from
|
|
18
|
-
import nextI18NextConfig from
|
|
19
|
-
import
|
|
2
|
+
import "@/utils/suppressWarnings";
|
|
3
|
+
|
|
4
|
+
import React, { useEffect, useState } from "react";
|
|
5
|
+
import { Provider } from "react-redux";
|
|
6
|
+
import { Spin } from "antd";
|
|
7
|
+
import { appWithTranslation } from "next-i18next";
|
|
8
|
+
import { useRouter } from "next/router";
|
|
9
|
+
import { useStore } from "@/redux/store";
|
|
10
|
+
import { Loading } from "@/styled/common";
|
|
11
|
+
import LayoutComponent from "@/layout";
|
|
12
|
+
import ClientProviders from "@/components/ClientProviders";
|
|
13
|
+
import SuppressHydrationWarnings from "@/components/SuppressHydrationWarnings";
|
|
14
|
+
import SSRSafeAntdProvider from "@/components/SSRSafeAntdProvider";
|
|
15
|
+
import { login } from "@/utils/sso";
|
|
16
|
+
import { getAntdLocale } from "@/utils/i18n";
|
|
17
|
+
import { navigateToLogin } from "@/utils/navigation";
|
|
18
|
+
import nextI18NextConfig from "../next-i18next.config.js";
|
|
19
|
+
import "antd/dist/reset.css";
|
|
20
20
|
|
|
21
21
|
const theme = {
|
|
22
22
|
colors: {
|
|
23
|
-
primary:
|
|
23
|
+
primary: "#0070f3",
|
|
24
24
|
},
|
|
25
|
-
}
|
|
25
|
+
};
|
|
26
26
|
|
|
27
27
|
const App = ({ Component, pageProps }) => {
|
|
28
|
-
const store = useStore(pageProps.initialReduxState)
|
|
29
|
-
const router = useRouter()
|
|
30
|
-
const [ssoUser, setSsoUser] = useState(null)
|
|
31
|
-
const [pageLoad, setPageLoad] = useState(false)
|
|
32
|
-
const [loginChecked, setLoginChecked] = useState(false)
|
|
33
|
-
const [mounted, setMounted] = useState(false)
|
|
34
|
-
const [currentLocale, setCurrentLocale] = useState(
|
|
28
|
+
const store = useStore(pageProps.initialReduxState);
|
|
29
|
+
const router = useRouter();
|
|
30
|
+
const [ssoUser, setSsoUser] = useState(null);
|
|
31
|
+
const [pageLoad, setPageLoad] = useState(false);
|
|
32
|
+
const [loginChecked, setLoginChecked] = useState(false);
|
|
33
|
+
const [mounted, setMounted] = useState(false);
|
|
34
|
+
const [currentLocale, setCurrentLocale] = useState("zh-CN");
|
|
35
35
|
|
|
36
36
|
// 检查是否为特殊页面(登录页、错误页)
|
|
37
37
|
// 避免在服务器端访问 router.pathname
|
|
38
38
|
const isSpecialPage =
|
|
39
|
-
Component.displayName ===
|
|
40
|
-
Component.name ===
|
|
41
|
-
Component.displayName ===
|
|
42
|
-
Component.name ===
|
|
39
|
+
Component.displayName === "ErrorPage" ||
|
|
40
|
+
Component.name === "ErrorPage" ||
|
|
41
|
+
Component.displayName === "LoginPage" ||
|
|
42
|
+
Component.name === "LoginPage";
|
|
43
43
|
|
|
44
44
|
// Get Antd locale based on current locale
|
|
45
|
-
const antdLocale = getAntdLocale(currentLocale)
|
|
45
|
+
const antdLocale = getAntdLocale(currentLocale);
|
|
46
46
|
|
|
47
47
|
useEffect(() => {
|
|
48
|
-
setMounted(true)
|
|
48
|
+
setMounted(true);
|
|
49
49
|
// 只在客户端设置当前语言
|
|
50
|
-
if (typeof window !==
|
|
51
|
-
const locale = router.locale ||
|
|
52
|
-
setCurrentLocale(locale)
|
|
50
|
+
if (typeof window !== "undefined") {
|
|
51
|
+
const locale = router.locale || "zh-CN";
|
|
52
|
+
setCurrentLocale(locale);
|
|
53
53
|
}
|
|
54
|
-
}, [router.locale])
|
|
54
|
+
}, [router.locale]);
|
|
55
55
|
|
|
56
56
|
useEffect(() => {
|
|
57
|
-
if (!mounted) return
|
|
57
|
+
if (!mounted) return;
|
|
58
58
|
|
|
59
59
|
// 对于特殊页面,跳过登录检查
|
|
60
60
|
if (isSpecialPage) {
|
|
61
|
-
setLoginChecked(true)
|
|
62
|
-
setPageLoad(true)
|
|
63
|
-
return
|
|
61
|
+
setLoginChecked(true);
|
|
62
|
+
setPageLoad(true);
|
|
63
|
+
return;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
// 检查当前路径是否为登录页或错误页
|
|
67
|
-
const isLoginPage = typeof window !==
|
|
67
|
+
const isLoginPage = typeof window !== "undefined" && window.location.pathname === "/login";
|
|
68
68
|
const isErrorPage =
|
|
69
|
-
typeof window !==
|
|
70
|
-
(window.location.pathname ===
|
|
71
|
-
window.location.pathname ===
|
|
72
|
-
window.location.pathname ===
|
|
69
|
+
typeof window !== "undefined" &&
|
|
70
|
+
(window.location.pathname === "/404" ||
|
|
71
|
+
window.location.pathname === "/500" ||
|
|
72
|
+
window.location.pathname === "/_error");
|
|
73
73
|
|
|
74
74
|
// 如果是登录页或错误页,直接设置加载完成,不进行登录检查
|
|
75
75
|
if (isLoginPage || isErrorPage) {
|
|
76
|
-
setLoginChecked(true)
|
|
77
|
-
setPageLoad(true)
|
|
78
|
-
return
|
|
76
|
+
setLoginChecked(true);
|
|
77
|
+
setPageLoad(true);
|
|
78
|
+
return;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
// 检查是否有登录凭证
|
|
82
|
-
const hasLoginCookie = typeof window !==
|
|
82
|
+
const hasLoginCookie = typeof window !== "undefined" && document.cookie.includes("_cas_nsgm");
|
|
83
83
|
|
|
84
84
|
// 如果没有登录凭证,直接跳转到登录页面(保持当前语言)
|
|
85
|
-
if (!hasLoginCookie && typeof window !==
|
|
86
|
-
navigateToLogin(router)
|
|
87
|
-
return
|
|
85
|
+
if (!hasLoginCookie && typeof window !== "undefined") {
|
|
86
|
+
navigateToLogin(router);
|
|
87
|
+
return;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
// 否则执行登录检查
|
|
91
91
|
login((user: any) => {
|
|
92
92
|
if (user) {
|
|
93
|
-
setSsoUser(user)
|
|
93
|
+
setSsoUser(user);
|
|
94
94
|
}
|
|
95
|
-
setLoginChecked(true)
|
|
96
|
-
})
|
|
95
|
+
setLoginChecked(true);
|
|
96
|
+
});
|
|
97
97
|
|
|
98
98
|
setTimeout(() => {
|
|
99
|
-
setPageLoad(true)
|
|
100
|
-
}, 100)
|
|
101
|
-
}, [mounted, isSpecialPage])
|
|
99
|
+
setPageLoad(true);
|
|
100
|
+
}, 100);
|
|
101
|
+
}, [mounted, isSpecialPage]);
|
|
102
102
|
|
|
103
103
|
return (
|
|
104
104
|
<>
|
|
@@ -130,10 +130,10 @@ const App = ({ Component, pageProps }) => {
|
|
|
130
130
|
</ClientProviders>
|
|
131
131
|
</SSRSafeAntdProvider>
|
|
132
132
|
</>
|
|
133
|
-
)
|
|
134
|
-
}
|
|
133
|
+
);
|
|
134
|
+
};
|
|
135
135
|
|
|
136
136
|
// 移除 getInitialProps 以启用静态优化
|
|
137
137
|
// 如果需要页面级别的数据获取,请在各个页面中使用 getStaticProps 或 getServerSideProps
|
|
138
138
|
|
|
139
|
-
export default appWithTranslation(App, nextI18NextConfig)
|
|
139
|
+
export default appWithTranslation(App, nextI18NextConfig);
|
package/pages/_document.tsx
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import Document, { Html, Head, Main, NextScript, DocumentContext } from
|
|
2
|
-
import React from
|
|
3
|
-
import { ServerStyleSheet } from
|
|
1
|
+
import Document, { Html, Head, Main, NextScript, DocumentContext } from "next/document";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { ServerStyleSheet } from "styled-components";
|
|
4
4
|
|
|
5
5
|
const MyDocument = (props) => {
|
|
6
6
|
// 从 props 中获取语言信息,如果没有则默认为 zh-CN
|
|
7
|
-
const locale = props.locale ||
|
|
7
|
+
const locale = props.locale || "zh-CN";
|
|
8
8
|
|
|
9
9
|
return (
|
|
10
10
|
<Html lang={locale}>
|
|
@@ -86,31 +86,31 @@ const MyDocument = (props) => {
|
|
|
86
86
|
<NextScript />
|
|
87
87
|
</body>
|
|
88
88
|
</Html>
|
|
89
|
-
)
|
|
90
|
-
}
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
91
|
|
|
92
92
|
// MyDocument.renderDocument = Document.renderDocument
|
|
93
93
|
|
|
94
94
|
MyDocument.getInitialProps = async (ctx: DocumentContext) => {
|
|
95
|
-
const sheet = new ServerStyleSheet()
|
|
96
|
-
const originalRenderPage = ctx.renderPage
|
|
95
|
+
const sheet = new ServerStyleSheet();
|
|
96
|
+
const originalRenderPage = ctx.renderPage;
|
|
97
97
|
|
|
98
98
|
try {
|
|
99
99
|
ctx.renderPage = () =>
|
|
100
100
|
originalRenderPage({
|
|
101
101
|
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
|
|
102
|
-
})
|
|
102
|
+
});
|
|
103
103
|
|
|
104
|
-
const initialProps = await Document?.getInitialProps(ctx)
|
|
104
|
+
const initialProps = await Document?.getInitialProps(ctx);
|
|
105
105
|
|
|
106
106
|
return {
|
|
107
107
|
...initialProps,
|
|
108
|
-
locale: ctx.locale ||
|
|
108
|
+
locale: ctx.locale || "zh-CN",
|
|
109
109
|
styles: [initialProps.styles, sheet.getStyleElement()],
|
|
110
|
-
}
|
|
110
|
+
};
|
|
111
111
|
} finally {
|
|
112
|
-
sheet.seal()
|
|
112
|
+
sheet.seal();
|
|
113
113
|
}
|
|
114
|
-
}
|
|
114
|
+
};
|
|
115
115
|
|
|
116
|
-
export default MyDocument
|
|
116
|
+
export default MyDocument;
|
package/pages/_error.tsx
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import React from
|
|
2
|
-
import { Result, Button } from
|
|
3
|
-
import { NextPageContext } from
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Result, Button } from "antd";
|
|
3
|
+
import { NextPageContext } from "next";
|
|
4
4
|
|
|
5
5
|
interface ErrorProps {
|
|
6
|
-
statusCode?: number
|
|
6
|
+
statusCode?: number;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
function ErrorPage({ statusCode }: ErrorProps) {
|
|
10
10
|
const getErrorMessage = () => {
|
|
11
11
|
if (statusCode === 404) {
|
|
12
|
-
return
|
|
12
|
+
return "页面未找到";
|
|
13
13
|
} else if (statusCode === 500) {
|
|
14
|
-
return
|
|
14
|
+
return "服务器错误";
|
|
15
15
|
}
|
|
16
|
-
return
|
|
17
|
-
}
|
|
16
|
+
return "发生了未知错误";
|
|
17
|
+
};
|
|
18
18
|
|
|
19
19
|
const getErrorTitle = () => {
|
|
20
20
|
if (statusCode === 404) {
|
|
21
|
-
return
|
|
21
|
+
return "404";
|
|
22
22
|
} else if (statusCode === 500) {
|
|
23
|
-
return
|
|
23
|
+
return "500";
|
|
24
24
|
}
|
|
25
|
-
return
|
|
26
|
-
}
|
|
25
|
+
return "错误";
|
|
26
|
+
};
|
|
27
27
|
|
|
28
28
|
return (
|
|
29
29
|
<div
|
|
30
30
|
style={{
|
|
31
|
-
minHeight:
|
|
32
|
-
display:
|
|
33
|
-
alignItems:
|
|
34
|
-
justifyContent:
|
|
35
|
-
padding:
|
|
31
|
+
minHeight: "100vh",
|
|
32
|
+
display: "flex",
|
|
33
|
+
alignItems: "center",
|
|
34
|
+
justifyContent: "center",
|
|
35
|
+
padding: "20px",
|
|
36
36
|
}}
|
|
37
37
|
>
|
|
38
38
|
<Result
|
|
39
|
-
status={statusCode === 404 ?
|
|
39
|
+
status={statusCode === 404 ? "404" : "500"}
|
|
40
40
|
title={getErrorTitle()}
|
|
41
41
|
subTitle={getErrorMessage()}
|
|
42
42
|
extra={
|
|
43
43
|
<Button
|
|
44
44
|
type="primary"
|
|
45
45
|
onClick={() => {
|
|
46
|
-
if (typeof window !==
|
|
47
|
-
window.location.href =
|
|
46
|
+
if (typeof window !== "undefined") {
|
|
47
|
+
window.location.href = "/";
|
|
48
48
|
}
|
|
49
49
|
}}
|
|
50
50
|
>
|
|
@@ -53,14 +53,14 @@ function ErrorPage({ statusCode }: ErrorProps) {
|
|
|
53
53
|
}
|
|
54
54
|
/>
|
|
55
55
|
</div>
|
|
56
|
-
)
|
|
56
|
+
);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
ErrorPage.getInitialProps = ({ res, err }: NextPageContext) => {
|
|
60
|
-
const statusCode = res ? res.statusCode : err ? err.statusCode : 404
|
|
61
|
-
return { statusCode }
|
|
62
|
-
}
|
|
60
|
+
const statusCode = res ? res.statusCode : err ? err.statusCode : 404;
|
|
61
|
+
return { statusCode };
|
|
62
|
+
};
|
|
63
63
|
|
|
64
|
-
ErrorPage.displayName =
|
|
64
|
+
ErrorPage.displayName = "ErrorPage";
|
|
65
65
|
|
|
66
|
-
export default ErrorPage
|
|
66
|
+
export default ErrorPage;
|