import $ from "../../utils/request"; Page({ data: { userInfo: null, deviceInfo: null }, onShow() { let token = wx.getStorageSync("token") || null; let userInfo = wx.getStorageSync("userInfo") || null; this.setData({ userInfo: userInfo }) if(token && userInfo) { this.getDevice(); } }, // 打开用户信息 openUserInfo() { if(this.data.userInfo) { wx.navigateTo({ url: "/pages/userInfo/userInfo?initBluetooth=1&customerType=1" }) } else { wx.login({ success: (res) => { if (res.code) { this.checkOpenIdLogin(res.code); } 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); if(res.result.token && res.result.user) { this.setData({ token: res.result.token, userInfo: res.result.user }) this.getDevice(); } else { wx.showToast({ icon: "none", title: "请先补全个人信息" }) setTimeout(() => { wx.navigateTo({ url: "/pages/userInfo/userInfo?initBluetooth=0&customerType=1" }) }, 1500) } }) }, // 打开设备详情 openDeviceInfo() { if(this.data.userInfo) { 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: "请先登录" }) } }, // 打开成员管理 openFamilyMembers() { if(this.data.userInfo) { wx.navigateTo({ url: "/pages/familyMembers/familyMembers" }) } 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) }) } } }) } })