logic.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { loginByPassword, loginBySms } from '@/api/auth'
  2. import { sendSmsCode } from '@/api/resource/sms'
  3. import { getAgreement } from '@/api/system/agreement'
  4. import { setToken } from '@/utils/auth'
  5. import { startGpsTimer } from '@/utils/gps'
  6. export default {
  7. data() {
  8. return {
  9. currentTab: 1, // 0: 免密, 1: 密码
  10. mobile: '',
  11. code: '',
  12. password: '',
  13. showPassword: false,
  14. isAgreed: false,
  15. countDown: 0,
  16. timer: null,
  17. showAgreementModal: false,
  18. agreementTitle: '', // 协议标题
  19. agreementContent: '', // 协议内容
  20. loginLoading: false
  21. }
  22. },
  23. onLoad() {
  24. // 进入登录页时,清除招募认证暂存数据
  25. uni.removeStorageSync('recruit_form_data');
  26. uni.removeStorageSync('recruit_auth_data');
  27. uni.removeStorageSync('recruit_qual_data');
  28. },
  29. methods: {
  30. /**
  31. * 显示协议弹窗
  32. * @param {Number} id 协议ID (1: 用户服务协议, 2: 隐私政策)
  33. */
  34. async showAgreement(id) {
  35. try {
  36. uni.showLoading({ title: '加载中...' });
  37. const res = await getAgreement(id);
  38. if (res.code === 200 && res.data) {
  39. this.agreementTitle = res.data.title;
  40. this.agreementContent = res.data.content;
  41. this.showAgreementModal = true;
  42. } else {
  43. uni.showToast({ title: res.msg || '获取协议失败', icon: 'none' });
  44. }
  45. } catch (err) {
  46. console.error('获取协议详情失败:', err);
  47. } finally {
  48. uni.hideLoading();
  49. }
  50. },
  51. /* async getVerifyCode() {
  52. if (this.currentTab === 1) return;
  53. if (this.countDown > 0) return;
  54. if (!this.mobile || this.mobile.length !== 11) {
  55. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  56. return;
  57. }
  58. try {
  59. const res = await sendSmsCode(this.mobile);
  60. // 发送成功,启动倒计时
  61. this.countDown = 60;
  62. this.timer = setInterval(() => {
  63. this.countDown--;
  64. if (this.countDown <= 0) {
  65. clearInterval(this.timer);
  66. }
  67. }, 1000);
  68. // TODO 【生产环境必须删除】开发模式下后端会返回验证码,自动填入方便测试
  69. const devCode = res.data;
  70. if (devCode) {
  71. this.code = devCode;
  72. uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 });
  73. } else {
  74. uni.showToast({ title: '验证码已发送', icon: 'none' });
  75. }
  76. } catch (err) {
  77. console.error('发送验证码失败:', err);
  78. }
  79. }, */
  80. async handleLogin() {
  81. if (!this.isAgreed) {
  82. uni.showToast({ title: '请先同意用户协议', icon: 'none' });
  83. return;
  84. }
  85. if (!this.mobile) {
  86. uni.showToast({ title: '请输入手机号', icon: 'none' });
  87. return;
  88. }
  89. /* if (this.currentTab === 0) {
  90. // 免密登录
  91. if (!this.code) {
  92. uni.showToast({ title: '请输入验证码', icon: 'none' });
  93. return;
  94. }
  95. } else {
  96. // 密码登录
  97. if (!this.password) {
  98. uni.showToast({ title: '请输入密码', icon: 'none' });
  99. return;
  100. }
  101. } */
  102. if (!this.password) {
  103. uni.showToast({ title: '请输入密码', icon: 'none' });
  104. return;
  105. }
  106. if (this.loginLoading) return;
  107. this.loginLoading = true;
  108. uni.showLoading({
  109. title: '登录中...',
  110. mask: true
  111. });
  112. try {
  113. let res;
  114. /* if (this.currentTab === 0) {
  115. // 短信验证码登录
  116. res = await loginBySms(this.mobile, this.code);
  117. } else {
  118. // 密码登录
  119. res = await loginByPassword(this.mobile, this.password);
  120. } */
  121. res = await loginByPassword(this.mobile, this.password);
  122. // 保存 Token
  123. const token = res.data?.access_token || res.access_token;
  124. if (token) {
  125. setToken(token);
  126. }
  127. startGpsTimer();
  128. uni.showToast({ title: '登录成功', icon: 'success' });
  129. setTimeout(() => {
  130. uni.switchTab({
  131. url: '/pages/home/index'
  132. });
  133. }, 1000);
  134. } catch (err) {
  135. // 错误已在 request.js 中统一处理
  136. console.error('登录失败:', err);
  137. } finally {
  138. this.loginLoading = false;
  139. uni.hideLoading();
  140. }
  141. },
  142. goToRecruit() {
  143. uni.navigateTo({
  144. url: '/pages/recruit/landing'
  145. });
  146. },
  147. goToForgotPwd() {
  148. uni.navigateTo({
  149. url: '/pages/login/reset-pwd-verify'
  150. });
  151. }
  152. }
  153. }