common.ui.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. window.copyCommand = function(selector)
  2. {
  3. const command = $(selector).text();
  4. // 首先尝试使用现代的 Clipboard API (仅在 HTTPS 下可用)
  5. if (navigator.clipboard && window.isSecureContext) {
  6. navigator.clipboard.writeText(command).then(function() {
  7. zui.Messager.show({type: 'success', message: copySuccess, timeout: 1000});
  8. }).catch(function(err) {
  9. console.warn('Clipboard API 失败:', err);
  10. fallbackCopyTextToClipboard(command);
  11. });
  12. } else {
  13. // HTTP 协议或不支持 Clipboard API,直接使用回退方法
  14. fallbackCopyTextToClipboard(command);
  15. }
  16. function fallbackCopyTextToClipboard(text) {
  17. const $textArea = $('<textarea>', {
  18. css: {
  19. position: 'fixed',
  20. top: '0',
  21. left: '0',
  22. width: '2em',
  23. height: '2em',
  24. padding: '0',
  25. border: 'none',
  26. outline: 'none',
  27. boxShadow: 'none',
  28. background: 'transparent'
  29. }
  30. });
  31. $('body').append($textArea);
  32. $textArea.val(text);
  33. $textArea[0].focus();
  34. $textArea[0].select();
  35. try {
  36. const successful = document.execCommand('copy');
  37. if(successful) {
  38. zui.Messager.show({type: 'success', message: copySuccess, timeout: 1000});
  39. } else {
  40. zui.Messager.show({type: 'danger', message: copyFail, timeout: 1000});
  41. }
  42. } catch (err) {
  43. zui.Messager.show({type: 'danger', message: copyFail, timeout: 1000});
  44. }
  45. $textArea.remove();
  46. }
  47. }