clipboard.thorui.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * 复制文本 兼容H5
  3. * 来自 ThorUI www.thorui.cn | 文档地址: www.donarui.com
  4. * @author echo.
  5. * @version 1.0.0
  6. **/
  7. // #ifdef H5
  8. import ClipboardJS from "@/lib/clipboard.min.js"
  9. // #endif
  10. const thorui = {
  11. getClipboardData: function(data, callback) {
  12. // #ifdef APP-PLUS || MP
  13. uni.setClipboardData({
  14. data: data,
  15. success(res) {
  16. uni.getClipboardData({
  17. success(res) {
  18. ("function" == typeof callback) && callback(true)
  19. uni.showToast({title:'复制成功',icon:'none'})
  20. },
  21. fail(res) {
  22. ("function" == typeof callback) && callback(false)
  23. uni.showToast({title:'复制失败',icon:'none'})
  24. }
  25. })
  26. },
  27. fail(res) {
  28. ("function" == typeof callback) && callback(false)
  29. }
  30. })
  31. // #endif
  32. // #ifdef H5
  33. let event = window.event || {}
  34. let clipboard = new ClipboardJS("", {
  35. text: () => data
  36. })
  37. clipboard.on('success', (e) => {
  38. ("function" == typeof callback) && callback(true)
  39. clipboard.off('success')
  40. clipboard.off('error')
  41. clipboard.destroy()
  42. });
  43. clipboard.on('error', (e) => {
  44. ("function" == typeof callback) && callback(false)
  45. clipboard.off('success')
  46. clipboard.off('error')
  47. clipboard.destroy()
  48. });
  49. clipboard.onClick(event)
  50. // #endif
  51. }
  52. };
  53. module.exports = {
  54. getClipboardData: thorui.getClipboardData
  55. };