feat:蓝牙绑定和在线客服
This commit is contained in:
165
src/pages/device/settings/room/use-device-room.ts
Normal file
165
src/pages/device/settings/room/use-device-room.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import { onLoad, onShow } from '@dcloudio/uni-app'
|
||||
import { ref } from 'vue'
|
||||
import { resolveValidHomeId } from '@/api/home'
|
||||
import { createLocation, fetchRoomList } from '@/api/location'
|
||||
import { getErrorMessage } from '@/utils/api-error'
|
||||
import { parseId, sameId } from '@/utils/device-id'
|
||||
import type { RoomOption } from '@/types/location'
|
||||
import type { DeviceRoomRouteQuery } from './room.types'
|
||||
|
||||
let openerEventChannel: UniApp.EventChannel | undefined
|
||||
|
||||
export function useDeviceRoom() {
|
||||
const homeId = ref('')
|
||||
const selectedLocationId = ref('')
|
||||
const pendingLocationName = ref<string>()
|
||||
const roomOptions = ref<RoomOption[]>([])
|
||||
const listLoading = ref(false)
|
||||
const saving = ref(false)
|
||||
const addDialogVisible = ref(false)
|
||||
const initialized = ref(false)
|
||||
|
||||
function applyQuery(query?: DeviceRoomRouteQuery) {
|
||||
const locationId = parseId(query?.locationId)
|
||||
if (locationId) {
|
||||
selectedLocationId.value = locationId
|
||||
}
|
||||
if (query?.locationName) {
|
||||
pendingLocationName.value = decodeURIComponent(String(query.locationName))
|
||||
}
|
||||
}
|
||||
|
||||
function syncSelectedLocation() {
|
||||
const options = roomOptions.value
|
||||
if (!options.length) return
|
||||
|
||||
if (selectedLocationId.value) {
|
||||
const matchedById = options.find((item) =>
|
||||
sameId(item.locationId, selectedLocationId.value),
|
||||
)
|
||||
if (matchedById) return
|
||||
}
|
||||
|
||||
if (pendingLocationName.value) {
|
||||
const matchedByName = options.find((item) => item.name === pendingLocationName.value)
|
||||
if (matchedByName) {
|
||||
selectedLocationId.value = String(matchedByName.locationId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveHomeId(query?: DeviceRoomRouteQuery) {
|
||||
homeId.value = await resolveValidHomeId(query?.homeId)
|
||||
}
|
||||
|
||||
async function loadLocations() {
|
||||
if (!homeId.value) return
|
||||
listLoading.value = true
|
||||
try {
|
||||
roomOptions.value = await fetchRoomList(homeId.value)
|
||||
syncSelectedLocation()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '加载房间列表失败'), icon: 'none' })
|
||||
} finally {
|
||||
listLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function initPage(query?: DeviceRoomRouteQuery) {
|
||||
applyQuery(query)
|
||||
await resolveHomeId(query)
|
||||
if (!homeId.value) {
|
||||
uni.showToast({ title: '未找到家庭信息', icon: 'none' })
|
||||
return
|
||||
}
|
||||
await loadLocations()
|
||||
initialized.value = true
|
||||
}
|
||||
|
||||
function onOpenAddDialog() {
|
||||
addDialogVisible.value = true
|
||||
}
|
||||
|
||||
async function onConfirmAddRoom(name: string) {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) {
|
||||
uni.showToast({ title: '请输入位置名称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (!homeId.value) {
|
||||
uni.showToast({ title: '未找到家庭信息', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (roomOptions.value.some((item) => item.name === trimmed)) {
|
||||
uni.showToast({ title: '位置名称已存在', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (saving.value) return
|
||||
|
||||
saving.value = true
|
||||
try {
|
||||
await createLocation({
|
||||
homeId: homeId.value,
|
||||
locationName: trimmed,
|
||||
})
|
||||
await loadLocations()
|
||||
const created = roomOptions.value.find((item) => item.name === trimmed)
|
||||
uni.showToast({ title: '添加成功', icon: 'success' })
|
||||
if (created) {
|
||||
void onSelectRoom(created)
|
||||
}
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '添加失败'), icon: 'none' })
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function onSelectRoom(room: RoomOption) {
|
||||
if (saving.value || listLoading.value) return
|
||||
if (sameId(selectedLocationId.value, room.locationId)) {
|
||||
uni.navigateBack()
|
||||
return
|
||||
}
|
||||
|
||||
selectedLocationId.value = String(room.locationId)
|
||||
pendingLocationName.value = room.name
|
||||
openerEventChannel?.emit('roomSelected', {
|
||||
locationId: room.locationId,
|
||||
name: room.name,
|
||||
})
|
||||
uni.navigateBack()
|
||||
}
|
||||
|
||||
onLoad((query?: DeviceRoomRouteQuery) => {
|
||||
const pages = getCurrentPages()
|
||||
const currentPage = pages[pages.length - 1] as {
|
||||
getOpenerEventChannel?: () => UniApp.EventChannel
|
||||
}
|
||||
if (typeof currentPage.getOpenerEventChannel === 'function') {
|
||||
openerEventChannel = currentPage.getOpenerEventChannel()
|
||||
}
|
||||
void initPage(query)
|
||||
})
|
||||
|
||||
onShow(() => {
|
||||
if (!initialized.value || !homeId.value) return
|
||||
void loadLocations()
|
||||
})
|
||||
|
||||
function isRoomSelected(locationId: string | number) {
|
||||
return sameId(locationId, selectedLocationId.value)
|
||||
}
|
||||
|
||||
return {
|
||||
roomOptions,
|
||||
selectedLocationId,
|
||||
listLoading,
|
||||
saving,
|
||||
addDialogVisible,
|
||||
isRoomSelected,
|
||||
onOpenAddDialog,
|
||||
onConfirmAddRoom,
|
||||
onSelectRoom,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user