CEES-manage/src/utils/index.ts

172 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-10-20 14:32:09 +08:00
import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
import type { App, Plugin } from 'vue';
import { unref } from 'vue';
import { isObject } from '/@/utils/is';
// update-begin--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开原因是带了#号,需要替换一下
2022-06-10 10:44:44 +08:00
export const URL_HASH_TAB = `__AGWE4H__HASH__TAG__PWHRG__`;
// update-end--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开原因是带了#号,需要替换一下
2021-10-20 14:32:09 +08:00
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;
}
2022-06-10 10:44:44 +08:00
export function openWindow(url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }) {
2021-10-20 14:32:09 +08:00
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>;
}
2022-03-10 09:47:29 +08:00
/**
*
* @param props
* @param field
* @updateBy:zyf
*/
2022-06-10 10:44:44 +08:00
export function getValueType(props, field) {
let formSchema = unref(unref(props)?.schemas);
let valueType = 'string';
2022-03-10 09:47:29 +08:00
if (formSchema) {
let schema = formSchema.filter((item) => item.field === field)[0];
2022-06-10 10:44:44 +08:00
valueType = schema.componentProps && schema.componentProps.valueType ? schema.componentProps.valueType : valueType;
2022-03-10 09:47:29 +08:00
}
return valueType;
}
2021-10-20 14:32:09 +08:00
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[],
};
}
2022-03-10 09:47:29 +08:00
/**
*
* @param obj
* @return
*/
export function cloneObject(obj) {
2022-06-10 10:44:44 +08:00
return JSON.parse(JSON.stringify(obj));
2022-03-10 09:47:29 +08:00
}
2021-10-20 14:32:09 +08:00
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;
};
2022-03-10 09:47:29 +08:00
/**
* url地址参数
* @param paraName
*/
export function getUrlParam(paraName) {
let url = document.location.toString();
2022-06-10 10:44:44 +08:00
let arrObj = url.split('?');
2022-03-10 09:47:29 +08:00
if (arrObj.length > 1) {
2022-06-10 10:44:44 +08:00
let arrPara = arrObj[1].split('&');
2022-03-10 09:47:29 +08:00
let arr;
for (let i = 0; i < arrPara.length; i++) {
2022-06-10 10:44:44 +08:00
arr = arrPara[i].split('=');
2022-03-10 09:47:29 +08:00
if (arr != null && arr[0] == paraName) {
return arr[1];
}
}
2022-06-10 10:44:44 +08:00
return '';
} else {
return '';
2022-03-10 09:47:29 +08:00
}
}
/**
* setTimeout的promise版
* @param ms
* @param fn callback
* @return Promise
*/
export function sleep(ms: number, fn?: Fn) {
2022-06-10 10:44:44 +08:00
return new Promise<void>((resolve) =>
setTimeout(() => {
fn && fn();
resolve();
}, ms)
);
2022-03-10 09:47:29 +08:00
}
/**
*
* @param text
* @param checker
* @param replacer
* @returns {String}
*/
export function replaceAll(text, checker, replacer) {
2022-06-10 10:44:44 +08:00
let lastText = text;
text = text.replace(checker, replacer);
if (lastText !== text) {
2022-06-10 10:44:44 +08:00
return replaceAll(text, checker, replacer);
}
2022-06-10 10:44:44 +08:00
return text;
}