logic.js 5.3 KB

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