import moment from 'moment'; import cloneDeep from 'lodash/cloneDeep'; import navData from '../common/nav'; export function fixedZero(val) { return val * 1 < 10 ? `0${val}` : val; } export function getTimeDistance(type) { const now = new Date(); const oneDay = 1000 * 60 * 60 * 24; if (type === 'today') { now.setHours(0); now.setMinutes(0); now.setSeconds(0); return [moment(now), moment(now.getTime() + (oneDay - 1000))]; } if (type === 'week') { let day = now.getDay(); now.setHours(0); now.setMinutes(0); now.setSeconds(0); if (day === 0) { day = 6; } else { day -= 1; } const beginTime = now.getTime() - (day * oneDay); return [moment(beginTime), moment(beginTime + ((7 * oneDay) - 1000))]; } if (type === 'month') { const year = now.getFullYear(); const month = now.getMonth(); const nextDate = moment(now).add(1, 'months'); const nextYear = nextDate.year(); const nextMonth = nextDate.month(); return [moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`), moment(new Date(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).getTime() - 1000)]; } if (type === 'year') { const year = now.getFullYear(); return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)]; } } function getPlainNode(nodeList, parentPath = '') { const arr = []; nodeList.forEach((node) => { const item = node; item.path = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/'); item.exact = true; if (item.children && !item.component) { arr.push(...getPlainNode(item.children, item.path)); } else { if (item.children && item.component) { item.exact = false; } arr.push(item); } }); return arr; } export function getRouteData(path) { if (!navData.some(item => item.layout === path) || !(navData.filter(item => item.layout === path)[0].children)) { return null; } const dataList = cloneDeep(navData.filter(item => item.layout === path)[0]); const nodeList = getPlainNode(dataList.children); return nodeList; }