Files
bodyWeight/components/replaceNetwork_3/replaceNetwork_3.js
T

114 lines
3.8 KiB
JavaScript

const app = getApp();
import $ from "../../utils/request";
// 超时时间:15 秒
const SDK_TIMEOUT = 15000;
Component({
properties: {
ssid: {
type: String,
value: ""
},
password: {
type: String,
value: ""
}
},
data: {
},
// 生命周期方法
lifetimes: {
attached() {
this.setNerwork();
},
detached() {
if (this._sdkCancel) { this._sdkCancel(); this._sdkCancel = null; }
}
},
// 组件的方法
methods: {
setNerwork() {
let ssid = this.properties.ssid;
let password = this.properties.password;
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: ssid,
password: password
}, done),
SDK_TIMEOUT,
(res) => {
console.log("setNetwork.js dataConfigNetWork 1", res);
this.netWorkCallBack(res);
},
() => {
wx.showToast({ title: "配网超时,请重试", icon: "none" });
setTimeout(() => {
this.triggerEvent('wifiEvent', { status: false });
}, 1500);
}
);
}
if (version === 'domain2') {
this._sdkCancel = app.callWithTimeout(
(done) => app.globalData.ppScale.activeProtocol.dataConfigUserNetWork({
domain: app.globalData.ppScale.domain2,
ssid: ssid,
password: password,
userName: "apiUser",
userPassword: "3acebb95eb49577e9c2a2082589b9bd6"
}, done),
SDK_TIMEOUT,
(res) => {
console.log("setNetwork.js dataConfigUserNetWork 2", res);
this.netWorkCallBack(res);
},
() => {
wx.showToast({ title: "配网超时,请重试", icon: "none" });
setTimeout(() => {
this.triggerEvent('wifiEvent', { status: false });
}, 1500);
}
);
}
},
netWorkCallBack(res) {
if(res === 23) {
app.globalData.ppScale.wifi.ssid = this.properties.ssid;
app.globalData.ppScale.wifi.password = this.properties.password;
let scaleDeviceId = app.globalData.ppScale.device.mac.replace(/:/g, '');
if(scaleDeviceId) {
let params = {
equipmentCode: scaleDeviceId,
wifiName: this.properties.ssid
};
$.ajax("weighingScale/edit/device", params, "POST").then((res) => {
this.triggerEvent('wifiEvent', {
status: true
});
})
}
} else {
app.globalData.ppScale.plugin.Blue.stop();
wx.showToast({
icon: "none",
title: "配网失败,错误码:" + res
})
setTimeout(() => {
this.triggerEvent('wifiEvent', {
status: false
});
}, 1500)
}
}
}
});