u-copy.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <view @click="handleClick">
  3. <slot>复制</slot>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: "up-copy",
  9. props: {
  10. content: {
  11. type: String,
  12. default: ''
  13. },
  14. alertStyle: {
  15. type: String,
  16. default: 'toast'
  17. },
  18. notice: {
  19. type: String,
  20. default: '复制成功'
  21. }
  22. },
  23. emits: ['success'],
  24. methods: {
  25. handleClick() {
  26. let content = this.content;
  27. if (!content) {
  28. uni.showToast({
  29. title: '暂无',
  30. icon: 'none',
  31. duration: 2000,
  32. });
  33. return false;
  34. }
  35. content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
  36. /**
  37. * 小程序端 和 app端的复制逻辑
  38. */
  39. let that = this;
  40. uni.setClipboardData({
  41. data: content,
  42. success: function() {
  43. if (that.alertStyle == 'modal') {
  44. uni.showModal({
  45. title: '提示',
  46. content: that.notice
  47. });
  48. } else {
  49. uni.showToast({
  50. title: that.notice,
  51. icon: 'none'
  52. });
  53. }
  54. that.$emit('success');
  55. },
  56. fail:function(){
  57. uni.showToast({
  58. title: '复制失败',
  59. icon: 'none',
  60. duration:3000,
  61. });
  62. }
  63. });
  64. }
  65. }
  66. }
  67. </script>
  68. <style lang="scss" scoped>
  69. </style>