需求是我需要拿多个用户信息去循环请求后端接口,当所有接口请求完后,拿存储了返回数据的数组去循环异步打开浏览器窗口,可以实现多个窗口弹出
//触发复选框函数
const handleSelectionChange = (val: refundData[]) => {
multipleSelection.value = val;
//存储用户信息的主要信息,用于下面的异步请求
ticketNo.value = multipleSelection.value.map(
(item) => item.ticket_no.split("-")[1]
);
};
//这是设置打开网址的窗口函数,配合定时器异步处理
const openWindows = async (arrId: any) => {
new Promise((resolve) => {
const timer = setTimeout(() => {
const screenWidth = window.screen.width;
const screenHeight = window.screen.height;
const width = Math.round(screenWidth * 0.8);
const height = Math.round(screenHeight * 0.7);
const left = Math.round((screenWidth - width) / 2);
const top = Math.round((screenHeight - height) / 2);
window.open(
`(自己要打开的IP地址)?tfid=${arrId}`,
"_blank",
`width=${width},height=${height},left=${left},top=${top}`
);
}, 500);
timers.push(timer); // 将定时器引用存入数组,记得要及时清除定时器
});
};
//这是根据选择的用户信息来请求数据,接口会返回每个用户的id值
const onSubmit = async () => {
loading.value = true;
idArrs.value = [];
// 使用Promise.all等待所有异步请求完成
await Promise.all(
//ticketNo.value 这是存储了选择后的用户的列表
ticketNo.value.map(async (ticket: string) => {
//请求接口
const res = (await getAsmsRefundID(ticket)) as any;
//把返回的数据存入自定义变量数组中
idArrs.value.push(res.data);
})
);
loading.value = false;
//lodashs 去重函数
const arr = _.uniq(idArrs.value);
//循环打开浏览器窗口
for (let i = 0; i < arr.length; i++) {
await openWindows(arr[i]);
}
dialogVisible.value = false;
};
//离开页面,销毁组件后,要清除定时器
onUnmounted(() => {
timers.forEach((timer: any) => {
clearTimeout(timer);
});
});