congming_huose-apk/common/mixins/deviceSettingsRoom.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

/**
* 设备设置页房间展示拉取房间列表弹窗选择后 PUT /bst/device 更新 roomId
* 依赖页面存在idxqobjthis.$http
*/
import { patchDeviceRoomInStorage } from '@/common/utils/deviceRoomOverlay.js'
export default {
data() {
return {
deviceRoomPickerVisible: false,
deviceRoomPickerList: [],
deviceRoomPickerLoading: false,
deviceRoomSaving: false,
/** 当前页是否已因详情拉取过房间列表(含空列表),用于点击时不再重复请求 */
deviceRoomListReady: false
}
},
computed: {
deviceRoomDisplayName() {
const o = this.xqobj || {}
if (o.roomName) return o.roomName
const rid = o.roomId
if (rid === undefined || rid === null || rid === '') return '—'
const hit = (this.deviceRoomPickerList || []).find(r => String(r.id) === String(rid))
return hit ? hit.name : '—'
}
},
methods: {
deviceRoomSpaceId() {
const o = this.xqobj || {}
return o.spaceId || uni.getStorageSync('kjid') || ''
},
/** 在 getxq 成功赋值 xqobj 后调用,用于展示当前房间名称 */
afterDeviceDetailLoaded() {
const spaceId = this.deviceRoomSpaceId()
if (!spaceId) {
this.deviceRoomListReady = true
return
}
this.$http
.get(`/bst/room/list?spaceId=${spaceId}&pageNum=1&pageSize=999`)
.then(res => {
this.deviceRoomListReady = true
if (res.code == 200) {
this.deviceRoomPickerList = res.rows || []
this.$forceUpdate()
}
})
.catch(() => {
this.deviceRoomListReady = true
})
},
onDeviceRoomRowTap() {
if (this.deviceRoomSaving) return
const spaceId = this.deviceRoomSpaceId()
if (!spaceId) {
uni.showToast({
title: this.$i18n.t('selectSpaceFirst'),
icon: 'none'
})
return
}
this.deviceRoomPickerVisible = true
// 进入详情时 afterDeviceDetailLoaded 已拉过房间列表,则直接展示(含 0 条),不再点一次请求一次
if (this.deviceRoomListReady) {
this.deviceRoomPickerLoading = false
return
}
// 详情回调尚未跑完时用户就点了,再拉一次,并标记就绪
this.deviceRoomPickerLoading = true
this.$http
.get(`/bst/room/list?spaceId=${spaceId}&pageNum=1&pageSize=999`)
.then(res => {
this.deviceRoomListReady = true
this.deviceRoomPickerLoading = false
if (res.code == 200) {
this.deviceRoomPickerList = res.rows || []
} else {
uni.showToast({
title: res.msg || this.$i18n.t('noData'),
icon: 'none'
})
this.deviceRoomPickerVisible = false
}
})
.catch(() => {
this.deviceRoomPickerLoading = false
this.deviceRoomPickerVisible = false
})
},
closeDeviceRoomPicker() {
this.deviceRoomPickerVisible = false
},
confirmDeviceRoomChange(room) {
if (!room || this.deviceRoomSaving) return
const spaceId = this.deviceRoomSpaceId()
const o = this.xqobj || {}
if (String(o.roomId) === String(room.id)) {
this.closeDeviceRoomPicker()
return
}
const data = {
id: this.id,
sn: o.sn,
spaceId,
roomId: room.id,
name: o.name
}
this.deviceRoomSaving = true
this.$http
.put('/bst/device', data)
.then(res => {
this.deviceRoomSaving = false
if (res.code == 200) {
this.xqobj.roomId = room.id
this.xqobj.roomName = room.name
patchDeviceRoomInStorage(this.id, room.id, room.name)
uni.showToast({
title: res.msg || this.$i18n.t('operationSuccess'),
icon: 'success'
})
uni.$emit('deviceListNeedRefresh', {
deviceId: this.id,
roomId: room.id,
roomName: room.name
})
this.closeDeviceRoomPicker()
this.$forceUpdate()
} else {
uni.showToast({
title: res.msg || this.$i18n.t('noData'),
icon: 'none'
})
}
})
.catch(() => {
this.deviceRoomSaving = false
})
}
}
}