logic.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { loginByPassword, loginBySms, sendSmsCode } from '@/api/auth'
  2. import { setToken } from '@/utils/auth'
  3. export default {
  4. data() {
  5. return {
  6. currentTab: 1, // 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.password) {
  82. uni.showToast({ title: '请输入密码', icon: 'none' });
  83. return;
  84. }
  85. if (this.loginLoading) return;
  86. this.loginLoading = true;
  87. uni.showLoading({
  88. title: '登录中...',
  89. mask: true
  90. });
  91. try {
  92. let res;
  93. /* if (this.currentTab === 0) {
  94. // 短信验证码登录
  95. res = await loginBySms(this.mobile, this.code);
  96. } else {
  97. // 密码登录
  98. res = await loginByPassword(this.mobile, this.password);
  99. } */
  100. res = await loginByPassword(this.mobile, this.password);
  101. // 保存 Token
  102. const token = res.data?.access_token || res.access_token;
  103. if (token) {
  104. setToken(token);
  105. }
  106. uni.showToast({ title: '登录成功', icon: 'success' });
  107. setTimeout(() => {
  108. uni.switchTab({
  109. url: '/pages/home/index'
  110. });
  111. }, 1000);
  112. } catch (err) {
  113. // 错误已在 request.js 中统一处理
  114. console.error('登录失败:', err);
  115. } finally {
  116. this.loginLoading = false;
  117. uni.hideLoading();
  118. }
  119. },
  120. goToRecruit() {
  121. uni.navigateTo({
  122. url: '/pages/recruit/landing'
  123. });
  124. },
  125. goToForgotPwd() {
  126. uni.navigateTo({
  127. url: '/pages/login/reset-pwd-verify'
  128. });
  129. }
  130. }
  131. }