28 lines
1.0 KiB
JavaScript
28 lines
1.0 KiB
JavaScript
const api = require('../../services/api')
|
|
const app = getApp()
|
|
|
|
Page({
|
|
data: { phone: '', loading: false, error: '', themeMode: 'light' },
|
|
onShow() {
|
|
this.setData({ themeMode: app.globalData.themeMode })
|
|
},
|
|
onLoad() {
|
|
if (app.globalData.token) wx.reLaunch({ url: '/pages/index/index' })
|
|
},
|
|
onPhoneInput(e) { this.setData({ phone: e.detail.value.replace(/\D/g, ''), error: '' }) },
|
|
async onLogin() {
|
|
if (this.data.phone.length !== 11) { this.setData({ error: '请输入正确的手机号' }); return }
|
|
this.setData({ loading: true, error: '' })
|
|
try {
|
|
const result = await api.post('/api/auth/login', { phone: this.data.phone })
|
|
app.globalData.token = result.token
|
|
app.globalData.user = result.user
|
|
wx.setStorageSync('token', result.token)
|
|
wx.setStorageSync('user', result.user)
|
|
wx.reLaunch({ url: '/pages/index/index' })
|
|
} catch (e) {
|
|
this.setData({ error: e.message || '登录失败' })
|
|
} finally { this.setData({ loading: false }) }
|
|
}
|
|
})
|