/** * 登录页 — 微信一键登录(规范 §4.1) */ // eslint-disable-next-line @typescript-eslint/no-unused-vars import { authStore, appStore } from '../../stores' // eslint-disable-next-line @typescript-eslint/no-unused-vars import { ROUTE } from '../../constants' interface LoginData { loggingIn: boolean error: string platformName: string slogan: string } 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 { const loginRes = await new Promise<{ code: string }>((resolve, reject) => { wx.login({ success: resolve, fail: reject }) }) if (!loginRes.code) throw new Error('获取微信登录凭证失败') await authStore.loginWithWechat(loginRes.code) const pages = getCurrentPages() if (pages.length > 1) { wx.navigateBack() } else { wx.switchTab({ url: ROUTE.TAB.HOME }) } } catch (err: unknown) { // api.js 的 reject 值是普通对象 { code, message, requestId },不是 Error 实例。 // 此前只认 `instanceof Error`,于是服务端文案(「账号已被封禁」「该手机号已被…」) // 全被丢掉,用户只看到笼统的「登录失败,请重试」,无从判断该怎么处理。 const fromRejection = err && typeof err === 'object' && typeof (err as { message?: unknown }).message === 'string' ? (err as { message: string }).message : '' const rawMessage = fromRejection || (err instanceof Error ? err.message : '') const message = /access_token missing|需要重新登录/.test(rawMessage) ? '微信登录会话已失效,请退出并重新登录微信开发者工具后再试' : rawMessage || '登录失败,请重试' console.error('[Login] 登录失败:', err) this.setData({ error: message, loggingIn: false }) appStore.toastError(message) } }, onViewPrivacy() { wx.navigateTo({ url: '/pages/help-center/help-center?tab=privacy' }) }, onOperatorEntry() { wx.navigateTo({ url: ROUTE.OPERATOR.LOGIN }) }, onViewTerms() { wx.navigateTo({ url: '/pages/help-center/help-center?tab=terms' }) }, })