util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. export function timeFix () {
  2. const time = new Date()
  3. const hour = time.getHours()
  4. return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
  5. }
  6. export function welcome () {
  7. const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  8. const index = Math.floor(Math.random() * arr.length)
  9. return arr[index]
  10. }
  11. /**
  12. * 触发 window.resize
  13. */
  14. export function triggerWindowResizeEvent () {
  15. const event = document.createEvent('HTMLEvents')
  16. event.initEvent('resize', true, true)
  17. event.eventType = 'message'
  18. window.dispatchEvent(event)
  19. }
  20. export function handleScrollHeader (callback) {
  21. let timer = 0
  22. let beforeScrollTop = window.pageYOffset
  23. callback = callback || function () {}
  24. window.addEventListener(
  25. 'scroll',
  26. event => {
  27. clearTimeout(timer)
  28. timer = setTimeout(() => {
  29. let direction = 'up'
  30. const afterScrollTop = window.pageYOffset
  31. const delta = afterScrollTop - beforeScrollTop
  32. if (delta === 0) {
  33. return false
  34. }
  35. direction = delta > 0 ? 'down' : 'up'
  36. callback(direction)
  37. beforeScrollTop = afterScrollTop
  38. }, 50)
  39. },
  40. false
  41. )
  42. }
  43. export function isIE () {
  44. const bw = window.navigator.userAgent
  45. const compare = (s) => bw.indexOf(s) >= 0
  46. const ie11 = (() => 'ActiveXObject' in window)()
  47. return compare('MSIE') || ie11
  48. }
  49. /**
  50. * Remove loading animate
  51. * @param id parent element id or class
  52. * @param timeout
  53. */
  54. export function removeLoadingAnimate (id = '', timeout = 1500) {
  55. if (id === '') {
  56. return
  57. }
  58. setTimeout(() => {
  59. document.body.removeChild(document.getElementById(id))
  60. }, timeout)
  61. }
  62. /**
  63. * 随机生成数字
  64. * @param min 最小值
  65. * @param max 最大值
  66. * @return int 生成后的数字
  67. */
  68. export function randomNumber (min, max) {
  69. return Math.floor(Math.random() * (max - min + 1) + min)
  70. }
  71. /**
  72. * 随机生成字符串
  73. * @param length 字符串的长度
  74. * @param chats 可选字符串区间(只会生成传入的字符串中的字符)
  75. * @return string 生成的字符串
  76. */
  77. export function randomString (length, chats) {
  78. if (!length) length = 1
  79. if (!chats) chats = '0123456789qwertyuioplkjhgfdsazxcvbnm'
  80. let str = ''
  81. for (let i = 0; i < length; i++) {
  82. const num = randomNumber(0, chats.length - 1)
  83. str += chats[num]
  84. }
  85. return str
  86. }
  87. /**
  88. * 随机生成uuid
  89. * @return string 生成的uuid
  90. */
  91. export function randomUUID () {
  92. const chats = '0123456789abcdef'
  93. return randomString(32, chats)
  94. }
  95. /**
  96. * json对象单层,转换url参数
  97. * @param {*} paramObj
  98. */
  99. export function formateObjToParamStr (paramObj) {
  100. const sdata = []
  101. for (const attr in paramObj) {
  102. sdata.push(`${attr} = ${filter(paramObj[attr])}`)
  103. }
  104. return sdata.join('&')
  105. }
  106. // 过滤特殊符号
  107. function filter (str) {
  108. // 特殊字符转义
  109. str += '' // 隐式转换
  110. str = str.replace(/%/g, '%25')
  111. str = str.replace(/\+/g, '%2B')
  112. str = str.replace(/ /g, '%20')
  113. str = str.replace(/\//g, '%2F')
  114. str = str.replace(/\?/g, '%3F')
  115. str = str.replace(/&/g, '%26')
  116. str = str.replace(/=/g, '%3D')
  117. str = str.replace(/#/g, '%23')
  118. return str
  119. }