musicUtil.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {getMapValueByKey} from "@/utils/objectUtil";
  2. import {clientType,} from "@/utils/constant";
  3. export const getOwnerName = (musicSheetExtend: any, sourceType: string) => {
  4. if (sourceType == 'PLATFORM') {
  5. return "--"
  6. }
  7. let ownerName = '';
  8. if (musicSheetExtend) {
  9. const appName = musicSheetExtend.applicationName;
  10. if (sourceType == 'ORG') {
  11. const organizationRole = musicSheetExtend.organizationRole ? '-' + musicSheetExtend.organizationRole : '';
  12. ownerName += appName + organizationRole
  13. } else if (sourceType == 'PERSON') {
  14. if (musicSheetExtend?.userName) {
  15. ownerName = musicSheetExtend.userName
  16. }
  17. let typeName = getMapValueByKey(musicSheetExtend.clientType, new Map(Object.entries(clientType)));
  18. typeName = typeName ? '-' + typeName : '';
  19. ownerName += ' (' + appName + typeName + ')'
  20. }
  21. }
  22. return ownerName;
  23. }
  24. export const copyText = (message: any, text: string) => {
  25. // 数字没有 .length 不能执行selectText 需要转化成字符串
  26. const textString = text.toString()
  27. let input = document.querySelector('#copy-input') as HTMLInputElement
  28. if (!input) {
  29. input = document.createElement('input')
  30. input.id = 'copy-input'
  31. input.readOnly = true // 防止ios聚焦触发键盘事件
  32. input.style.position = 'fixed'
  33. input.style.left = '-1000px'
  34. input.style.zIndex = '-1000'
  35. // 为了处理,页面滑动到底部的问题
  36. document.body.appendChild(input)
  37. // document.querySelector('#input-copy-container')?.appendChild(input)
  38. }
  39. input.value = textString
  40. // ios必须先选中文字且不支持 input.select();
  41. selectText(input, 0, textString.length)
  42. if (document.execCommand('copy')) {
  43. document.execCommand('copy')
  44. message.success('复制成功')
  45. }
  46. input.blur()
  47. // input自带的select()方法在苹果端无法进行选择,所以需要自己去写一个类似的方法
  48. // 选择文本。createTextRange(setSelectionRange)是input方法
  49. function selectText(textbox: any, startIndex: any, stopIndex: any) {
  50. if (textbox.createTextRange) {
  51. //ie
  52. const range = textbox.createTextRange()
  53. range.collapse(true)
  54. range.moveStart('character', startIndex) //起始光标
  55. range.moveEnd('character', stopIndex - startIndex) //结束光标
  56. range.select() //不兼容苹果
  57. } else {
  58. //firefox/chrome
  59. textbox.setSelectionRange(startIndex, stopIndex)
  60. textbox.focus()
  61. }
  62. }
  63. }