/* pages/mine/mine.js */ const storage = require('../../utils/storage.js'); const { gateway } = require('../../services/gateway.js'); Page({ data: { userInfo: null, activeDevice: '无', mqttStatus: '未连接', mqttStatusColor: '#999', bleStatus: '未运行', bleStatusColor: '#999', pendingCount: 0, appVersion: '1.0.0', }, onLoad() { this.loadUserInfo(); }, onShow() { if (!this._unsub) { this._unsub = gateway.subscribe(() => this.checkStatus()); } this.loadUserInfo(); this.checkStatus(); }, onHide() { if (this._unsub) { this._unsub(); this._unsub = null; } }, // 加载用户信息 loadUserInfo() { const userInfo = storage.getUserInfo(); this.setData({ userInfo }); }, // 检查连接状态(统一读网关快照,修复旧字符串/对象 API 回归) checkStatus() { const snap = gateway.getSnapshot(); const devName = snap.device ? (snap.device.name || snap.device.serialNumber || '') : ''; // MQTT let mqttStatus = '未连接'; let mqttStatusColor = '#999'; if (snap.mqttConnected) { mqttStatus = '已连接'; mqttStatusColor = '#52c41a'; } else if (snap.mqttState === 'connecting') { mqttStatus = '连接中'; mqttStatusColor = '#1890ff'; } // 蓝牙/采集 let bleStatus = '未运行'; let bleStatusColor = '#999'; if (snap.bleConnected && devName) { bleStatus = '采集中'; bleStatusColor = '#52c41a'; } else if (snap.phase === 'starting') { bleStatus = '连接中'; bleStatusColor = '#1890ff'; } else if (snap.phase === 'error') { bleStatus = '异常'; bleStatusColor = '#ff4d4f'; } else if (snap.device && !snap.bleConnected) { bleStatus = '未连接'; bleStatusColor = '#fa8c16'; } const pendingData = storage.getPendingData(); const pendingCount = pendingData ? pendingData.length : 0; this.setData({ activeDevice: devName || '无', mqttStatus, mqttStatusColor, bleStatus, bleStatusColor, pendingCount, }); }, // 清除缓存 clearCache() { wx.showModal({ title: '提示', content: '确定要清除缓存吗?这将清除所有离线数据。', success: (res) => { if (res.confirm) { storage.clearPendingData(); wx.showToast({ title: '清除成功', icon: 'success', }); this.checkStatus(); } }, }); }, // 退出登录 async logout() { wx.showModal({ title: '提示', content: '确定要退出登录吗?', success: async (res) => { if (res.confirm) { // 先停网关(断开 BLE/MQTT) await gateway.stop(); // 清除所有本地数据 storage.clearAll(); // 跳转到登录页 wx.reLaunch({ url: '/pages/login/login', }); } }, }); }, // 关于我们 showAbout() { wx.showModal({ title: '岩安测控网关', content: '版本:' + this.data.appVersion + '\n\n轻量化蓝牙网关,采集 BLE 设备数据透传上报至 yack 平台。', showCancel: false, }); }, });