159 lines
4.1 KiB
JavaScript
159 lines
4.1 KiB
JavaScript
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) {
|
|
this.getDevice(false);
|
|
}
|
|
},
|
|
|
|
openLoginOrReg() {
|
|
if(this.data.token) {
|
|
this.openLoginOrReg_();
|
|
} else {
|
|
this.starLogin().then(() => {
|
|
this.getDevice(true);
|
|
}).catch(() => {
|
|
wx.showToast({
|
|
title: "登录失败,请重试!",
|
|
icon: "none"
|
|
})
|
|
})
|
|
}
|
|
},
|
|
openLoginOrReg_() {
|
|
let deviceInfo = this.data.deviceInfo;
|
|
if(deviceInfo) {
|
|
wx.navigateTo({
|
|
url: "/pages/userInfo/userInfo"
|
|
})
|
|
} else {
|
|
wx.showModal({
|
|
title: "提示",
|
|
content: "当前用户暂未绑定设备,需要绑定吗?",
|
|
confirmText: "去绑定",
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
wx.navigateTo({
|
|
url: "/pages/searchDevice/searchDevice"
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
},
|
|
|
|
// 打开设备详情
|
|
openDeviceInfo() {
|
|
if(this.data.token) {
|
|
this.openDeviceInfo_();
|
|
} else {
|
|
this.starLogin().then(() => {
|
|
this.openDeviceInfo_();
|
|
}).catch(() => {
|
|
wx.showToast({
|
|
title: "登录失败,请重试!",
|
|
icon: "none"
|
|
})
|
|
})
|
|
}
|
|
},
|
|
openDeviceInfo_ () {
|
|
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"
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
},
|
|
|
|
starLogin() {
|
|
return new Promise((resolve, reject) => {
|
|
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
|
|
})
|
|
resolve();
|
|
}).catch((err) => {
|
|
reject();
|
|
})
|
|
} else {
|
|
reject();
|
|
}
|
|
}
|
|
})
|
|
})
|
|
},
|
|
|
|
// 获取已经绑定的设备列表
|
|
getDevice(bol) {
|
|
let params = {};
|
|
$.ajax("weighingScale/list/device", params, "GET", false).then((res) => {
|
|
this.setData({
|
|
deviceInfo: res.result && res.result.length ? res.result[0] : null
|
|
})
|
|
if(bol) {
|
|
this.openLoginOrReg_();
|
|
}
|
|
})
|
|
},
|
|
|
|
// 退出登录
|
|
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)
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}) |