utils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. export default {
  2. downloadFile (data, opts, callback) {
  3. if (typeof data === 'string') {
  4. downloadLink(data)
  5. } else {
  6. let _Blob = new Blob([data], { type: opts.type }),
  7. BlobURL = window.URL.createObjectURL(_Blob)
  8. downloadLink(BlobURL)
  9. window.URL.revokeObjectURL(BlobURL)
  10. }
  11. callback && callback()
  12. function downloadLink (url) {
  13. const link = document.createElement('a')
  14. link.href = url
  15. link.download = opts.name || ''
  16. link.style.display = 'none'
  17. document.body.appendChild(link)
  18. link.click()
  19. document.body.removeChild(link)
  20. }
  21. },
  22. copy (data, deep = true) {
  23. return deep ? JSON.parse(JSON.stringify(data)) : Object.assign(data)
  24. },
  25. getDate (dateType, dateString, calculate) {
  26. let result = dateType
  27. let d = dateString ? new Date(dateString) : new Date()
  28. calculate && (d = setDate(d, calculate))
  29. const week = ['日', '一', '二', '三', '四', '五', '六']
  30. if (dateType.length == 0) {
  31. return d.getTime()
  32. }
  33. if (dateType.includes('Y')) {
  34. result = result.replace(/Y+/g, d.getFullYear().toString())
  35. }
  36. if (dateType.includes('y')) {
  37. result = result.replace(/y+/g, (d.getFullYear() + '').slice(2, 4))
  38. }
  39. if (dateType.includes('M')) {
  40. result = result.replace(/M+/g, (101 + d.getMonth() * 1 + '').slice(1, 3))
  41. }
  42. if (dateType.includes('m')) {
  43. result = result.replace(/m+/g, (d.getMonth() * 1 + 1).toString())
  44. }
  45. if (dateType.includes('D')) {
  46. result = result.replace(/D+/g, (100 + d.getDate() * 1 + '').slice(1, 3))
  47. }
  48. if (dateType.includes('d')) {
  49. result = result.replace(/d+/g, d.getDate().toString())
  50. }
  51. if (dateType.includes('H')) {
  52. result = result.replace(/H+/g, (100 + d.getHours() * 1 + '').slice(1, 3))
  53. }
  54. if (dateType.includes('h')) {
  55. result = result.replace(/h+/g, d.getHours().toString())
  56. }
  57. if (dateType.includes('I')) {
  58. result = result.replace(/I+/g, (100 + d.getMinutes() * 1 + '').slice(1, 3))
  59. }
  60. if (dateType.includes('i')) {
  61. result = result.replace(/i+/g, d.getMinutes().toString())
  62. }
  63. if (dateType.includes('S')) {
  64. result = result.replace(/S+/g, (100 + d.getSeconds() * 1 + '').slice(1, 3))
  65. }
  66. if (dateType.includes('s')) {
  67. result = result.replace(/s+/g, d.getSeconds().toString())
  68. }
  69. if (dateType.includes('w') || dateType.includes('W')) {
  70. result = result.replace(/[wW]+/g, week[d.getDay() * 1])
  71. }
  72. return result
  73. function setDate (d, calculate = {}) {
  74. Object.keys(calculate).forEach((v) => {
  75. if (!setFn[v]) {
  76. return
  77. }
  78. let val = Number(calculate[v]) || 0
  79. let getDateMethod = d[getFn[v]]()
  80. let setDateMethod = d[setFn[v]](Number(getDateMethod) + val)
  81. d = new Date(setDateMethod)
  82. })
  83. return d
  84. }
  85. }
  86. }