feat:蓝牙绑定和在线客服
This commit is contained in:
@@ -1,49 +1,192 @@
|
||||
import { ref } from 'vue'
|
||||
import { onShow } from '@dcloudio/uni-app'
|
||||
import { computed, ref } from 'vue'
|
||||
import { resolveValidHomeId } from '@/api/home'
|
||||
import { getErrorMessage } from '@/utils/api-error'
|
||||
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 { bindDevice } from '../add-device.api'
|
||||
import { parseId, sameId } from '@/utils/device-id'
|
||||
import { readBindDeviceContext } from '../add-device.types'
|
||||
|
||||
const ROOM_OPTIONS = [
|
||||
{ name: '客厅', locationId: 1 },
|
||||
{ name: '主卧', locationId: 2 },
|
||||
{ name: '卧室', locationId: 3 },
|
||||
{ name: '厨房', locationId: 4 },
|
||||
{ name: '卫生间', locationId: 5 },
|
||||
] as const
|
||||
import { fetchProductOptions } from './complete.api'
|
||||
import type { ProductOption } from './complete.types'
|
||||
|
||||
export function useCompleteDevice() {
|
||||
const currentStep = 4
|
||||
const deviceNo = ref('')
|
||||
const deviceName = ref('Z2 空气净化器')
|
||||
const serialMasked = ref('****A3B2')
|
||||
const productId = ref<number>()
|
||||
const model = ref<string>()
|
||||
const productId = ref('')
|
||||
const model = ref('')
|
||||
const productOptions = ref<ProductOption[]>([])
|
||||
const productOptionsLoading = ref(false)
|
||||
const productIndex = ref(0)
|
||||
const imei = ref<string>()
|
||||
const mac = ref<string>()
|
||||
const roomName = ref(ROOM_OPTIONS[0].name)
|
||||
const locationId = ref<number | undefined>(ROOM_OPTIONS[0].locationId)
|
||||
const homeId = ref('')
|
||||
const roomOptions = ref<RoomOption[]>([])
|
||||
const roomOptionsLoading = ref(false)
|
||||
const roomName = ref('请选择所在房间')
|
||||
const locationId = ref<string | number>()
|
||||
const loading = ref(false)
|
||||
const initialized = ref(false)
|
||||
const bluetoothConnected = ref(false)
|
||||
|
||||
const roomSelected = computed(() => !!locationId.value && roomName.value !== '请选择所在房间')
|
||||
const productSelected = computed(() => !!productId.value && !!model.value)
|
||||
const productDisplayText = computed(() => {
|
||||
if (productOptionsLoading.value) return '加载中...'
|
||||
if (model.value) return model.value
|
||||
return '请选择产品型号'
|
||||
})
|
||||
const bluetoothStatusText = computed(() =>
|
||||
bluetoothConnected.value ? '蓝牙已连接' : '蓝牙未连接',
|
||||
)
|
||||
|
||||
function applyProduct(option: ProductOption) {
|
||||
const parsedProductId = parseId(option.productId)
|
||||
if (parsedProductId) productId.value = parsedProductId
|
||||
if (option.model) model.value = option.model
|
||||
const index = productOptions.value.findIndex((item) => sameId(item.productId, option.productId))
|
||||
if (index >= 0) productIndex.value = index
|
||||
}
|
||||
|
||||
function syncProductFromOptions() {
|
||||
const options = productOptions.value
|
||||
if (!options.length) return
|
||||
|
||||
if (productId.value) {
|
||||
const matched = options.find((item) => sameId(item.productId, productId.value))
|
||||
if (matched) {
|
||||
applyProduct(matched)
|
||||
return
|
||||
}
|
||||
}
|
||||
if (model.value) {
|
||||
const matched = options.find((item) => item.model === model.value)
|
||||
if (matched) {
|
||||
applyProduct(matched)
|
||||
return
|
||||
}
|
||||
}
|
||||
applyProduct(options[0])
|
||||
}
|
||||
|
||||
async function loadProductOptions() {
|
||||
if (productOptionsLoading.value) return
|
||||
productOptionsLoading.value = true
|
||||
try {
|
||||
productOptions.value = await fetchProductOptions()
|
||||
if (productOptions.value.length) {
|
||||
syncProductFromOptions()
|
||||
} else {
|
||||
productId.value = ''
|
||||
model.value = ''
|
||||
productIndex.value = 0
|
||||
}
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '加载产品型号失败'), icon: 'none' })
|
||||
} finally {
|
||||
productOptionsLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function syncRoomFromOptions() {
|
||||
if (locationId.value) {
|
||||
const matched = roomOptions.value.find((item) =>
|
||||
sameId(item.locationId, locationId.value),
|
||||
)
|
||||
if (matched) {
|
||||
roomName.value = matched.name
|
||||
return
|
||||
}
|
||||
}
|
||||
const first = roomOptions.value[0]
|
||||
if (first && !locationId.value) {
|
||||
roomName.value = first.name
|
||||
locationId.value = String(first.locationId)
|
||||
}
|
||||
}
|
||||
|
||||
async function loadRoomOptionList() {
|
||||
if (roomOptionsLoading.value) return
|
||||
roomOptionsLoading.value = true
|
||||
try {
|
||||
homeId.value = await resolveValidHomeId(homeId.value)
|
||||
if (!homeId.value) {
|
||||
uni.showToast({ title: '未找到家庭信息', icon: 'none' })
|
||||
return
|
||||
}
|
||||
roomOptions.value = await fetchRoomList(homeId.value)
|
||||
if (roomOptions.value.length) {
|
||||
syncRoomFromOptions()
|
||||
} else {
|
||||
roomName.value = '请选择所在房间'
|
||||
locationId.value = ''
|
||||
}
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '加载房间列表失败'), icon: 'none' })
|
||||
} finally {
|
||||
roomOptionsLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function applyQuery(query: Record<string, string | undefined>) {
|
||||
const ctx = readBindDeviceContext(query)
|
||||
if (ctx.deviceNo) deviceNo.value = ctx.deviceNo
|
||||
if (ctx.name) deviceName.value = ctx.name
|
||||
if (ctx.serialMasked) serialMasked.value = ctx.serialMasked
|
||||
if (ctx.productId != null) productId.value = ctx.productId
|
||||
const parsedProductId = parseId(ctx.productId)
|
||||
if (parsedProductId) productId.value = parsedProductId
|
||||
if (ctx.model) model.value = ctx.model
|
||||
if (productOptions.value.length) {
|
||||
syncProductFromOptions()
|
||||
}
|
||||
if (ctx.imei) imei.value = ctx.imei
|
||||
if (ctx.mac) mac.value = ctx.mac
|
||||
bluetoothConnected.value = ctx.bluetoothConnected === '1'
|
||||
if (roomOptions.value.length) {
|
||||
syncRoomFromOptions()
|
||||
}
|
||||
}
|
||||
|
||||
async function initPage(query: Record<string, string | undefined>) {
|
||||
applyQuery(query)
|
||||
await Promise.all([loadRoomOptionList(), loadProductOptions()])
|
||||
initialized.value = true
|
||||
}
|
||||
|
||||
function onProductChange(event: { detail: { value: number } }) {
|
||||
const index = Number(event.detail.value)
|
||||
const option = productOptions.value[index]
|
||||
if (!option) return
|
||||
applyProduct(option)
|
||||
}
|
||||
|
||||
function onPickRoom() {
|
||||
uni.showActionSheet({
|
||||
itemList: ROOM_OPTIONS.map((item) => item.name),
|
||||
success: (res) => {
|
||||
const picked = ROOM_OPTIONS[res.tapIndex]
|
||||
if (picked) {
|
||||
roomName.value = picked.name
|
||||
locationId.value = picked.locationId
|
||||
}
|
||||
if (roomOptionsLoading.value) {
|
||||
uni.showToast({ title: '房间列表加载中', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!homeId.value) {
|
||||
uni.showToast({ title: '未找到家庭信息', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
const params = [
|
||||
`homeId=${homeId.value}`,
|
||||
...(locationId.value ? [`locationId=${locationId.value}`] : []),
|
||||
`locationName=${encodeURIComponent(roomName.value)}`,
|
||||
]
|
||||
|
||||
uni.navigateTo({
|
||||
url: `/pages/device/settings/room/index?${params.join('&')}`,
|
||||
events: {
|
||||
roomSelected: (data: { locationId: string | number; name: string }) => {
|
||||
locationId.value = String(data.locationId)
|
||||
roomName.value = data.name
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -58,19 +201,41 @@ export function useCompleteDevice() {
|
||||
uni.showToast({ title: '缺少设备编号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!locationId.value) {
|
||||
uni.showToast({ title: '请选择所在房间', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!bluetoothConnected.value) {
|
||||
uni.showToast({ title: '设备未连接蓝牙,请返回重新配对', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (loading.value) return
|
||||
|
||||
loading.value = true
|
||||
try {
|
||||
const validHomeId = await resolveValidHomeId(homeId.value)
|
||||
homeId.value = validHomeId
|
||||
await bindDevice({
|
||||
deviceNo: deviceNo.value.trim(),
|
||||
name,
|
||||
...(productId.value != null ? { productId: productId.value } : {}),
|
||||
...(model.value ? { model: model.value } : {}),
|
||||
productId: productId.value,
|
||||
model: model.value,
|
||||
...(imei.value ? { imei: imei.value } : {}),
|
||||
...(mac.value ? { mac: mac.value } : {}),
|
||||
...(locationId.value != null ? { locationId: locationId.value } : {}),
|
||||
...(validHomeId ? { homeId: validHomeId } : {}),
|
||||
...(locationId.value ? { locationId: locationId.value } : {}),
|
||||
})
|
||||
|
||||
const list = await fetchDeviceList()
|
||||
const bound = list?.find((item) => item.deviceNo === deviceNo.value.trim())
|
||||
if (bound?.deviceId != null) {
|
||||
await updateDeviceRuntime({
|
||||
deviceId: bound.deviceId,
|
||||
status: bluetoothConnected.value ? '1' : '2',
|
||||
bluetoothConnected: bluetoothConnected.value ? '1' : '0',
|
||||
})
|
||||
}
|
||||
|
||||
uni.showToast({ title: '添加成功', icon: 'success' })
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({ url: '/pages/device/index' })
|
||||
@@ -82,13 +247,28 @@ export function useCompleteDevice() {
|
||||
}
|
||||
}
|
||||
|
||||
onShow(() => {
|
||||
if (!initialized.value || !homeId.value) return
|
||||
void loadRoomOptionList()
|
||||
})
|
||||
|
||||
return {
|
||||
currentStep,
|
||||
deviceName,
|
||||
serialMasked,
|
||||
productOptions,
|
||||
productIndex,
|
||||
productDisplayText,
|
||||
productSelected,
|
||||
productOptionsLoading,
|
||||
roomName,
|
||||
roomSelected,
|
||||
roomOptionsLoading,
|
||||
loading,
|
||||
applyQuery,
|
||||
bluetoothConnected,
|
||||
bluetoothStatusText,
|
||||
initPage,
|
||||
onProductChange,
|
||||
onPickRoom,
|
||||
onFinishAdd,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user