import $ from "../../utils/request"; Page({ data: { userInfo: null, token: null, deviceInfo: null }, onShow() { let token = wx.getStorageSync("token") || null; let userInfo = wx.getStorageSync("userInfo") || null; this.setData({ userInfo: userInfo, token: token }) if(token && userInfo) { this.getDevice(); } }, openLoginOrReg() { if(this.data.userInfo === null && this.data.token === null) { wx.login({ success: (res) => { if (res.code) { let params = { code: res.code }; $.ajax("weighingScale/checkOpenIdLogin", params, "POST", true, "登录中...").then((res) => { wx.setStorageSync("userInfo", res.result.user); wx.setStorageSync("token", res.result.token); this.setData({ token: res.result.token, userInfo: res.result.user }) }) } else { wx.showToast({ icon: "none", title: "登录失败,请稍后再试!", }) } } }) } }, // 打开设备详情 openDeviceInfo() { if(this.data.userInfo && this.data.token) { let deviceInfo = this.data.deviceInfo; if(deviceInfo) { wx.navigateTo({ url: "/pages/deviceInfo/deviceInfo" }) } else { wx.showModal({ title: "提示", content: "当前暂未绑定设备,需要绑定吗?", confirmText: "去绑定", success: (res) => { if (res.confirm) { wx.navigateTo({ url: "/pages/searchDevice/searchDevice" }) } } }) } } else { wx.showToast({ icon: "none", title: "请先登录" }) } }, // 获取已经绑定的设备列表 getDevice() { let params = {}; $.ajax("weighingScale/list/device", params, "GET", false).then((res) => { this.setData({ deviceInfo: res.result && res.result.length ? res.result[0] : null }) }) }, // 退出登录 exitLogin() { wx.showModal({ title: "提示", content: "你确定要退出登录吗?", success: (res) => { if (res.confirm) { $.ajax("weighingScale/logout", {}, "POST", true, "正在退出登录...").then(res => { wx.showToast({ icon: "none", title: res.message }) setTimeout(() => { wx.clearStorageSync(); wx.switchTab({ url: "/pages/home/home" }) }, 1500) }) } } }) } })