背景:在提交之前,后端进行多个校验,包含阻断校验和非阻断校验。
返回阻断标识及提示语。阻断的校验直接返回
非阻断校验设置弹窗,为否则返回
选择是则继续下一个弹窗(回调下一个非阻断),最后一个为是则调用最终提交方法
data(){
return {
stopindex:0,
typeText:null,
}
}
//校验
toconfirm(){
this.api.xxx(xxx).then(res => {
if(!res.data.length){
//成功
this.submit()
}else{
//1为阻断校验,0为非阻断校验
let result = res.data
//let result = [{type:'0',text:'22222'},{type:'0',text:'3333'}]
let stop = result.filter(ele => {return ele.type == '1'});
let notstop = result.filter(ele=>{return ele.type == '0'})
if(stop.length){
//阻断
return this.$message.error(stop[0].text)
}else{
//非阻断
let newArr = []
for (let i = 0; i < notstop.length; i++) {
const item = notstop[i];
let sonarr = []
if(i==notstop.length-1){
sonarr = [item.text,()=>this.submit()]
}else{
sonarr = [item.text,()=>this.confirmText()]
}
newArr.push(sonarr)
}
this.typeText = newArr
this.confirmText()
}
}
})
},
confirmText(){
let _this = this;
this.$confirm({
title:'提示',
cancelText:'否',
okText:'是',
okType: 'primary',
content:_this.typeText[_this.stopindex][0],
onOk() {
let index = _this.stopindex++
_this.typeText[index][1].call(_this)
},
onCancel() {
_this.stopindex = '0'
}
})
},
submit(){
this.stopindex = '0';
xxx
}