util.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : `0${n}`
  13. }
  14. // 处理省市县数据结构
  15. const createMenu = node => {
  16. var that = this
  17. let obj = Object.keys(node).map((key,index,item)=>{
  18. var elNode = {
  19. label: key,
  20. value: key,
  21. item:node[key],
  22. }
  23. return elNode;
  24. })
  25. obj.forEach(e=>{
  26. if ((e.item) instanceof Array) {
  27. e.children = []
  28. e.item.forEach(c=>{
  29. e.children.push({
  30. label:c,
  31. value:c
  32. })
  33. })
  34. } else {
  35. if (Object.keys(e.item).length !== 0) {
  36. e.children = createMenu(e.item)
  37. }
  38. }
  39. })
  40. return obj
  41. }
  42. module.exports = {
  43. formatTime,
  44. createMenu
  45. }