优化100J代码

This commit is contained in:
微微一笑
2026-03-24 16:26:37 +08:00
parent 1598065457
commit 4e518e7340
2 changed files with 49 additions and 11 deletions

View File

@ -545,19 +545,48 @@ export function deviceDetail(id) {
function execWithBleFirst(bleExec, httpExec, logName, onWaiting) {
const doBle = () => bleExec().then(res => ({ ...(res || {}), _channel: 'ble' }));
const do4G = () => httpExec().then(res => { res._channel = '4g'; return res; });
const hideWaitUi = () => {
if (typeof onWaiting === 'function') return;
try {
uni.hideLoading();
} catch (e) {}
};
const showWaitUi = (title) => {
if (typeof onWaiting === 'function') return;
try {
uni.hideLoading();
uni.showLoading({ title, mask: false });
} catch (e) {}
};
if (protocolInstance.isBleConnected && protocolInstance.bleDeviceId) {
return doBle().catch(() => { console.log('[100J] 蓝牙失败回退4G'); return do4G(); });
}
// 无 bleDeviceId 时:可能扫描中,先等待连接(设备页在后台可能完成连接)
if (!protocolInstance.bleDeviceId) {
if (typeof onWaiting === 'function') onWaiting();
return waitForBleConnection(12000).then(connected => {
return connected ? doBle().catch(() => { console.log('[100J] 蓝牙失败回退4G'); return do4G(); }) : do4G();
return doBle().catch(() => {
console.log('[100J]', logName || '指令', '蓝牙失败回退4G');
return do4G();
});
}
return tryReconnectBle(2500).then(reconnected => {
return reconnected ? doBle().catch(() => { console.log('[100J] 蓝牙失败回退4G'); return do4G(); }) : do4G();
});
// 无 bleDeviceId页面可能在扫描最多等 12s否则用户以为点了没反应
if (!protocolInstance.bleDeviceId) {
if (typeof onWaiting === 'function') onWaiting();
else showWaitUi('蓝牙连接中…');
return waitForBleConnection(12000)
.then(connected => {
return connected ? doBle().catch(() => {
console.log('[100J]', logName || '指令', '蓝牙失败回退4G');
return do4G();
}) : do4G();
})
.finally(hideWaitUi);
}
if (typeof onWaiting !== 'function') showWaitUi('正在重连蓝牙…');
return tryReconnectBle(2500)
.then(reconnected => {
return reconnected ? doBle().catch(() => {
console.log('[100J]', logName || '指令', '蓝牙失败回退4G');
return do4G();
}) : do4G();
})
.finally(hideWaitUi);
}
// 爆闪模式