init
This commit is contained in:
21
src/hooks/business/auth.ts
Normal file
21
src/hooks/business/auth.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { useAuthStore } from '@/store/modules/auth';
|
||||
|
||||
export function useAuth() {
|
||||
const authStore = useAuthStore();
|
||||
|
||||
function hasAuth(codes: string | string[]) {
|
||||
if (!authStore.isLogin) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof codes === 'string') {
|
||||
return authStore.userInfo.buttons.includes(codes);
|
||||
}
|
||||
|
||||
return codes.some(code => authStore.userInfo.buttons.includes(code));
|
||||
}
|
||||
|
||||
return {
|
||||
hasAuth
|
||||
};
|
||||
}
|
||||
70
src/hooks/business/captcha.ts
Normal file
70
src/hooks/business/captcha.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { computed } from 'vue';
|
||||
import { useCountDown, useLoading } from '@sa/hooks';
|
||||
import { REG_PHONE } from '@/constants/reg';
|
||||
import { fetchSendPhoneNumberOtp } from '@/service/api';
|
||||
import { $t } from '@/locales';
|
||||
|
||||
export function useCaptcha() {
|
||||
const { loading, startLoading, endLoading } = useLoading();
|
||||
const { count, start, stop, isCounting } = useCountDown(10);
|
||||
|
||||
const label = computed(() => {
|
||||
let text = $t('page.login.codeLogin.getCode');
|
||||
|
||||
const countingLabel = $t('page.login.codeLogin.reGetCode', { time: count.value });
|
||||
|
||||
if (loading.value) {
|
||||
text = '';
|
||||
}
|
||||
|
||||
if (isCounting.value) {
|
||||
text = countingLabel;
|
||||
}
|
||||
|
||||
return text;
|
||||
});
|
||||
|
||||
function isPhoneValid(phone: string) {
|
||||
if (phone.trim() === '') {
|
||||
window.$message?.error?.($t('form.phone.required'));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!REG_PHONE.test(phone)) {
|
||||
window.$message?.error?.($t('form.phone.invalid'));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async function getCaptcha(phone: string) {
|
||||
const valid = isPhoneValid(phone);
|
||||
|
||||
if (!valid || loading.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
startLoading();
|
||||
|
||||
// request
|
||||
await fetchSendPhoneNumberOtp(phone);
|
||||
|
||||
window.$message?.success?.($t('page.login.codeLogin.sendCodeSuccess'));
|
||||
|
||||
start();
|
||||
|
||||
endLoading();
|
||||
}
|
||||
|
||||
return {
|
||||
label,
|
||||
start,
|
||||
stop,
|
||||
isCounting,
|
||||
loading,
|
||||
getCaptcha
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user