update:优化ota
This commit is contained in:
@@ -9,10 +9,10 @@ import useStore from "@/store";
|
||||
import { capsuleHeight } from "@/util";
|
||||
import {
|
||||
connectDeviceWifiAPI,
|
||||
getDeviceDetailAPI,
|
||||
getDeviceWifiStatusAPI,
|
||||
getHardwareBoxVersionAPI,
|
||||
} from "@/apis";
|
||||
import { useOtaUpdate } from "@/composables/useOtaUpdate";
|
||||
import { getOtaInfoFromDetail, useOtaUpdate } from "@/composables/useOtaUpdate";
|
||||
|
||||
const STATES = {
|
||||
SCANNING: "SCANNING",
|
||||
@@ -23,7 +23,7 @@ const STATES = {
|
||||
};
|
||||
|
||||
const isIOS = uni.getDeviceInfo().osName === "ios";
|
||||
const { deviceStatus } = storeToRefs(useStore());
|
||||
const { device, deviceStatus } = storeToRefs(useStore());
|
||||
const currentState = ref(STATES.SCANNING);
|
||||
const connectedWifi = ref(null);
|
||||
const connectedWifiName = ref("");
|
||||
@@ -45,10 +45,7 @@ const showPassword = ref(false);
|
||||
// 刷新防抖标志:扫描进行中为 true,禁止重复点击;扫描结束(成功/失败)后重置为 false。
|
||||
const isRefreshing = ref(false);
|
||||
const fromFirmwareUpdate = ref(false);
|
||||
const routeOtaInfo = ref({
|
||||
versionNumber: "",
|
||||
resourceUrl: "",
|
||||
});
|
||||
const routeOtaVersionNumber = ref("");
|
||||
const countdownVisible = ref(false);
|
||||
const countdownSeconds = ref(3);
|
||||
const firmwareMessageVisible = ref(false);
|
||||
@@ -395,37 +392,38 @@ const joinNetwork = () => {
|
||||
submitDeviceWifiConfig({ ssid, password });
|
||||
};
|
||||
|
||||
// 获取 OTA 更新版本信息,正常使用入口传参,缺失时再请求后端兜底。
|
||||
const getOtaVersionInfo = async () => {
|
||||
if (routeOtaInfo.value.versionNumber && routeOtaInfo.value.resourceUrl) {
|
||||
return {
|
||||
needUpdate: true,
|
||||
versionNumber: routeOtaInfo.value.versionNumber,
|
||||
resourceUrl: routeOtaInfo.value.resourceUrl,
|
||||
};
|
||||
}
|
||||
return getHardwareBoxVersionAPI();
|
||||
};
|
||||
|
||||
const startFirmwareUpdate = async () => {
|
||||
if (!fromFirmwareUpdate.value || !connectedWifi.value || otaUpdating.value) return;
|
||||
const deviceId = device.value.deviceId;
|
||||
const wifi = connectedWifi.value;
|
||||
if (!deviceId) return;
|
||||
try {
|
||||
const versionInfo = await getOtaVersionInfo();
|
||||
if (!versionInfo?.needUpdate) {
|
||||
const response = await getDeviceDetailAPI(deviceId);
|
||||
const detail = response?.detail || response?.data?.detail;
|
||||
if (!detail || device.value.deviceId !== deviceId) throw new Error("设备详情已失效");
|
||||
const otaInfo = getOtaInfoFromDetail(detail);
|
||||
if (!otaInfo.needUpdate) {
|
||||
firmwareMessage.value = "当前已是最新版本";
|
||||
firmwareMessageVisible.value = true;
|
||||
return;
|
||||
}
|
||||
await startOtaUpdate({
|
||||
versionNumber: versionInfo.versionNumber,
|
||||
wifiSsid: connectedWifi.value.SSID,
|
||||
wifiPassword: connectedWifi.value.password || "",
|
||||
resourceUrl: versionInfo.resourceUrl,
|
||||
});
|
||||
if (!otaInfo.versionNumber) {
|
||||
firmwareMessage.value = "缺少固件目标版本,请返回后重试";
|
||||
firmwareMessageVisible.value = true;
|
||||
return;
|
||||
}
|
||||
// WiFi 连接可能耗时较长,以更新前的最新设备详情为准。
|
||||
routeOtaVersionNumber.value = otaInfo.versionNumber;
|
||||
} catch (error) {
|
||||
firmwareMessage.value = "获取更新版本失败,请重试";
|
||||
firmwareMessage.value = "获取设备详情失败,请返回后重试";
|
||||
firmwareMessageVisible.value = true;
|
||||
return;
|
||||
}
|
||||
await startOtaUpdate({
|
||||
versionNumber: routeOtaVersionNumber.value,
|
||||
wifiSsid: wifi.SSID,
|
||||
wifiPassword: wifi.password || "",
|
||||
});
|
||||
};
|
||||
|
||||
const clearFirmwareCountdown = () => {
|
||||
@@ -466,10 +464,7 @@ const togglePasswordVisibility = () => {
|
||||
// 页面加载时识别普通 WiFi 入口和固件更新入口。
|
||||
onLoad((options = {}) => {
|
||||
fromFirmwareUpdate.value = options.source === "firmware-update";
|
||||
routeOtaInfo.value = {
|
||||
versionNumber: decodeURIComponent(options.versionNumber || ""),
|
||||
resourceUrl: decodeURIComponent(options.resourceUrl || ""),
|
||||
};
|
||||
routeOtaVersionNumber.value = decodeURIComponent(options.versionNumber || "");
|
||||
});
|
||||
|
||||
// 页面挂载后启动 WiFi 扫描流程。
|
||||
|
||||
Reference in New Issue
Block a user