| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- import { loginByPassword, loginBySms } from '@/api/auth'
- import { sendSmsCode } from '@/api/resource/sms'
- import { getAgreement } from '@/api/system/agreement'
- import { getAppSetting } from '@/api/system/appSetting'
- import { setToken } from '@/utils/auth'
- import { startGpsTimer } from '@/utils/gps'
- export default {
- data() {
- return {
- currentTab: 1, // 0: 免密, 1: 密码
- mobile: '',
- code: '',
- password: '',
- showPassword: false,
- isAgreed: false,
- countDown: 0,
- timer: null,
- showAgreementModal: false,
- agreementTitle: '', // 协议标题
- agreementContent: '', // 协议内容
- loginLoading: false,
- loginIconUrl: '/static/logo.png', // 登录图标
- loginBackgroundUrl: '/static/header.png' // 登录背景
- }
- },
- async onLoad() {
- // 进入登录页时,清除招募认证暂存数据
- uni.removeStorageSync('recruit_form_data');
- uni.removeStorageSync('recruit_auth_data');
- uni.removeStorageSync('recruit_qual_data');
-
- // 获取应用配置
- await this.fetchAppSetting();
- },
- methods: {
- /**
- * 获取应用配置
- */
- async fetchAppSetting() {
- try {
- const res = await getAppSetting(1);
- if (res.code === 200 && res.data) {
- if (res.data.loginIconUrl) {
- this.loginIconUrl = res.data.loginIconUrl;
- }
- if (res.data.loginBackgroundUrl) {
- this.loginBackgroundUrl = res.data.loginBackgroundUrl;
- }
- }
- } catch (err) {
- console.error('获取应用配置失败:', err);
- }
- },
- /**
- * 显示协议弹窗
- * @param {Number} id 协议ID (1: 用户服务协议, 2: 隐私政策)
- */
- async showAgreement(id) {
- try {
- uni.showLoading({ title: '加载中...' });
- const res = await getAgreement(id);
- if (res.code === 200 && res.data) {
- this.agreementTitle = res.data.title;
- this.agreementContent = res.data.content;
- this.showAgreementModal = true;
- } else {
- uni.showToast({ title: res.msg || '获取协议失败', icon: 'none' });
- }
- } catch (err) {
- console.error('获取协议详情失败:', err);
- } finally {
- uni.hideLoading();
- }
- },
- /* async getVerifyCode() {
- if (this.currentTab === 1) return;
- if (this.countDown > 0) return;
- if (!this.mobile || this.mobile.length !== 11) {
- uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
- return;
- }
- try {
- const res = await sendSmsCode(this.mobile);
- // 发送成功,启动倒计时
- this.countDown = 60;
- this.timer = setInterval(() => {
- this.countDown--;
- if (this.countDown <= 0) {
- clearInterval(this.timer);
- }
- }, 1000);
- // TODO 【生产环境必须删除】开发模式下后端会返回验证码,自动填入方便测试
- const devCode = res.data;
- if (devCode) {
- this.code = devCode;
- uni.showToast({ title: '验证码: ' + devCode, icon: 'none', duration: 3000 });
- } else {
- uni.showToast({ title: '验证码已发送', icon: 'none' });
- }
- } catch (err) {
- console.error('发送验证码失败:', err);
- }
- }, */
- async handleLogin() {
- if (!this.isAgreed) {
- uni.showToast({ title: '请先同意用户协议', icon: 'none' });
- return;
- }
- if (!this.mobile) {
- uni.showToast({ title: '请输入手机号', icon: 'none' });
- return;
- }
- /* if (this.currentTab === 0) {
- // 免密登录
- if (!this.code) {
- uni.showToast({ title: '请输入验证码', icon: 'none' });
- return;
- }
- } else {
- // 密码登录
- if (!this.password) {
- uni.showToast({ title: '请输入密码', icon: 'none' });
- return;
- }
- } */
- if (!this.password) {
- uni.showToast({ title: '请输入密码', icon: 'none' });
- return;
- }
- if (this.loginLoading) return;
- this.loginLoading = true;
- uni.showLoading({
- title: '登录中...',
- mask: true
- });
- try {
- let res;
- /* if (this.currentTab === 0) {
- // 短信验证码登录
- res = await loginBySms(this.mobile, this.code);
- } else {
- // 密码登录
- res = await loginByPassword(this.mobile, this.password);
- } */
- res = await loginByPassword(this.mobile, this.password);
- // 保存 Token
- const token = res.data?.access_token || res.access_token;
- if (token) {
- setToken(token);
- }
- startGpsTimer();
- uni.showToast({ title: '登录成功', icon: 'success' });
- setTimeout(() => {
- uni.switchTab({
- url: '/pages/home/index'
- });
- }, 1000);
- } catch (err) {
- // 错误已在 request.js 中统一处理
- console.error('登录失败:', err);
- } finally {
- this.loginLoading = false;
- uni.hideLoading();
- }
- },
- goToRecruit() {
- uni.navigateTo({
- url: '/pages/recruit/landing'
- });
- },
- goToForgotPwd() {
- uni.navigateTo({
- url: '/pages/login/reset-pwd-verify'
- });
- }
- }
- }
|