logic.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { loginByPassword, loginBySms, sendSmsCode } from '@/api/auth'
  2. import { setToken } from '@/utils/auth'
  3. import { startGpsTimer } from '@/utils/gps'
  4. export default {
  5. data() {
  6. return {
  7. currentTab: 1, // 0: 免密, 1: 密码
  8. mobile: '',
  9. code: '',
  10. password: '',
  11. showPassword: false,
  12. isAgreed: false,
  13. countDown: 0,
  14. timer: null,
  15. showAgreementModal: false,
  16. currentAgreementId: '', // 当前显示的协议ID
  17. loginLoading: false
  18. }
  19. },
  20. methods: {
  21. /**
  22. * 显示协议弹窗
  23. * @param {Number} id 协议ID (1: 用户服务协议, 2: 隐私政策)
  24. */
  25. showAgreement(id) {
  26. this.currentAgreementId = id;
  27. this.showAgreementModal = true;
  28. },
  29. /* async getVerifyCode() {
  30. if (this.currentTab === 1) return;
  31. if (this.countDown > 0) return;
  32. if (!this.mobile || this.mobile.length !== 11) {
  33. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  34. return;
  35. }
  36. try {
  37. const res = await sendSmsCode(this.mobile);
  38. // 发送成功,启动倒计时
  39. this.countDown = 60;
  40. this.timer = setInterval(() => {
  41. this.countDown--;
  42. if (this.countDown <= 0) {
  43. clearInterval(this.timer);
  44. }
  45. }, 1000);
  46. // TODO 【生产环境必须删除】开发模式下后端会返回验证码,自动填入方便测试
  47. const devCode = res.data;
  48. if (devCode) {
  49. this.code = devCode;
  50. uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 });
  51. } else {
  52. uni.showToast({ title: '验证码已发送', icon: 'none' });
  53. }
  54. } catch (err) {
  55. console.error('发送验证码失败:', err);
  56. }
  57. }, */
  58. async handleLogin() {
  59. if (!this.isAgreed) {
  60. uni.showToast({ title: '请先同意用户协议', icon: 'none' });
  61. return;
  62. }
  63. if (!this.mobile) {
  64. uni.showToast({ title: '请输入手机号', icon: 'none' });
  65. return;
  66. }
  67. /* if (this.currentTab === 0) {
  68. // 免密登录
  69. if (!this.code) {
  70. uni.showToast({ title: '请输入验证码', icon: 'none' });
  71. return;
  72. }
  73. } else {
  74. // 密码登录
  75. if (!this.password) {
  76. uni.showToast({ title: '请输入密码', icon: 'none' });
  77. return;
  78. }
  79. } */
  80. if (!this.password) {
  81. uni.showToast({ title: '请输入密码', icon: 'none' });
  82. return;
  83. }
  84. if (this.loginLoading) return;
  85. this.loginLoading = true;
  86. uni.showLoading({
  87. title: '登录中...',
  88. mask: true
  89. });
  90. try {
  91. let res;
  92. /* if (this.currentTab === 0) {
  93. // 短信验证码登录
  94. res = await loginBySms(this.mobile, this.code);
  95. } else {
  96. // 密码登录
  97. res = await loginByPassword(this.mobile, this.password);
  98. } */
  99. res = await loginByPassword(this.mobile, this.password);
  100. // 保存 Token
  101. const token = res.data?.access_token || res.access_token;
  102. if (token) {
  103. setToken(token);
  104. }
  105. startGpsTimer();
  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. }