import store from '@/store/index' export const isValue = (val: any) => { if (val === null || val === undefined || val === '') { return false } return true } export const arrayToMap = (array: Array, key: any) => { const map = new Map() array.forEach((v: any) => { map.set(v[key], v) }) return map } export const YMDHms = (date: string | number | Date) => { const _date = new Date(date) const Y = `${_date.getFullYear()}`; const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`; const D = `${_date.getDate() + 1 < 10 ? `0${_date.getDate()}` : _date.getDate()}`; const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`; const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`; const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds(); return `${Y}-${M}-${D} ${H}:${m}:${s}`; } export const YMD = (date: string | number | Date) => { const _date = new Date(date) const Y = `${_date.getFullYear()}`; const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`; const D = `${_date.getDate() + 1 < 10 ? `0${_date.getDate()}` : _date.getDate()}`; return `${Y}-${M}-${D}`; } export const comTime = (time: any) => { const sAll = time const d = Math.floor(sAll / (1000 * 60 * 60 * 24)) const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60)) const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000) return{ d, h, m ,s } } export const comTimeByArea = (start: string | number | Date, end: string | number | Date) => { const sAll = new Date(end).getTime() - new Date(start).getTime() const d = Math.floor(sAll / (1000 * 60 * 60 * 24)) const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60)) const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000) return{ d, h, m ,s } } export const deepAssign = (...obj: Object[]) => { const result = Object.assign({}, ...obj) for (let item of obj) { for (let [idx, val] of Object.entries(item)) { if (val instanceof Array) { result[idx] = val } else if (val instanceof Object) { result[idx] = deepAssign(result[idx], val) } } } return result } export const copy = (value: string) => { const str = document.createElement('input') str.setAttribute('value', value) document.body.appendChild(str) str.select() document.execCommand('copy') document.body.removeChild(str) console.log(value) } export const downloadFile = ({ data, headers }: any, fName: string = '') => { let fileName = /.*filename=(.*)/i.exec(headers["content-disposition"])?.[1] || '下载'; if (fName) { fileName = `${fName}${fileName.substring(fileName.indexOf('.'), fileName.length)}` } const a = document.createElement("a"); a.style.display = "none"; const url = (window.URL || window.webkitURL).createObjectURL( new Blob([data], { type: headers["content-type"] }) ); a.href = url a.download = decodeURIComponent((fileName)); a.dispatchEvent(new MouseEvent("click")); (window.URL || window.webkitURL).revokeObjectURL(url); } export const formatUrlByInfo = (url: string) => { let _url = url const _date = new Date() const Y = `${_date.getFullYear()}`; const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`; const D = `${_date.getDate() + 1 < 10 ? `0${_date.getDate()}` : _date.getDate()}`; const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`; const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`; const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds(); const mapper = { "${token}": localStorage.getItem("sc_token"), "${displayName}": store.state.app.userInfo.displayName, "${username}": store.state.app.userInfo.username, "${Y}": Y, "${M}": M, "${D}": D, "${H}": H, "${m}": m, "${s}": s, } Object.entries(mapper).forEach(([k, v]: any, i) => { if (_url.includes(k)) { const before = _url.split(k)[0] const after = _url.split(k)[1] _url = before + v + after } }) return _url }