util.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * 存储localStorage
  3. */
  4. export const setStore = (name, content) => {
  5. if (!name) return
  6. if (typeof content !== 'string') {
  7. content = JSON.stringify(content)
  8. }
  9. window.localStorage.setItem(name, content)
  10. }
  11. /**
  12. * 获取localStorage
  13. */
  14. export const getStore = name => {
  15. if (!name) return
  16. return window.localStorage.getItem(name)
  17. }
  18. /**
  19. * 删除localStorage
  20. */
  21. export const removeStore = name => {
  22. if (!name) return
  23. window.localStorage.removeItem(name)
  24. }
  25. /**
  26. * 获取style样式
  27. */
  28. export const getStyle = (element, attr, NumberMode = 'int') => {
  29. let target
  30. // scrollTop 获取方式不同,没有它不属于style,而且只有document.body才能用
  31. if (attr === 'scrollTop') {
  32. target = element.scrollTop
  33. } else if (element.currentStyle) {
  34. target = element.currentStyle[attr]
  35. } else {
  36. target = document.defaultView.getComputedStyle(element, null)[attr]
  37. }
  38. // 在获取 opactiy 时需要获取小数 parseFloat
  39. return NumberMode === 'float' ? parseFloat(target) : parseInt(target)
  40. }
  41. /**
  42. * 数组分组
  43. * 如[1,1,1,1,1,1,1,1] => [[1,1,1],[1,1,1],[1,1]]
  44. */
  45. export const group = (array, subGroupLength) => {
  46. let index = 0
  47. let newArray = []
  48. while (index < array.length) {
  49. newArray.push(array.slice(index, index += subGroupLength))
  50. }
  51. return newArray
  52. }
  53. /**
  54. * 在元素后插入
  55. */
  56. export const insertAfter = (newElement, targetElement) => {
  57. let parent = targetElement.parentNode
  58. if (parent.lastChild === targetElement) {
  59. parent.appendChild(newElement)
  60. } else {
  61. parent.insertBefore(newElement, targetElement.nextSibling)
  62. }
  63. }
  64. /**
  65. * 格式化时间
  66. * @param {格式化格式} fmt 如 yyyy-MM-dd hh:mm:ss
  67. * @param {时间对象} date
  68. */
  69. export const Format = (fmt, date) => {
  70. let o = {
  71. 'M+': date.getMonth() + 1, // 月份
  72. 'd+': date.getDate(), // 日
  73. 'h+': date.getHours(), // 小时
  74. 'm+': date.getMinutes(), // 分
  75. 's+': date.getSeconds(), // 秒
  76. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  77. 'S': date.getMilliseconds() // 毫秒
  78. }
  79. if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) }
  80. for (var k in o) {
  81. if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
  82. }
  83. return fmt
  84. }
  85. /**
  86. * 只执行一次
  87. * @param {*} fn 要执行的函数
  88. */
  89. export const once = fn => {
  90. let flag = false
  91. return function () {
  92. if (flag) return
  93. fn.apply(this, arguments)
  94. flag = true
  95. }
  96. }
  97. /*
  98. * 判断数据类型函数的生产函数,传入的参数为数据类型,返回一个判断传入的参数是否为该类型的函数
  99. */
  100. const isType = type => target => `[object ${type}]` === Object.prototype.toString.call(target)
  101. export const isString = isType('String')
  102. export const isObject = isType('Object')
  103. export const isArray = isType('Array')
  104. export const dealBloToURL = (blob, name) => {
  105. // const url = window.URL.createObjectURL(blob)
  106. // window.open(url)
  107. const href = window.URL.createObjectURL(blob)
  108. // window.open(url)
  109. // downloadElement.download = 'wudi'
  110. let downloadElement = document.createElement('a')
  111. downloadElement.href = href
  112. downloadElement.download = name
  113. document.body.appendChild(downloadElement)
  114. downloadElement.click()
  115. // document.body.removeChild(downloadElement)
  116. // window.URL.revokeObjectURL(href)
  117. }
  118. // 判断今天是否在某个时间段内 Sdate:开始时间,结束时间
  119. export const isDuringDate = (Sdate, Edate) => {
  120. var today = new Date()
  121. var beginDate = new Date(Sdate)
  122. var endDate = new Date(Edate)
  123. if (today >= beginDate && today <= endDate) {
  124. return true
  125. }
  126. return false
  127. }