后端: 添加统一响应格式工具
This commit is contained in:
parent
ae9203316d
commit
22b22e1187
36
backend/src/utils/response.js
Normal file
36
backend/src/utils/response.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// 统一响应格式
|
||||||
|
class ApiResponse {
|
||||||
|
static success(data, message = 'success') {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message,
|
||||||
|
data,
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static error(message = 'error', code = 500) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message,
|
||||||
|
code,
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static paginate(data, page, pageSize, total) {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
total,
|
||||||
|
totalPages: Math.ceil(total / pageSize)
|
||||||
|
},
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ApiResponse;
|
||||||
Loading…
Reference in New Issue
Block a user