30 lines
732 B
JavaScript
30 lines
732 B
JavaScript
module.exports = () => {
|
|
return async function formatResponse(ctx, next) {
|
|
try {
|
|
await next();
|
|
// 统一输出数据格式
|
|
if(ctx.page!=true){
|
|
if (ctx.body) {
|
|
ctx.body = {
|
|
code: ctx.status || 200,
|
|
data: ctx.body,
|
|
message: ctx.message || 'Success',
|
|
};
|
|
} else {
|
|
ctx.status = 404;
|
|
ctx.body = {
|
|
code: 404,
|
|
message: 'Not Found',
|
|
};
|
|
}
|
|
}
|
|
} catch (err) {
|
|
// 处理异常情况
|
|
ctx.status = err.status || 500;
|
|
ctx.body = {
|
|
code: ctx.status,
|
|
message: err.message || 'Internal Server Error',
|
|
};
|
|
}
|
|
};
|
|
}; |