diff --git a/src/components/DeviceRegisterDialog.vue b/src/components/DeviceRegisterDialog.vue index be9e136..0305fa5 100644 --- a/src/components/DeviceRegisterDialog.vue +++ b/src/components/DeviceRegisterDialog.vue @@ -33,8 +33,10 @@ const newUuid = ref('') const deviceName = ref('') const bindToAccount = ref(false) const accountDevices = ref([]) +const historyDevices = ref([]) +const manualUuid = ref('') const loadingDevices = ref(false) -const activeTab = ref('load') // 'load' 或 'register' +const activeTab = ref('load') // 'load' | 'history' | 'register' const showLoginDialog = ref(false) // 登录对话框状态 const isOpen = computed({ @@ -47,6 +49,9 @@ watch(isOpen, (newVal) => { if (newVal && accountStore.isAuthenticated && activeTab.value === 'load') { loadAccountDevices() } + if (newVal && activeTab.value === 'history') { + loadHistoryDevices() + } // 切换到注册选项卡时,自动生成UUID if (newVal && activeTab.value === 'register' && !newUuid.value) { generateRandomUuid() @@ -58,6 +63,9 @@ watch(activeTab, (newVal) => { if (newVal === 'load' && accountStore.isAuthenticated && isOpen.value) { loadAccountDevices() } + if (newVal === 'history' && isOpen.value) { + loadHistoryDevices() + } if (newVal === 'register' && !newUuid.value) { generateRandomUuid() } @@ -121,12 +129,35 @@ const loadAccountDevices = async () => { // 加载选中的设备 const loadDevice = (device) => { deviceStore.setDeviceUuid(device.uuid) + // 写入历史 + deviceStore.addDeviceToHistory({ uuid: device.uuid, name: device.name }) isOpen.value = false emit('confirm') resetForm() toast.success(`已切换到设备: ${device.name || device.uuid}`) } +// 手动输入UUID加载 +const loadByUuid = () => { + const id = manualUuid.value?.trim() + if (!id) { + toast.error('请输入设备 UUID') + return + } + // 可选:基本格式校验(宽松处理,避免误判合法UUID) + const ok = /^[0-9a-fA-F-]{8,}$/.test(id) + if (!ok) { + toast.error('UUID 格式不正确') + return + } + deviceStore.setDeviceUuid(id) + deviceStore.addDeviceToHistory({ uuid: id }) + isOpen.value = false + emit('confirm') + resetForm() + toast.success(`已切换到设备: ${id}`) +} + // 注册新设备 const registerDevice = async () => { if (!newUuid.value.trim()) { @@ -142,6 +173,8 @@ const registerDevice = async () => { try { // 1. 保存UUID到本地 deviceStore.setDeviceUuid(newUuid.value.trim()) + // 写入历史 + deviceStore.addDeviceToHistory({ uuid: newUuid.value.trim(), name: deviceName.value.trim() }) // 2. 调用设备注册接口(会自动在云端创建设备) await apiClient.registerDevice( @@ -181,6 +214,7 @@ const resetForm = () => { bindToAccount.value = accountStore.isAuthenticated accountDevices.value = [] activeTab.value = 'load' + manualUuid.value = '' } // 处理弹框关闭 @@ -211,6 +245,11 @@ onMounted(() => { onUnmounted(() => { document.removeEventListener('keydown', handleKeydown, true) }) + +// 加载本地历史设备 +const loadHistoryDevices = () => { + historyDevices.value = deviceStore.getDeviceHistory() +}