feat: 登录注册

This commit is contained in:
rd
2025-09-08 16:02:23 +08:00
parent 67e8196ce9
commit 6c5373689b
11 changed files with 1018 additions and 344 deletions

View File

@ -0,0 +1,337 @@
<!-- 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" />
<Tabs v-model:activeKey="activeKey" class="mb-24px">
<TabPane tab="密码登录" key="1" />
<TabPane tab="短信登录" key="2" />
</Tabs>
<Form ref="formRef" :model="loginForm" :rules="formRules" class="w-320 form-wrap">
<FormItem name="mobile">
<Input v-model:value="loginForm.mobile" placeholder="请输入手机号" allowClear :maxlength="11" />
</FormItem>
<FormItem v-if="isCaptchaLogin" name="captcha" class="captcha-form-item">
<Input v-model:value="loginForm.captcha" placeholder="请输入验证码" :maxlength="6">
<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>
</FormItem>
<FormItem v-else name="password" class="password-form-item">
<Input.Password v-model:value="loginForm.password" placeholder="请输入密码">
<template #iconRender="visible">
<img :src="visible ? icon2 : icon1" width="20" height="20" class="cursor-pointer" />
</template>
</Input.Password>
</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';
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);
const loginForm = reactive({
mobile: '',
captcha: '',
password: '',
});
// 表单校验规则
const formRules = {
mobile: [
{
required: true,
validator: (_rule: any, value: string) => {
if (!value) {
isLegalMobile.value = false;
return Promise.reject('手机号不能为空');
}
if (!/^1[3-9]\d{9}$/.test(value)) {
isLegalMobile.value = false;
return Promise.reject('手机号格式不正确');
} else {
isLegalMobile.value = true;
return Promise.resolve();
}
},
trigger: ['blur'],
},
],
password: [
{
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'],
},
],
captcha: [
{
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'],
},
],
};
const isCaptchaLogin = computed(() => {
return activeKey.value === '2';
});
const canGetCaptcha = computed(() => {
return isLegalMobile.value && countdown.value === 0;
});
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(() => {
isVerificationVisible.value = true;
});
};
// 验证码验证通过后
const handleVerificationSubmit = async () => {
isVerificationVisible.value = false;
startCountdown();
try {
const { code, message: msg } = await fetchLoginCaptCha({ mobile: loginForm.mobile });
if (code === 200) {
message.success(msg);
}
} catch (error) {
// 重置倒计时
countdown.value = 0;
clearInterval(timer.value);
}
};
// 获取用户信息
const getProfileInfo = async () => {
const { code, data } = await fetchProfileInfo();
if (code === 200) {
let enterprises = data['enterprises'];
mobileNumber.value = data['mobile'];
accounts.value = enterprises;
if (enterprises.length > 0) {
enterpriseStore.setEnterpriseInfo(data.enterprises[0]);
if (enterprises.length === 1) {
handleUserLogin();
} else {
// 多个企业时候需要弹窗让用户选择企业
selectAccountModalRef.value.open();
}
}
}
};
// 提交表单
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;
const { code, data } = await _fn(loginForm);
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();
}
} catch (error) {
// 错误信息会显示在输入框下方
} 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>

View File

@ -0,0 +1,34 @@
:deep(.ant-tabs) {
.ant-tabs-nav {
padding: 0;
&::before {
display: none;
}
.ant-tabs-nav-list {
.ant-tabs-tab {
padding: 0 0 4px;
.ant-tabs-tab-btn {
color: #939499;
font-size: 18px;
font-style: normal;
font-weight: 500;
line-height: 26px;
font-family: $font-family-medium;
}
&.ant-tabs-tab-active {
.ant-tabs-tab-btn {
color: #211f24;
}
}
}
.ant-tabs-ink-bar {
border-radius: 4px;
background: var(--Brand-6, #6d4cfe);
width: 24px !important;
height: 4px;
margin-left: 24px;
}
}
}
}

View File

@ -0,0 +1,364 @@
<!-- 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">
<div class="flex items-center mb-24px w-full cursor-pointer" @click="onBack">
<icon-left size="24" class="mr-4px color-#000" />
<span class="color-#000 text-20px font-500 lh-28px font-family-medium">{{
isResetPassword ? '重置密码' : '手机注册'
}}</span>
</div>
<Form ref="formRef" :model="formData" :rules="formRules" auto-label-width class="w-320 form-wrap">
<FormItem name="mobile">
<Input v-model:value="formData.mobile" placeholder="请输入手机号" allowClear :maxlength="11" />
</FormItem>
<FormItem name="password" class="password-form-item">
<Input.Password v-model:value="formData.password" placeholder="新密码">
<template #iconRender="visible">
<img :src="visible ? icon2 : icon1" width="20" height="20" class="cursor-pointer" />
</template>
</Input.Password>
</FormItem>
<FormItem name="confirm_password" class="password-form-item">
<Input.Password v-model:value="formData.confirm_password" placeholder="密码确认">
<template #iconRender="visible">
<img :src="visible ? icon2 : icon1" width="20" height="20" class="cursor-pointer" />
</template>
</Input.Password>
</FormItem>
<FormItem name="captcha" class="captcha-form-item">
<Input v-model:value="formData.captcha" placeholder="请输入验证码" :maxlength="6">
<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': (isPassPassword && isLegalMobile) || countdown > 0,
'!cursor-pointer': canGetCaptcha,
}"
@click="getCode"
>{{ countdown > 0 ? `${countdown}s` : hasGetCode ? '重新发送' : '发送验证码' }}</span
>
</div>
</template>
</Input>
</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"
>
{{ isResetPassword ? '重置' : '注册' }}
</Button>
</FormItem>
</Form>
</div>
</div>
<PuzzleVerification
:show="isVerificationVisible"
@submit="handleVerificationSubmit"
@cancel="isVerificationVisible = false"
/>
<SelectAccountModal ref="selectAccountModalRef" :mobileNumber="mobileNumber" :accounts="accounts" />
</template>
<script setup lang="js">
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 { postRegisterCaptcha,postForgetPasswordCaptcha, fetchProfileInfo, postRegister, postForgetPassword } from '@/api/all/login';
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 pageType = inject('pageType');
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 isLegalMobile = ref(false);
const formData = ref({
mobile: '',
captcha: '',
password: '',
confirm_password: '',
});
// 表单校验规则
const formRules = {
mobile: [
{
required: true,
validator: (_rule, value) => {
if (!value) {
isLegalMobile.value = false;
return Promise.reject('手机号不能为空');
}
if (!/^1[3-9]\d{9}$/.test(value)) {
isLegalMobile.value = false;
return Promise.reject('手机号格式不正确');
} else {
isLegalMobile.value = true;
return Promise.resolve();
}
},
trigger: ['blur'],
},
],
password: [
{
required: true,
validator: (_rule, value) => {
if (!value) {
return Promise.reject('请输入新密码');
}
if (value.length < 6) {
return Promise.reject('密码长度不能小于6位');
}
if(formData.value.confirm_password) {
formRef.value.validateFields('confirm_password');
}
return Promise.resolve();
},
trigger: ['blur'],
},
],
confirm_password: [
{
required: true,
validator: (_rule, value) => {
if (!value) {
return Promise.reject('请输入密码确认');
}
if (value.length < 6) {
return Promise.reject('密码长度不能小于6位');
}
if (value !== formData.value.password) {
return Promise.reject('确认密码与设置的密码不同');
}
return Promise.resolve();
},
trigger: ['blur'],
},
],
captcha: [
{
required: true,
validator: (_rule, value) => {
if (!value) {
return Promise.reject('请输入验证码');
}
if (!/^\d{6}$/.test(value)) {
return Promise.reject('验证码必须是6位数字');
} else {
return Promise.resolve();
}
},
trigger: ['blur'],
},
],
};
// 重置密码
const isResetPassword = computed(() => {
return pageType.value === 'resetPasswordForm';
});
const isPassPassword = computed(() => {
return (
formData.value.confirm_password &&
formData.value.password &&
formData.value.confirm_password === formData.value.password
);
});
const canGetCaptcha = computed(() => {
return isLegalMobile.value && countdown.value === 0 && isPassPassword.value;
});
const disabledSubmitBtn = computed(() => {
return !isPassPassword.value || !hasCheck.value || !isLegalMobile.value || !formData.value.password.trim();
});
const validateField = (field) => {
formRef.value.validateFields(field);
};
const clearError = (field) => {
formRef.value.clearValidate(field);
};
const getCode = async () => {
if (!canGetCaptcha.value) {
if (!formData.value.mobile.trim()) {
formRef.value.validateFields('mobile');
return;
}
return;
}
formRef.value.validateFields('mobile').then(() => {
isVerificationVisible.value = true;
});
};
// 验证码验证通过后
const handleVerificationSubmit = async () => {
isVerificationVisible.value = false;
startCountdown();
try {
const fn = isResetPassword.value ? postForgetPasswordCaptcha : postRegisterCaptcha;
const { code, message: msg } = await fn({ mobile: formData.value.mobile });
if (code === 200) {
message.success(msg);
}
} catch (error) {
// 重置倒计时
countdown.value = 0;
clearInterval(timer.value);
}
};
// 获取用户信息
const getProfileInfo = async () => {
const { code, data } = await fetchProfileInfo();
if (code === 200) {
let enterprises = data['enterprises'];
mobileNumber.value = data['mobile'];
accounts.value = enterprises;
if (enterprises.length > 0) {
enterpriseStore.setEnterpriseInfo(data.enterprises[0]);
if (enterprises.length === 1) {
setTimeout(() => {
handleUserLogin();
}, 1500);
} else {
selectAccountModalRef.value.open();
}
}
}
};
// 提交表单
const handleSubmit = async () => {
if (disabledSubmitBtn.value) return;
try {
// 校验所有字段
await formRef.value.validate();
if (!hasCheck.value) {
message.error('请先勾选同意用户协议');
return;
}
submitting.value = true;
const _fn = isResetPassword.value ? postForgetPassword : postRegister;
const { code, data } = await _fn(formData.value);
if (code === 200) {
message.success(isResetPassword.value ? '重置成功' : '注册成功');
// 注册成功后跳转登录页
if(!isResetPassword.value) {
setTimeout(() => {
setPageType('loginForm');
}, 1500);
return;
};
userStore.setToken(data.access_token);
const { invite_code } = route.query;
if (invite_code) {
const { code } = await joinEnterpriseByInviteCode(invite_code);
if (code === 200) {
message.success('加入企业成功');
}
}
getProfileInfo();
}
} catch (error) {
// 错误信息会显示在输入框下方
} finally {
submitting.value = false;
}
};
const onBack = () => {
setPageType('loginForm');
};
// 开始倒计时
const startCountdown = () => {
countdown.value = 60;
hasGetCode.value = true;
timer.value = setInterval(() => {
countdown.value--;
if (countdown.value <= 0) {
clearInterval(timer.value );
timer.value = null;
}
}, 1000);
};
onUnmounted(() => {
if (timer.value) {
clearInterval(timer.value);
}
});
</script>
<style scoped lang="scss">
@import './style.scss';
</style>

View File

@ -0,0 +1,171 @@
<template>
<Modal v-model:open="visible" centered unmountOnClose @cancel="handleCancel">
<template #title>
<span style="text-align: left; width: 100%">选择账号</span>
</template>
<div class="account-bind-container">
<Card :bordered="false" class="bind-card">
<div class="bind-header">
<Typography.Text class="mobile-number">{{ mobileNumber }} 已在以下企业绑定了账号</Typography.Text>
</div>
<List :bordered="false" :split="false" class="account-list">
<List.Item
v-for="(account, index) in accounts"
:key="index"
class="account-item"
:class="{
selected: selectedAccountIndex === index,
'cursor-no-drop': account.status === 0,
'cursor-pointer': account.status !== 0,
}"
@click="selectAccount(account, index)"
>
<List.Item.Meta>
<template #title>
<div style="display: flex; align-items: center; gap: 12px">
<Checkbox :checked="selectedAccountIndex === index" />
<Typography.Text>{{ account.name || '-' }}</Typography.Text>
</div>
</template>
</List.Item.Meta>
</List.Item>
</List>
</Card>
</div>
<template #footer>
<div class="flex">
<Button type="primary" @click="handleOk">确定</Button>
</div>
</template>
</Modal>
</template>
<script setup lang="ts">
import { Checkbox, Modal, Button, message, Card, List, Typography } from 'ant-design-vue';
import { handleUserLogin } from '@/utils/user';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
const props = defineProps({
mobileNumber: {
type: String,
default: '',
},
accounts: {
type: Array,
default: () => [],
},
});
const enterpriseStore = useEnterpriseStore();
const visible = ref(false);
const selectedAccountIndex = ref(0);
const open = () => {
visible.value = true;
};
const handleCancel = () => {
visible.value = false;
};
const handleOk = async () => {
visible.value = false;
handleUserLogin();
};
const selectAccount = (account, index) => {
if (account.status === 0) {
return;
}
enterpriseStore.setEnterpriseInfo(account);
selectedAccountIndex.value = index;
};
defineExpose({
open,
});
</script>
<style scoped lang="scss">
.account-bind-container {
width: 100%;
max-width: 400px;
margin: 0 auto;
display: flex;
.bind-card {
background-color: var(--color-bg-2);
width: 100%;
flex-direction: column;
align-items: start;
box-shadow: none;
}
.bind-header {
margin-bottom: 8px;
text-align: left;
}
.phone-number {
font-size: 14px;
color: var(--color-text-4);
text-align: left;
}
.account-list {
margin-top: 16px;
.account-item {
padding: 12px 16px;
margin-bottom: 8px;
background-color: var(--color-bg-2);
border-radius: 4px;
cursor: pointer;
transition: all 0.2s ease;
display: flex;
align-items: center;
}
.account-item {
padding: 12px 16px;
margin-bottom: 8px;
background-color: var(--color-bg-2);
border-radius: 4px;
cursor: pointer;
transition: all 0.2s ease;
border: 1px solid var(--color-border-2);
box-shadow: 0 2px 4px 0 #b1b2b5;
&.selected {
border-color: #6d4cfe;
background-color: rgba(109, 76, 254, 0.1);
box-shadow: 0 2px 4px 0 rgba(109, 76, 254, 0.5);
}
:deep(.ant-list-item-meta-title) {
margin: 0;
}
}
.account-item:deep(.arco-list-item-main) {
padding: 0;
}
:deep(.arco-list-item-actions) {
margin-left: 12px;
}
:deep(.arco-checkbox) {
margin-right: 0;
}
:deep(.arco-checkbox-checked .arco-checkbox-mask) {
background-color: #6d4cfe;
border-color: #6d4cfe;
}
}
}
</style>

View File

@ -8,335 +8,31 @@
<div class="flex flex-col justify-center flex-column h-100% mt--12.5">
<img src="@/assets/img/Frame.svg" class="w-480 h-480 mr-40" alt="" />
</div>
<div class="flex items-center w-400 h-100%">
<a-space
direction="vertical"
size="large"
align="center"
class="w-400 bg-#fff rounded-8px shadow-[0_4px_10px_0_#6D4CFE33] px-40px py-48px"
>
<img src="@/assets/img/icon-logo.png" alt="" width="96" height="24" class="mb-8px" />
<span class="text-4 color-#737478">AI营销工具</span>
<a-form ref="formRef" :model="loginForm" :rules="formRules" auto-label-width class="w-320 mt-48px form-wrap">
<a-form-item field="mobile" hide-label>
<a-input
v-model="loginForm.mobile"
placeholder="输入手机号"
class="form-input border border-solid border-#d7d7d9 x w-100% h-48px text-14 rounded-4px color-#333 bg-#fff"
clearable
allow-clear
@blur="validateField('mobile')"
@input="clearError('mobile')"
/>
</a-form-item>
<a-form-item field="captcha" hide-label>
<div
class="form-input border border-solid border-#d7d7d9 w-100% h-48px text-14 rounded-4px color-#333 bg-#fff flex justify-between items-center"
>
<a-input
v-model="loginForm.captcha"
placeholder="验证码"
style="background-color: #fff; border: none"
allow-clear
maxlength="6"
@blur="validateField('captcha')"
@input="clearError('captcha')"
/>
<span
class="w-120 font-400 text-right mr-4 text-16px"
:style="{
color: countdown > 0 || hasGetCode ? '#6D4CFE' : '#211F24',
cursor: countdown > 0 ? 'not-allowed' : 'pointer',
}"
@click="getCode"
>{{ countdown > 0 ? `${countdown}s` : hasGetCode ? '重新发送' : '发送验证码' }}</span
>
</div>
</a-form-item>
<a-form-item hide-label class="mt-68px mb-16px">
<a-button
type="primary"
class="w-480 h-48 !text-16px !rounded-8px"
:class="disabledSubmitBtn ? 'cursor-no-drop' : 'cursor-pointer'"
:disabled="disabledSubmitBtn"
@click="handleSubmit"
>
{{ isLogin ? '登录' : '注册并开通企业账号' }}
</a-button>
</a-form-item>
</a-form>
<a-space class="text-12px color-#737478 justify-start items-center">
<a-checkbox v-model="hasCheck" class="!text-12px mr-8px"></a-checkbox>
<span class="text-12px color-#737478">{{ isLogin ? '登录' : '注册' }}即代表同意</span>
<a-link href="link" class="form-link color-#211F24" target="_blank">用户协议</a-link>
<span class="text-12px color-#737478"></span>
<a-link href="link" class="form-link color-#211f24" target="_blank">隐私政策</a-link>
</a-space>
</a-space>
</div>
</section>
<section class="login-footer">
<p class="text">闽公网安备 352018502850842 闽ICP备20250520582号 © 2025小题科技</p>
<component :is="pageType === 'loginForm' ? LoginForm : RegisterForm" ref="formRef" :pageType="pageType" />
<section class="login-footer">
<p class="text">闽公网安备 352018502850842 闽ICP备20250520582号 © 2025小题科技</p>
</section>
</section>
</div>
<PuzzleVerification
:show="isVerificationVisible"
@submit="handleVerificationSubmit"
@cancel="isVerificationVisible = false"
/>
<a-modal :visible="visible" unmountOnClose hide-cancel @ok="handleOk" @cancel="handleCancel">
<template #title>
<span style="text-align: left; width: 100%">选择账号</span>
</template>
<div class="account-bind-container">
<a-card :bordered="false" class="bind-card">
<div class="bind-header">
<a-typography-text class="mobile-number">{{ mobileNumber }} 已在以下企业绑定了账号</a-typography-text>
</div>
<a-list :bordered="false" :split="false" class="account-list">
<a-list-item
v-for="(account, index) in accounts"
:key="index"
class="account-item"
:class="{
selected: selectedAccountIndex === index,
'cursor-no-drop': account.status === 0,
'cursor-pointer': account.status !== 0,
}"
@click="selectAccount(account, index)"
>
<a-list-item-meta>
<template #title>
<div style="display: flex; align-items: center; gap: 12px">
<a-checkbox :model-value="selectedAccountIndex === index" />
<a-typography-text>{{ account.name || '-' }}</a-typography-text>
</div>
</template>
</a-list-item-meta>
</a-list-item>
</a-list>
</a-card>
</div>
</a-modal>
</template>
<script setup lang="ts">
import PuzzleVerification from './components/PuzzleVerification.vue';
import { fetchLoginCaptCha, fetchAuthorizationsCaptcha, fetchProfileInfo } from '@/api/all/login';
import { joinEnterpriseByInviteCode } from '@/api/all';
import { ref, reactive, onUnmounted, computed } from 'vue';
import { useUserStore } from '@/stores';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
import { handleUserLogin } from '@/utils/user';
import router from '@/router';
import { useRoute } from 'vue-router';
import { ref } from 'vue';
import LoginForm from './components/login-form/index.vue';
import RegisterForm from './components/register-form/index.vue';
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 visible = ref(false);
const hasGetCode = ref(false);
const submitting = ref(false);
const hasCheck = ref(false);
const mobileNumber = ref('');
const selectedAccountIndex = ref(0);
const accounts = ref([]);
type ShowPageType = 'loginForm' | 'registerForm' | 'resetPasswordForm';
const loginForm = reactive({
mobile: '',
captcha: '',
});
const pageType = ref<ShowPageType>('loginForm');
// 表单校验规则
const formRules = {
mobile: [
{
required: true,
message: '请填写手机号',
trigger: ['blur', 'change'],
},
{
validator: (value: string, callback: (error?: string) => void) => {
if (!/^1[3-9]\d{9}$/.test(value)) {
callback('手机号格式不正确');
} else {
callback();
}
},
trigger: ['blur', 'change'],
},
],
captcha: [
{
required: true,
message: '请填写验证码',
trigger: ['blur', 'change'],
},
{
validator: (value: string, callback: (error?: string) => void) => {
if (!/^\d{6}$/.test(value)) {
callback('验证码必须是6位数字');
} else {
callback();
}
},
trigger: ['blur', 'change'],
},
],
const formRef = ref(null);
const setPageType = (val: ShowPageType) => {
pageType.value = val;
};
// 表单是否有效
const isFormValid = computed(() => {
return (
loginForm.mobile.trim() !== '' &&
/^1[3-9]\d{9}$/.test(loginForm.mobile) &&
loginForm.captcha.trim() !== '' &&
/^\d{6}$/.test(loginForm.captcha)
);
});
const disabledSubmitBtn = computed(() => {
return !isFormValid.value;
});
const selectAccount = (account: any, index: any) => {
if (account.status === 0) {
return;
}
enterpriseStore.setEnterpriseInfo(account);
selectedAccountIndex.value = index;
};
const validateField = (field: string) => {
formRef.value.validateField(field);
};
const clearError = (field: string) => {
formRef.value.clearValidate(field);
};
const handleOk = async () => {
visible.value = false;
handleUserLogin();
};
const handleCancel = () => {
visible.value = false;
};
const getCode = async () => {
if (countdown.value > 0) return;
// 先重置验证状态
formRef.value.clearValidate('mobile');
const result = await formRef.value.validateField('mobile');
// 只有当验证通过时才会显示滑块验证
if (result === true || result === undefined) {
isVerificationVisible.value = true;
}
};
// 验证码验证通过后
const handleVerificationSubmit = async () => {
isVerificationVisible.value = false;
startCountdown();
try {
await fetchLoginCaptCha({ mobile: loginForm.mobile });
AMessage.success('验证码发送成功');
} catch (error) {
// 重置倒计时
countdown.value = 0;
clearInterval(timer.value);
}
};
// 获取用户信息
const getProfileInfo = async () => {
const { code, data } = await fetchProfileInfo();
if (code === 200) {
let enterprises = data['enterprises'];
mobileNumber.value = data['mobile'];
accounts.value = enterprises;
if (enterprises.length > 0) {
enterpriseStore.setEnterpriseInfo(data.enterprises[0]);
if (enterprises.length === 1) {
handleUserLogin();
} else {
// 多个企业时候需要弹窗让用户选择企业
visible.value = true;
}
}
}
};
// 提交表单
const handleSubmit = async () => {
if (disabledSubmitBtn.value) return;
try {
// 校验所有字段
await formRef.value.validate();
if (!hasCheck.value) {
AMessage.error('请先勾选同意用户协议');
return;
}
submitting.value = true;
const { code, data } = await fetchAuthorizationsCaptcha(loginForm);
if (code === 200) {
// 处理登录成功逻辑
AMessage.success(isLogin.value ? '登录成功' : '注册成功');
userStore.setToken(data.access_token);
const { invite_code } = route.query;
if (invite_code) {
const { code } = await joinEnterpriseByInviteCode(invite_code as string);
if (code === 200) {
AMessage.success('加入企业成功');
}
}
getProfileInfo();
}
} catch (error) {
// 错误信息会显示在输入框下方
} 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);
};
onUnmounted(() => {
if (timer.value) {
clearInterval(timer.value);
}
});
provide('setPageType', setPageType);
provide('pageType', pageType);
</script>
<style scoped lang="scss">

View File

@ -1,17 +1,57 @@
.login-wrap {
.arco-input-wrapper,
.arco-select-view-single,
.arco-textarea-wrapper,
.arco-picker,
.arco-select-view-multiple {
border-radius: 4px;
border-color: #d7d7d9 !important;
background-color: #fff !important;
&:focus-within,
&.arco-input-focus {
background-color: var(--color-bg-2);
// border-color: rgb(var(--primary-6));
box-shadow: 0 0 0 0 var(--color-primary-light-2);
:deep(.ant-form) {
.ant-form-item {
&:not(:last-child) {
margin-bottom: 24px !important;
}
.ant-input-affix-wrapper {
height: 48px;
padding: 0 10px;
border-radius: 8px !important;
.ant-input {
border-radius: 8px !important;
font-size: 16px;
font-style: normal;
font-weight: 400;
line-height: 24px;
height: 24px;
}
}
}
.captcha-form-item {
.ant-input-suffix {
position: relative;
&::before {
content: '';
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 0;
width: 1px;
height: 16px;
background-color: #d7d7d9;
}
margin-inline-start: 0;
padding-left: 16px;
// border-left: 1px solid #d7d7d9;
}
}
.password-form-item {
.ant-input-suffix {
cursor: pointer;
padding: 0;
}
}
}
:deep(.btn-login) {
&:disabled {
background-color: #c5b7ff !important;
}
&:not(:disabled) {
&:hover {
background-color: $color-primary-3 !important;
}
}
}
.login-bg {
@ -26,11 +66,6 @@
background-position: center;
background-size: cover;
}
.form-link {
color: #211f24;
font-size: 12px;
margin: 0px;
}
.login-footer {
position: absolute;
@ -64,6 +99,7 @@
width: 100%;
flex-direction: column;
align-items: start;
box-shadow: none;
}
.bind-header {
@ -106,6 +142,9 @@
background-color: rgba(109, 76, 254, 0.1);
box-shadow: 0 2px 4px 0 rgba(109, 76, 254, 0.5);
}
:deep(.ant-list-item-meta-title) {
margin: 0;
}
}
.account-item:deep(.arco-list-item-main) {