Files
lingji-work-fe/src/views/login/components/login-form/index.vue

376 lines
11 KiB
Vue
Raw Normal View History

2025-09-08 16:02:23 +08:00
<!-- eslint-disable vue/no-duplicate-attributes -->
<template>
<div class="flex items-center w-400 h-100%">
<div class="w-full bg-#fff rounded-16px px-40px py-32px flex flex-col items-center">
<img src="@/assets/img/icon-logo.png" alt="" width="144" height="36" class="mb-24px" />
2025-09-08 17:19:56 +08:00
<Tabs v-model:activeKey="activeKey" class="mb-24px" @change="onTabChange">
2025-09-08 16:02:23 +08:00
<TabPane tab="密码登录" key="1" />
<TabPane tab="短信登录" key="2" />
</Tabs>
<Form ref="formRef" :model="loginForm" :rules="formRules" class="w-320 form-wrap">
<FormItem name="mobile">
2025-09-08 17:19:56 +08:00
<Input
v-model:value="loginForm.mobile"
placeholder="请输入手机号"
allowClear
:maxlength="11"
@change="clearErrorMsg"
/>
2025-09-08 16:02:23 +08:00
</FormItem>
<FormItem v-if="isCaptchaLogin" name="captcha" class="captcha-form-item">
2025-09-08 17:19:56 +08:00
<Input v-model:value="loginForm.captcha" placeholder="请输入验证码" :maxlength="6" @change="clearErrorMsg">
2025-09-08 16:02:23 +08:00
<template #suffix>
<div class="w-79px flex justify-center whitespace-nowrap">
<span
class="color-#939499 font-family-regular text-16px font-400 lh-24px cursor-not-allowed"
:class="{
'!color-#6D4CFE': isLegalMobile || countdown > 0,
'!cursor-pointer': canGetCaptcha,
}"
@click="getCode"
>{{ countdown > 0 ? `${countdown}s` : hasGetCode ? '重新发送' : '发送验证码' }}</span
>
</div>
</template>
</Input>
<p class="color-#F64B31 text-12px font-400 lh-20px font-family-regular" v-show="errMsg">
2025-09-08 17:19:56 +08:00
{{ errMsg }}
</p>
2025-09-08 16:02:23 +08:00
</FormItem>
<FormItem v-else name="password" class="password-form-item">
2025-09-08 17:19:56 +08:00
<Input.Password v-model:value="loginForm.password" placeholder="请输入密码" @change="clearErrorMsg">
2025-09-08 16:02:23 +08:00
<template #iconRender="visible">
<img :src="visible ? icon2 : icon1" width="20" height="20" class="cursor-pointer" />
</template>
</Input.Password>
<p class="color-#F64B31 text-12px font-400 lh-20px font-family-regular" v-show="errMsg">
2025-09-08 17:19:56 +08:00
{{ errMsg }}
</p>
2025-09-08 16:02:23 +08:00
</FormItem>
<FormItem class="mt-52px">
<div class="text-12px flex justify-center items-center mb-16px">
<Checkbox v-model:checked="hasCheck" class="mr-8px"></Checkbox>
<span class="text-12px color-#737478 font-400 lh-20px font-family-regular"
>登录即代表同意<span class="color-#6D4CFE"> 用户协议 </span><span class="color-#6D4CFE">
隐私政策</span
></span
>
</div>
<Button
type="primary"
class="w-full h-48 mb-8px !text-16px !font-500 !rounded-8px btn-login"
:class="disabledSubmitBtn ? 'cursor-no-drop' : 'cursor-pointer'"
:disabled="disabledSubmitBtn"
@click="handleSubmit"
>
登录
</Button>
<div class="flex justify-between btn-row">
<Button
type="text"
class="!color-#939499 !p-0 !h-22px hover:color-#6D4CFE"
size="small"
@click="onForgetPassword"
>
忘记密码
</Button>
<Button
type="text"
class="!color-#939499 !p-0 !h-22px hover:color-#6D4CFE"
size="small"
@click="onRegister"
>
注册
</Button>
</div>
</FormItem>
</Form>
</div>
</div>
<PuzzleVerification
:show="isVerificationVisible"
@submit="handleVerificationSubmit"
@cancel="isVerificationVisible = false"
/>
<SelectAccountModal ref="selectAccountModalRef" :mobileNumber="mobileNumber" :accounts="accounts" />
</template>
<script setup lang="ts">
import {
Checkbox,
Modal,
Button,
Form,
FormItem,
Input,
Space,
message,
Typography,
Card,
List,
Tabs,
} from 'ant-design-vue';
const { Link } = Typography;
const { TabPane } = Tabs;
import PuzzleVerification from '../PuzzleVerification.vue';
import SelectAccountModal from '../select-account-modal/index.vue';
import { fetchLoginCaptCha, fetchAuthorizationsCaptcha, fetchProfileInfo, postLoginPassword } from '@/api/all/login';
2025-09-08 17:59:19 +08:00
import { postClearRateLimiter } from '@/api/all/common';
2025-09-08 16:02:23 +08:00
import { joinEnterpriseByInviteCode } from '@/api/all';
import { ref, reactive, onUnmounted, computed } from 'vue';
import { useUserStore } from '@/stores';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
import { handleUserLogin, goUserLogin } from '@/utils/user';
import router from '@/router';
import { useRoute } from 'vue-router';
import icon1 from '@/assets/img/login/icon-close.png';
import icon2 from '@/assets/img/login/icon-open.png';
const setPageType = inject('setPageType');
const formRef = ref();
const route = useRoute();
const userStore = useUserStore();
const enterpriseStore = useEnterpriseStore();
const countdown = ref(0);
let timer = ref();
const isLogin = ref(true);
const isVerificationVisible = ref(false);
const hasGetCode = ref(false);
const submitting = ref(false);
const hasCheck = ref(false);
const mobileNumber = ref('');
const selectAccountModalRef = ref(null);
const accounts = ref([]);
const activeKey = ref('1');
const isLegalMobile = ref(false);
2025-09-08 17:19:56 +08:00
const errMsg = ref('');
2025-09-08 16:02:23 +08:00
const loginForm = reactive({
mobile: '',
captcha: '',
password: '',
});
// 表单校验规则
const formRules = {
mobile: [
{
required: true,
validator: (_rule: any, value: string) => {
if (!value) {
isLegalMobile.value = false;
2025-09-08 17:19:56 +08:00
// return Promise.reject('手机号不能为空');
2025-09-08 16:02:23 +08:00
}
2025-09-08 17:19:56 +08:00
if (value && !/^1[3-9]\d{9}$/.test(value)) {
2025-09-08 16:02:23 +08:00
isLegalMobile.value = false;
return Promise.reject('手机号格式不正确');
} else {
isLegalMobile.value = true;
return Promise.resolve();
}
},
trigger: ['blur'],
},
],
password: [
{
2025-09-08 17:19:56 +08:00
// required: true,
// validator: (_rule: any, value: string) => {
// if (!value) {
// return Promise.reject('请输入密码');
// }
// if (value.length < 6) {
// return Promise.reject('密码长度不能小于6位');
// }
// return Promise.resolve();
// },
// trigger: ['blur'],
2025-09-08 16:02:23 +08:00
},
],
captcha: [
{
2025-09-08 17:19:56 +08:00
// required: true,
// validator: (_rule: any, value: string) => {
// if (!value) {
// return Promise.reject('请输入验证码');
// }
// if (!/^\d{6}$/.test(value)) {
// return Promise.reject('验证码必须是6位数字');
// } else {
// return Promise.resolve();
// }
// },
// trigger: ['blur'],
2025-09-08 16:02:23 +08:00
},
],
};
const isCaptchaLogin = computed(() => {
return activeKey.value === '2';
});
const canGetCaptcha = computed(() => {
return isLegalMobile.value && countdown.value === 0;
});
2025-09-08 17:19:56 +08:00
const clearErrorMsg = () => {
errMsg.value = '';
};
2025-09-08 16:02:23 +08:00
const disabledSubmitBtn = computed(() => {
if (isCaptchaLogin.value) {
return !hasCheck.value || !isLegalMobile.value || !loginForm.captcha.trim() || !/^\d{6}$/.test(loginForm.captcha);
}
// 密码登录时的验证逻辑
return !hasCheck.value || !isLegalMobile.value || !loginForm.password.trim();
});
const validateField = (field: string) => {
formRef.value.validateFields(field);
};
const clearError = (field: string) => {
formRef.value.clearValidate(field);
};
const getCode = async () => {
if (!canGetCaptcha.value) return;
formRef.value.validateFields('mobile').then(() => {
2025-09-08 17:59:19 +08:00
getCaptcha();
2025-09-08 16:02:23 +08:00
});
};
2025-09-08 17:59:19 +08:00
const getCaptcha = async () => {
2025-09-08 16:02:23 +08:00
try {
const { code, message: msg } = await fetchLoginCaptCha({ mobile: loginForm.mobile });
if (code === 200) {
2025-09-08 17:59:19 +08:00
startCountdown();
2025-09-08 16:02:23 +08:00
message.success(msg);
}
} catch (error) {
// 重置倒计时
countdown.value = 0;
clearInterval(timer.value);
2025-09-08 17:59:19 +08:00
if (error.status === 429) {
isVerificationVisible.value = true;
}
2025-09-08 16:02:23 +08:00
}
};
2025-09-08 17:59:19 +08:00
// 验证码验证通过后
const handleVerificationSubmit = async () => {
2025-09-08 17:59:19 +08:00
isVerificationVisible.value = false;
await postClearRateLimiter();
2025-09-08 17:59:19 +08:00
getCaptcha();
};
2025-09-08 17:19:56 +08:00
const onTabChange = () => {
errMsg.value = '';
};
2025-09-08 16:02:23 +08:00
// 获取用户信息
const getProfileInfo = async () => {
const { code, data } = await fetchProfileInfo();
if (code === 200) {
2025-09-10 17:57:42 +08:00
// 已开通
if (data.primary_enterprise?.subscribe_status === 1) {
const enterprises = data['enterprises'];
mobileNumber.value = data['mobile'];
accounts.value = enterprises;
2025-09-08 16:02:23 +08:00
2025-09-10 17:57:42 +08:00
if (enterprises.length > 0) {
enterpriseStore.setEnterpriseInfo(data.enterprises[0]);
if (enterprises.length === 1) {
handleUserLogin();
} else {
// 多个企业时候需要弹窗让用户选择企业
selectAccountModalRef.value.open();
}
2025-09-08 16:02:23 +08:00
}
2025-09-10 17:57:42 +08:00
} else {
router.push({name: 'Trial'})
2025-09-08 16:02:23 +08:00
}
}
};
// 提交表单
const handleSubmit = async () => {
console.log('handleSubmit', disabledSubmitBtn.value);
if (disabledSubmitBtn.value) return;
try {
// 校验所有字段
await formRef.value.validate();
if (!hasCheck.value) {
message.error('请先勾选同意用户协议');
return;
}
submitting.value = true;
const _fn = isCaptchaLogin.value ? fetchAuthorizationsCaptcha : postLoginPassword;
2025-09-08 17:19:56 +08:00
const { code, data, message: errorInfo } = await _fn(loginForm);
2025-09-08 16:02:23 +08:00
if (code === 10001) {
errMsg.value = errorInfo;
return;
}
2025-09-08 16:02:23 +08:00
if (code === 200) {
// 处理登录成功逻辑
message.success('登录成功');
userStore.setToken(data.access_token);
const { invite_code } = route.query;
if (invite_code) {
const { code } = await joinEnterpriseByInviteCode(invite_code as string);
if (code === 200) {
message.success('加入企业成功');
}
}
getProfileInfo();
}
} finally {
submitting.value = false;
}
};
// 开始倒计时
const startCountdown = () => {
countdown.value = 60;
hasGetCode.value = true;
timer.value = setInterval(() => {
countdown.value--;
if (countdown.value <= 0) {
clearInterval(timer.value as number);
timer.value = null;
}
}, 1000);
};
const onForgetPassword = () => {
setPageType('resetPasswordForm');
};
const onRegister = () => {
console.log('onRegister');
setPageType('registerForm');
};
onUnmounted(() => {
if (timer.value) {
clearInterval(timer.value);
}
});
</script>
<style scoped lang="scss">
@import './style.scss';
</style>