
const DEFAULT_WAIT 3000 const lastMap new WeakMap() // vm - { [key]: timestamp } /** * 提交防重复校验通过后、请求前调用。 * param {Vue} vm 组件实例用于 $message * param {string} key 同一页面不同操作区分的键 * param {object} [options] * param {number} [options.wait3000] 节流间隔 ms * param {boolean} [options.busyfalse] 是否正在提交 * param {string[]} [options.loadingKeys] 组件上的 loading 字段名 * param {string} [options.message] 拦截提示文案 * returns {boolean} true可继续请求false已拦截 */ export function checkSubmitThrottle(vm, key, options {}) { const { wait DEFAULT_WAIT, busy false, loadingKeys [], message 请求过于频繁请稍后再试, } options const isBusy busy || loadingKeys.some((k) vm[k]) if (isBusy) { vm.$message.warning(message) return false } let map lastMap.get(vm) if (!map) { map Object.create(null) lastMap.set(vm, map) } const now Date.now() if (now - (map[key] || 0) wait) { vm.$message.warning(message) return false } map[key] now return true }//页面引用使用 import { checkSubmitThrottle } from /utils/submitThrottle; //请求前使用 if (!checkSubmitThrottle(this, delivery, { loadingKeys: [dialoading] })) { return; } /* key 是你自己起的操作标识字符串用来在同一个页面组件里区分不同提交动作的节流计时。 loadingKeys 是组件 data 里 loading 状态字段名的数组。方法会用这些名字去读 vm 上的值任一为 true 就视为「正在提交」直接拦截并提示。 */