feat:增加 register 账号注册功能

pull/71/head
YunaiV 2025-04-08 13:10:36 +08:00
parent fa8e7bdc12
commit 446cb6cbbf
5 changed files with 151 additions and 15 deletions

View File

@ -35,6 +35,13 @@ export namespace AuthApi {
code: string;
}
/** 注册接口参数 */
export interface RegisterParams {
tenantName: string
username: string
password: string
captchaVerification: string
}
}
/** 登录 */
@ -93,4 +100,9 @@ export const sendSmsCode = (data: AuthApi.SmsCodeParams) => {
/** 短信验证码登录 */
export const smsLogin = (data: AuthApi.SmsLoginParams) => {
return requestClient.post('/system/auth/sms-login', data)
}
/** 注册 */
export const register = (data: AuthApi.RegisterParams) => {
return requestClient.post('/system/auth/register', data)
}

View File

@ -9,7 +9,7 @@ import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
import { notification } from 'ant-design-vue';
import { defineStore } from 'pinia';
import { type AuthApi, getAuthPermissionInfoApi, loginApi, logoutApi, smsLogin } from '#/api';
import { type AuthApi, getAuthPermissionInfoApi, loginApi, logoutApi, smsLogin, register } from '#/api';
import { $t } from '#/locales';
export const useAuthStore = defineStore('auth', () => {
@ -27,7 +27,7 @@ export const useAuthStore = defineStore('auth', () => {
* @param onSuccess
*/
async function authLogin(
type: 'mobile' | 'username',
type: 'mobile' | 'username' | 'register',
params: Recordable<any>,
onSuccess?: () => Promise<void> | void,
) {
@ -36,6 +36,7 @@ export const useAuthStore = defineStore('auth', () => {
try {
loginLoading.value = true;
const { accessToken, refreshToken } = type === 'mobile' ? await smsLogin(params as AuthApi.SmsLoginParams)
: type === 'register' ? await register(params as AuthApi.RegisterParams)
: await loginApi(params);
// 如果成功获取到 accessToken

View File

@ -1,18 +1,120 @@
<script lang="ts" setup>
import type { VbenFormSchema } from '@vben/common-ui';
import type { Recordable } from '@vben/types';
import { type AuthApi, checkCaptcha, getCaptcha } from '#/api/core/auth';
import { computed, h, ref } from 'vue';
import { computed, h, onMounted, ref } from 'vue';
import { AuthenticationRegister, z } from '@vben/common-ui';
import { AuthenticationRegister, Verification, z } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { useAppConfig } from '@vben/hooks';
import { useAccessStore } from '@vben/stores';
import { useAuthStore } from '#/store';
import { getTenantSimpleList, getTenantByWebsite } from '#/api/core/auth';
const { tenantEnable, captchaEnable } = useAppConfig(import.meta.env, import.meta.env.PROD);
defineOptions({ name: 'Register' });
const loading = ref(false);
const registerRef = ref();
const verifyRef = ref();
const accessStore = useAccessStore();
const authStore = useAuthStore();
const captchaType = 'blockPuzzle'; // 'blockPuzzle' | 'clickWord'
/** 获取租户列表,并默认选中 */
const tenantList = ref<AuthApi.TenantResult[]>([]); //
const fetchTenantList = async () => {
if (!tenantEnable) {
return;
}
try {
//
const websiteTenantPromise = getTenantByWebsite(window.location.hostname);
tenantList.value = await getTenantSimpleList();
// > store >
let tenantId: number | null = null;
const websiteTenant = await websiteTenantPromise;
if (websiteTenant?.id) {
tenantId = websiteTenant.id;
}
// store
if (!tenantId && accessStore.tenantId) {
tenantId = accessStore.tenantId;
}
// 使
if (!tenantId && tenantList.value?.[0]?.id) {
tenantId = tenantList.value[0].id;
}
//
accessStore.setTenantId(tenantId);
registerRef.value.getFormApi().setFieldValue('tenantId', tenantId);
} catch (error) {
console.error('获取租户列表失败:', error);
}
};
/** 执行注册 */
const handleRegister = async (values: any) => {
//
if (captchaEnable) {
verifyRef.value.show();
return;
}
//
await authStore.authLogin('register', values);
};
/** 验证码通过,执行注册 */
const handleVerifySuccess = async ({ captchaVerification }: any) => {
try {
await authStore.authLogin('register', {
...(await registerRef.value.getFormApi().getValues()),
captchaVerification,
});
} catch (error) {
console.error('Error in handleRegister:', error);
}
};
/** 组件挂载时获取租户信息 */
onMounted(() => {
fetchTenantList();
});
const formSchema = computed((): VbenFormSchema[] => {
return [
{
component: 'VbenSelect',
componentProps: {
options: tenantList.value.map((item) => ({
label: item.name,
value: item.id,
})),
placeholder: $t('authentication.tenantTip'),
},
fieldName: 'tenantId',
label: $t('authentication.tenant'),
rules: z
.number()
.nullable()
.refine((val) => val != null && val > 0, $t('authentication.tenantTip'))
.default(null),
dependencies: {
triggerFields: ['tenantId'],
if: tenantEnable,
trigger(values) {
if (values.tenantId) {
accessStore.setTenantId(values.tenantId);
}
},
},
},
{
component: 'VbenInput',
componentProps: {
@ -22,6 +124,15 @@ const formSchema = computed((): VbenFormSchema[] => {
label: $t('authentication.username'),
rules: z.string().min(1, { message: $t('authentication.usernameTip') }),
},
{
component: 'VbenInput',
componentProps: {
placeholder: $t('authentication.nicknameTip'),
},
fieldName: 'nickname',
label: $t('authentication.nickname'),
rules: z.string().min(1, { message: $t('authentication.nicknameTip') }),
},
{
component: 'VbenInputPassword',
componentProps: {
@ -80,17 +191,25 @@ const formSchema = computed((): VbenFormSchema[] => {
},
];
});
function handleSubmit(value: Recordable<any>) {
// eslint-disable-next-line no-console
console.log('register submit:', value);
}
</script>
<template>
<AuthenticationRegister
:form-schema="formSchema"
:loading="loading"
@submit="handleSubmit"
/>
<div>
<AuthenticationRegister
ref="registerRef"
:form-schema="formSchema"
:loading="loading"
@submit="handleRegister"
/>
<Verification
ref="verifyRef"
v-if="captchaEnable"
:captcha-type="captchaType"
:check-captcha-api="checkCaptcha"
:get-captcha-api="getCaptcha"
:img-size="{ width: '400px', height: '200px' }"
mode="pop"
@on-success="handleVerifySuccess"
/>
</div>
</template>

View File

@ -8,10 +8,12 @@
"selectAccount": "Quick Select Account",
"username": "Username",
"password": "Password",
"nickname": "Nickname",
"tenant": "Tenant",
"usernameTip": "Please enter username",
"passwordErrorTip": "Password is incorrect",
"passwordTip": "Please enter password",
"nicknameTip": "Please enter nickname",
"tenantTip": "Please select tenant",
"verifyRequiredTip": "Please complete the verification first",
"rememberMe": "Remember Me",

View File

@ -8,9 +8,11 @@
"selectAccount": "快速选择账号",
"username": "账号",
"password": "密码",
"nickname": "昵称",
"tenant": "租户",
"usernameTip": "请输入用户名",
"passwordTip": "请输入密码",
"nicknameTip": "请输入昵称",
"tenantTip": "请选择租户",
"verifyRequiredTip": "请先完成验证",
"passwordErrorTip": "密码错误",