201 lines
5.2 KiB
TypeScript
201 lines
5.2 KiB
TypeScript
import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'
|
|
import type { App, Plugin } from 'vue'
|
|
|
|
import { unref } from 'vue'
|
|
import { isObject } from '/@/utils/is'
|
|
import dayjs from 'dayjs'
|
|
|
|
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<T = any>(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<T, U>(props: T): Partial<U> {
|
|
const ret: Recordable = {}
|
|
|
|
Object.keys(props).map((key) => {
|
|
ret[key] = unref((props as Recordable)[key])
|
|
})
|
|
|
|
return ret as Partial<U>
|
|
}
|
|
|
|
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 = <T>(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
|
|
}
|
|
export function formatDataByObject(obj: any) {
|
|
const arr: any[] = []
|
|
Object.keys(obj).forEach((e) => {
|
|
arr.push({
|
|
label: obj[e],
|
|
value: e,
|
|
})
|
|
})
|
|
return arr
|
|
}
|
|
|
|
//递归生成树形结构
|
|
export function getTreeData(
|
|
data: any,
|
|
pid: any,
|
|
pidName = 'parentId',
|
|
idName = 'id',
|
|
childrenName = 'children',
|
|
key = 'key',
|
|
condition = 0,
|
|
isNull = true,
|
|
) {
|
|
let arr = []
|
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
if (data[i][pidName] == pid) {
|
|
data[i][key] = data[i][idName]
|
|
data[i]['disabled'] = data[i].is_end == condition ? false : true
|
|
data[i][childrenName] = getTreeData(
|
|
data,
|
|
data[i][idName],
|
|
pidName,
|
|
idName,
|
|
childrenName,
|
|
key,
|
|
condition,
|
|
isNull,
|
|
)
|
|
if (isNull && data[i][childrenName].length == 0) delete data[i][childrenName]
|
|
arr.push(data[i])
|
|
}
|
|
}
|
|
|
|
return arr
|
|
}
|
|
|
|
// 获取近7的时间
|
|
export function getWeek() {
|
|
let toData = new Date(new Date().toLocaleDateString()).getTime()
|
|
//今天
|
|
let todayStart = toData
|
|
//昨天
|
|
let yesterdayStart = toData - 3600 * 24 * 1000
|
|
let yesterdayEnd = yesterdayStart + 24 * 60 * 60 * 1000 - 1
|
|
//最近7天
|
|
let past7daysStart = todayStart - 7 * 3600 * 24 * 1000
|
|
// const date = new Date()
|
|
// const year = date.getFullYear()
|
|
// const month = date.getMonth()
|
|
// const day = date.getDate()
|
|
// const week = date.getDay()
|
|
// //开始时间
|
|
// let WeekStartDate = dayjs(new Date(year, month, day - week + 1)).format('YYYY-MM-DD')
|
|
// // 结束时间
|
|
// let WeekEndDate = dayjs(new Date()).format('YYYY-MM-DD')
|
|
return {
|
|
WeekStartDate: dayjs(past7daysStart).format('YYYY-MM-DD'),
|
|
WeekEndDate: dayjs(yesterdayEnd).format('YYYY-MM-DD'),
|
|
}
|
|
}
|
|
|
|
// 获取近一月的数据
|
|
export function getMonth() {
|
|
let toData = new Date(new Date().toLocaleDateString()).getTime()
|
|
//昨天
|
|
let yesterdayStart = toData - 3600 * 24 * 1000
|
|
let yesterdayEnd = yesterdayStart + 24 * 60 * 60 * 1000 - 1
|
|
//最近30天
|
|
let past30daysStart = toData - 30 * 3600 * 24 * 1000
|
|
// const date = new Date()
|
|
// const year = date.getFullYear()
|
|
// const month = date.getMonth()
|
|
// let MonthStartDate = dayjs(new Date(year, month, 1)).format('YYYY-MM-DD')
|
|
// let MonthEndDate = dayjs(new Date()).format('YYYY-MM-DD')
|
|
return {
|
|
MonthStartDate: dayjs(past30daysStart).format('YYYY-MM-DD'),
|
|
MonthEndDate: dayjs(yesterdayEnd).format('YYYY-MM-DD'),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 是否json字符串
|
|
*/
|
|
export function jsonString(value) {
|
|
if (typeof value === 'string') {
|
|
try {
|
|
const obj = JSON.parse(value)
|
|
if (typeof obj === 'object' && obj) {
|
|
return true
|
|
}
|
|
return false
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
}
|
|
return false
|
|
}
|