util.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  44. * 数组转树形结构
  45. * @param {array} list 被转换的数组
  46. * @param {number|string} root 根节点(最外层节点)的 id
  47. * @return array
  48. */
  49. export function arrayToTree (list, root) {
  50. const result = [] // 用于存放结果
  51. const map = {} // 用于存放 list 下的节点
  52. // 1. 遍历 list,将 list 下的所有节点以 id 作为索引存入 map
  53. for (const item of list) {
  54. map[item.id] = { ...item } // 浅拷贝
  55. }
  56. // 2. 再次遍历,将根节点放入最外层,子节点放入父节点
  57. for (const item of list) {
  58. // 3. 获取节点的 id 和 父 id
  59. const { id, parentId } = item // ES6 解构赋值
  60. // 4. 如果是根节点,存入 result
  61. if (item.parentId == root) {
  62. result.push(map[id])
  63. } else {
  64. // 5. 反之,存入到父节点
  65. map[parentId].children
  66. ? map[parentId].children.push(map[id])
  67. : (map[parentId].children = [map[id]])
  68. }
  69. }
  70. // 将结果返回
  71. return result
  72. }
  73. /**
  74. * Remove loading animate
  75. * @param id parent element id or class
  76. * @param timeout
  77. */
  78. export function removeLoadingAnimate (id = '', timeout = 1500) {
  79. if (id === '') {
  80. return
  81. }
  82. setTimeout(() => {
  83. document.body.removeChild(document.getElementById(id))
  84. }, timeout)
  85. }
  86. /**
  87. * @param {String|Number} value 要验证的字符串或数值
  88. * @param {*} validList 用来验证的列表
  89. */
  90. export function oneOf (value, validList) {
  91. for (let i = 0; i < validList.length; i++) {
  92. if (value === validList[i]) {
  93. return true
  94. }
  95. }
  96. return false
  97. }
  98. // 选择文本
  99. export function onTextSelected ($copyText, $message) {
  100. const textDiv = document.getElementsByClassName('copy-rect')
  101. const selection = window.getSelection()
  102. if (!selection) {
  103. return
  104. }
  105. if (selection.rangeCount <= 0) {
  106. return
  107. }
  108. const range = selection.getRangeAt(0)
  109. if (!range) {
  110. return
  111. }
  112. const rect = range.getBoundingClientRect()
  113. if (!rect) {
  114. return
  115. }
  116. if (rect.width <= 0) {
  117. return
  118. }
  119. const wholeText = range.startContainer.wholeText
  120. if (!wholeText) {
  121. return
  122. }
  123. if (wholeText && wholeText.length <= 0) {
  124. return
  125. }
  126. const str = wholeText.slice(range.startOffset, range.endOffset)
  127. if (!str) {
  128. return
  129. }
  130. let copyNode = textDiv[0]
  131. if (!copyNode) {
  132. if (str.length > 0) {
  133. copyNode = document.createElement('div')
  134. copyNode.innerHTML = '复制'
  135. copyNode.className = 'copy-rect'
  136. copyNode.style.position = 'fixed'
  137. copyNode.style.top = rect.top - rect.height - 10 + 'px'
  138. copyNode.style.left = rect.left + rect.width - 20 + 'px'
  139. copyNode.style.height = 'auto'
  140. copyNode.style.width = 'auto'
  141. document.body.appendChild(copyNode)
  142. copyNode.addEventListener('mousedown', (event) => {
  143. console.log(event)
  144. event.stopPropagation()
  145. console.log(str)
  146. $copyText(str).then(message => {
  147. console.log('copy', message)
  148. $message.success('复制完毕')
  149. document.body.removeChild(copyNode)
  150. }).catch(err => {
  151. console.log('copy.err', err)
  152. $message.error('复制失败')
  153. document.body.removeChild(copyNode)
  154. })
  155. })
  156. }
  157. }
  158. }