chanjs 2.6.1 → 2.6.3
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/App.js +2 -20
- package/common/pages.js +61 -27
- package/middleware/template.js +4 -3
- package/package.json +1 -1
- package/response.js +1 -1
- package/utils/response.js +1 -1
package/App.js
CHANGED
|
@@ -9,7 +9,7 @@ import DatabaseManager from "./base/Database.js";
|
|
|
9
9
|
import { Paths } from "./config/index.js";
|
|
10
10
|
import { loadConfig, loaderSort, loadController } from "./helper/index.js";
|
|
11
11
|
import { Cors, setBody, setCookie, setFavicon, setHeader, setStatic, setTemplate, waf, log } from "./middleware/index.js";
|
|
12
|
-
import { notFoundResponse, parseDatabaseError,
|
|
12
|
+
import { notFoundResponse, parseDatabaseError, errorResponse } from "./utils/index.js";
|
|
13
13
|
import { importFile } from "./global/import.js";
|
|
14
14
|
|
|
15
15
|
import "./global/index.js";
|
|
@@ -218,27 +218,9 @@ class Chan {
|
|
|
218
218
|
const requestInfo = `${req.method} ${req.originalUrl}`;
|
|
219
219
|
console.error(`[Global Error] ${requestInfo} - ${errorInfo.file}:${errorInfo.line || "?"} - ${errorInfo.message}`);
|
|
220
220
|
|
|
221
|
-
if (err?.isError && err.message) {
|
|
222
|
-
const statusCode = err.statusCode >= 500 ? 500 : 400;
|
|
223
|
-
console.error(`[Global Error Details] Code: ${err.code || 500}, Status: ${statusCode}`);
|
|
224
|
-
return res.status(statusCode).json({
|
|
225
|
-
success: false,
|
|
226
|
-
msg: err.message,
|
|
227
|
-
code: err.code || 500,
|
|
228
|
-
data: process.env.NODE_ENV === "development" ? {
|
|
229
|
-
stack: err.stack,
|
|
230
|
-
file: errorInfo.file,
|
|
231
|
-
line: errorInfo.line,
|
|
232
|
-
column: errorInfo.column,
|
|
233
|
-
url: req.originalUrl,
|
|
234
|
-
method: req.method
|
|
235
|
-
} : {},
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
|
|
239
221
|
const parsedError = parseDatabaseError(err);
|
|
240
222
|
console.error(`[Global Error Details] Database Error - ${parsedError.msg}`);
|
|
241
|
-
res.status(parsedError.statusCode).json(
|
|
223
|
+
res.status(parsedError.statusCode).json(errorResponse(err, req));
|
|
242
224
|
});
|
|
243
225
|
}
|
|
244
226
|
|
package/common/pages.js
CHANGED
|
@@ -17,53 +17,87 @@ import path from 'path';
|
|
|
17
17
|
*/
|
|
18
18
|
export function pages(current, total, pageSize, href, query='') {
|
|
19
19
|
let pageTemp = [];
|
|
20
|
+
|
|
21
|
+
// 确保参数是数字
|
|
22
|
+
current = parseInt(current) || 1;
|
|
23
|
+
total = parseInt(total) || 0;
|
|
24
|
+
pageSize = parseInt(pageSize) || 10;
|
|
25
|
+
|
|
20
26
|
let totalPage = Math.ceil(total / pageSize);
|
|
21
|
-
|
|
27
|
+
|
|
28
|
+
// 如果没有数据或只有一页,不显示分页
|
|
29
|
+
if (totalPage <= 1 || total === 0) {
|
|
22
30
|
return '';
|
|
23
31
|
}
|
|
24
32
|
|
|
25
|
-
|
|
33
|
+
// 确保当前页在有效范围内
|
|
34
|
+
if (current < 1) current = 1;
|
|
35
|
+
if (current > totalPage) current = totalPage;
|
|
36
|
+
|
|
37
|
+
// 上一页
|
|
26
38
|
if (current == 1) {
|
|
27
39
|
pageTemp.push(`<li class="disabled">上一页</li>`);
|
|
28
40
|
} else {
|
|
29
|
-
pageTemp.push(`<li><a href='${href}${
|
|
41
|
+
pageTemp.push(`<li><a href='${href}${current - 1}.html${query}'>上一页</a></li>`);
|
|
30
42
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
43
|
+
|
|
44
|
+
// 页码逻辑
|
|
45
|
+
if (totalPage <= 7) {
|
|
46
|
+
// 总页数少于7页,全部显示
|
|
47
|
+
for (let i = 1; i <= totalPage; i++) {
|
|
48
|
+
if (current == i) {
|
|
49
|
+
pageTemp.push(`<li class="current">${i}</li>`);
|
|
37
50
|
} else {
|
|
38
|
-
pageTemp.push(`<li><a href='${href}${i
|
|
51
|
+
pageTemp.push(`<li><a href='${href}${i}.html${query}'>${i}</a></li>`);
|
|
39
52
|
}
|
|
40
53
|
}
|
|
41
54
|
} else {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
|
|
55
|
+
// 总页数大于7页,需要显示省略号
|
|
56
|
+
if (current <= 4) {
|
|
57
|
+
// 当前页在前4页:显示 1 2 3 4 5 ... 最后一页
|
|
58
|
+
for (let i = 1; i <= 5; i++) {
|
|
59
|
+
if (current == i) {
|
|
60
|
+
pageTemp.push(`<li class="current">${i}</li>`);
|
|
61
|
+
} else {
|
|
62
|
+
pageTemp.push(`<li><a href='${href}${i}.html${query}'>${i}</a></li>`);
|
|
63
|
+
}
|
|
49
64
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
pageTemp.push(`<li class="disabled">...</li>`);
|
|
66
|
+
pageTemp.push(`<li><a href='${href}${totalPage}.html${query}'>${totalPage}</a></li>`);
|
|
67
|
+
} else if (current >= totalPage - 3) {
|
|
68
|
+
// 当前页在后4页:显示 1 ... 最后5页
|
|
69
|
+
pageTemp.push(`<li><a href='${href}1.html${query}'>1</a></li>`);
|
|
70
|
+
pageTemp.push(`<li class="disabled">...</li>`);
|
|
71
|
+
for (let i = totalPage - 4; i <= totalPage; i++) {
|
|
72
|
+
if (current == i) {
|
|
73
|
+
pageTemp.push(`<li class="current">${i}</li>`);
|
|
74
|
+
} else {
|
|
75
|
+
pageTemp.push(`<li><a href='${href}${i}.html${query}'>${i}</a></li>`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
// 当前页在中间:显示 1 ... 当前页前1页 当前页 当前页后1页 ... 最后一页
|
|
80
|
+
pageTemp.push(`<li><a href='${href}1.html${query}'>1</a></li>`);
|
|
81
|
+
pageTemp.push(`<li class="disabled">...</li>`);
|
|
82
|
+
for (let i = current - 1; i <= current + 1; i++) {
|
|
83
|
+
if (current == i) {
|
|
84
|
+
pageTemp.push(`<li class="current">${i}</li>`);
|
|
85
|
+
} else {
|
|
86
|
+
pageTemp.push(`<li><a href='${href}${i}.html${query}'>${i}</a></li>`);
|
|
87
|
+
}
|
|
59
88
|
}
|
|
89
|
+
pageTemp.push(`<li class="disabled">...</li>`);
|
|
90
|
+
pageTemp.push(`<li><a href='${href}${totalPage}.html${query}'>${totalPage}</a></li>`);
|
|
60
91
|
}
|
|
61
92
|
}
|
|
93
|
+
|
|
94
|
+
// 下一页
|
|
62
95
|
if (current == totalPage) {
|
|
63
96
|
pageTemp.push(`<li class="disabled">下一页</li>`);
|
|
64
97
|
} else {
|
|
65
|
-
pageTemp.push(`<li><a href='${href}${
|
|
98
|
+
pageTemp.push(`<li><a href='${href}${current + 1}.html${query}'>下一页</a></li>`);
|
|
66
99
|
}
|
|
100
|
+
|
|
67
101
|
return pageTemp.join("");
|
|
68
102
|
}
|
|
69
103
|
|
package/middleware/template.js
CHANGED
|
@@ -23,11 +23,12 @@ import { importjs } from "../global/import.js";
|
|
|
23
23
|
*/
|
|
24
24
|
export let setTemplate = (app, config) => {
|
|
25
25
|
const { views, NODE_ENV } = config;
|
|
26
|
-
|
|
26
|
+
const isProduction = NODE_ENV === "production" || NODE_ENV === "prd";
|
|
27
|
+
console.log("cache", isProduction);
|
|
27
28
|
const all = [...views];
|
|
28
29
|
app.set("view options", {
|
|
29
|
-
debug:
|
|
30
|
-
cache:
|
|
30
|
+
debug: !isProduction,
|
|
31
|
+
cache: isProduction,
|
|
31
32
|
minimize: true,
|
|
32
33
|
});
|
|
33
34
|
app.set("view engine", "html");
|
package/package.json
CHANGED
package/response.js
CHANGED
|
@@ -78,7 +78,7 @@ export const error = ({ err, data = {}, code = 500 } = {}) => {
|
|
|
78
78
|
if (err) {
|
|
79
79
|
console.error("[DB Error]", err?.message || err);
|
|
80
80
|
const errorCode = err?.code && DB_ERROR[err.code] ? DB_ERROR[err.code] : getDefaultErrorCode(err);
|
|
81
|
-
const msg =
|
|
81
|
+
const msg = ERROR_MESSAGES[errorCode] || "操作失败";
|
|
82
82
|
|
|
83
83
|
return {
|
|
84
84
|
success: false,
|
package/utils/response.js
CHANGED
|
@@ -78,7 +78,7 @@ export const error = ({ err, data = {}, code = 500 } = {}) => {
|
|
|
78
78
|
if (err) {
|
|
79
79
|
console.error("[DB Error]", err?.message || err);
|
|
80
80
|
const errorCode = err?.code && DB_ERROR[err.code] ? DB_ERROR[err.code] : getDefaultErrorCode(err);
|
|
81
|
-
const msg =
|
|
81
|
+
const msg = ERROR_MESSAGES[errorCode] || "操作失败";
|
|
82
82
|
|
|
83
83
|
return {
|
|
84
84
|
success: false,
|