index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  3. * @description 格式化时间
  4. * @param time
  5. * @param cFormat
  6. * @returns {string|null}
  7. */
  8. export function parseTime(time, cFormat) {
  9. if (arguments.length === 0) {
  10. return null
  11. }
  12. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  13. let date
  14. if (typeof time === 'object') {
  15. date = time
  16. } else {
  17. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  18. time = parseInt(time)
  19. }
  20. if (typeof time === 'number' && time.toString().length === 10) {
  21. time = time * 1000
  22. }
  23. date = new Date(time)
  24. }
  25. const formatObj = {
  26. y: date.getFullYear(),
  27. m: date.getMonth() + 1,
  28. d: date.getDate(),
  29. h: date.getHours(),
  30. i: date.getMinutes(),
  31. s: date.getSeconds(),
  32. a: date.getDay(),
  33. }
  34. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  35. let value = formatObj[key]
  36. if (key === 'a') {
  37. return ['日', '一', '二', '三', '四', '五', '六'][value]
  38. }
  39. if (result.length > 0 && value < 10) {
  40. value = '0' + value
  41. }
  42. return value || 0
  43. })
  44. return time_str
  45. }
  46. /**
  47. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  48. * @description 格式化时间
  49. * @param time
  50. * @param option
  51. * @returns {string}
  52. */
  53. export function formatTime(time, option) {
  54. if (('' + time).length === 10) {
  55. time = parseInt(time) * 1000
  56. } else {
  57. time = +time
  58. }
  59. const d = new Date(time)
  60. const now = Date.now()
  61. const diff = (now - d) / 1000
  62. if (diff < 30) {
  63. return '刚刚'
  64. } else if (diff < 3600) {
  65. // less 1 hour
  66. return Math.ceil(diff / 60) + '分钟前'
  67. } else if (diff < 3600 * 24) {
  68. return Math.ceil(diff / 3600) + '小时前'
  69. } else if (diff < 3600 * 24 * 2) {
  70. return '1天前'
  71. }
  72. if (option) {
  73. return parseTime(time, option)
  74. } else {
  75. return (
  76. d.getMonth() +
  77. 1 +
  78. '月' +
  79. d.getDate() +
  80. '日' +
  81. d.getHours() +
  82. '时' +
  83. d.getMinutes() +
  84. '分'
  85. )
  86. }
  87. }
  88. /**
  89. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  90. * @description 将url请求参数转为json格式
  91. * @param url
  92. * @returns {{}|any}
  93. */
  94. export function paramObj(url) {
  95. const search = url.split('?')[1]
  96. if (!search) {
  97. return {}
  98. }
  99. return JSON.parse(
  100. '{"' +
  101. decodeURIComponent(search)
  102. .replace(/"/g, '\\"')
  103. .replace(/&/g, '","')
  104. .replace(/=/g, '":"')
  105. .replace(/\+/g, ' ') +
  106. '"}'
  107. )
  108. }
  109. /**
  110. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  111. * @description 父子关系的数组转换成树形结构数据
  112. * @param data
  113. * @returns {*}
  114. */
  115. export function translateDataToTree(data) {
  116. const parent = data.filter(
  117. (value) => value.parentId === 'undefined' || value.parentId == null
  118. )
  119. const children = data.filter(
  120. (value) => value.parentId !== 'undefined' && value.parentId != null
  121. )
  122. const translator = (parent, children) => {
  123. parent.forEach((parent) => {
  124. children.forEach((current, index) => {
  125. if (current.parentId === parent.id) {
  126. const temp = JSON.parse(JSON.stringify(children))
  127. temp.splice(index, 1)
  128. translator([current], temp)
  129. typeof parent.children !== 'undefined'
  130. ? parent.children.push(current)
  131. : (parent.children = [current])
  132. }
  133. })
  134. })
  135. }
  136. translator(parent, children)
  137. return parent
  138. }
  139. /**
  140. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  141. * @description 树形结构数据转换成父子关系的数组
  142. * @param data
  143. * @returns {[]}
  144. */
  145. export function translateTreeToData(data) {
  146. const result = []
  147. data.forEach((item) => {
  148. const loop = (data) => {
  149. result.push({
  150. id: data.id,
  151. name: data.name,
  152. parentId: data.parentId,
  153. })
  154. const child = data.children
  155. if (child) {
  156. for (let i = 0; i < child.length; i++) {
  157. loop(child[i])
  158. }
  159. }
  160. }
  161. loop(item)
  162. })
  163. return result
  164. }
  165. /**
  166. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  167. * @description 10位时间戳转换
  168. * @param time
  169. * @returns {string}
  170. */
  171. export function tenBitTimestamp(time) {
  172. const date = new Date(time * 1000)
  173. const y = date.getFullYear()
  174. let m = date.getMonth() + 1
  175. m = m < 10 ? '' + m : m
  176. let d = date.getDate()
  177. d = d < 10 ? '' + d : d
  178. let h = date.getHours()
  179. h = h < 10 ? '0' + h : h
  180. let minute = date.getMinutes()
  181. let second = date.getSeconds()
  182. minute = minute < 10 ? '0' + minute : minute
  183. second = second < 10 ? '0' + second : second
  184. return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合
  185. }
  186. /**
  187. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  188. * @description 13位时间戳转换
  189. * @param time
  190. * @returns {string}
  191. */
  192. export function thirteenBitTimestamp(time) {
  193. const date = new Date(time / 1)
  194. const y = date.getFullYear()
  195. let m = date.getMonth() + 1
  196. m = m < 10 ? '' + m : m
  197. let d = date.getDate()
  198. d = d < 10 ? '' + d : d
  199. let h = date.getHours()
  200. h = h < 10 ? '0' + h : h
  201. let minute = date.getMinutes()
  202. let second = date.getSeconds()
  203. minute = minute < 10 ? '0' + minute : minute
  204. second = second < 10 ? '0' + second : second
  205. return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合
  206. }
  207. /**
  208. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  209. * @description 获取随机id
  210. * @param length
  211. * @returns {string}
  212. */
  213. export function uuid(length = 32) {
  214. const num = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  215. let str = ''
  216. for (let i = 0; i < length; i++) {
  217. str += num.charAt(Math.floor(Math.random() * num.length))
  218. }
  219. return str
  220. }
  221. /**
  222. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  223. * @description m到n的随机数
  224. * @param m
  225. * @param n
  226. * @returns {number}
  227. */
  228. export function random(m, n) {
  229. return Math.floor(Math.random() * (m - n) + n)
  230. }
  231. /**
  232. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  233. * @description addEventListener
  234. * @type {function(...[*]=)}
  235. */
  236. export const on = (function () {
  237. return function (element, event, handler, useCapture = false) {
  238. if (element && event && handler) {
  239. element.addEventListener(event, handler, useCapture)
  240. }
  241. }
  242. })()
  243. /**
  244. * @author chuzhixin 1204505056@qq.com (不想保留author可删除)
  245. * @description removeEventListener
  246. * @type {function(...[*]=)}
  247. */
  248. export const off = (function () {
  249. return function (element, event, handler, useCapture = false) {
  250. if (element && event) {
  251. element.removeEventListener(event, handler, useCapture)
  252. }
  253. }
  254. })()