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

92 lines
1.8 KiB
JavaScript

import $ from "../../utils/request";
Page({
data: {
token: null,
userInfo: null,
deviceList: []
},
onShow() {
let token = wx.getStorageSync("token") || null;
let userInfo = wx.getStorageSync("userInfo") || null;
this.setData({
token: token,
userInfo: userInfo
})
if(token && userInfo) {
this.getDevice();
}
},
// 点击登录或注册
openLoginOrReg() {
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)
}
})
},
// 获取已经绑定的设备列表
getDevice() {
let params = {};
$.ajax("weighingScale/list/device", params, "GET", true, "获取设备列表").then((res) => {
this.setData({
deviceList: res.result
})
})
},
// 添加设备
openSearchDevice() {
let token = this.data.token;
let userInfo = this.data.userInfo;
if (token && userInfo) {
wx.navigateTo({
url: "/pages/searchDevice/searchDevice"
})
} else {
wx.showToast({
icon: "none",
title: "请先登录"
})
}
}
})