import { download } from '@/utils/request' /** * 获取assets文件夹下的静态图片资源 * @param url // 文件路径 */ export const getAssetsFile = (url) => new URL(`../assets/images/${url}`, import.meta.url).href // 日期格式化 export function parseTime(time, pattern) { if (arguments.length === 0 || !time) { return null } const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if (typeof time === 'string' && /^[0-9]+$/.test(time)) { time = parseInt(time) } else if (typeof time === 'string') { time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '') } if (typeof time === 'number' && time.toString().length === 10) { time *= 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return [ '日', '一', '二', '三', '四', '五', '六' ][value] } if (result.length > 0 && value < 10) { value = `0${value}` } return value || 0 }) return timeStr } /** * 参数处理 * @param {*} params 参数 */ export const tansParams = (params) => { let result = '' for (const propName of Object.keys(params)) { const value = params[propName] let part = encodeURIComponent(propName) + '=' if (value !== null && value !== '' && typeof value !== 'undefined') { if (typeof value === 'object') { for (const key of Object.keys(value)) { if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') { let params = propName + '[' + key + ']' let subPart = encodeURIComponent(params) + '=' result += subPart + encodeURIComponent(value[key]) + '&' } } } else { result += part + encodeURIComponent(value) + '&' } } } return result } /** * 构造树型结构数据 * @param {*} data 数据源 * @param {*} id id字段 默认 'id' * @param {*} parentId 父节点字段 默认 'parentId' * @param {*} children 孩子节点字段 默认 'children' */ export const handleTree = (data, id, parentId, children) => { let config = { id: id || 'id', parentId: parentId || 'parentId', childrenList: children || 'children' } let childrenListMap = {} let nodeIds = {} let tree = [] for (let d of data) { let parentId = d[config.parentId] if (!childrenListMap[parentId]) { childrenListMap[parentId] = [] } nodeIds[d[config.id]] = d childrenListMap[parentId].push(d) } for (let d of data) { let parentId = d[config.parentId] if (!nodeIds[parentId]) { tree.push(d) } } for (let t of tree) { adaptToChildrenList(t) } function adaptToChildrenList(o) { if (childrenListMap[o[config.id]] !== null) { o[config.childrenList] = childrenListMap[o[config.id]] } if (o[config.childrenList]) { for (let c of o[config.childrenList]) { adaptToChildrenList(c) } } } return tree } // 添加日期范围 export const addDateRange = (params, dateRange, propName) => { let search = params search.params = typeof search.params === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {} dateRange = Array.isArray(dateRange) ? dateRange : [] if (typeof propName === 'undefined') { search.params.beginTime = dateRange[0] search.params.endTime = dateRange[1] } else { search.params['begin' + propName] = dateRange[0] search.params['end' + propName] = dateRange[1] } return search } // 验证是否为blob格式 export function blobValidate(data) { return data.type !== 'application/json' } /** * 判断url是否是http或https * @param {string} url * @returns {Boolean} */ export function isHttp(url) { return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1 } export function downloadFile(url, params, filename, config, method) { return download(url, params, filename, config, method) } /** * 拖拽事件 * 添加到元素上的mousedown事件 @mousedown */ export function dragEvent(e, className, moving, moveEnd) { if (e.target.nodeName === 'INPUT' || e.target.nodeName === 'TEXTAREA' || e.target.nodeName === 'svg' || e.target.nodeName === 'path') { return } if (className && !e.target.className.includes(className)) { return } // if (className && className !== e.target.className) return const divDom = e.target // 获取目标元素 const [ disX, disY ] = [ e.clientX - divDom.offsetLeft, e.clientY - divDom.offsetTop ] document.onmousemove = (e) => { // 鼠标按下并移动的事件//用鼠标的位置减去鼠标相对元素的位置,得到元素的位置 const left = e.clientX - disX const top = e.clientY - disY divDom.style.left = `${left}px` divDom.style.top = `${top}px` if (moving) { moving() } } document.onmouseup = (e) => { document.onmousemove = null document.onmouseup = null if (moveEnd) { moveEnd() } } } /** * @param value 将十进制转为度分秒 * @returns { degrees: number, minutes: number, seconds: number } degrees:度 minutes:分 seconds:秒 */ export const formatDecimalToDegrees = (value) => { const degrees = Math.floor(value) const minutes = Math.floor((value - degrees) * 60) const seconds = (value - degrees - minutes / 60) * 3600 return { degrees: degrees, minutes: minutes, seconds: seconds.toFixed(2) } }