ruoyi.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * 通用js方法封装处理
  3. * Copyright (c) 2019 ruoyi
  4. */
  5. // 日期格式化
  6. export function parseTime(time, pattern) {
  7. if (arguments.length === 0 || !time) {
  8. return null
  9. }
  10. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  11. let date
  12. if (typeof time === 'object') {
  13. date = time
  14. } else {
  15. if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
  16. time = parseInt(time)
  17. } else if (typeof time === 'string') {
  18. time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
  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. // Note: getDay() returns 0 on Sunday
  37. if (key === 'a') {
  38. return ['日', '一', '二', '三', '四', '五', '六'][value]
  39. }
  40. if (result.length > 0 && value < 10) {
  41. value = '0' + value
  42. }
  43. return value || 0
  44. })
  45. return time_str
  46. }
  47. /**
  48. * 构造树型结构数据
  49. * @param {*} data 数据源
  50. * @param {*} id id字段 默认 'id'
  51. * @param {*} parentId 父节点字段 默认 'parentId'
  52. * @param {*} children 孩子节点字段 默认 'children'
  53. * @param {*} rootId 根Id 默认 0
  54. */
  55. export function handleTree(data, id, parentId, children, rootId) {
  56. id = id || 'id'
  57. parentId = parentId || 'parentId'
  58. children = children || 'children'
  59. rootId = rootId || Math.min.apply(Math, data.map(item => {
  60. return item[parentId]
  61. })) || 0
  62. //对源数据深度克隆
  63. const cloneData = JSON.parse(JSON.stringify(data))
  64. //循环所有项
  65. const treeData = cloneData.filter(father => {
  66. let branchArr = cloneData.filter(child => {
  67. //返回每一项的子级数组
  68. return father[id] === child[parentId]
  69. });
  70. branchArr.length > 0 ? father.children = branchArr : '';
  71. //返回第一层
  72. return father[parentId] === rootId;
  73. });
  74. return treeData !== '' ? treeData : data;
  75. }
  76. /**
  77. * 获取文件类型
  78. * @param {String} fileName 文件名称
  79. * @returns {String} fileType => '', image, video, audio, office, unknown
  80. */
  81. export function getFileType(fileName = '') {
  82. const flieArr = fileName.split('.');
  83. let suffix = flieArr[flieArr.length - 1];
  84. if (!suffix) return '';
  85. suffix = suffix.toLocaleLowerCase();
  86. const image = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp'];
  87. if (image.includes(suffix)) return 'image';
  88. const video = ['mp4', 'm4v'];
  89. if (video.includes(suffix)) return 'video';
  90. const audio = ['mp3', 'm4a', 'wav', 'aac'];
  91. if (audio.includes(suffix)) return 'audio';
  92. const office = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'plain'];
  93. if (office.includes(suffix)) return 'office';
  94. return 'unknown';
  95. }