fix:最新
This commit is contained in:
@@ -236,6 +236,12 @@
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
|
||||
}
|
||||
|
||||
.device-detail-mode-item--pressed {
|
||||
transform: scale(0.97);
|
||||
opacity: 0.88;
|
||||
}
|
||||
|
||||
.device-detail-mode-item--active {
|
||||
@@ -266,6 +272,12 @@
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
|
||||
}
|
||||
|
||||
.device-detail-fan-item--pressed {
|
||||
transform: scale(0.97);
|
||||
opacity: 0.88;
|
||||
}
|
||||
|
||||
.device-detail-fan-item--active {
|
||||
|
||||
@@ -41,11 +41,10 @@ export function formatConnectionText(options: {
|
||||
}): string {
|
||||
const { status, bluetoothConnected, locationName } = options
|
||||
if (status === '0') return `未激活 · ${locationName}`
|
||||
if (status === '2' || !isDeviceOnline(status)) {
|
||||
return `设备离线 · ${locationName}`
|
||||
if (isBluetoothConnected(bluetoothConnected)) {
|
||||
return `蓝牙已连接 · ${locationName}`
|
||||
}
|
||||
const btText = isBluetoothConnected(bluetoothConnected) ? '蓝牙已连接' : '蓝牙未连接'
|
||||
return `${btText} · ${locationName}`
|
||||
return `设备离线 · ${locationName}`
|
||||
}
|
||||
|
||||
/** 供电模式:0USB 1电池 */
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<template>
|
||||
<template>
|
||||
<view class="device-detail-page">
|
||||
<view class="device-detail-hero" :style="{ paddingTop: statusBarHeight + 'px' }">
|
||||
<image
|
||||
@@ -65,6 +65,8 @@
|
||||
:key="item.value"
|
||||
class="device-detail-mode-item"
|
||||
:class="{ 'device-detail-mode-item--active': runMode === item.value }"
|
||||
hover-class="device-detail-mode-item--pressed"
|
||||
hover-stay-time="80"
|
||||
@tap="onSelectRunMode(item.value)"
|
||||
>
|
||||
<text class="device-detail-mode-item__text">{{ item.label }}</text>
|
||||
@@ -84,6 +86,8 @@
|
||||
:key="item.value"
|
||||
class="device-detail-fan-item"
|
||||
:class="{ 'device-detail-fan-item--active': fanLevel === item.value }"
|
||||
hover-class="device-detail-fan-item--pressed"
|
||||
hover-stay-time="80"
|
||||
@tap="onSelectFanLevel(item.value)"
|
||||
>
|
||||
<text class="device-detail-fan-item__text">{{ item.label }}</text>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { onUnload } from '@dcloudio/uni-app'
|
||||
import { onShow, onUnload } from '@dcloudio/uni-app'
|
||||
import { ref } from 'vue'
|
||||
import { getErrorMessage } from '@/utils/api-error'
|
||||
import {
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
ensureBleConnection,
|
||||
ensureBluetoothReady,
|
||||
getBluetoothErrorMessage,
|
||||
getConnectedBleDeviceByMac,
|
||||
getDiscoveredBluetoothDevices,
|
||||
isBluetoothApiSupported,
|
||||
rssiToSignalLevel,
|
||||
@@ -38,27 +39,14 @@ export function useDeviceBluetoothSync(context: DeviceBluetoothSyncContext) {
|
||||
let syncTimer: ReturnType<typeof setInterval> | undefined
|
||||
let unsubscribeConnection: (() => void) | undefined
|
||||
let active = false
|
||||
let tearingDown = false
|
||||
let hadSuccessfulSync = false
|
||||
|
||||
function setSyncText(text: string) {
|
||||
context.onSyncTextChange(text)
|
||||
}
|
||||
|
||||
async function reportBleDisconnected() {
|
||||
const deviceId = context.getDeviceId()
|
||||
if (!deviceId) return
|
||||
context.onBluetoothConnectedChange(false)
|
||||
try {
|
||||
await updateDeviceRuntime({
|
||||
deviceId,
|
||||
bluetoothConnected: '0',
|
||||
signalLevel: '0',
|
||||
})
|
||||
} catch {
|
||||
// 上报失败时保留本地状态
|
||||
}
|
||||
}
|
||||
|
||||
async function reportDeviceOffline() {
|
||||
const deviceId = context.getDeviceId()
|
||||
if (!deviceId) return
|
||||
context.onBluetoothConnectedChange(false)
|
||||
@@ -74,6 +62,69 @@ export function useDeviceBluetoothSync(context: DeviceBluetoothSyncContext) {
|
||||
}
|
||||
}
|
||||
|
||||
function reportBleLinkLostLocal() {
|
||||
context.onBluetoothConnectedChange(false)
|
||||
}
|
||||
|
||||
async function resolveBleTarget(
|
||||
mac: string,
|
||||
preferredBleId?: string,
|
||||
): Promise<UniApp.BluetoothDeviceInfo | null> {
|
||||
const connected = await getConnectedBleDeviceByMac(mac)
|
||||
if (connected) return connected
|
||||
|
||||
if (preferredBleId) {
|
||||
const discovered = await getDiscoveredBluetoothDevices()
|
||||
const cached = discovered.find((item) => item.deviceId === preferredBleId)
|
||||
if (cached) return cached
|
||||
}
|
||||
|
||||
const deviceName = context.getDeviceName?.()
|
||||
return scanForBleDeviceByMac(mac, {
|
||||
timeoutMs: SCAN_TIMEOUT_MS,
|
||||
name: deviceName,
|
||||
skipCached: !preferredBleId && !hadSuccessfulSync,
|
||||
})
|
||||
}
|
||||
|
||||
async function syncBleData(
|
||||
targetBleId: string,
|
||||
foundDevice: UniApp.BluetoothDeviceInfo | null,
|
||||
): Promise<boolean> {
|
||||
const deviceId = context.getDeviceId()
|
||||
if (!deviceId) return false
|
||||
|
||||
setSyncText('正在同步设备数据…')
|
||||
const blePayload = await readRuntimeFromBle(targetBleId)
|
||||
const hasBleData = Object.keys(blePayload).length > 0
|
||||
if (!hasBleData) {
|
||||
throw new Error('设备无响应,请确认已开机并靠近手机')
|
||||
}
|
||||
|
||||
hadSuccessfulSync = true
|
||||
context.onBluetoothConnectedChange(true)
|
||||
context.onBleRuntimeRead?.(blePayload)
|
||||
|
||||
const runtimePatch = context.getRuntimePatch()
|
||||
const signalLevel = rssiToSignalLevel(foundDevice?.RSSI)
|
||||
|
||||
try {
|
||||
await updateDeviceRuntime({
|
||||
deviceId,
|
||||
...runtimePatch,
|
||||
...blePayload,
|
||||
status: '1',
|
||||
bluetoothConnected: '1',
|
||||
signalLevel,
|
||||
})
|
||||
setSyncText('刚刚更新 · 数据实时同步中')
|
||||
await context.onSynced?.()
|
||||
} catch (apiErr) {
|
||||
setSyncText(`蓝牙已连接 · 云端同步失败:${getErrorMessage(apiErr, '请检查网络')}`)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
async function connectAndSync(): Promise<boolean> {
|
||||
const deviceId = context.getDeviceId()
|
||||
const mac = context.getMac()
|
||||
@@ -92,78 +143,48 @@ export function useDeviceBluetoothSync(context: DeviceBluetoothSyncContext) {
|
||||
if (syncing.value) return false
|
||||
syncing.value = true
|
||||
|
||||
let connected = false
|
||||
let targetBleId = bleDeviceId.value
|
||||
let foundDevice: UniApp.BluetoothDeviceInfo | null = null
|
||||
|
||||
try {
|
||||
setSyncText('正在连接蓝牙…')
|
||||
await ensureBluetoothReady()
|
||||
|
||||
let foundDevice: UniApp.BluetoothDeviceInfo | null = null
|
||||
|
||||
if (!targetBleId) {
|
||||
const deviceName = context.getDeviceName?.()
|
||||
foundDevice = await scanForBleDeviceByMac(mac ?? '', {
|
||||
timeoutMs: SCAN_TIMEOUT_MS,
|
||||
name: deviceName,
|
||||
})
|
||||
if (!foundDevice) {
|
||||
setSyncText('未找到设备,请确认已开机并靠近手机')
|
||||
await reportDeviceOffline()
|
||||
return false
|
||||
foundDevice = await resolveBleTarget(mac, targetBleId)
|
||||
if (!foundDevice) {
|
||||
setSyncText('未找到设备,请确认已开机并靠近手机')
|
||||
if (!tearingDown) {
|
||||
reportBleLinkLostLocal()
|
||||
}
|
||||
targetBleId = foundDevice.deviceId
|
||||
bleDeviceId.value = targetBleId
|
||||
} else {
|
||||
const discovered = await getDiscoveredBluetoothDevices()
|
||||
foundDevice = discovered.find((item) => item.deviceId === targetBleId) ?? null
|
||||
return false
|
||||
}
|
||||
|
||||
targetBleId = foundDevice.deviceId
|
||||
bleDeviceId.value = targetBleId
|
||||
|
||||
if (!hadSuccessfulSync) {
|
||||
setSyncText('正在连接蓝牙…')
|
||||
}
|
||||
await ensureBleConnection(targetBleId)
|
||||
connected = true
|
||||
context.onBluetoothConnectedChange(true)
|
||||
|
||||
setSyncText('正在同步设备数据…')
|
||||
const blePayload = await readRuntimeFromBle(targetBleId)
|
||||
if (Object.keys(blePayload).length) {
|
||||
context.onBleRuntimeRead?.(blePayload)
|
||||
}
|
||||
|
||||
const runtimePatch = context.getRuntimePatch()
|
||||
const signalLevel = rssiToSignalLevel(foundDevice?.RSSI)
|
||||
const hasBleData = Object.keys(blePayload).length > 0
|
||||
|
||||
try {
|
||||
await updateDeviceRuntime({
|
||||
deviceId,
|
||||
...runtimePatch,
|
||||
...blePayload,
|
||||
status: '1',
|
||||
bluetoothConnected: '1',
|
||||
signalLevel,
|
||||
})
|
||||
if (!hasBleData) {
|
||||
setSyncText('蓝牙已连接,等待设备上报数据…')
|
||||
} else {
|
||||
setSyncText('刚刚更新 · 数据实时同步中')
|
||||
}
|
||||
await context.onSynced?.()
|
||||
} catch (apiErr) {
|
||||
if (hasBleData) {
|
||||
setSyncText('蓝牙已连接 · 本地数据已更新,云端同步失败')
|
||||
} else {
|
||||
setSyncText(
|
||||
`蓝牙已连接 · 云端同步失败:${getErrorMessage(apiErr, '请检查网络')}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
return true
|
||||
return await syncBleData(targetBleId, foundDevice)
|
||||
} catch (err) {
|
||||
const stillConnected = await getConnectedBleDeviceByMac(mac)
|
||||
if (stillConnected) {
|
||||
bleDeviceId.value = stillConnected.deviceId
|
||||
context.onBluetoothConnectedChange(true)
|
||||
try {
|
||||
return await syncBleData(stillConnected.deviceId, stillConnected)
|
||||
} catch (retryErr) {
|
||||
setSyncText(getBluetoothErrorMessage(retryErr))
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
setSyncText(getBluetoothErrorMessage(err))
|
||||
if (!connected) {
|
||||
if (!tearingDown) {
|
||||
bleDeviceId.value = undefined
|
||||
clearZ2BleHandleCache(targetBleId)
|
||||
await reportDeviceOffline()
|
||||
reportBleLinkLostLocal()
|
||||
}
|
||||
return false
|
||||
} finally {
|
||||
@@ -199,11 +220,24 @@ export function useDeviceBluetoothSync(context: DeviceBluetoothSyncContext) {
|
||||
active = true
|
||||
|
||||
unsubscribeConnection = subscribeBleConnectionStateChange((res) => {
|
||||
if (tearingDown) return
|
||||
if (!bleDeviceId.value || res.deviceId !== bleDeviceId.value) return
|
||||
if (!res.connected) {
|
||||
bleDeviceId.value = undefined
|
||||
void reportBleDisconnected()
|
||||
setSyncText('蓝牙已断开,正在尝试重连…')
|
||||
void (async () => {
|
||||
const mac = context.getMac()
|
||||
if (mac) {
|
||||
const stillConnected = await getConnectedBleDeviceByMac(mac)
|
||||
if (stillConnected) {
|
||||
bleDeviceId.value = stillConnected.deviceId
|
||||
return
|
||||
}
|
||||
}
|
||||
bleDeviceId.value = undefined
|
||||
clearZ2BleHandleCache(res.deviceId)
|
||||
void reportBleDisconnected()
|
||||
setSyncText('蓝牙已断开,正在尝试重连…')
|
||||
void connectAndSync()
|
||||
})()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -215,7 +249,9 @@ export function useDeviceBluetoothSync(context: DeviceBluetoothSyncContext) {
|
||||
}
|
||||
|
||||
async function stopSync() {
|
||||
tearingDown = true
|
||||
active = false
|
||||
hadSuccessfulSync = false
|
||||
if (syncTimer) {
|
||||
clearInterval(syncTimer)
|
||||
syncTimer = undefined
|
||||
@@ -229,10 +265,15 @@ export function useDeviceBluetoothSync(context: DeviceBluetoothSyncContext) {
|
||||
if (currentBleId) {
|
||||
clearZ2BleHandleCache(currentBleId)
|
||||
await closeBleConnection(currentBleId).catch(() => {})
|
||||
await reportBleDisconnected()
|
||||
}
|
||||
tearingDown = false
|
||||
}
|
||||
|
||||
onShow(() => {
|
||||
if (!active || tearingDown) return
|
||||
void connectAndSync()
|
||||
})
|
||||
|
||||
onUnload(() => {
|
||||
void stopSync()
|
||||
})
|
||||
|
||||
@@ -45,9 +45,8 @@ export function useDeviceDetail() {
|
||||
const deviceStatus = ref<string>()
|
||||
const bluetoothConnected = ref(false)
|
||||
const charging = ref(false)
|
||||
const isOnline = ref(false)
|
||||
const loading = ref(false)
|
||||
const updating = ref(false)
|
||||
const isOnline = computed(() => bluetoothConnected.value)
|
||||
|
||||
const runMode = ref<RunMode>('0')
|
||||
const fanLevel = ref<FanLevel>('0')
|
||||
@@ -148,13 +147,15 @@ export function useDeviceDetail() {
|
||||
filterLife.value = view.filterLife
|
||||
filterRemainText.value = view.filterRemainText
|
||||
filterReplaced.value = view.filterReplaced
|
||||
isOnline.value = view.isOnline
|
||||
if (data.deviceBattery != null) applyBatteryLevel(data.deviceBattery, false)
|
||||
if (data.status !== undefined) deviceStatus.value = data.status
|
||||
if (!bluetoothConnected.value && data.status !== undefined) {
|
||||
deviceStatus.value = data.status
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshDeviceMeta() {
|
||||
if (!deviceId.value) return
|
||||
const bleConnectedLocally = bluetoothConnected.value
|
||||
try {
|
||||
const list = await fetchDeviceList()
|
||||
const device = list?.find((item) => String(item.deviceId) === deviceId.value)
|
||||
@@ -171,10 +172,13 @@ export function useDeviceDetail() {
|
||||
const matched = locations.find((item) => sameId(item.locationId, parsedLocationId))
|
||||
if (matched) locationName.value = matched.name
|
||||
}
|
||||
bluetoothConnected.value = isBluetoothConnected(device.bluetoothConnected)
|
||||
charging.value = device.charging === '1'
|
||||
if (device.status !== undefined) deviceStatus.value = device.status
|
||||
isOnline.value = device.status === '1'
|
||||
if (!bleConnectedLocally) {
|
||||
bluetoothConnected.value = isBluetoothConnected(device.bluetoothConnected)
|
||||
deviceStatus.value = bluetoothConnected.value
|
||||
? '1'
|
||||
: (device.status === '0' ? '0' : '2')
|
||||
}
|
||||
} catch {
|
||||
// 列表刷新失败时保留路由参数状态
|
||||
}
|
||||
@@ -198,28 +202,37 @@ export function useDeviceDetail() {
|
||||
}
|
||||
|
||||
async function patchRuntime(patch: { runMode?: RunMode; fanLevel?: FanLevel }) {
|
||||
if (!deviceId.value || updating.value) return
|
||||
updating.value = true
|
||||
if (!deviceId.value) return
|
||||
if (patch.runMode != null && runMode.value === patch.runMode) return
|
||||
if (patch.fanLevel != null && fanLevel.value === patch.fanLevel) return
|
||||
|
||||
const snapshot = { runMode: runMode.value, fanLevel: fanLevel.value }
|
||||
if (patch.runMode != null) runMode.value = patch.runMode
|
||||
if (patch.fanLevel != null) fanLevel.value = patch.fanLevel
|
||||
|
||||
const bleCmd = patch.runMode != null
|
||||
? encodeRunModeBleCommand(patch.runMode)
|
||||
: patch.fanLevel != null
|
||||
? encodeFanLevelBleCommand(patch.fanLevel)
|
||||
: undefined
|
||||
|
||||
try {
|
||||
const bleCmd = patch.runMode != null
|
||||
? encodeRunModeBleCommand(patch.runMode)
|
||||
: patch.fanLevel != null
|
||||
? encodeFanLevelBleCommand(patch.fanLevel)
|
||||
: undefined
|
||||
if (bleCmd) {
|
||||
await sendBleCommand(bleCmd)
|
||||
}
|
||||
await updateDeviceRuntime({
|
||||
const apiTask = updateDeviceRuntime({
|
||||
deviceId: deviceId.value,
|
||||
...patch,
|
||||
})
|
||||
const data = await fetchDeviceRuntime({ deviceId: deviceId.value })
|
||||
applyRuntime(data)
|
||||
await refreshDeviceMeta()
|
||||
if (bleCmd) {
|
||||
const [bleOk] = await Promise.all([sendBleCommand(bleCmd), apiTask])
|
||||
if (!bleOk) {
|
||||
throw new Error('蓝牙指令发送失败')
|
||||
}
|
||||
} else {
|
||||
await apiTask
|
||||
}
|
||||
} catch (err) {
|
||||
runMode.value = snapshot.runMode
|
||||
fanLevel.value = snapshot.fanLevel
|
||||
uni.showToast({ title: getErrorMessage(err, '设置失败'), icon: 'none' })
|
||||
} finally {
|
||||
updating.value = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,10 +291,7 @@ export function useDeviceDetail() {
|
||||
}),
|
||||
onBluetoothConnectedChange: (connected) => {
|
||||
bluetoothConnected.value = connected
|
||||
if (connected) {
|
||||
isOnline.value = true
|
||||
deviceStatus.value = '1'
|
||||
}
|
||||
deviceStatus.value = connected ? '1' : '2'
|
||||
},
|
||||
onSyncTextChange: (text) => {
|
||||
syncText.value = text
|
||||
@@ -309,12 +319,9 @@ export function useDeviceDetail() {
|
||||
const parsedHomeId = parseId(query?.homeId)
|
||||
if (parsedHomeId) homeId.value = parsedHomeId
|
||||
if (query?.model) productModel.value = decodeURIComponent(String(query.model))
|
||||
if (query?.status) {
|
||||
deviceStatus.value = String(query.status)
|
||||
isOnline.value = query.status === '1'
|
||||
}
|
||||
if (query?.mac) deviceMac.value = decodeURIComponent(String(query.mac))
|
||||
bluetoothConnected.value = isBluetoothConnected(query?.bluetoothConnected)
|
||||
bluetoothConnected.value = false
|
||||
deviceStatus.value = '2'
|
||||
charging.value = query?.charging === '1'
|
||||
void loadRuntime().then(() => {
|
||||
startSyncLoop()
|
||||
|
||||
Reference in New Issue
Block a user