add Debounce and Throttle

This commit is contained in:
landaiqing
2024-08-22 23:58:29 +08:00
parent 57a56ec5ef
commit d91e683e87
4 changed files with 106 additions and 28 deletions

View File

@@ -62,7 +62,7 @@
</AInputPassword> </AInputPassword>
</AFormItem> </AFormItem>
<AFormItem> <AFormItem>
<AButton @click="resetPasswordSubmit" style="width: 100%;" type="primary" size="large">{{ <AButton @click="resetPassword" style="width: 100%;" type="primary" size="large">{{
t("login.resetPassword") t("login.resetPassword")
}} }}
</AButton> </AButton>
@@ -103,6 +103,7 @@ import {Rule} from "ant-design-vue/lib/form";
import {checkRotatedCaptcha, getRotatedCaptchaData} from "@/api/captcha"; import {checkRotatedCaptcha, getRotatedCaptchaData} from "@/api/captcha";
import {message} from "ant-design-vue"; import {message} from "ant-design-vue";
import {resetPasswordApi, sendMessage} from "@/api/user"; import {resetPasswordApi, sendMessage} from "@/api/user";
import {useDebounceFn} from "@vueuse/core";
const router = useRouter(); const router = useRouter();
const {t} = useI18n(); const {t} = useI18n();
@@ -118,7 +119,7 @@ const resetPasswordRotateEvent = {
showRotateCaptcha.value = false; showRotateCaptcha.value = false;
}, },
refresh: () => { refresh: () => {
getRotateCaptcha(); refreshCaptcha();
}, },
}; };
const ResetPasswordForm: UnwrapRef<ResetPassword> = reactive({ const ResetPasswordForm: UnwrapRef<ResetPassword> = reactive({
@@ -231,6 +232,11 @@ async function sendCaptcha() {
}); });
} }
/**
* 重置密码表单提交 防抖
*/
const resetPassword = useDebounceFn(resetPasswordSubmit, 1000);
/** /**
* 重置密码表单提交 * 重置密码表单提交
*/ */
@@ -251,6 +257,11 @@ async function resetPasswordSubmit() {
}); });
} }
/**
* 刷新验证码 节流
*/
const refreshCaptcha = useDebounceFn(getRotateCaptcha, 3000);
/** /**
* 获取旋转验证码数据 * 获取旋转验证码数据
*/ */

View File

@@ -6,14 +6,14 @@
router.push('/qrlogin') router.push('/qrlogin')
}" class="login-form-bottom-button" :icon="h(QrcodeOutlined)">{{ t("login.qrLogin") }} }" class="login-form-bottom-button" :icon="h(QrcodeOutlined)">{{ t("login.qrLogin") }}
</AButton> </AButton>
<AButton @click="openQQUrl" class="login-form-bottom-button" :icon="h(QqOutlined)"></AButton> <AButton @click="openQQbUrlDebounce" class="login-form-bottom-button" :icon="h(QqOutlined)"></AButton>
<AButton @click="openGiteeUrl" class="login-form-bottom-button" <AButton @click="openGiteebUrlDebounce" class="login-form-bottom-button"
style="display: flex; align-items: center;justify-content: center;"> style="display: flex; align-items: center;justify-content: center;">
<template #icon> <template #icon>
<img :src=gitee alt="gitee" class="gitee-icon" style="width: 16px; height: 16px;"/> <img :src=gitee alt="gitee" class="gitee-icon" style="width: 16px; height: 16px;"/>
</template> </template>
</AButton> </AButton>
<AButton @click="openGithubUrl" class="login-form-bottom-button" :icon="h(GithubOutlined)"></AButton> <AButton @click="openGithubUrlDebounce" class="login-form-bottom-button" :icon="h(GithubOutlined)"></AButton>
</AFlex> </AFlex>
</div> </div>
</template> </template>
@@ -29,6 +29,7 @@ import {message} from "ant-design-vue";
import gitee from "@/assets/svgs/gitee.svg"; import gitee from "@/assets/svgs/gitee.svg";
import {generateClientId} from "@/api/oauth/wechat.ts"; import {generateClientId} from "@/api/oauth/wechat.ts";
import {getQQUrl} from "@/api/oauth/qq.ts"; import {getQQUrl} from "@/api/oauth/qq.ts";
import {useDebounceFn} from "@vueuse/core";
const router = useRouter(); const router = useRouter();
const {t} = useI18n(); const {t} = useI18n();
@@ -94,10 +95,15 @@ function getLocalClientId(): string | null {
} }
} }
/**
* Open the Github OAuth page in a new window with debounce
*/
const openGithubUrlDebounce = useDebounceFn(openGithubUrl, 1000);
/** /**
* Open the Github OAuth page in a new window * Open the Github OAuth page in a new window
*/ */
const openGithubUrl = () => { function openGithubUrl() {
const iWidth = 800; //弹出窗口的宽度; const iWidth = 800; //弹出窗口的宽度;
const iHeight = 500; //弹出窗口的高度; const iHeight = 500; //弹出窗口的高度;
//window.screen.height获得屏幕的高window.screen.width获得屏幕的宽 //window.screen.height获得屏幕的高window.screen.width获得屏幕的宽
@@ -124,11 +130,17 @@ const openGithubUrl = () => {
} }
}; };
window.addEventListener("message", messageHandler); window.addEventListener("message", messageHandler);
}; }
/**
* Open the Gitee OAuth page in a new window with debounce
*/
const openGiteebUrlDebounce = useDebounceFn(openGiteeUrl, 1000);
/** /**
* Open the Gitee OAuth page in a new window * Open the Gitee OAuth page in a new window
*/ */
const openGiteeUrl = () => { function openGiteeUrl() {
const iWidth = 800; //弹出窗口的宽度; const iWidth = 800; //弹出窗口的宽度;
const iHeight = 500; //弹出窗口的高度; const iHeight = 500; //弹出窗口的高度;
//window.screen.height获得屏幕的高window.screen.width获得屏幕的宽 //window.screen.height获得屏幕的高window.screen.width获得屏幕的宽
@@ -156,11 +168,17 @@ const openGiteeUrl = () => {
} }
}; };
window.addEventListener("message", messageHandler); window.addEventListener("message", messageHandler);
}; }
/**
* Open the QQ OAuth page in a new window with debounce
*/
const openQQbUrlDebounce = useDebounceFn(openQQUrl, 1000);
/** /**
* Open the QQ OAuth page in a new window * Open the QQ OAuth page in a new window
*/ */
const openQQUrl = () => { function openQQUrl() {
const iWidth = 800; //弹出窗口的宽度; const iWidth = 800; //弹出窗口的宽度;
const iHeight = 500; //弹出窗口的高度; const iHeight = 500; //弹出窗口的高度;
//window.screen.height获得屏幕的高window.screen.width获得屏幕的宽 //window.screen.height获得屏幕的高window.screen.width获得屏幕的宽
@@ -187,7 +205,7 @@ const openQQUrl = () => {
} }
}; };
window.addEventListener("message", messageHandler); window.addEventListener("message", messageHandler);
}; }
onBeforeMount(() => { onBeforeMount(() => {
getGithubRedirectUrl(); getGithubRedirectUrl();

View File

@@ -46,7 +46,8 @@
<SafetyOutlined/> <SafetyOutlined/>
</template> </template>
</AInput> </AInput>
<AButton v-if="!state.showCountDown" @click="sendCaptcha" style="margin-left: 10px" size="large">{{ <AButton v-if="!state.showCountDown" @click="sendCaptchaThrottle" style="margin-left: 10px"
size="large">{{
t("login.sendCaptcha") t("login.sendCaptcha")
}} }}
</AButton> </AButton>
@@ -67,7 +68,7 @@
</AFormItem> </AFormItem>
<AFormItem> <AFormItem>
<AButton @click="phoneLoginSubmit" type="primary" size="large" class="login-form-button"> <AButton @click="phoneLoginSubmitDebounce" type="primary" size="large" class="login-form-button">
{{ t("login.loginAndRegister") }} {{ t("login.loginAndRegister") }}
</AButton> </AButton>
</AFormItem> </AFormItem>
@@ -118,7 +119,7 @@
</AFormItem> </AFormItem>
<AFormItem> <AFormItem>
<AButton @click="accountLoginSubmit" type="primary" size="large" class="login-form-button"> <AButton @click="accountLoginSubmitDebounce" type="primary" size="large" class="login-form-button">
{{ {{
t("login.login") t("login.login")
}} }}
@@ -191,6 +192,7 @@ import {checkRotatedCaptcha, getRotatedCaptchaData} from "@/api/captcha";
import {message} from "ant-design-vue"; import {message} from "ant-design-vue";
import {accountLoginApi, phoneLoginApi, sendMessage} from "@/api/user"; import {accountLoginApi, phoneLoginApi, sendMessage} from "@/api/user";
import useStore from "@/store"; import useStore from "@/store";
import {useDebounceFn, useThrottleFn} from "@vueuse/core";
const router = useRouter(); const router = useRouter();
const {t} = useI18n(); const {t} = useI18n();
@@ -202,24 +204,24 @@ const captchaData = reactive({angle: 0, image: "", thumb: "", key: ""});
const captchaErrorCount = ref<number>(0); const captchaErrorCount = ref<number>(0);
const phoneLoginRotateEvent = { const phoneLoginRotateEvent = {
confirm: (angle: number) => { confirm: (angle: number) => {
checkPhoneLoginCaptcha(angle); checkPhoneLoginCaptchaDebounce(angle);
}, },
close: () => { close: () => {
showPhoneRotateCaptcha.value = false; showPhoneRotateCaptcha.value = false;
}, },
refresh: () => { refresh: () => {
getRotateCaptcha(); refreshCaptcha();
}, },
}; };
const accountLoginRotateEvent = { const accountLoginRotateEvent = {
confirm: (angle: number) => { confirm: (angle: number) => {
checkAccountLoginCaptcha(angle); checkAccountLoginCaptchaDebounce(angle);
}, },
close: () => { close: () => {
showAccountRotateCaptcha.value = false; showAccountRotateCaptcha.value = false;
}, },
refresh: () => { refresh: () => {
getRotateCaptcha(); refreshCaptcha();
}, },
}; };
// 账号登录表单数据 // 账号登录表单数据
@@ -264,7 +266,7 @@ const rules: Record<string, Rule[]> = {
] ]
}; };
const state = reactive({ const state: any = reactive<any>({
countDownTime: 60, countDownTime: 60,
showCountDown: false, showCountDown: false,
}); });
@@ -307,6 +309,11 @@ onMounted(() => {
} }
}); });
/**
* 发送验证码节流
*/
const sendCaptchaThrottle = useThrottleFn(sendCaptcha, 3000);
/** /**
* 发送验证码 * 发送验证码
*/ */
@@ -323,6 +330,11 @@ async function sendCaptcha() {
}); });
} }
/**
* 账号登录 防抖
*/
const accountLoginSubmitDebounce = useDebounceFn(accountLoginSubmit, 3000);
/** /**
* 账号登录提交 * 账号登录提交
*/ */
@@ -339,6 +351,11 @@ async function accountLoginSubmit() {
}); });
} }
/**
* 手机登录提交 防抖
*/
const phoneLoginSubmitDebounce = useDebounceFn(phoneLoginSubmit, 3000);
/** /**
* 手机登录提交 * 手机登录提交
*/ */
@@ -367,6 +384,11 @@ async function phoneLoginSubmit() {
}); });
} }
/**
* 刷新验证码 节流
*/
const refreshCaptcha = useThrottleFn(getRotateCaptcha, 3000);
/** /**
* 获取旋转验证码数据 * 获取旋转验证码数据
*/ */
@@ -383,6 +405,11 @@ async function getRotateCaptcha() {
} }
} }
/**
* 检查手机登录旋转验证码 防抖
*/
const checkPhoneLoginCaptchaDebounce = useDebounceFn(checkPhoneLoginCaptcha, 500);
/** /**
* 验证旋转验证码 * 验证旋转验证码
* @param angle * @param angle
@@ -413,6 +440,11 @@ async function checkPhoneLoginCaptcha(angle: number) {
} }
} }
/**
* 检查账户登录旋转验证码 防抖
*/
const checkAccountLoginCaptchaDebounce = useDebounceFn(checkAccountLoginCaptcha, 500);
/** /**
* 检查账户登录旋转验证码 * 检查账户登录旋转验证码
* @param angle * @param angle

View File

@@ -6,14 +6,14 @@
router.push('/login') router.push('/login')
}" class="qrlogin-form-bottom-button" :icon="h(TabletOutlined)">{{ t("login.phoneLoginAndRegister") }} }" class="qrlogin-form-bottom-button" :icon="h(TabletOutlined)">{{ t("login.phoneLoginAndRegister") }}
</AButton> </AButton>
<AButton @click="openQQUrl" class="qrlogin-form-bottom-button" :icon="h(QqOutlined)"></AButton> <AButton @click="openQQbUrlDebounce" class="qrlogin-form-bottom-button" :icon="h(QqOutlined)"></AButton>
<AButton @click="openGiteeUrl" class="qrlogin-form-bottom-button" <AButton @click="openGiteebUrlDebounce" class="qrlogin-form-bottom-button"
style="display: flex; align-items: center;justify-content: center;"> style="display: flex; align-items: center;justify-content: center;">
<template #icon> <template #icon>
<img :src=gitee alt="gitee" class="gitee-icon" style="width: 16px; height: 16px;"/> <img :src=gitee alt="gitee" class="gitee-icon" style="width: 16px; height: 16px;"/>
</template> </template>
</AButton> </AButton>
<AButton @click="openGithubUrl" class="qrlogin-form-bottom-button" :icon="h(GithubOutlined)"></AButton> <AButton @click="openGithubUrlDebounce" class="qrlogin-form-bottom-button" :icon="h(GithubOutlined)"></AButton>
</AFlex> </AFlex>
</div> </div>
</template> </template>
@@ -29,6 +29,7 @@ import {message} from "ant-design-vue";
import gitee from "@/assets/svgs/gitee.svg"; import gitee from "@/assets/svgs/gitee.svg";
import {generateClientId} from "@/api/oauth/wechat.ts"; import {generateClientId} from "@/api/oauth/wechat.ts";
import {getQQUrl} from "@/api/oauth/qq.ts"; import {getQQUrl} from "@/api/oauth/qq.ts";
import {useDebounceFn} from "@vueuse/core";
const router = useRouter(); const router = useRouter();
const {t} = useI18n(); const {t} = useI18n();
@@ -95,10 +96,15 @@ function getLocalClientId(): string | null {
} }
} }
/**
* Open the Github OAuth page in a new window with debounce
*/
const openGithubUrlDebounce = useDebounceFn(openGithubUrl, 1000);
/** /**
* Open the Github OAuth page in a new window * Open the Github OAuth page in a new window
*/ */
const openGithubUrl = () => { function openGithubUrl() {
const iWidth = 800; //弹出窗口的宽度; const iWidth = 800; //弹出窗口的宽度;
const iHeight = 500; //弹出窗口的高度; const iHeight = 500; //弹出窗口的高度;
//window.screen.height获得屏幕的高window.screen.width获得屏幕的宽 //window.screen.height获得屏幕的高window.screen.width获得屏幕的宽
@@ -125,11 +131,17 @@ const openGithubUrl = () => {
} }
}; };
window.addEventListener("message", messageHandler); window.addEventListener("message", messageHandler);
}; }
/**
* Open the Gitee OAuth page in a new window with debounce
*/
const openGiteebUrlDebounce = useDebounceFn(openGiteeUrl, 1000);
/** /**
* Open the Gitee OAuth page in a new window * Open the Gitee OAuth page in a new window
*/ */
const openGiteeUrl = () => { function openGiteeUrl() {
const iWidth = 800; //弹出窗口的宽度; const iWidth = 800; //弹出窗口的宽度;
const iHeight = 500; //弹出窗口的高度; const iHeight = 500; //弹出窗口的高度;
//window.screen.height获得屏幕的高window.screen.width获得屏幕的宽 //window.screen.height获得屏幕的高window.screen.width获得屏幕的宽
@@ -157,13 +169,17 @@ const openGiteeUrl = () => {
} }
}; };
window.addEventListener("message", messageHandler); window.addEventListener("message", messageHandler);
}; }
/**
* Open the QQ OAuth page in a new window with debounce
*/
const openQQbUrlDebounce = useDebounceFn(openQQUrl, 1000);
/** /**
* Open the QQ OAuth page in a new window * Open the QQ OAuth page in a new window
*/ */
const openQQUrl = () => { function openQQUrl() {
const iWidth = 800; //弹出窗口的宽度; const iWidth = 800; //弹出窗口的宽度;
const iHeight = 500; //弹出窗口的高度; const iHeight = 500; //弹出窗口的高度;
//window.screen.height获得屏幕的高window.screen.width获得屏幕的宽 //window.screen.height获得屏幕的高window.screen.width获得屏幕的宽
@@ -190,7 +206,8 @@ const openQQUrl = () => {
} }
}; };
window.addEventListener("message", messageHandler); window.addEventListener("message", messageHandler);
}; }
onBeforeMount(() => { onBeforeMount(() => {
getGithubRedirectUrl(); getGithubRedirectUrl();
getGiteeRedirectUrl(); getGiteeRedirectUrl();