update:代码备份
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import { computed, ref } from "vue";
|
||||
import { getDeviceBatteryAPI, getMyDevicesAPI, unbindDeviceAPI } from "@/apis";
|
||||
|
||||
export const DEVICE_NAME_STORAGE_KEY = "device_name_overrides";
|
||||
|
||||
export function useDeviceStatus({
|
||||
user,
|
||||
device,
|
||||
online,
|
||||
updateDevice,
|
||||
updateOnline,
|
||||
clearDevice,
|
||||
unbindDialogVisible,
|
||||
}) {
|
||||
const deviceStatus = ref({});
|
||||
const deviceDetails = ref({});
|
||||
|
||||
const isDeviceOnline = computed(
|
||||
() => deviceStatus.value.online === true || online.value === true
|
||||
);
|
||||
const statusText = computed(() => (isDeviceOnline.value ? "已连接" : "未连接"));
|
||||
const statusClass = computed(() =>
|
||||
isDeviceOnline.value ? "device-status--online" : "device-status--offline"
|
||||
);
|
||||
const battery = computed(() => {
|
||||
const value = Number(
|
||||
deviceStatus.value.battery ?? deviceStatus.value.power ?? 0
|
||||
);
|
||||
return Number.isFinite(value) && value > 0 ? Math.min(100, value) : 0;
|
||||
});
|
||||
const batteryText = computed(() =>
|
||||
battery.value ? `${battery.value}%` : "暂无数据"
|
||||
);
|
||||
const networkText = computed(() => {
|
||||
const netType = String(deviceStatus.value.netType || "").toLowerCase();
|
||||
if (netType === "wifi") return "WiFi";
|
||||
if (netType === "4g") return "4G";
|
||||
return isDeviceOnline.value ? "在线" : "未连接";
|
||||
});
|
||||
const maskedDeviceId = computed(() => {
|
||||
const id = String(device.value.deviceId || "");
|
||||
if (!id) return "暂无设备编号";
|
||||
if (id.length <= 3) return id;
|
||||
return `${"*".repeat(Math.min(5, id.length - 3))}${id.slice(-3)}`;
|
||||
});
|
||||
const deviceRows = computed(() => [
|
||||
{ label: "设备型号", value: deviceDetails.value.model || "射灵智能弓" },
|
||||
{ label: "当前电量", value: batteryText.value },
|
||||
{ label: "连接方式", value: networkText.value },
|
||||
{ label: "设备编号", value: maskedDeviceId.value },
|
||||
]);
|
||||
|
||||
const getDeviceNameOverrides = () => {
|
||||
const value = uni.getStorageSync(DEVICE_NAME_STORAGE_KEY);
|
||||
return value && typeof value === "object" ? value : {};
|
||||
};
|
||||
|
||||
const refreshDeviceStatus = async () => {
|
||||
if (!device.value.deviceId) return;
|
||||
try {
|
||||
const result = await getDeviceBatteryAPI();
|
||||
deviceStatus.value = result || {};
|
||||
updateOnline(result?.online === true);
|
||||
} catch (error) {
|
||||
deviceStatus.value = {};
|
||||
console.log("获取设备状态失败", error);
|
||||
}
|
||||
};
|
||||
|
||||
const syncDeviceBinding = async () => {
|
||||
if (!user.value.id) return;
|
||||
try {
|
||||
const devices = await getMyDevicesAPI();
|
||||
if (Array.isArray(devices?.bindings) && devices.bindings.length > 0) {
|
||||
const currentDevice = devices.bindings[0];
|
||||
const nameOverrides = getDeviceNameOverrides();
|
||||
deviceDetails.value = currentDevice;
|
||||
updateDevice(
|
||||
currentDevice.deviceId,
|
||||
nameOverrides[currentDevice.deviceId] ||
|
||||
currentDevice.deviceName ||
|
||||
currentDevice.name ||
|
||||
"我的智能弓"
|
||||
);
|
||||
await refreshDeviceStatus();
|
||||
return;
|
||||
}
|
||||
clearDevice();
|
||||
deviceStatus.value = {};
|
||||
deviceDetails.value = {};
|
||||
} catch (error) {
|
||||
console.log("同步设备绑定失败", error);
|
||||
}
|
||||
};
|
||||
|
||||
const unbindDevice = async () => {
|
||||
if (!device.value.deviceId) return;
|
||||
try {
|
||||
await unbindDeviceAPI(device.value.deviceId);
|
||||
uni.setStorageSync("calibration", false);
|
||||
clearDevice();
|
||||
deviceStatus.value = {};
|
||||
deviceDetails.value = {};
|
||||
unbindDialogVisible.value = false;
|
||||
uni.showToast({ title: "解绑成功", icon: "success" });
|
||||
} catch (error) {
|
||||
console.error("解绑设备失败", error);
|
||||
if (error?.type === "DEVICE_BIND_INVALID") {
|
||||
clearDevice();
|
||||
unbindDialogVisible.value = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
batteryText,
|
||||
deviceDetails,
|
||||
deviceRows,
|
||||
getDeviceNameOverrides,
|
||||
isDeviceOnline,
|
||||
maskedDeviceId,
|
||||
networkText,
|
||||
refreshDeviceStatus,
|
||||
statusClass,
|
||||
statusText,
|
||||
syncDeviceBinding,
|
||||
unbindDevice,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user