diff --git a/src/views/purchase/purchaseagent/index.vue b/src/views/purchase/purchaseagent/index.vue index ca54c22..92f70ca 100644 --- a/src/views/purchase/purchaseagent/index.vue +++ b/src/views/purchase/purchaseagent/index.vue @@ -362,13 +362,8 @@ const handleResetPassword = async (row: any) => { type: 'success', beforeClose: (action, instance, done) => { if (action === 'confirm') { - navigator.clipboard.writeText(copyText).then(() => { - useMessage().success('已复制到剪贴板'); - done(); - }).catch(() => { - useMessage().error('复制失败,请手动复制'); - done(); - }); + copyToClipboard(copyText); + done(); } else { done(); } @@ -379,6 +374,40 @@ const handleResetPassword = async (row: any) => { useMessage().error(err?.msg || err?.message || '重置密码失败'); } }; + +/** + * 复制文本到剪贴板(兼容HTTP环境) + */ +const copyToClipboard = (text: string) => { + if (navigator.clipboard && navigator.clipboard.writeText) { + navigator.clipboard.writeText(text).then(() => { + useMessage().success('已复制到剪贴板'); + }).catch(() => { + fallbackCopy(text); + }); + } else { + fallbackCopy(text); + } +}; + +/** + * 降级复制方法 + */ +const fallbackCopy = (text: string) => { + const textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.left = '-9999px'; + document.body.appendChild(textarea); + textarea.select(); + try { + document.execCommand('copy'); + useMessage().success('已复制到剪贴板'); + } catch { + useMessage().error('复制失败,请手动复制'); + } + document.body.removeChild(textarea); +};