logic.js 6.4 KB

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