const app = getApp(); const { authApi } = require('../../utils/api'); Page({ data: { phone: '', password: '', username: '', isRegister: false, loading: false, error: '', }, onPhoneInput(e) { this.setData({ phone: e.detail.value }); }, onPasswordInput(e) { this.setData({ password: e.detail.value }); }, onUsernameInput(e) { this.setData({ username: e.detail.value }); }, toggleMode() { this.setData({ isRegister: !this.data.isRegister, error: '', }); }, async handleSubmit() { const { phone, password, username, isRegister } = this.data; if (!phone || !password) { this.setData({ error: '请输入手机号和密码' }); return; } if (isRegister && !username) { this.setData({ error: '请输入用户名' }); return; } this.setData({ loading: true, error: '' }); try { if (isRegister) { await authApi.register(phone, password, username); wx.showToast({ title: '注册成功,请登录', icon: 'success' }); this.setData({ isRegister: false }); } else { const res = await authApi.login(phone, password); app.setToken(res.access_token); app.globalData.userInfo = res.user; wx.showToast({ title: '登录成功', icon: 'success' }); setTimeout(() => { wx.switchTab({ url: '/pages/index/index' }); }, 1000); } } catch (err) { this.setData({ error: err.message || '操作失败,请重试' }); } finally { this.setData({ loading: false }); } }, handleWechatLogin() { wx.getUserProfile({ desc: '用于完善用户资料', success: (res) => { console.log('微信登录', res.userInfo); wx.showToast({ title: '微信登录开发中', icon: 'none' }); }, fail: (err) => { console.log('微信登录失败', err); } }); }, });