InputCopy.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div>
  3. <!-- 本地读取失败的粘贴板 -->
  4. <van-dialog v-model="copyTextVisible" class="copy-text-modal" confirm-button-text="确定" show-cancel-button confirm-button-color="#3cc51f" v-cloak :before-close="beforeCloseCopy">
  5. <div class="tc">
  6. 读取剪贴板失败
  7. </div>
  8. <div>
  9. <van-field v-model="copyTextValue" placeholder="请输入复制到剪切板的内容" />
  10. </div>
  11. </van-dialog>
  12. </div>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'InputCopy',
  17. data() {
  18. return {
  19. copyTextVisible: false, // 读取粘贴板失败弹窗
  20. copyTextValue: '',
  21. }
  22. },
  23. methods: {
  24. async pasteText() {
  25. try {
  26. this.copyTextValue = '';
  27. // 获取剪贴板内容
  28. let clipText = await navigator.clipboard.readText();
  29. if (typeof clipText === 'boolean') throw new Error('读取失败');
  30. // 打开云机粘贴板
  31. this.$emit('openPasteboard', clipText);
  32. } catch (error) {
  33. // 读取失败, 打开粘贴板弹窗
  34. this.copyTextVisible = true;
  35. }
  36. },
  37. // 复制弹窗是否关闭回调
  38. beforeCloseCopy(action, done){
  39. // 取消
  40. if (action !== 'confirm') {
  41. done()
  42. // 打开云机粘贴板
  43. this.$emit('openPasteboard');
  44. return
  45. }
  46. // 确定
  47. if (!this.copyTextValue) {
  48. this.$toast('请输入复制到粘贴板的内容')
  49. done(false)
  50. return
  51. }
  52. done()
  53. // 打开云机粘贴板
  54. this.$emit('openPasteboard', this.copyTextValue);
  55. },
  56. }
  57. }
  58. </script>
  59. <style lang="scss" scoped>
  60. .copy-text-modal {
  61. color: #000;
  62. padding: 10px;
  63. }
  64. .copy-text-modal .van-field__control {
  65. border: 1px solid rgba(214, 209, 209, 0.4941176471);
  66. border-radius: 0.16rem;
  67. padding: 10px;
  68. }
  69. </style>