2
0

fix: 注册功能接口变更

This commit is contained in:
TsMask
2024-11-30 17:31:47 +08:00
parent 8f198f789d
commit 5e54aaf24d
5 changed files with 231 additions and 213 deletions

View File

@@ -2,7 +2,7 @@ import { computed } from 'vue';
import { useCountDown, useLoading } from '@sa/hooks';
import { $t } from '@/locales';
import { REG_EMAIL } from '@/constants/reg';
import {useAuthStore} from "@/store/modules/auth";
import { useAuthStore } from '@/store/modules/auth';
export function useCaptcha() {
const { loading, startLoading, endLoading } = useLoading();
@@ -39,20 +39,17 @@ export function useCaptcha() {
return true;
}
//获取验证码方法
async function getCaptcha(email: string) {
console.log(email)
//const valid = isPhoneValid(phone);
const valid = isEmailValid(email);
if (!valid || loading.value) {
return;
return null;
}
startLoading();
await authStore.captcha(
email,
);
const data = await authStore.captcha(email);
await new Promise(resolve => {
setTimeout(resolve, 500);
});
@@ -62,6 +59,7 @@ export function useCaptcha() {
start();
endLoading();
return data;
}
return {

View File

@@ -13,21 +13,24 @@ export function fetchLogin(body: Api.Auth.LoginBody) {
data: body
});
}
//邮箱验证码接口
export function sendCaptcha(body: Api.Auth.EmailCaptcha) {
return request({
url:`/system/email/code?email=${body.email}`,
method:'get',
})
url: `/code?email=${body.email}`,
method: 'get'
});
}
//验证注册
export function doCheckUserRepeat(body: Api.Auth.CheckBody) {
return request<boolean>({
url:'/u/user/checkRepeat',
url: '/auth/checkRepeat',
method: 'post',
data: body
})
});
}
//添加注册
export function fetchRegister(body: Api.Auth.RegisterBody) {
return request({
@@ -36,6 +39,7 @@ export function fetchRegister(body: Api.Auth.RegisterBody) {
data: body
});
}
/** logout */
export function doDeleteLogout() {
return request<App.Service.Response<null>>({

View File

@@ -7,7 +7,7 @@ import { localStg } from '@/utils/storage';
import { $t } from '@/locales';
import { useRouteStore } from '../route';
import { clearAuthStorage, emptyInfo, getToken } from './shared';
import {doCheckUserRepeat, sendCaptcha} from "@/service/api/auth";
import { doCheckUserRepeat, sendCaptcha } from '@/service/api/auth';
export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
const routeStore = useRouteStore();
@@ -122,7 +122,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
}
return false;
}
//check User
/**
* 检查用户信息是否已存在
@@ -149,16 +148,13 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
endLoading();
return !error;
}
async function captcha(email: string) {
if (!email) {
return;
}
try {
await sendCaptcha({ email }); // 这里调用后端接口发送验证码
} catch (error) {
return null;
}
const { data, error } = await sendCaptcha({ email }); // 这里调用后端接口发送验证码
return { data, error };
}
return {
@@ -173,6 +169,6 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
refreshUserInfo,
register,
captcha,
checkUserRepeat,
checkUserRepeat
};
});

View File

@@ -156,6 +156,7 @@ declare namespace Api {
username?: string;
email?: string;
phonenumber?:string;
authType: string;
}
}

View File

@@ -31,17 +31,19 @@ const currentStep = ref(0);
// 是否同意协议
const agreeTerms = ref(false);
// 定义一个统一的数据模型
interface RegisterModel {
username: string;
password: string;
email: string;
fullName: string;
age:0,
age: 0;
gender: string;
phone: string;
address: string;
code: string;
uuid: string;
authType: string;
}
@@ -56,6 +58,7 @@ const model = reactive<RegisterModel>({
phone: '',
address: '',
code: '',
uuid: '',
authType: 'u'
});
@@ -82,6 +85,7 @@ const basicModel = reactive<BasicFormModel>({
interface SecurityFormModel {
email: string;
code: string;
uuid: string;
password: string;
confirmPassword: string;
}
@@ -89,6 +93,7 @@ interface SecurityFormModel {
const securityModel = reactive<SecurityFormModel>({
email: '',
code: '',
uuid: '',
password: '',
confirmPassword: ''
});
@@ -100,7 +105,7 @@ const basicRules = computed<Record<string, Rule | Rule[]>>(() => {
return Promise.reject(t('page.login.register.usernameLengthLimit'));
}
if (value) {
const { exists } = await authStore.checkUserRepeat({ username: value });
const { exists } = await authStore.checkUserRepeat({ username: value, authType: 'u' });
if (exists) {
return Promise.reject(t('page.login.register.usernameExists'));
}
@@ -117,7 +122,7 @@ const basicRules = computed<Record<string, Rule | Rule[]>>(() => {
return Promise.reject(t('page.login.register.phoneInvalid'));
}
const { exists } = await authStore.checkUserRepeat({ phonenumber: value });
const { exists } = await authStore.checkUserRepeat({ phonenumber: value, authType: 'u' });
if (exists) {
return Promise.reject(t('page.login.register.phoneExists'));
}
@@ -150,7 +155,7 @@ const securityRules = computed<Record<string, Rule | Rule[]>>(() => {
return Promise.reject(t('page.login.register.emailInvalid'));
}
const { exists } = await authStore.checkUserRepeat({ email: value });
const { exists } = await authStore.checkUserRepeat({ email: value, authType: 'u' });
if (exists) {
return Promise.reject(t('page.login.register.emailExists'));
}
@@ -197,10 +202,22 @@ async function nextStep() {
console.error('Validation failed:', error);
}
}
// 返回
function prevStep() {
currentStep.value -= 1;
}
async function handleCaptcha() {
const res = await getCaptcha(securityModel.email);
if (res) {
securityModel.uuid = res.data.uuid;
if (res.data?.text) {
securityModel.code = res.data.text;
}
}
}
// 注册按钮
async function handleSubmit() {
try {
@@ -215,12 +232,13 @@ async function handleSubmit() {
model.phone = basicModel.phone;
model.address = basicModel.address;
model.code = securityModel.code;
model.uuid = securityModel.uuid;
const success = await authStore.register({
...model,
age: dayjs().diff(dayjs(basicModel.birthDate), 'year'),
sex: model.gender,
phonenumber: model.phone,
phonenumber: model.phone
});
if (success) {
@@ -296,8 +314,8 @@ const showSteps = computed(() => !isMobile.value);
<ACol :xs="12" :sm="12" :lg="24">
<AFormItem name="gender" :label="t('page.login.register.gender')">
<ASelect v-model:value="basicModel.gender">
<ASelectOption value="male">{{ t('page.login.register.male') }}</ASelectOption>
<ASelectOption value="female">{{ t('page.login.register.female') }}</ASelectOption>
<ASelectOption value="0">{{ t('page.login.register.male') }}</ASelectOption>
<ASelectOption value="1">{{ t('page.login.register.female') }}</ASelectOption>
</ASelect>
</AFormItem>
</ACol>
@@ -380,7 +398,7 @@ const showSteps = computed(() => !isMobile.value);
size="small"
:disabled="isCounting"
:loading="loading"
@click="getCaptcha(securityModel.email)"
@click="handleCaptcha()"
>
{{ label }}
</AButton>
@@ -504,6 +522,7 @@ const showSteps = computed(() => !isMobile.value);
:deep(.ant-form-item-label) {
white-space: normal;
text-align: left;
> label {
height: auto !important;
padding-bottom: 4px;