2 Star 11 Fork 8

BASE / base-cloud-v3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BaseResponseInter.js 1007 Bytes
一键复制 编辑 原始数据 按行查看 历史
BASE 提交于 2021-07-13 09:48 . 调整目录
const {
isUnstrictBoolean,
isUnstrictTrue,
isObject,
isNull,
isString
} = require("../util");
module.exports = (options = {}) => {
let { codeKey = "code", messageKey = "message", success, fail } = options;
success = success || { [codeKey]: "ok" };
fail = fail || { [codeKey]: "fail", [messageKey]: "操作失败" };
return async function response(ctx, next) {
try {
await next();
} catch (error) {
console.error("error :>> ", error);
ctx.body = { ...fail , [messageKey] : error.message } ;
return ;
}
//服务端响应结果
let body = ctx.body;
//返回undefined
if (isNull(body)) {
ctx.body = success;
return;
}
//返回1、0、true、false
if (isUnstrictBoolean(body)) {
ctx.body = isUnstrictTrue(body) ? success : fail;
return;
}
//返回JSON
if (isObject(body)) {
ctx.body = Object.assign( {} ,success , ctx.body );
return;
}
//返回字符串
if (isString(body)) {
ctx.body = { ...fail, [messageKey]: body };
}
};
};
NodeJS
1
https://gitee.com/phoooob/base-cloud-v3.git
git@gitee.com:phoooob/base-cloud-v3.git
phoooob
base-cloud-v3
base-cloud-v3
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891