Sfoglia il codice sorgente

feat:处理复制邀请链接问题

leo 1 anno fa
parent
commit
f49fcd235c

+ 13 - 15
pages/claimCloudPhone/components/bargainingAssistance.vue

@@ -210,13 +210,13 @@
           placeholder="手机号"
           type="digit"
           v-model="laoginData.phone"
-          clearable 
+          clearable
           maxLength="11"
         />
         <van-field
           placeholder="验证码"
           v-model="laoginData.code"
-          clearable 
+          clearable
           type="digit"
           maxLength="6"
         >
@@ -272,6 +272,7 @@ import customProgress from './customProgress.vue';
 import loadList from '@/components/loadList';
 import { fileKeyToUrl } from '@/plugins/file-center.js';
 import Verify from '@/components/verifition/Verify';
+import { writeToClipboard } from '@/plugins/plugins.js';
 export default {
   props: {
     operateActivityId: {
@@ -647,20 +648,17 @@ export default {
                   if (this.$userAgent.isSzx || this.$userAgent.isSzxBrowser) {
                     this.$native.share(shareInfo);
                   } else {
-                    // 微信环境不支持复制
-                    if (this.$userAgent.isWx || this.$userAgent.isMiniProgram) {
-                      setTimeout(() => {
-                        this.$toast.fail(
-                          '链接复制失败,请访问客户端或网页复制该链接',
-                        );
+                    writeToClipboard(res.data.link)
+                      .then(() => {
+                        setTimeout(() => {
+                          this.$toast('链接复制成功');
+                        });
+                      })
+                      .catch((err) => {
+                        setTimeout(() => {
+                          this.$toast('链接复制失败');
+                        });
                       });
-                      return;
-                    }
-                    // 浏览器环境
-                    await this.$native.clipboard.writeText(res.data.link);
-                    setTimeout(() => {
-                      this.$toast.success('链接复制成功');
-                    });
                   }
                 }
               })

+ 31 - 0
plugins/plugins.js

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