fix:最新
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { onShow } from '@dcloudio/uni-app'
|
||||
import { onShow, onUnload } from '@dcloudio/uni-app'
|
||||
import { computed, ref } from 'vue'
|
||||
import { resolveValidHomeId } from '@/api/home'
|
||||
import { getErrorMessage } from '@/utils/api-error'
|
||||
@@ -6,8 +6,17 @@ import { fetchRoomList } from '@/api/location'
|
||||
import type { RoomOption } from '@/types/location'
|
||||
import { fetchDeviceList } from '@/pages/device/device.api'
|
||||
import { updateDeviceRuntime } from '@/pages/device/detail/device-detail.api'
|
||||
import { updateDeviceSettings } from '@/pages/device/settings/device-settings.api'
|
||||
import { buildBleDeviceName, isStandardBleDeviceName } from '@/utils/ble-device-name'
|
||||
import { releaseBleDevice } from '@/utils/bluetooth'
|
||||
import { clearZ2BleHandleCache, sendZ2BleRenameCommand } from '@/utils/z2-ble'
|
||||
import {
|
||||
getZ2RenameProgressLabel,
|
||||
isZ2RenameSuccessful,
|
||||
} from '@/utils/z2-ble-protocol'
|
||||
import { bindDevice } from '../add-device.api'
|
||||
import { parseId, sameId } from '@/utils/device-id'
|
||||
import { readStoredHomeId, saveSelectedHomeId } from '@/utils/selected-home'
|
||||
import { readBindDeviceContext } from '../add-device.types'
|
||||
import { fetchProductOptions } from './complete.api'
|
||||
import type { ProductOption } from './complete.types'
|
||||
@@ -32,6 +41,7 @@ export function useCompleteDevice() {
|
||||
const loading = ref(false)
|
||||
const initialized = ref(false)
|
||||
const bluetoothConnected = ref(false)
|
||||
const bleDeviceId = ref('')
|
||||
|
||||
const roomSelected = computed(() => !!locationId.value && roomName.value !== '请选择所在房间')
|
||||
const productSelected = computed(() => !!productId.value && !!model.value)
|
||||
@@ -145,7 +155,10 @@ export function useCompleteDevice() {
|
||||
}
|
||||
if (ctx.imei) imei.value = ctx.imei
|
||||
if (ctx.mac) mac.value = ctx.mac
|
||||
const parsedHomeId = parseId(ctx.homeId)
|
||||
if (parsedHomeId) homeId.value = parsedHomeId
|
||||
bluetoothConnected.value = ctx.bluetoothConnected === '1'
|
||||
if (ctx.bleDeviceId) bleDeviceId.value = ctx.bleDeviceId
|
||||
if (roomOptions.value.length) {
|
||||
syncRoomFromOptions()
|
||||
}
|
||||
@@ -153,7 +166,13 @@ export function useCompleteDevice() {
|
||||
|
||||
async function initPage(query: Record<string, string | undefined>) {
|
||||
applyQuery(query)
|
||||
if (!homeId.value) {
|
||||
homeId.value = readStoredHomeId()
|
||||
}
|
||||
await Promise.all([loadRoomOptionList(), loadProductOptions()])
|
||||
if (homeId.value) {
|
||||
saveSelectedHomeId(homeId.value)
|
||||
}
|
||||
initialized.value = true
|
||||
}
|
||||
|
||||
@@ -228,8 +247,41 @@ export function useCompleteDevice() {
|
||||
|
||||
loading.value = true
|
||||
try {
|
||||
if (bleDeviceId.value && bluetoothConnected.value && mac.value) {
|
||||
const bleName = buildBleDeviceName(mac.value)
|
||||
const needRename = !isStandardBleDeviceName(mac.value, deviceName.value)
|
||||
if (needRename) {
|
||||
uni.showLoading({ title: '正在同步蓝牙名称…', mask: true })
|
||||
try {
|
||||
const renameProgress = await sendZ2BleRenameCommand(bleDeviceId.value, bleName, {
|
||||
timeoutMs: 25000,
|
||||
onProgress: (progress) => {
|
||||
uni.showLoading({
|
||||
title: getZ2RenameProgressLabel(progress) || '正在同步蓝牙名称…',
|
||||
mask: true,
|
||||
})
|
||||
},
|
||||
})
|
||||
if (!isZ2RenameSuccessful(renameProgress)) {
|
||||
uni.showToast({
|
||||
title: '蓝牙名称同步未完成,将继续添加设备',
|
||||
icon: 'none',
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
uni.showToast({
|
||||
title: '蓝牙名称同步失败,将继续添加设备',
|
||||
icon: 'none',
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const validHomeId = await resolveValidHomeId(homeId.value)
|
||||
homeId.value = validHomeId
|
||||
if (validHomeId) saveSelectedHomeId(validHomeId)
|
||||
await bindDevice({
|
||||
deviceNo: deviceNo.value.trim(),
|
||||
name,
|
||||
@@ -244,6 +296,13 @@ export function useCompleteDevice() {
|
||||
const list = await fetchDeviceList()
|
||||
const bound = list?.find((item) => item.deviceNo === deviceNo.value.trim())
|
||||
if (bound?.deviceId != null) {
|
||||
const boundName = bound.name?.trim()
|
||||
if (boundName !== name) {
|
||||
await updateDeviceSettings({
|
||||
deviceId: bound.deviceId,
|
||||
name,
|
||||
})
|
||||
}
|
||||
await updateDeviceRuntime({
|
||||
deviceId: bound.deviceId,
|
||||
status: bluetoothConnected.value ? '1' : '2',
|
||||
@@ -263,10 +322,19 @@ export function useCompleteDevice() {
|
||||
}
|
||||
|
||||
onShow(() => {
|
||||
uni.hideLoading()
|
||||
if (!initialized.value || !homeId.value) return
|
||||
void loadRoomOptionList()
|
||||
})
|
||||
|
||||
onUnload(() => {
|
||||
uni.hideLoading()
|
||||
if (bleDeviceId.value) {
|
||||
clearZ2BleHandleCache(bleDeviceId.value)
|
||||
void releaseBleDevice(bleDeviceId.value)
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
currentStep,
|
||||
deviceName,
|
||||
|
||||
Reference in New Issue
Block a user