util.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import store from '@/store/index'
  2. export const isValue = (val: any) => {
  3. if (val === null || val === undefined || val === '') {
  4. return false
  5. }
  6. return true
  7. }
  8. export const arrayToMap = (array: Array<any>, key: any) => {
  9. const map = new Map()
  10. array.forEach((v: any) => {
  11. map.set(v[key], v)
  12. })
  13. return map
  14. }
  15. export const YMDHms = (date: string | number | Date) => {
  16. const _date = new Date(date)
  17. const Y = `${_date.getFullYear()}`;
  18. const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  19. const D = `${_date.getDate() + 1 < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
  20. const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
  21. const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
  22. const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
  23. return `${Y}-${M}-${D} ${H}:${m}:${s}`;
  24. }
  25. export const YMD = (date: string | number | Date) => {
  26. const _date = new Date(date)
  27. const Y = `${_date.getFullYear()}`;
  28. const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  29. const D = `${_date.getDate() + 1 < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
  30. return `${Y}-${M}-${D}`;
  31. }
  32. export const comTime = (time: any) => {
  33. const sAll = time
  34. const d = Math.floor(sAll / (1000 * 60 * 60 * 24))
  35. const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  36. const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60))
  37. const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000)
  38. return{
  39. d, h, m ,s
  40. }
  41. }
  42. export const comTimeByArea = (start: string | number | Date, end: string | number | Date) => {
  43. const sAll = new Date(end).getTime() - new Date(start).getTime()
  44. const d = Math.floor(sAll / (1000 * 60 * 60 * 24))
  45. const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  46. const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60))
  47. const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000)
  48. return{
  49. d, h, m ,s
  50. }
  51. }
  52. export const deepAssign = (...obj: Object[]) => {
  53. const result = Object.assign({}, ...obj)
  54. for (let item of obj) {
  55. for (let [idx, val] of Object.entries(item)) {
  56. if (val instanceof Array) {
  57. result[idx] = val
  58. } else if (val instanceof Object) {
  59. result[idx] = deepAssign(result[idx], val)
  60. }
  61. }
  62. }
  63. return result
  64. }
  65. export const copy = (value: string) => {
  66. const str = document.createElement('input')
  67. str.setAttribute('value', value)
  68. document.body.appendChild(str)
  69. str.select()
  70. document.execCommand('copy')
  71. document.body.removeChild(str)
  72. console.log(value)
  73. }
  74. export const downloadFile = ({ data, headers }: any, fName: string = '') => {
  75. let fileName = /.*filename=(.*)/i.exec(headers["content-disposition"])?.[1] || '下载';
  76. if (fName) {
  77. fileName = `${fName}${fileName.substring(fileName.indexOf('.'), fileName.length)}`
  78. }
  79. const a = document.createElement("a");
  80. a.style.display = "none";
  81. const url = (window.URL || window.webkitURL).createObjectURL(
  82. new Blob([data], {
  83. type: headers["content-type"]
  84. })
  85. );
  86. a.href = url
  87. a.download = decodeURIComponent((fileName));
  88. a.dispatchEvent(new MouseEvent("click"));
  89. (window.URL || window.webkitURL).revokeObjectURL(url);
  90. }
  91. export const formatUrlByInfo = (url: string) => {
  92. let _url = url
  93. const _date = new Date()
  94. const Y = `${_date.getFullYear()}`;
  95. const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  96. const D = `${_date.getDate() + 1 < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
  97. const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
  98. const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
  99. const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
  100. const mapper = {
  101. "${token}": localStorage.getItem("sc_token"),
  102. "${displayName}": store.state.app.userInfo.displayName,
  103. "${username}": store.state.app.userInfo.username,
  104. "${Y}": Y,
  105. "${M}": M,
  106. "${D}": D,
  107. "${H}": H,
  108. "${m}": m,
  109. "${s}": s,
  110. }
  111. Object.entries(mapper).forEach(([k, v]: any, i) => {
  112. if (_url.includes(k)) {
  113. const before = _url.split(k)[0]
  114. const after = _url.split(k)[1]
  115. _url = before + v + after
  116. }
  117. })
  118. return _url
  119. }