Browse Source

[修复] H5不能复制的问题

master
邓平艺 5 years ago
parent
commit
35f28f83d5
  1. 18
      common/junyi-h5-copy.js
  2. 17
      common/mixin.js
  3. 4
      pages/my/exclusiveMatch.vue
  4. 10
      pages/my/memberDetails.vue
  5. 5
      pages/my/service.vue

18
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
}

17
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
}
}
}

4
pages/my/exclusiveMatch.vue

@ -55,9 +55,7 @@
//
copy(text) {
if(!text) return;
uni.setClipboardData({
data: text
});
this.$copy(text);
}
}
}

10
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('复制链接成功!');
}
}
}

5
pages/my/service.vue

@ -58,11 +58,8 @@
},
//
copy(text) {
// TODO H5
if(!text) return;
uni.setClipboardData({
data: text
});
this.$copy(text);
}
}
}

Loading…
Cancel
Save