循环一个数组,
调用方法1,返回ture或false
如果true,调用接口a
如果false,调用接口b
获取当a和b全部返回的时候
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var arr = [1, 2, 3, 4, 5]
var promiseData = []
for (let i = 0; i < arr.length; i++) {
let answer = aa(i)
if (answer) {
console.log('调用settimeout1')
promiseData.push(settimeout1())
}
else {
console.log('调用settimeout2')
promiseData.push(settimeout2())
}
}
Promise.all(promiseData).then(data => { // 数据
console.log(data)
})
function aa(i) {
let aAnswer
if (i % 2 != 0) { // 1,2,3
aAnswer = true
}
else { // 4,5
aAnswer = false
}
return aAnswer
}
function settimeout1() {
return new Promise((resolved, rej) => {
setTimeout(() => {
resolved('settimeout1')
}, 4000)
})
}
function settimeout2() {
return new Promise((resolved, rej) => {
setTimeout(() => {
resolved('settimeout2')
}, 2000)
})
}
</script>
</body>
</html>
/*================ 批量硬件配置 开始 ================*/
// 获取表单内容
// 获取选中的列表
// 循环选中的列表
// 看选中的每一个绑定过,还是没有绑定过
// 没绑定过的,走新增接口
// 绑定过的,走修改
batchHard() {
// 获取表单内容
let resolvingPowerArr = this.hardForm.resolvingPower.split('*')
let customerObj = {
locateInterval: this.hardForm.locateInterval == undefined ? '' : this.hardForm.locateInterval,
videoWidth: Number(resolvingPowerArr[0]) == undefined ? '' : Number(resolvingPowerArr[0]),
videoHeight: Number(resolvingPowerArr[1]) == undefined ? '' : Number(resolvingPowerArr[1]),
videoBitRate: this.hardForm.videoBitRate == undefined ? '' : this.hardForm.videoBitRate,
apk: this.hardForm.apk == undefined ? '' : this.hardForm.apk,
version: this.hardForm.version == undefined ? '' : this.hardForm.version,
functioncode: this.hardForm.functioncode == undefined ? '' : this.hardForm.functioncode,
rtmp: this.hardForm.rtmp == undefined ? '' : this.hardForm.rtmp,
mqtt: this.hardForm.mqtt == undefined ? '' : this.hardForm.mqtt,
}
console.log('是表单数据哩')
console.log(customerObj)
console.log('选中的设备列表')
console.log(this.multipleSelection)
var promiseData = [] // 用来放promise的
for (let i = 0; i < this.multipleSelection.length; i++) {
let deviceid = this.multipleSelection[i].deviceid
let devicetype = this.multipleSelection[i].devicetype
let name = this.multipleSelection[i].name
let station = this.multipleSelection[i].station
let obj = {
deviceid: deviceid, // 设备id
server: this.hardForm.server, // 服务器接口地址
customer: JSON.stringify(customerObj), // 客户
devicetype: devicetype, // 设备类型
name: name, // 设备名
station: station// 微站名
}
console.log('是提交对象哩')
console.log(obj)
this.getIfBind(deviceid).then(data => {
let ifBind = data
if (ifBind) {
console.log('绑定过的,走修改')
promiseData.push(this.batchUpdata(obj))
} else {
console.log('没有绑定过的,走新增')
promiseData.push(this.batchAdd(obj))
}
})
}
Promise.all(promiseData).then(data => { // 数据
console.log(data)
this.$message({
message: '批量成功!',
type: 'success'
});
})
},
getIfBind(deviceid) { // 获取是否已绑定
let obj = {
deviceid: deviceid,
}
return new Promise((resolved, rej) => {
hardList(obj).then(res => {
let data = res.data
if (data.length > 0) { // 已经绑过了
resolved(true)
}
else {
resolved(false)
}
})
})
},
batchAdd(obj) { // 新增
return new Promise((resolved, rej) => {
hardAdd(obj).then(res => {
resolved(res)
})
})
},
batchUpdata(obj) { // 修改
return new Promise((resolved, rej) => {
hardSet(obj).then(res => {
resolved(res)
})
})
},
/*================ 批量硬件配置 结束 ================*/