From 35f28f83d58ee02090225b376a78e72e0dc647fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E5=B9=B3=E8=89=BA?= <52643018@qq.com> Date: Mon, 30 Aug 2021 15:53:29 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BF=AE=E5=A4=8D]=20H5=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/junyi-h5-copy.js | 18 ++++++++++++++++++ common/mixin.js | 17 +++++++++++++++++ pages/my/exclusiveMatch.vue | 4 +--- pages/my/memberDetails.vue | 10 +++------- pages/my/service.vue | 5 +---- 5 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 common/junyi-h5-copy.js diff --git a/common/junyi-h5-copy.js b/common/junyi-h5-copy.js new file mode 100644 index 0000000..91fff37 --- /dev/null +++ b/common/junyi-h5-copy.js @@ -0,0 +1,18 @@ +export default function h5Copy(content) { + + if (!document.queryCommandSupported('copy')) { + // 不支持 + return false + } + + let textarea = document.createElement("textarea") + textarea.value = content + textarea.readOnly = "readOnly" + document.body.appendChild(textarea) + textarea.select() // 选择对象 + textarea.setSelectionRange(0, content.length) //核心 + let result = document.execCommand("copy") // 执行浏览器复制命令 + textarea.remove() + return result + +} \ No newline at end of file diff --git a/common/mixin.js b/common/mixin.js index 167e151..aba9874 100644 --- a/common/mixin.js +++ b/common/mixin.js @@ -1,3 +1,4 @@ +import h5Copy from "@/common/junyi-h5-copy.js"; export default{ data(){ return { @@ -98,6 +99,22 @@ export default{ }else{ uni.switchTab({url:'/pages/index/index'}); } + }, + $copy(orderNumber){ + // #ifdef H5 + const result = h5Copy(orderNumber); + if (result === false) { + this.$msg('当前环境不支持复制'); + } else { + this.$msg('复制成功', {icon: 'success'}) + } + // #endif + + // #ifdef MP + uni.setClipboardData({ + data: orderNumber + }); + // #endif } } } \ No newline at end of file diff --git a/pages/my/exclusiveMatch.vue b/pages/my/exclusiveMatch.vue index d6018e8..2118c1c 100644 --- a/pages/my/exclusiveMatch.vue +++ b/pages/my/exclusiveMatch.vue @@ -55,9 +55,7 @@ // 点击复制 copy(text) { if(!text) return; - uni.setClipboardData({ - data: text - }); + this.$copy(text); } } } diff --git a/pages/my/memberDetails.vue b/pages/my/memberDetails.vue index d00082f..427bcf7 100644 --- a/pages/my/memberDetails.vue +++ b/pages/my/memberDetails.vue @@ -217,13 +217,9 @@ }) }, share(){ - this.$msg('复制链接成功!') - uni.setClipboardData({ - data: 'a', - complete: () => { - console.log("aaa") - } - }); + let text = `${this.API.CALLBACKURL}/pages/my/memberDetails?id=${this.number_id}`; + this.$copy(text); + this.$msg('复制链接成功!'); } } } diff --git a/pages/my/service.vue b/pages/my/service.vue index 9522e0e..71e565e 100644 --- a/pages/my/service.vue +++ b/pages/my/service.vue @@ -58,11 +58,8 @@ }, // 点击复制 copy(text) { - // TODO H5复制有问题??? if(!text) return; - uni.setClipboardData({ - data: text - }); + this.$copy(text); } } }