Files
Pet3/miniprogram/pages/login/login.js
2026-06-25 18:24:29 +08:00

60 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// pages/login/login — 0a 微信授权登录
// 登录后:无狗 → 建档(0b);有狗 → 首页(1)docs/05 R1
// 持久化登录:老用户曾登录过则自动静默登录,无需每次手动点击。
const { call } = require('../../utils/cloud.js');
const store = require('../../utils/store.js');
const LOGGED_FLAG = 'hasLoggedIn'; // 本地标记:是否已完成过一次登录授权
Page({
data: { loading: false, autoLogging: false },
onLoad() {
// 已登录过的用户:跳过手动点击,静默登录后直接进入
if (wx.getStorageSync(LOGGED_FLAG)) {
this.autoLogin();
}
},
// 静默登录(老用户),失败则回退到手动登录按钮
async autoLogin() {
this.setData({ autoLogging: true });
const res = await call('login');
if (res.ok) {
this.handleLoggedIn(res.data);
} else {
this.setData({ autoLogging: false });
}
},
// 手动登录(点击「微信一键登录」=同意用户协议与隐私政策)
async onLogin() {
if (this.data.loading) return;
this.setData({ loading: true });
const res = await call('login', {}, { loading: true, loadingText: '登录中' });
this.setData({ loading: false });
if (!res.ok) return;
wx.setStorageSync(LOGGED_FLAG, true); // 记住已登录,下次自动登录
this.handleLoggedIn(res.data);
},
// 登录成功后的统一跳转:有狗进首页,无狗去建档
handleLoggedIn({ userInfo, hasDog }) {
store.set({ userInfo });
if (hasDog) {
wx.switchTab({ url: '/pages/home/home' });
} else {
wx.redirectTo({ url: '/pages/dog-edit/dog-edit' });
}
},
openAgreement() {
wx.navigateTo({ url: '/pages/agreement/agreement' });
},
openPrivacy() {
wx.navigateTo({ url: '/pages/privacy/privacy' });
},
});