import { loginByPassword, loginBySms, sendSmsCode } from '@/api/auth' import { setToken } from '@/utils/auth' export default { data() { return { currentTab: 0, // 0: 免密, 1: 密码 mobile: '', code: '', password: '', showPassword: false, isAgreed: false, countDown: 0, timer: null, showAgreementModal: false, agreementTitle: '', agreementContent: '', loginLoading: false } }, methods: { showAgreement(type) { this.agreementTitle = type === 1 ? '用户服务协议' : '隐私政策'; if (type === 1) { this.agreementContent = '1. 服务条款\n欢迎使用宠宝平台。您在使用本服务时需遵守以下条款...\n\n2. 用户责任\n用户需对自己的行为负责...\n\n3. 账号管理\n请妥善保管您的账号密码...'; } else { this.agreementContent = '1. 信息收集\n为了提供服务,我们需要收集您的手机号、地理位置、设备信息等必要数据。\n\n2. 信息使用\n您的位置信息将用于订单匹配和路径规划;您的联系方式将用于接单通知和客户沟通。\n\n3. 信息保护\n我们将采取严格的安全措施保护您的个人信息,未经授权不会向第三方披露。'; } this.showAgreementModal = true; }, async getVerifyCode() { if (this.currentTab === 1) return; if (this.countDown > 0) return; if (!this.mobile || this.mobile.length !== 11) { uni.showToast({ title: '请输入正确的手机号', icon: 'none' }); return; } try { const res = await sendSmsCode(this.mobile); // 发送成功,启动倒计时 this.countDown = 60; this.timer = setInterval(() => { this.countDown--; if (this.countDown <= 0) { clearInterval(this.timer); } }, 1000); // TODO 【生产环境必须删除】开发模式下后端会返回验证码,自动填入方便测试 const devCode = res.data; if (devCode) { this.code = devCode; uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 }); } else { uni.showToast({ title: '验证码已发送', icon: 'none' }); } } catch (err) { console.error('发送验证码失败:', err); } }, async handleLogin() { if (!this.isAgreed) { uni.showToast({ title: '请先同意用户协议', icon: 'none' }); return; } if (!this.mobile) { uni.showToast({ title: '请输入手机号', icon: 'none' }); return; } if (this.currentTab === 0) { // 免密登录 if (!this.code) { uni.showToast({ title: '请输入验证码', icon: 'none' }); return; } } else { // 密码登录 if (!this.password) { uni.showToast({ title: '请输入密码', icon: 'none' }); return; } } if (this.loginLoading) return; this.loginLoading = true; try { let res; if (this.currentTab === 0) { // 短信验证码登录 res = await loginBySms(this.mobile, this.code); } else { // 密码登录 res = await loginByPassword(this.mobile, this.password); } // 保存 Token const token = res.data?.access_token || res.access_token; if (token) { setToken(token); } uni.showToast({ title: '登录成功', icon: 'success' }); setTimeout(() => { uni.switchTab({ url: '/pages/home/index' }); }, 1000); } catch (err) { // 错误已在 request.js 中统一处理 console.error('登录失败:', err); } finally { this.loginLoading = false; } }, goToRecruit() { uni.navigateTo({ url: '/pages/recruit/landing' }); }, goToForgotPwd() { uni.navigateTo({ url: '/pages/login/reset-pwd-verify' }); } } }