Files
bodyWeight/pages/deviceInfo/deviceInfo.js
T
2025-03-25 20:52:15 +08:00

151 lines
4.0 KiB
JavaScript

const app = getApp();
import $ from "../../utils/request";
Page({
data: {
deviceConnect: false,
deviceInfo: null
},
onLoad(options) {
this.getDevice();
this.initBluetooth();
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
console.log('deviceInfo ===》deviceConnect', res);
app.globalData.ppScale.plugin.ScaleAction.startDataProgress();
app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol();
app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => {
console.log("deviceInfo ===》codeUpdateMTU", res);
$.ajax("weighingScale/list/device", {}, "GET", false).then((res) => {
if(res.result && res.result.length) {
let deviceConnect = res.result.some(item => item.macAddress === app.globalData.ppScale.device.mac);
if(deviceConnect) {
this.setData({
deviceConnect: true
})
} else {
app.globalData.ppScale.plugin.Blue.disconnect();
}
}
})
});
});
},
// 初始化蓝牙
initBluetooth() {
wx.openBluetoothAdapter({
success: () => {
this.getDeviceSettingList();
},
fail: (err) => {
if (err.errCode === 10001) {
wx.showModal({
title: "提示",
content: "请打开手机蓝牙",
});
}
},
});
},
async refreshToken(fn, flag) {
let bodyToken = wx.getStorageSync('bodyToken');
let bodyTokenTime = wx.getStorageSync('bodyTokenTime');
if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) {
let res = await app.globalData.ppScale.plugin.refreshToken({
url: "https://uniquehealth.lefuenergy.com",
data: {
"appKey": app.globalData.ppScale.options.key,
"appSecret": app.globalData.ppScale.options.secret
}
});
if (res.data.code == 200) {
wx.setStorageSync('bodyToken', res.data.data.token);
wx.setStorageSync('bodyTokenTime', res.data.data.expireTime);
fn();
}
} else {
fn();
}
},
getDeviceSettingList() {
let bodyToken = wx.getStorageSync("bodyToken");
app.globalData.ppScale.plugin.getDeviceSettingList({
url: "https://uniquehealth.lefuenergy.com",
data: {
appKey: app.globalData.ppScale.options.key
},
header: {
"token": bodyToken,
"Accept-Language": wx.getStorageSync("lang") || 'zh'
}
}).then((res) => {
if (res.data.code == 200) {
app.globalData.ppScale.plugin.Blue.setDeviceSetting(res.data.data);
app.globalData.ppScale.device.setting = res.data.data;
let deviceNames = res.data.data.map(item => item.deviceName);
app.globalData.ppScale.plugin.Blue.start(deviceNames, false);
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[0]);
});
} else if (res.data.code == 401 || res.data.code == 4008) {
// 401 token过期 / 4008 token为空
// 重新请求token解析数据
this.refreshToken(() => {
this.getDeviceSettingList();
}, true);
}
});
},
// 获取已经绑定的设备列表
getDevice() {
let params = {};
$.ajax("weighingScale/list/device", params, "GET", false).then((res) => {
this.setData({
deviceInfo: res.result[0]
})
})
},
delDevice() {
if(this.data.deviceConnect) {
app.globalData.ppScale.activeProtocol.codeClearDeviceData("00", (res) => {
console.log("deviceInfo === codeClearDeviceData", res);
let scaleDeviceId = app.globalData.ppScale.device.mac.replace(/:/g, '');
let params = {
equipmentCode: scaleDeviceId,
};
$.ajax("weighingScale/del/device", params, "POST", true, "正在解绑设备...").then(res_ => {
wx.showToast({
icon: "none",
title: res_.message,
})
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 1500);
});
})
} else {
wx.showToast({
icon: "none",
title: "删除失败,请靠近设备并点亮后再试"
})
this.initBluetooth();
}
},
onUnload() {
app.globalData.ppScale.plugin.Blue.stop();
}
})