logic.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { loginByPassword, loginBySms, sendSmsCode } from '@/api/auth'
  2. import { setToken } from '@/utils/auth'
  3. export default {
  4. data() {
  5. return {
  6. currentTab: 0, // 0: 免密, 1: 密码
  7. mobile: '',
  8. code: '',
  9. password: '',
  10. showPassword: false,
  11. isAgreed: false,
  12. countDown: 0,
  13. timer: null,
  14. showAgreementModal: false,
  15. agreementTitle: '',
  16. agreementContent: '',
  17. loginLoading: false
  18. }
  19. },
  20. methods: {
  21. showAgreement(type) {
  22. this.agreementTitle = type === 1 ? '用户服务协议' : '隐私政策';
  23. if (type === 1) {
  24. this.agreementContent = '1. 服务条款\n欢迎使用宠宝平台。您在使用本服务时需遵守以下条款...\n\n2. 用户责任\n用户需对自己的行为负责...\n\n3. 账号管理\n请妥善保管您的账号密码...';
  25. } else {
  26. this.agreementContent = '1. 信息收集\n为了提供服务,我们需要收集您的手机号、地理位置、设备信息等必要数据。\n\n2. 信息使用\n您的位置信息将用于订单匹配和路径规划;您的联系方式将用于接单通知和客户沟通。\n\n3. 信息保护\n我们将采取严格的安全措施保护您的个人信息,未经授权不会向第三方披露。';
  27. }
  28. this.showAgreementModal = true;
  29. },
  30. async getVerifyCode() {
  31. if (this.currentTab === 1) return;
  32. if (this.countDown > 0) return;
  33. if (!this.mobile || this.mobile.length !== 11) {
  34. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  35. return;
  36. }
  37. try {
  38. const res = await sendSmsCode(this.mobile);
  39. // 发送成功,启动倒计时
  40. this.countDown = 60;
  41. this.timer = setInterval(() => {
  42. this.countDown--;
  43. if (this.countDown <= 0) {
  44. clearInterval(this.timer);
  45. }
  46. }, 1000);
  47. // TODO 【生产环境必须删除】开发模式下后端会返回验证码,自动填入方便测试
  48. const devCode = res.data;
  49. if (devCode) {
  50. this.code = devCode;
  51. uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 });
  52. } else {
  53. uni.showToast({ title: '验证码已发送', icon: 'none' });
  54. }
  55. } catch (err) {
  56. console.error('发送验证码失败:', err);
  57. }
  58. },
  59. async handleLogin() {
  60. if (!this.isAgreed) {
  61. uni.showToast({ title: '请先同意用户协议', icon: 'none' });
  62. return;
  63. }
  64. if (!this.mobile) {
  65. uni.showToast({ title: '请输入手机号', icon: 'none' });
  66. return;
  67. }
  68. if (this.currentTab === 0) {
  69. // 免密登录
  70. if (!this.code) {
  71. uni.showToast({ title: '请输入验证码', icon: 'none' });
  72. return;
  73. }
  74. } else {
  75. // 密码登录
  76. if (!this.password) {
  77. uni.showToast({ title: '请输入密码', icon: 'none' });
  78. return;
  79. }
  80. }
  81. if (this.loginLoading) return;
  82. this.loginLoading = true;
  83. try {
  84. let res;
  85. if (this.currentTab === 0) {
  86. // 短信验证码登录
  87. res = await loginBySms(this.mobile, this.code);
  88. } else {
  89. // 密码登录
  90. res = await loginByPassword(this.mobile, this.password);
  91. }
  92. // 保存 Token
  93. const token = res.data?.access_token || res.access_token;
  94. if (token) {
  95. setToken(token);
  96. }
  97. uni.showToast({ title: '登录成功', icon: 'success' });
  98. setTimeout(() => {
  99. uni.switchTab({
  100. url: '/pages/home/index'
  101. });
  102. }, 1000);
  103. } catch (err) {
  104. // 错误已在 request.js 中统一处理
  105. console.error('登录失败:', err);
  106. } finally {
  107. this.loginLoading = false;
  108. }
  109. },
  110. goToRecruit() {
  111. uni.navigateTo({
  112. url: '/pages/recruit/landing'
  113. });
  114. },
  115. goToForgotPwd() {
  116. uni.navigateTo({
  117. url: '/pages/login/reset-pwd-verify'
  118. });
  119. }
  120. }
  121. }