/** * 登录页 — 微信一键登录(规范 §4.1) * * 流程: * 1. wx.login() 获取临时 code * 2. POST /api/v1/auth/wechat/login 换取 JWT * 3. 存储 token → authStore.isLoggedIn = true * 4. 返回上一页(或由路由系统自动跳转首页) */ const { authStore, appStore } = require('../../stores'); const { ROUTE } = require('../../constants'); Page({ data: { loggingIn: false, error: '', platformName: getApp().globalData.platformName, slogan: getApp().globalData.slogan, }, onLoad() { // 如果已登录,直接返回 if (authStore.state.isLoggedIn) { const pages = getCurrentPages(); if (pages.length > 1) { wx.navigateBack(); } else { wx.switchTab({ url: ROUTE.TAB.HOME }); } } }, async onLoginTap() { if (this.data.loggingIn) return; this.setData({ loggingIn: true, error: '' }); try { // 1. 获取微信登录 code const loginRes = await new Promise((resolve, reject) => { wx.login({ success: resolve, fail: reject, }); }); const code = loginRes.code; if (!code) { throw new Error('获取微信登录凭证失败'); } // 2. 调用后端微信登录接口 const result = await authStore.loginWithWechat(code); // 3. 登录成功 → 返回上一页或跳转首页 const pages = getCurrentPages(); if (pages.length > 1) { wx.navigateBack(); } else { wx.switchTab({ url: ROUTE.TAB.HOME }); } } catch (err) { console.error('[Login] 登录失败:', err); this.setData({ error: err?.message || '登录失败,请重试', loggingIn: false, }); appStore.toastError(err?.message || '登录失败,请重试'); } }, onViewPrivacy() { wx.navigateTo({ url: '/pages/help-center/help-center?tab=privacy' }); }, onViewTerms() { wx.navigateTo({ url: '/pages/help-center/help-center?tab=terms' }); }, });