update:优化设备缓存逻辑

This commit is contained in:
2026-06-11 18:10:59 +08:00
parent bfdd40ec93
commit 5cf243d187
6 changed files with 55 additions and 6 deletions

View File

@@ -22,7 +22,8 @@
const { const {
updateUser, updateUser,
updateOnline, updateOnline,
clearSessionState clearSessionState,
clearDevice
} = store; } = store;
watch( watch(
@@ -63,6 +64,11 @@
updateOnline(data.online); updateOnline(data.online);
} }
function onDeviceBindInvalid() {
clearDevice();
uni.setStorageSync("calibration", false);
}
function onDeviceShoot() { function onDeviceShoot() {
// audioManager.play("射箭声音") // audioManager.play("射箭声音")
} }
@@ -78,6 +84,7 @@
uni.$on("update-user", emitUpdateUser); uni.$on("update-user", emitUpdateUser);
uni.$on("update-online", emitUpdateOnline); uni.$on("update-online", emitUpdateOnline);
uni.$on("session-kicked-out", onSessionKickedOut); uni.$on("session-kicked-out", onSessionKickedOut);
uni.$on("device-bind-invalid", onDeviceBindInvalid);
const token = uni.getStorageSync( const token = uni.getStorageSync(
`${uni.getAccountInfoSync().miniProgram.envVersion}_token` `${uni.getAccountInfoSync().miniProgram.envVersion}_token`
); );
@@ -91,6 +98,7 @@
uni.$off("update-user", emitUpdateUser); uni.$off("update-user", emitUpdateUser);
uni.$off("update-online", emitUpdateOnline); uni.$off("update-online", emitUpdateOnline);
uni.$off("session-kicked-out", onSessionKickedOut); uni.$off("session-kicked-out", onSessionKickedOut);
uni.$off("device-bind-invalid", onDeviceBindInvalid);
websocket.closeWebSocket(); websocket.closeWebSocket();
}); });
</script> </script>

View File

@@ -70,6 +70,15 @@ function request(method, url, data = {}) {
resolve({binded: true}); resolve({binded: true});
return; return;
} }
if (message === "BIND_FAILD") {
uni.$emit("device-bind-invalid");
uni.showToast({
title: "设备绑定状态已失效,请重新绑定",
icon: "none",
});
reject({type: "DEVICE_BIND_INVALID", message});
return;
}
if (message === "ERROR_ORDER_UNPAY") { if (message === "ERROR_ORDER_UNPAY") {
uni.showToast({ uni.showToast({
title: "当前有未支付订单", title: "当前有未支付订单",

View File

@@ -16,7 +16,7 @@ import {
import useStore from "@/store"; import useStore from "@/store";
const store = useStore(); const store = useStore();
const { updateUser, updateDevice, updateOnline } = store; const { updateUser, updateDevice, updateOnline, clearDevice } = store;
const props = defineProps({ const props = defineProps({
show: { show: {
@@ -107,6 +107,8 @@ async function doLogin() {
); );
const data = await getDeviceBatteryAPI(); const data = await getDeviceBatteryAPI();
updateOnline(data.online); updateOnline(data.online);
} else {
clearDevice();
} }
props.onClose(); props.onClose();
} catch (error) { } catch (error) {

View File

@@ -26,6 +26,7 @@ const {
updateConfig, updateConfig,
updateUser, updateUser,
updateDevice, updateDevice,
clearDevice,
getLvlName, getLvlName,
getLvlNameByScore, getLvlNameByScore,
updateOnline, updateOnline,
@@ -127,6 +128,8 @@ onShow(async () => {
); );
const data = await getDeviceBatteryAPI(); const data = await getDeviceBatteryAPI();
updateOnline(data.online); updateOnline(data.online);
} else {
clearDevice();
} }
} }
} }

View File

@@ -16,7 +16,7 @@ const showTip = ref(false);
const confirmBindTip = ref(false); const confirmBindTip = ref(false);
const addDevice = ref(); const addDevice = ref();
const store = useStore(); const store = useStore();
const { updateDevice } = store; const { updateDevice, clearDevice } = store;
const { user, device } = storeToRefs(store); const { user, device } = storeToRefs(store);
const justBind = ref(false); const justBind = ref(false);
const calibration = ref(false); const calibration = ref(false);
@@ -84,13 +84,21 @@ const toFristTryPage = () => {
}; };
const unbindDevice = async () => { const unbindDevice = async () => {
try {
await unbindDeviceAPI(device.value.deviceId); await unbindDeviceAPI(device.value.deviceId);
} catch (error) {
if (error?.type === "DEVICE_BIND_INVALID") {
uni.setStorageSync("calibration", false);
clearDevice();
}
return;
}
uni.setStorageSync("calibration", false); uni.setStorageSync("calibration", false);
uni.showToast({ uni.showToast({
title: "解绑成功", title: "解绑成功",
icon: "success", icon: "success",
}); });
device.value = {}; clearDevice();
}; };
const toDeviceIntroPage = () => { const toDeviceIntroPage = () => {
@@ -122,8 +130,23 @@ const goCalibration = async () => {
}); });
}; };
onShow(() => { const syncDeviceBinding = async () => {
if (!user.value.id) return;
try {
const devices = await getMyDevicesAPI();
if (devices.bindings && devices.bindings.length) {
updateDevice(devices.bindings[0].deviceId, devices.bindings[0].deviceName);
} else {
clearDevice();
}
} catch (error) {
console.log("sync device binding error", error);
}
};
onShow(async () => {
calibration.value = uni.getStorageSync("calibration"); calibration.value = uni.getStorageSync("calibration");
await syncDeviceBinding();
}); });
</script> </script>

View File

@@ -137,6 +137,10 @@ export default defineStore("store", {
this.device.deviceId = deviceId; this.device.deviceId = deviceId;
this.device.deviceName = deviceName; this.device.deviceName = deviceName;
}, },
clearDevice() {
this.device = getDefaultDevice();
this.online = false;
},
async updateConfig(config) { async updateConfig(config) {
this.config = config; this.config = config;
if (this.user.scores !== undefined) { if (this.user.scores !== undefined) {