const APP = getApp(); Page({ /** * 页面的初始数据 */ data: { showMore: false,//显示更多 showModify: false, //显示修改框 deviceList: [], currentDevice: null,//当前选中的设备 }, onShow: function () { this.getDeviceList(); }, /** * 请求设备集合 */ getDeviceList: function () { APP.Get(APP.Url.getListByUserId, {}, false).then(res => { this.setData({ deviceList: res.data, }) APP.globalData.deviceList = res.data; }) }, /** * 切换当前设备 */ changeDefaultDevice: function (event) { const params = { deviceNum: event.currentTarget.dataset.num } APP.Post(APP.Url.changeDefaultDevice, params).then(res => { APP.Modal.tips("设置成功"); this.getDeviceList(); }) }, /** * 解除设备 */ unbind: function () { const that = this; const params = { deviceNum: this.data.currentDevice.deviceNum, } wx.showModal({ title: '提示', content: '确定解绑这台设备吗?', success(res) { if (res.confirm) { APP.Post(APP.Url.unbind, params).then(res => { that.hideAll(); APP.Modal.tips("解绑成功"); that.getDeviceList(); }) } } }) }, /** * 显示重命名框 */ showModify: function () { this.setData({ showMore: false, showModify: true, }) }, /** * 修改设备名 */ modifyName: function (form) { const that = this; const params = { num: this.data.currentDevice.deviceNum, name: form.detail.value.deviceName } APP.Post(APP.Url.modifyDeviceName, params).then(res => { APP.Modal.tips("修改成功"); that.hideAll(); that.getDeviceList(); }) }, /** * 跳转至添加设备 */ toAddDevice: function () { APP.Route.routeTo(APP.Route.path.addition); }, /** * 显示更多 */ showMore: function (event) { let deviceNum = event.currentTarget.dataset.num; for (let i = 0; i < this.data.deviceList.length; i++) { if (this.data.deviceList[i].deviceNum == deviceNum) { this.setData({ currentDevice: this.data.deviceList[i], showMore: true, showModify: false, }) break; } } }, /** * 隐藏所有模态框 */ hideAll: function () { this.setData({ showMore: false, showModify: false, }) }, })