From 5004d96e7751654858bd9debe9d262a11aa62215 Mon Sep 17 00:00:00 2001 From: luJianJun <2587063613@qq.com> Date: Wed, 16 Aug 2023 11:40:19 +0800 Subject: [PATCH] =?UTF-8?q?pnpm=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useCopy.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/hooks/useCopy.ts b/src/hooks/useCopy.ts index 873c9ad..74fd650 100644 --- a/src/hooks/useCopy.ts +++ b/src/hooks/useCopy.ts @@ -3,9 +3,31 @@ import { writeClipboard } from '@solid-primitives/clipboard' export const useClipboardCopy = (source: string, delay = 1000) => { const [copied, setCopied] = createSignal(false) + //判断平台 + const isMobile = () => { + let flag = navigator.userAgent.match( + /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i + ); + return flag; + } + //手机端复制功能 + const h5CopyText = (text: string) => { + var tempInput = document.createElement("input"); + tempInput.value = text; + document.body.appendChild(tempInput); + tempInput.select(); + document.execCommand("copy"); + document.body.removeChild(tempInput); + }; + + const copy = async () => { + if (isMobile()) { + //复制功能 + writeClipboard(source) + } else { + h5CopyText(source) + } - const copy = async() => { - writeClipboard(source) setCopied(true) } @@ -17,4 +39,4 @@ export const useClipboardCopy = (source: string, delay = 1000) => { }) return [copied, copy] as const -} +} \ No newline at end of file