70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
/**
|
|||
|
|
* 登录页 — 微信一键登录(规范 §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) {
|
||
|
|
const message = err instanceof Error ? err.message : '登录失败,请重试'
|
||
|
|
console.error('[Login] 登录失败:', err)
|
||
|
|
this.setData({ error: message, loggingIn: false })
|
||
|
|
appStore.toastError(message)
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
onViewPrivacy() {
|
||
|
|
wx.navigateTo({ url: '/pages/help-center/help-center?tab=privacy' })
|
||
|
|
},
|
||
|
|
|
||
|
|
onViewTerms() {
|
||
|
|
wx.navigateTo({ url: '/pages/help-center/help-center?tab=terms' })
|
||
|
|
},
|
||
|
|
})
|