clipboard.uvue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-title">请输入剪贴板内容</view>
  6. <view class="uni-list">
  7. <view class="uni-list-cell">
  8. <input class="uni-input" type="text" placeholder="请输入剪贴板内容" :value="data" @input="dataChange" />
  9. </view>
  10. </view>
  11. <view class="uni-btn-v">
  12. <button type="primary" @click="setClipboard">存储数据</button>
  13. <button @tap="getClipboard">读取数据</button>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. title: 'get/setClipboardData',
  23. data: '',
  24. // 自动化测试
  25. getDataTest: '',
  26. setClipboardTest: false
  27. }
  28. },
  29. methods: {
  30. dataChange: function (e:UniInputEvent) {
  31. this.data = e.detail.value
  32. },
  33. getClipboard: function () {
  34. uni.getClipboardData({
  35. success: (res) => {
  36. console.log(res.data);
  37. this.getDataTest = res.data;
  38. const content = res.data != "" ? '剪贴板内容为:' + res.data : '剪贴板暂无内容';
  39. uni.showModal({
  40. content,
  41. title: '读取剪贴板',
  42. showCancel: false
  43. })
  44. },
  45. fail: (err) => {
  46. uni.showModal({
  47. content: `读取剪贴板失败: ${err.errMsg}`,
  48. showCancel: false
  49. })
  50. }
  51. });
  52. },
  53. setClipboard: function () {
  54. if (this.data.length == 0) {
  55. uni.showModal({
  56. title: '设置剪贴板失败',
  57. content: '内容不能为空',
  58. showCancel: false
  59. })
  60. } else {
  61. uni.setClipboardData({
  62. data: this.data,
  63. success: () => {
  64. this.setClipboardTest = true
  65. // 成功处理
  66. uni.showToast({
  67. title: '设置剪贴板成功',
  68. icon: "success"
  69. })
  70. },
  71. fail: () => {
  72. // bug:自动化测试时设置成功也进入了fail
  73. this.setClipboardTest = false
  74. // 失败处理
  75. uni.showToast({
  76. title: '储存数据失败!',
  77. icon: "none"
  78. })
  79. }
  80. });
  81. }
  82. }
  83. }
  84. }
  85. </script>
  86. <style>
  87. </style>