156 lines
3.7 KiB
JavaScript
156 lines
3.7 KiB
JavaScript
/**
|
||
* 首页 - 设备列表
|
||
*/
|
||
const storage = require('../../utils/storage.js');
|
||
const { gateway } = require('../../services/gateway.js');
|
||
const app = getApp();
|
||
|
||
Page({
|
||
data: {
|
||
userInfo: null,
|
||
devices: [],
|
||
},
|
||
|
||
onLoad() {
|
||
// 未登录跳转登录页
|
||
if (!app.isLoggedIn()) {
|
||
wx.reLaunch({ url: '/pages/login/login' });
|
||
return;
|
||
}
|
||
},
|
||
|
||
onShow() {
|
||
// 每次显示时确保网关在采集(登录后/从扫描返回自动恢复),并刷新设备列表
|
||
gateway.ensureStarted();
|
||
this.refreshData();
|
||
// 跟随网关运行状态刷新“采集中”标签(仅在运行态变化时刷新,避免高频 setData)
|
||
this._bindRunning();
|
||
},
|
||
|
||
onHide() {
|
||
if (this._unsub) {
|
||
this._unsub();
|
||
this._unsub = null;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 订阅网关运行态;仅在“采集中的设备”变化时才整表刷新
|
||
*/
|
||
_bindRunning() {
|
||
const snap = gateway.getSnapshot();
|
||
this._lastRunningSn = (snap.device && snap.bleConnected) ? snap.device.serialNumber : null;
|
||
if (this._unsub) return;
|
||
this._unsub = gateway.subscribe(() => {
|
||
const s = gateway.getSnapshot();
|
||
const sn = (s.device && s.bleConnected) ? s.device.serialNumber : null;
|
||
if (sn !== this._lastRunningSn) {
|
||
this._lastRunningSn = sn;
|
||
this.refreshData();
|
||
}
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 刷新数据
|
||
*/
|
||
refreshData() {
|
||
const devices = storage.getDeviceList();
|
||
const snap = gateway.getSnapshot();
|
||
const runningSn = (snap.device && snap.bleConnected) ? snap.device.serialNumber : null;
|
||
devices.forEach((d) => {
|
||
d.running = runningSn !== null && d.serialNumber === runningSn;
|
||
});
|
||
this.setData({
|
||
userInfo: app.globalData.userInfo,
|
||
devices: devices,
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 点击设备卡片 - 跳转详情页
|
||
*/
|
||
handleDeviceTap(e) {
|
||
const index = e.currentTarget.dataset.index;
|
||
const device = this.data.devices[index];
|
||
if (!device) return;
|
||
|
||
// 保存当前设备到全局
|
||
app.globalData.currentDevice = device;
|
||
|
||
wx.navigateTo({
|
||
url: `/pages/device/detail/detail?sn=${device.serialNumber}`,
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 删除设备
|
||
*/
|
||
handleDeviceDelete(e) {
|
||
const serialNumber = e.detail.serialNumber;
|
||
wx.showModal({
|
||
title: '删除设备',
|
||
content: '确定删除该设备配置吗?',
|
||
confirmColor: '#ff4d4f',
|
||
success: async (res) => {
|
||
if (res.confirm) {
|
||
storage.removeDevice(serialNumber);
|
||
// 删除的若是正在采集的设备,网关自动停/切换
|
||
await gateway.ensureStarted();
|
||
this.refreshData();
|
||
wx.showToast({ title: '已删除', icon: 'success' });
|
||
}
|
||
},
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 设备启停切换(单 enabled:开启任意一个会禁用其余设备)
|
||
*/
|
||
async handleDeviceSwitch(e) {
|
||
const serialNumber = e.detail.serialNumber;
|
||
const enabled = e.detail.enabled;
|
||
|
||
if (enabled) {
|
||
storage.activateDevice(serialNumber);
|
||
} else {
|
||
storage.deactivateDevice(serialNumber);
|
||
}
|
||
// 网关跟随启停:启用→启动采集;关闭→停止
|
||
await gateway.ensureStarted();
|
||
this.refreshData();
|
||
},
|
||
|
||
/**
|
||
* 添加设备 - 跳转扫描页
|
||
*/
|
||
handleAddDevice() {
|
||
wx.navigateTo({
|
||
url: '/pages/device/scan/scan',
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 退出登录
|
||
*/
|
||
handleLogout() {
|
||
wx.showModal({
|
||
title: '退出登录',
|
||
content: '确定要退出登录吗?',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
app.clearLoginInfo();
|
||
wx.reLaunch({ url: '/pages/login/login' });
|
||
}
|
||
},
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 下拉刷新
|
||
*/
|
||
onPullDownRefresh() {
|
||
this.refreshData();
|
||
wx.stopPullDownRefresh();
|
||
},
|
||
}); |