// 树结构获取字典
getTreeDic() {
let arr=[];
//防化
let param1 = {
groupID: "9C68BC06C6A0576C6CEE"
};
// 调用获取字典方法,匹配字典,生成zds
treeDicEvents(param1).then((res) => {
if (res.data.result.resultCode == 10000) {
let modelsList1=res.data.constantTreeModels[0];
arr.push(modelsList1);
//防护
let param2 = {
groupID: "01606133111E25CD1D7D"
};
// 调用获取字典方法,匹配字典,生成zds
treeDicEvents(param2).then((res) => {
if (res.data.result.resultCode == 10000) {
let modelsList3=res.data.constantTreeModels[0];
arr.push(modelsList3);
//信息化
let param3 = {
groupID: "B6677142242640A1CF8B"
};
// 调用获取字典方法,匹配字典,生成zds
treeDicEvents(param3).then((res) => {
if (res.data.result.resultCode == 10000) {
let modelsList5=res.data.constantTreeModels[0];
arr.push(modelsList5);
this.zds.lxArr=this.getCascaderData(arr);
} else {
this.$message.error('获取字典失败');
}
});
} else {
this.$message.error('获取字典失败');
}
});
} else {
this.$message.error('获取字典失败');
}
});
},
先确定这么长的回调是因为什么,重复的函数有哪些
if (res.data.result.resultCode == 10000) {
let modelsList5=res.data.constantTreeModels[0];
arr.push(modelsList5);
this.zds.lxArr=this.getCascaderData(arr);
} else {
this.$message.error('获取字典失败');
}
});
既然我们每个回调都用到了treeDicEvents
,和回调判断
,不如直接拆开
// 先写个函数用来判断,return false直接从此打断同步任务
let arr = []
let param = ["9C68BC06C6A0576C6CEE","B6677142242640A1CF8B","B6677142242640A1CF8B"]
function isCode(data){
if (data != 10000) {
this.$message.error('获取字典失败');
return false
}
}
// 用来遍历每次请求,两个参数,一个是传id,一个判断是否进行赋值
function getData(param,islxArr){
treeDicEvents({groupID:param}).then((res) => {
isCode(res.data.result.resultCode)
arr.push(res.data.constantTreeModels[0]);
islxArr ? this.zds.lxArr=this.getCascaderData(arr) : ""
});
}
// 循环id,拿到id执行请求
param.forEach(v=>{
getData(v,islxArr==="B6677142242640A1CF8B")
})
网友评论