2026-03-31 18:09:37 +08:00

59 lines
1.3 KiB
JavaScript

const auth = require('../../../utils/auth');
const PHONE_RE = /^1[3456789]\d{9}$/;
Page({
data: {
mobile: '',
password: '',
logging: false
},
onShow() {
const accessToken = wx.getStorageSync('accessToken') || '';
if (accessToken) {
wx.redirectTo({ url: '/pages/mine/mine' });
}
},
onMobileInput(e) {
this.setData({ mobile: (e.detail.value || '').trim() });
},
onPasswordInput(e) {
this.setData({ password: e.detail.value || '' });
},
onLoginTap() {
if (this.data.logging) return;
const { mobile, password } = this.data;
if (!PHONE_RE.test(mobile)) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' });
return;
}
if (!password) {
wx.showToast({ title: '请输入密码', icon: 'none' });
return;
}
this.setData({ logging: true });
wx.showLoading({ title: '登录中...' });
auth
.loginWithMobilePassword({ mobile, password })
.then(() => {
wx.showToast({ title: '登录成功', icon: 'success' });
setTimeout(() => {
wx.redirectTo({ url: '/pages/mine/mine' });
}, 400);
})
.catch(() => {
wx.showToast({ title: '登录失败', icon: 'none' });
})
.finally(() => {
this.setData({ logging: false });
wx.hideLoading();
});
}
});