From f744ad286e417c3a31c3eacb11ee9c962f15b04c Mon Sep 17 00:00:00 2001 From: xuziqiang <1344691446@qq.com> Date: Wed, 19 Jun 2024 10:03:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20005=20=E5=A4=8D=E5=88=B6=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hx-op/src/util/copyText.ts | 23 ++++++++++++++++++++++ .../organizationManage/enterpriseManage/config.ts | 10 ++-------- 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 hx-op/src/util/copyText.ts diff --git a/hx-op/src/util/copyText.ts b/hx-op/src/util/copyText.ts new file mode 100644 index 0000000..f373cf0 --- /dev/null +++ b/hx-op/src/util/copyText.ts @@ -0,0 +1,23 @@ +import { NsMessage } from '/nerv-lib/component'; +const flag = navigator.clipboard && window.isSecureContext; + +export const copyText = (text: any) => { + if (flag) { + // navigator clipboard 向剪贴板写文本 + navigator.clipboard.writeText(text).then(() => { + NsMessage.success('复制成功'); + }); + } else { + // 创建text area + const textArea = document.createElement('textarea'); + textArea.value = text; + // 使text area不在viewport,同时设置不可见 + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + NsMessage.success('复制成功'); + // 执行复制命令并移除文本框 + document.execCommand('copy'); + textArea.remove(); + } +}; diff --git a/hx-op/src/view/organizationManage/enterpriseManage/config.ts b/hx-op/src/view/organizationManage/enterpriseManage/config.ts index cbd3de7..1cbda4b 100644 --- a/hx-op/src/view/organizationManage/enterpriseManage/config.ts +++ b/hx-op/src/view/organizationManage/enterpriseManage/config.ts @@ -7,6 +7,7 @@ import { ExclamationCircleOutlined } from '@ant-design/icons-vue'; import { enterPrise } from '/@/api/origanizemanage'; import { http } from '/nerv-lib/util/http'; import { generateRandomString } from '/@/util/generateRandomString'; +import { copyText } from '/@/util/copyText'; type status = 'NORMAL' | 'FREEZE'; export const formConfig = [ @@ -144,14 +145,7 @@ const handle = async (record: any) => { createVNode('p', {}, '重置成功'), createVNode('p', {}, '点击【确认】后自动复制密码并离开'), ]), - onOk: () => { - try { - navigator.clipboard.writeText(password); - NsMessage.success('复制成功'); - } catch (err) { - console.error('复制失败', err); - } - }, + onOk: () => copyText(password), }); }); },