封装实时接口,请求,共多个页面调用

This commit is contained in:
fengerli
2025-08-19 11:48:12 +08:00
parent 823c54aed7
commit f8a02b7f9f
4 changed files with 840 additions and 861 deletions

View File

@ -271,7 +271,8 @@
<script>
import MqttClient from '@/utils/mqtt.js';
import {
generateShortId
generateShortId,
getdeviceSTatus
} from '@/utils/function.js';
import {
deviceDetail,
@ -334,7 +335,9 @@
popupType: 'person', //弹框类型
isLaserOn: false,
isSending: false,
isProcessing: false
isProcessing: false,
isLoading: false, // 主加载状态
isPolling: false // 轮询状态
}
},
computed: {
@ -346,44 +349,6 @@
}
},
methods: {
/**
* 获取设备状态(带自动轮询)
* @param {number} val - 功能模式
* @param {string} batchId - 批次ID
* @param {number} [interval=800] - 轮询间隔(毫秒)
*/
async getdeviceSTatus(val, batchId, interval = 800) {
const checkStatus = async () => {
try {
const data = {
functionMode: val,
batchId: batchId,
typeName: this.itemInfo.typeName,
deviceImei: this.itemInfo.deviceImei
};
const res = await deviceRealTimeStatus(data);
if (res.code !== 200) {
throw new Error(res.msg || '请求失败');
}
switch (res.data.functionAccess) {
case 'OK':
return res; // 成功完成
case 'ACTIVE':
await new Promise(r => setTimeout(r, interval));
return checkStatus(); // 继续轮询
case 'FAILED':
throw new Error('设备操作失败');
case 'TIMEOUT':
throw new Error('设备响应超时');
default:
throw new Error('未知状态');
}
} catch (error) {
throw error;
}
};
return checkStatus();
},
// 点击弹框外的区域关闭
closePopup() {
this.lightModeA = false;
@ -554,22 +519,6 @@
toggleForm() {
this.isFormExpanded = !this.isFormExpanded;
},
// onItemClick(index) {
// const selectedItem = this.items[index];
// if (selectedItem.text === '激光') {
// this.lightModeC = true;
// } else {
// // 更新选中状态
// this.items = this.items.map((item, i) => ({
// ...item,
// selected: i === index
// }));
// this.currentMainMode = selectedItem.text;
// this.selectedItemIndex = index;
// // 强制更新视图(如果需要)
// this.$forceUpdate();
// }
// },
onItemClick(index) {
const item = this.items[index];
// 激光模式特殊处理
@ -585,7 +534,7 @@
selected: i === index
}));
// 显示临时选中的模式名称(视觉反馈)
this.pendingMainMode = item.text;
this.pendingMainMode = item.text;
},
// 灯光模式的确认
handleSumbit() {
@ -749,6 +698,7 @@
mask: true
});
loadingShown = true;
this.isPolling = true; // 标记开始轮询
uni.uploadFile({
url: baseURL + '/app/bjq/device/uploadLogo',
filePath: this.selectedImage,
@ -773,7 +723,19 @@
});
try {
// 获取设备状态
const statusRes = await this.getdeviceSTatus(1);
// 4. 获取设备状态
let deviceImei = this.itemInfo.deviceImei
let typeName = this.itemInfo.typeName
let batchId = ''
const statusRes = await getdeviceSTatus({
functionMode: 1,
batchId,
typeName,
deviceImei,
interval: 800
},
deviceRealTimeStatus
);
if (statusRes.data.functionAccess === 'OK') {
uni.hideLoading();
this.selectedImage = '';
@ -784,6 +746,7 @@
this.lightModeB = false;
}
} catch (error) {
this.isPolling = false; // 标记轮询结束
uni.hideLoading();
uni.showToast({
title: error.message,
@ -791,6 +754,7 @@
});
}
} else {
this.isPolling = false; // 标记轮询结束
uni.hideLoading();
uni.showToast({
title: responseData.msg,
@ -798,6 +762,7 @@
});
}
} catch (e) {
this.isPolling = false; // 标记轮询结束
uni.hideLoading();
uni.showToast({
title: e.message,
@ -918,6 +883,7 @@
});
return;
}
this.isPolling = true; // 标记开始轮询
try {
uni.showLoading({
title: '解除告警中...',
@ -942,7 +908,17 @@
return
}
// 4. 获取设备状态
const statusRes = await this.getdeviceSTatus(2, batchId);
let deviceImei = this.itemInfo.deviceImei
let typeName = this.itemInfo.typeName
const statusRes = await getdeviceSTatus({
functionMode: 2,
batchId,
typeName,
deviceImei,
interval: 500
},
deviceRealTimeStatus
);
if (statusRes.data.functionAccess === 'OK') {
uni.showToast({
title: statusRes.msg,
@ -964,6 +940,7 @@
icon: 'none'
});
} finally {
this.isPolling = false; // 标记开始轮询
uni.hideLoading();
}
},
@ -987,6 +964,7 @@
}
this.isSending = true;
let loadingShown = false;
this.isPolling = true; // 标记开始轮询
try {
uni.showLoading({
title: '正在发送...',
@ -1011,8 +989,18 @@
})
return
}
// 轮询获取状态重要这里必须await
const statusRes = await this.getdeviceSTatus(2, batchId);
// 4. 获取设备状态
let deviceImei = this.itemInfo.deviceImei
let typeName = this.itemInfo.typeName
const statusRes = await getdeviceSTatus({
functionMode: 2,
batchId,
typeName,
deviceImei,
interval: 500
},
deviceRealTimeStatus
);
// 只有当状态为'OK'时才显示成功弹窗
if (statusRes.data.functionAccess === 'OK') {
this.popupType = 'person';
@ -1025,7 +1013,7 @@
icon: 'none'
});
} finally {
console.log('失败还是成功了');
this.isPolling = false; // 标记轮询结束
uni.hideLoading();
this.isSending = false;
}
@ -1062,9 +1050,16 @@
this.isSharedDevice = false;
this.activePermissions = []; // 非分享设备清空权限
}
if (!isUpdate) {
// 关闭加载中
uni.hideLoading()
// 只有不是轮询状态时才关闭loading
if (!this.isPolling) {
this.pageLoading = false;
uni.hideLoading(); // 确保关闭
}
} else {
// 只有不是轮询状态时才关闭loading
if (!this.isPolling) {
this.pageLoading = false;
uni.hideLoading(); // 确保关闭
}
}
},
@ -1081,25 +1076,15 @@
try {
const res = await deviceDetail(id)
if (res.code == 200) {
this.handleDeviceData(res, false, isUpdate)
} else {
uni.showToast({
title: res.msg,
icon: 'none'
})
if (!isUpdate) {
uni.hideLoading()
}
this.handleDeviceData(res, false, isUpdate, true)
}
} catch (error) {
if (!isUpdate) {
uni.hideLoading()
// 只有不是轮询状态时才关闭loading
if (!this.isPolling) {
this.pageLoading = false;
uni.hideLoading(); // 确保关闭
}
// uni.showToast({
// title: error.msg,
// icon: 'none'
// })
}
},
// 获取分享设备详情
@ -1107,20 +1092,13 @@
try {
const res = await deviceShareId(id)
if (res.code == 200) {
this.handleDeviceData(res, true)
} else {
uni.showToast({
title: res.msg,
icon: 'none'
})
this.handleDeviceData(res, false, false, true)
}
} catch (error) {
uni.showToast({
title: error.msg,
icon: 'none'
})
uni.hideLoading()
if (!this.isPolling) {
this.pageLoading = false;
uni.hideLoading(); // 确保关闭
}
}
},
// 操作说明