Files
inchstep/miniapp/pages/login/index.js
T
zeronline b01a5f91c3
CI / Server (Go) (push) Has been cancelled
CI / Web (Next.js) (push) Has been cancelled
CI / Mobile (React Native) (push) Has been cancelled
CI / Admin (React) (push) Has been cancelled
CI / Docker Build (push) Has been cancelled
提交修改
2026-06-29 16:49:54 +08:00

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 }) }
}
})