const app = getApp(); import $ from "../../utils/request"; Page({ data: { name: "", deviceSelect: null }, onLoad(options) { app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => { console.log('connectedDevice ===》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("connectedDevice ===》codeUpdateMTU", res); app.globalData.ppScale.activeProtocol.codeFetchBindingState((res) => { console.log("connectedDevice ===》codeFetchBindingState", res); wx.hideLoading(); if (res === 1) { wx.showModal({ title: '提示', content: '当前设备已被绑定,你确定要覆盖绑定吗?', success: (res) => { if (res.confirm) { 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, }) let pages = getCurrentPages(); let prevPage = pages[pages.length - 2]; prevPage.setData({ getDeviceSetting: true }) setTimeout(() => { wx.navigateBack({ delta: 2 }) }, 1500); }); }) } else if (res.cancel) { app.globalData.ppScale.plugin.Blue.disconnect(); } } }) } else { app.globalData.ppScale.device.name = this.data.name; wx.redirectTo({ url: "/pages/connectionSuccessful/connectionSuccessful" }) } }) }); }); }, onShow() { let token = wx.getStorageSync("token") || null; let userInfo = wx.getStorageSync("userInfo") || null; let deviceSelect = app.globalData.ppScale.device.connection; this.setData({ token: token, userInfo: userInfo, name: deviceSelect.name, deviceSelect: deviceSelect }) }, nameInput(e) { this.setData({ name: e.detail.value }) }, // 配对 openConnectionSuccessful() { let token = this.data.token; let userInfo = this.data.userInfo; if (token && userInfo) { this.starConnection(); } else { wx.login({ success: (res) => { if (res.code) { this.checkOpenIdLogin(res.code); } else { wx.showToast({ icon: "none", title: "登录失败,请稍后再试!", }) } } }) } }, starConnection() { let deviceSelect = this.data.deviceSelect; if (deviceSelect) { let name = this.data.name; if (name) { wx.showLoading({ title: "蓝牙配对中...", mask: true }); app.globalData.ppScale.device.name = name; app.globalData.ppScale.plugin.Blue.createBLEConnection(deviceSelect); } else { wx.showToast({ icon: "none", title: "设备名称不能为空" }) } } else { wx.showToast({ icon: "none", title: "配对设备不能为空" }) } }, // 微信一键登录 checkOpenIdLogin(code) { let params = { code: code }; $.ajax("weighingScale/checkOpenIdLogin", params, "POST", true, "登录中...").then((res) => { wx.setStorageSync("userInfo", res.result.user); wx.setStorageSync("token", res.result.token); this.starConnection(); }) }, })