131 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const {
Result,
startSearchWiFi,
selectLock,
starEventOn,
connectWiFi,
starEventOff,
} = requirePlugin('starCloud')
Page({
data: {
lock: null, // 当前选中的锁
accountInfo: {},
loading: false,
wifiList: [],
showConnectDialog: false,
connectSsid: '',
connectPassword: '',
},
onLoad() {
starEventOn('searchWiFiResult', async (data) => {
if (data.code === 0) {
console.log("收到wifi搜索列表的事件回调", data)
this.setData({wifiList: data.wifiList || [], loading: false});
}
this.setData({loading: false});
wx.hideLoading();
});
const app = getApp();
// 尝试获取全局数据
const lock = app.globalData.lock;
const accountInfo = app.globalData.accountInfo;
console.log('lockDetail onLoad:', {lock, accountInfo});
if (!lock || !accountInfo) {
wx.showToast({
title: '获取设备信息失败',
icon: 'none'
});
setTimeout(() => {
wx.navigateBack();
}, 1500);
return;
}
// 设置数据到页面
this.setData({
lock,
accountInfo
});
this.searchWifi();
},
async searchWifi() {
console.log('启动搜索WiFi...');
this.setData({loading: true});
wx.showLoading({title: '正在搜索WiFi...', mask: true});
try {
await selectLock({
accountInfo: this.data.accountInfo,
lockId: this.data.lock.lockId
});
const result = await startSearchWiFi({
disconnect: false
});
console.log('searchWifi result', result);
if (result.code === 0) {
// 已经开始搜索loading状态保持等待事件回调结束loading
} else {
this.setData({loading: false});
wx.hideLoading();
wx.showModal({
title: '搜索失败',
content: result.message || '启动WiFi搜索失败',
showCancel: false
});
}
} catch (error) {
this.setData({loading: false});
wx.hideLoading();
wx.showModal({
title: '异常',
content: error.message || 'WiFi搜索异常',
showCancel: false
});
}
},
onWifiItemTap(e) {
const ssid = e.currentTarget.dataset.ssid;
this.setData({
showConnectDialog: true,
connectSsid: ssid,
connectPassword: ''
});
},
onInputSsid(e) {
this.setData({connectSsid: e.detail.value});
},
onInputPassword(e) {
this.setData({connectPassword: e.detail.value});
},
onDialogCancel() {
this.setData({showConnectDialog: false});
},
async onDialogConfirm() {
const ssid = this.data.connectSsid;
const password = this.data.connectPassword;
this.setData({loading: true});
wx.showLoading({title: '正在连接WiFi...', mask: true});
await selectLock({
accountInfo: this.data.accountInfo,
lockId: this.data.lock.lockId
});
// 这里可以进行连接WiFi的逻辑
const result = await connectWiFi({
ssid: ssid,
password: password,
});
console.log('connectWiFi result', result);
this.setData({loading: false, showConnectDialog: false});
wx.hideLoading();
},
onUnload() {
if (typeof starEventOff === 'function') {
starEventOff('searchWiFiResult');
}
},
});