From 2129a8ad8bd001bcf401088a48284453a64b82f4 Mon Sep 17 00:00:00 2001 From: aixianling Date: Wed, 22 Mar 2023 15:41:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E7=BB=9F=E4=B8=80=E7=AE=A1=E7=90=86,=E8=A3=85?= =?UTF-8?q?=E9=A5=B0=E5=99=A8=E7=BB=9F=E4=B8=80=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/lib/js/decorator.js | 24 ++++++++++++++++++++++++ ui/lib/js/directives.js | 28 ++++++++++++++++++++++++++++ ui/packages/index.js | 20 +++----------------- 3 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 ui/lib/js/directives.js diff --git a/ui/lib/js/decorator.js b/ui/lib/js/decorator.js index 54b29c5e..a086892f 100644 --- a/ui/lib/js/decorator.js +++ b/ui/lib/js/decorator.js @@ -59,3 +59,27 @@ export function throttle(wait) { } } } + +/** + * 加载第三方sdk + * @param sdk + * @param interval + * @param name + * @returns {(function(*, *, *): void)|*} + */ +export function load(sdk, interval = 200, name = "") { + return function (target, n, descriptor) { + const origin = descriptor.value + let c = 0 + const loop = (that, args) => { + if (!!sdk) { + origin.apply(that, args) + } else if (c < 10) { + setTimeout(() => ++c && loop(that, args), interval) + } else throw new Error("无法加载" + name) + } + descriptor.value = function () { + loop(this, arguments) + } + } +} diff --git a/ui/lib/js/directives.js b/ui/lib/js/directives.js new file mode 100644 index 00000000..e9b4ce7f --- /dev/null +++ b/ui/lib/js/directives.js @@ -0,0 +1,28 @@ +const map = { + throttle: { + bind: function (el, obj) { + let timerId = null + let flag = true + + el.addEventListener('input', function () { + if (!flag) return + + flag = false + timerId && clearTimeout(timerId) + timerId = setTimeout(function () { + flag = true + obj.value() + }, 800) + }) + } + } +} + + +export default { + install(Vue) { + for (const key in map) { + Vue.directive(key, map[key]) + } + } +} diff --git a/ui/packages/index.js b/ui/packages/index.js index c5ebbee1..d63557c4 100644 --- a/ui/packages/index.js +++ b/ui/packages/index.js @@ -1,29 +1,15 @@ //本地仓库外部组件 // 存储组件列表 +import directives from "../lib/js/directives"; + let components = []; // 定义 install 方法,接收 Vue 作为参数。如果使用 use 注册插件,则所有的组件都将被注册 const install = function (Vue) { if (install.installed) return; // 遍历注册全局组件 let contexts = require.context('.', true, /[\\\/]Ai([^\\\/]+)\.vue$/); - Vue.directive('throttle', { - bind: function (el, obj) { - let timerId = null - let flag = true - - el.addEventListener('input', function () { - if (!flag) return - - flag = false - timerId && clearTimeout(timerId) - timerId = setTimeout(function () { - flag = true - obj.value() - }, 800) - }) - } - }) + Vue.use(directives) if (contexts) { contexts.keys().map((e) => { components.push(contexts(e).default); From 81dbd135e227b6963147c0e270282a71e62d656e Mon Sep 17 00:00:00 2001 From: aixianling Date: Wed, 22 Mar 2023 15:56:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0v-permit=E7=94=A8?= =?UTF-8?q?=E6=9D=A5=E6=8E=A7=E5=88=B6=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/lib/js/directives.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ui/lib/js/directives.js b/ui/lib/js/directives.js index e9b4ce7f..836c3178 100644 --- a/ui/lib/js/directives.js +++ b/ui/lib/js/directives.js @@ -15,6 +15,15 @@ const map = { }, 800) }) } + }, + permit: { + bind(el, binding) { + const code = binding.value + const permits = JSON.parse(localStorage.getItem('vuex') || null)?.user?.info?.buttons || [] + if (!permits.find(e => e.id == code || e.permission == code)) { + el.parentNode.removeChild(el) + } + } } } From 99480dc8d36ac9ecb8a56b64ca1a47ff0be22894 Mon Sep 17 00:00:00 2001 From: aixianling Date: Wed, 22 Mar 2023 16:30:43 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=98=B2=E6=8A=96=E8=8A=82=E6=B5=81?= =?UTF-8?q?=E8=A3=85=E9=A5=B0=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/lib/js/decorator.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ui/lib/js/decorator.js b/ui/lib/js/decorator.js index a086892f..0bcc2656 100644 --- a/ui/lib/js/decorator.js +++ b/ui/lib/js/decorator.js @@ -45,15 +45,14 @@ export function loading() { * @returns {(function(*, *, *): void)|*} */ export function throttle(wait) { - return function (target, name, descriptor) { + let timer; + return function (t, n, descriptor) { const origin = descriptor.value - let lock = false descriptor.value = function () { - if (!lock) { - lock = true - origin.apply(this, arguments) + if (!timer) { setTimeout(() => { - lock = false + origin.apply(this, arguments) + timer = null }, wait) } } @@ -83,3 +82,21 @@ export function load(sdk, interval = 200, name = "") { } } } + +/** + * 防抖装饰器 + * @param delay 防抖时间 + * @returns {(function(*, *, *): void)|*} + */ +export function debounce(delay) { + let timer = null; + return function (t, n, descriptor) { + const origin = descriptor.value + descriptor.value = function () { + clearTimeout(timer); + timer = setTimeout(() => { + origin.apply(this, arguments) + }, delay) + } + } +}