Explorar o código

优化复制功能

leo hai 1 ano
pai
achega
2694f3fc26
Modificáronse 2 ficheiros con 52 adicións e 6 borrados
  1. 17 6
      pages/claimCloudPhone/components/bargainingAssistance.vue
  2. 35 0
      plugins/plugins.js

+ 17 - 6
pages/claimCloudPhone/components/bargainingAssistance.vue

@@ -341,6 +341,7 @@ import loadList from '@/components/loadList';
 import { fileKeyToUrl } from '@/plugins/file-center.js';
 import Verify from '@/components/verifition/Verify';
 import lottie from '@/components/lottie';
+import { writeToClipboard } from '@/plugins/plugins.js';
 export default {
   props: {
     operateActivityId: {
@@ -757,16 +758,26 @@ export default {
                   if (this.$userAgent.isSzx || this.$userAgent.isSzxBrowser) {
                     this.$native.share(shareInfo);
                   } else {
-                    this.$copyText(res.data.link).then(
-                      (e) => {
+                    writeToClipboard(
+                      res.data.link,
+                      () => {
                         setTimeout(() => {
                           this.$toast('链接复制成功');
                         });
                       },
-                      (e) => {
-                        setTimeout(() => {
-                          this.$toast('链接复制失败');
-                        });
+                      () => {
+                        this.$copyText(res.data.link).then(
+                          (e) => {
+                            setTimeout(() => {
+                              this.$toast('链接复制成功');
+                            });
+                          },
+                          (e) => {
+                            setTimeout(() => {
+                              this.$toast('链接复制失败');
+                            });
+                          },
+                        );
                       },
                     );
                   }

+ 35 - 0
plugins/plugins.js

@@ -13,4 +13,39 @@ export function timeStamp(StatusMinute, userCardType, nextSendTime) {
         StatusMinute += parseFloat(min) + '分钟';
     }
     return StatusMinute.length ? StatusMinute : userCardType === 1 ? '0小时' : '';
+}
+
+
+// 封装对剪贴板的访问为异步函数
+export function writeToClipboard(text, success = () => { }, error = () => { }) {
+    // 如果navigator.clipboard存在,直接使用复制功能
+    if (navigator.clipboard) {
+        try {
+            setTimeout(() => {
+                navigator.clipboard.writeText(text).then(() => {
+                    success()
+                }).catch((err) => {
+                    error()
+                })
+            }, 0);
+        } catch (err) {
+            error()
+        }
+    } else {
+        const textArea = document.createElement('textarea');
+        textArea.value = text;
+        document.body.appendChild(textArea);
+        textArea.select();
+        try {
+            const successful = document.execCommand('copy');
+            if (successful) {
+                success()
+            } else {
+                throw '复制失败'
+            }
+        } catch (err) {
+            error()
+        }
+        document.body.removeChild(textArea);
+    }
 }