import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router' import type { App, Plugin } from 'vue' import { unref } from 'vue' import { isObject } from '/@/utils/is' export const noop = () => {} /** * @description: Set ui mount node */ export function getPopupContainer(node?: HTMLElement): HTMLElement { return (node?.parentNode as HTMLElement) ?? document.body } /** * Add the object as a parameter to the URL * @param baseUrl url * @param obj * @returns {string} * eg: * let obj = {a: '3', b: '4'} * setObjToUrlParams('www.baidu.com', obj) * ==>www.baidu.com?a=3&b=4 */ export function setObjToUrlParams(baseUrl: string, obj: any): string { let parameters = '' for (const key in obj) { parameters += key + '=' + encodeURIComponent(obj[key]) + '&' } parameters = parameters.replace(/&$/, '') return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters } export function deepMerge(src: any = {}, target: any = {}): T { let key: string for (key in target) { src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]) } return src } export function openWindow( url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }, ) { const { target = '__blank', noopener = true, noreferrer = true } = opt || {} const feature: string[] = [] noopener && feature.push('noopener=yes') noreferrer && feature.push('noreferrer=yes') window.open(url, target, feature.join(',')) } // dynamic use hook props export function getDynamicProps(props: T): Partial { const ret: Recordable = {} Object.keys(props).map((key) => { ret[key] = unref((props as Recordable)[key]) }) return ret as Partial } export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized { if (!route) return route const { matched, ...opt } = route return { ...opt, matched: (matched ? matched.map((item) => ({ meta: item.meta, name: item.name, path: item.path, })) : undefined) as RouteRecordNormalized[], } } export const withInstall = (component: T, alias?: string) => { const comp = component as any comp.install = (app: App) => { app.component(comp.name || comp.displayName, component) if (alias) { app.config.globalProperties[alias] = component } } return component as T & Plugin } /** * Parse the time to string * @param {(Object|string|number)} time * @param {string} cFormat * @returns {string} */ export function parseTime(time: any, cFormat: string) { if (arguments.length === 0) { return null } if (!time) { return '' } const format = cFormat || '{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) } if (typeof time === 'number' && time.toString().length === 10) { time = time * 1000 } date = new Date(time) } const formatObj: { [key: string]: any } = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay(), }, time_str = 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 time_str } /** * 获取预览地址 * */ export function getObjectUrl(file: Blob | MediaSource) { let url = null if ((window as any).createObjectURL !== undefined) { url = (window as any).createObjectURL(file) } else if (window.webkitURL !== undefined) { try { url = window.webkitURL.createObjectURL(file) } catch (er) { console.log(er) } } else if (window.URL !== undefined) { try { url = window.URL.createObjectURL(file) } catch (er) { console.log(902, er) } } return url } /** * 计算距离现在过去了多久 * @param {string} time * @returns {string} */ export function diffTime(time: string) { const startTime = new Date(time).valueOf() // 包含时间的日期字符串 const nowTime = new Date().valueOf(), Day = 24 * 60 * 60 * 1000, Hours = 60 * 60 * 1000, Minutes = 60 * 1000, theDiffDate = nowTime - startTime, diffDay = Math.floor(theDiffDate / Day), diffHours = Math.floor(theDiffDate / Hours), diffMinutes = Math.floor(theDiffDate / Minutes) if (diffDay != 0 && diffDay < 7) { return diffDay + '天前' } else if (diffDay == 0 && diffHours != 0) { return diffHours + '小时前' } else if (diffDay == 0 && diffHours == 0 && diffMinutes != 0) { return diffMinutes + '分钟前' } else if (diffDay == 0 && diffHours == 0 && diffMinutes == 0) { return '刚刚' } else { return time } }