u-copy.vue 1.6 KB

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