Files
cuotibiye/miniprogram/pages/login/login.js
T

49 lines
1.3 KiB
JavaScript

const api = require("../../utils/api");
Page({
data: { loading: false, apiBase: "" },
onLoad() {
const app = getApp();
this.setData({ apiBase: app.globalData.apiBase });
if (app.globalData.token) {
wx.reLaunch({ url: "/pages/home/home" });
}
},
onLogin() {
if (this.data.loading) return;
this.setData({ loading: true });
wx.login({
success: (res) => {
if (!res.code) {
this.setData({ loading: false });
wx.showToast({ title: "没拿到登录 code", icon: "none" });
return;
}
api
.post("/api/auth/wx-login", { code: res.code })
.then((d) => {
getApp().setToken(d.token, d.guardian);
wx.showToast({ title: "登录成功", icon: "success", duration: 800 });
setTimeout(() => {
if (d.needs_consent) {
wx.reLaunch({ url: "/pages/consent/consent" });
} else {
wx.reLaunch({ url: "/pages/home/home" });
}
}, 500);
})
.catch((e) => {
api.toastError(e);
this.setData({ loading: false });
});
},
fail: () => {
this.setData({ loading: false });
wx.showToast({ title: "微信登录失败", icon: "none" });
},
});
},
});