后端: 添加统一响应格式工具

This commit is contained in:
Backend Developer 2026-03-10 09:54:56 +00:00
parent ae9203316d
commit 22b22e1187

View 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;