copy.js 886 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * 复制文本到剪贴板
  3. * @param {string} text - 要复制的文本内容
  4. * @param {string} successMsg - 复制成功时的提示信息
  5. * @param {string} failMsg - 复制失败时的提示信息
  6. */
  7. export const copyToClipboard = (text, successMsg = '复制成功', failMsg = '复制失败') => {
  8. // #ifdef H5
  9. const input = document.createElement('input')
  10. input.value = text
  11. document.body.appendChild(input)
  12. input.select()
  13. document.execCommand('copy')
  14. document.body.removeChild(input)
  15. uni.showToast({
  16. title: successMsg,
  17. icon: 'success'
  18. })
  19. // #endif
  20. // #ifdef APP-PLUS || MP
  21. uni.setClipboardData({
  22. data: text,
  23. success: () => {
  24. uni.showToast({
  25. title: successMsg,
  26. icon: 'success'
  27. })
  28. },
  29. fail: () => {
  30. uni.showToast({
  31. title: failMsg,
  32. icon: 'none'
  33. })
  34. }
  35. })
  36. // #endif
  37. }