70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
const {
|
|
checkAuth,
|
|
loginWithPassword,
|
|
loginWithWechatCode
|
|
} = require('../../utils/api');
|
|
const { MINIAPP_LOGIN_PATH } = require('../../utils/config');
|
|
|
|
Page({
|
|
data: {
|
|
username: '',
|
|
password: '',
|
|
error: '',
|
|
wechatLoading: false,
|
|
passwordLoading: false
|
|
},
|
|
|
|
async onLoad() {
|
|
try {
|
|
const status = await checkAuth();
|
|
if (status?.authenticated) {
|
|
wx.switchTab({ url: '/pages/chat/index' });
|
|
}
|
|
} catch {
|
|
// Stay on the login page when the existing session cannot be verified.
|
|
}
|
|
},
|
|
|
|
handleUsername(event) {
|
|
this.setData({ username: event.detail.value, error: '' });
|
|
},
|
|
|
|
handlePassword(event) {
|
|
this.setData({ password: event.detail.value, error: '' });
|
|
},
|
|
|
|
async handleWechatLogin() {
|
|
this.setData({ wechatLoading: true, error: '' });
|
|
try {
|
|
await loginWithWechatCode(MINIAPP_LOGIN_PATH);
|
|
wx.switchTab({ url: '/pages/chat/index' });
|
|
} catch (error) {
|
|
this.setData({
|
|
error:
|
|
error?.message ||
|
|
'微信登录暂不可用。若后端尚未开放小程序登录接口,请先使用 H5 账号登录。'
|
|
});
|
|
} finally {
|
|
this.setData({ wechatLoading: false });
|
|
}
|
|
},
|
|
|
|
async handlePasswordLogin() {
|
|
const username = this.data.username.trim();
|
|
const password = this.data.password;
|
|
if (!username || !password) {
|
|
this.setData({ error: '请输入用户名和密码' });
|
|
return;
|
|
}
|
|
this.setData({ passwordLoading: true, error: '' });
|
|
try {
|
|
await loginWithPassword(username, password);
|
|
wx.switchTab({ url: '/pages/chat/index' });
|
|
} catch (error) {
|
|
this.setData({ error: error?.message || '登录失败,请重试' });
|
|
} finally {
|
|
this.setData({ passwordLoading: false });
|
|
}
|
|
}
|
|
});
|