feat: 支付的时候防止重复点击

This commit is contained in:
2025-12-01 15:15:48 +08:00
parent 9632f5c065
commit 172bae663b
4 changed files with 34 additions and 3 deletions

28
src/utils/noclick.js Normal file
View File

@@ -0,0 +1,28 @@
// 防止处理多次点击
// methods是需要点击后需要执行的函数 info是点击需要传的参数
export function noMultipleClicks(fn, info, delay = 2000) {
if (typeof fn !== 'function') return;
// this 会是组件实例(因为通过 globalProperties 调用时 Vue 会把组件实例作为上下文)
const ctx = this || {};
if (!ctx.__noClickMap) ctx.__noClickMap = new WeakMap();
const map = ctx.__noClickMap;
if (map.get(fn)) {
console.log('请勿重复点击:', fn.name);
return;
}
map.set(fn, true);
// 保留组件上下文调用方法
fn.call(ctx, info);
setTimeout(() => {
map.set(fn, false);
}, delay);
}
export default {
install(app) {
app.config.globalProperties.$noMultipleClicks = noMultipleClicks;
}
}