fastbee/yack-mini/miniprogram/pages/index/index.js
2026-09-05 10:19:07 +08:00

119 lines
2.4 KiB
JavaScript

/**
* 首页 - 设备列表
*/
const storage = require('../../utils/storage.js');
const app = getApp();
Page({
data: {
userInfo: null,
devices: [],
},
onLoad() {
// 未登录跳转登录页
if (!app.isLoggedIn()) {
wx.reLaunch({ url: '/pages/login/login' });
return;
}
},
onShow() {
// 每次显示时刷新设备列表
this.refreshData();
},
/**
* 刷新数据
*/
refreshData() {
const devices = storage.getDeviceList();
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: (res) => {
if (res.confirm) {
storage.removeDevice(serialNumber);
this.refreshData();
wx.showToast({ title: '已删除', icon: 'success' });
}
},
});
},
/**
* 设备启停切换
*/
handleDeviceSwitch(e) {
const serialNumber = e.detail.serialNumber;
const enabled = e.detail.enabled;
const devices = this.data.devices;
const index = devices.findIndex(d => d.serialNumber === serialNumber);
if (index >= 0) {
devices[index].enabled = enabled;
this.setData({ devices });
storage.updateDevice(devices[index]);
}
},
/**
* 添加设备 - 跳转扫描页
*/
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();
},
});