148 lines
4.9 KiB
JavaScript
148 lines
4.9 KiB
JavaScript
const app = getApp();
|
|
|
|
// 超时时间:15 秒
|
|
const SDK_TIMEOUT = 15000;
|
|
|
|
Component({
|
|
data: {
|
|
progress: 0,
|
|
|
|
wifiList: [],
|
|
|
|
selectWifi: null,
|
|
type: true,
|
|
selectWifiPWD: ""
|
|
},
|
|
|
|
lifetimes: {
|
|
attached() {
|
|
// WiFi 列表获取由父页面在 activeProtocol 就绪后显式调用,此处不自动触发
|
|
},
|
|
detached() {
|
|
console.log('configureDevice3 detached');
|
|
if (this._sdkCancel) { this._sdkCancel(); this._sdkCancel = null; }
|
|
}
|
|
},
|
|
|
|
// 组件的方法
|
|
methods: {
|
|
initWifi() {
|
|
wx.showLoading({ title: "正在获取Wi-Fi列表...", mask: true });
|
|
// activeProtocol 由父页面在 deviceConnect 回调中赋值,此处直接使用
|
|
const activeProtocol = app.globalData.ppScale.activeProtocol;
|
|
if (!activeProtocol) {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: '设备未连接,请重试', icon: 'none' });
|
|
return;
|
|
}
|
|
this._sdkCancel = app.callWithTimeout(
|
|
(done) => activeProtocol.dataFindSurroundDevice(done),
|
|
SDK_TIMEOUT,
|
|
(res) => {
|
|
wx.hideLoading();
|
|
this.setData({ wifiList: Array.isArray(res) ? res : [] });
|
|
},
|
|
() => {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: "获取Wi-Fi列表超时,请重试", icon: "none" });
|
|
}
|
|
);
|
|
},
|
|
|
|
// 最小化后重连,重置所有状态并重新获取 WiFi 列表
|
|
reInitWifi() {
|
|
this.setData({
|
|
progress: 0,
|
|
wifiList: [],
|
|
selectWifi: null,
|
|
selectWifiPWD: ""
|
|
});
|
|
this.initWifi();
|
|
},
|
|
|
|
selectDevice(e) {
|
|
this.setData({
|
|
progress: 1,
|
|
selectWifi: e.currentTarget.dataset.item,
|
|
})
|
|
},
|
|
|
|
inputTypeChange() {
|
|
this.setData({
|
|
type: !this.data.type
|
|
})
|
|
},
|
|
|
|
passwordInput(e) {
|
|
this.setData({
|
|
selectWifiPWD: e.detail.value
|
|
})
|
|
},
|
|
|
|
setWifiPWD() {
|
|
this.setData({ progress: 2 });
|
|
|
|
let version = app.globalData.ppScale.version;
|
|
if (version === 'domain1') {
|
|
this._sdkCancel = app.callWithTimeout(
|
|
(done) => app.globalData.ppScale.activeProtocol.dataConfigNetWork({
|
|
domain: app.globalData.ppScale.domain1,
|
|
ssid: this.data.selectWifi.ssid,
|
|
password: this.data.selectWifiPWD
|
|
}, done),
|
|
SDK_TIMEOUT,
|
|
(res) => {
|
|
console.log("setNetwork.js dataConfigNetWork 1", res);
|
|
this.netWorkCallBack(res);
|
|
},
|
|
() => {
|
|
this.setData({ progress: 1 });
|
|
wx.showToast({ title: "配网超时,请重试", icon: "none" });
|
|
}
|
|
);
|
|
}
|
|
if (version === 'domain2') {
|
|
this._sdkCancel = app.callWithTimeout(
|
|
(done) => app.globalData.ppScale.activeProtocol.dataConfigUserNetWork({
|
|
domain: app.globalData.ppScale.domain2,
|
|
ssid: this.data.selectWifi.ssid,
|
|
password: this.data.selectWifiPWD,
|
|
userName: "apiUser",
|
|
userPassword: "3acebb95eb49577e9c2a2082589b9bd6",
|
|
}, done),
|
|
SDK_TIMEOUT,
|
|
(res) => {
|
|
console.log("setNetwork.js dataConfigUserNetWork 2", res);
|
|
this.netWorkCallBack(res);
|
|
},
|
|
() => {
|
|
this.setData({ progress: 1 });
|
|
wx.showToast({ title: "配网超时,请重试", icon: "none" });
|
|
}
|
|
);
|
|
}
|
|
},
|
|
|
|
netWorkCallBack(res) {
|
|
if(res == 23) {
|
|
app.globalData.ppScale.wifi.ssid = this.data.selectWifi.ssid;
|
|
app.globalData.ppScale.wifi.password = this.data.selectWifiPWD;
|
|
|
|
this.triggerEvent('deviceEvent', {
|
|
status: true
|
|
});
|
|
} else {
|
|
app.globalData.ppScale.plugin.Blue.stop();
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: "配网失败,错误码:" + res
|
|
})
|
|
setTimeout(() => {
|
|
wx.navigateBack({
|
|
delta: 2
|
|
})
|
|
}, 1500)
|
|
}
|
|
}
|
|
}
|
|
}); |