const codeData = {
SUCCESS: {
code: 0,
msg: "成功"
}
};
[{ code: 0, msg: '成功' }, { code: -1, msg: '请求失败' }, { code: 1, msg: '测试结果' }]
// const codeMap = new Map();
// codeMap.set(0, "请求成功");
// codeMap.set(-1, "请求失败");
// codeMap.set(1, "测试结果");
// codeMap.set(502, "咦! 您的网络跑丢了?");
// console.log(codeMap.get(0));
function CommonResponse() {
// console.log(arguments.length);
let code = codeData.SUCCESS.code,
msg = codeData.SUCCESS.msg,
data = {};
if (arguments.length >= 1) { //有值
const oneData = arguments[0]
const twoData = arguments[1]
const threeData = arguments[2]
if (typeof oneData === 'object') { //如果第一值是对象
if (oneData.hasOwnProperty('code') && oneData.hasOwnProperty('msg')) { //如果对象里面的key有code 和msg
code = oneData.code;
msg = oneData.msg;
if (twoData) {
data = twoData
}
} else {
//可能就是data数据
data = oneData;
}
} else if (typeof oneData === 'number') {
code = oneData;
if (twoData && typeof twoData === 'string') {
msg = twoData
} else if (typeof twoData === 'object') {
data = twoData;
}
if (threeData) {
data = threeData;
}
}
}
return {
code,
msg,
data
}
}
let test = codeData.SUCCESS;
let test1 = { name: "123", age: 12 };
console.log('测试:', JSON.stringify(CommonResponse(test)))
console.log('测试1:', JSON.stringify(CommonResponse(test1)))
console.log('测试2:', JSON.stringify(CommonResponse(10, test1)));
console.log('测试3:', JSON.stringify(CommonResponse(0, "我是大白", test1)));
console