mart-admin/src/utils/other.ts

120 lines
2.9 KiB
TypeScript
Raw Normal View History

import { nextTick } from 'vue';
import type { App } from 'vue';
import * as svg from '@element-plus/icons-vue';
import router from '/@/router/index';
import { store } from '/@/store/index';
import { i18n } from '/@/i18n/index';
import { Local } from '/@/utils/storage';
import SvgIcon from '/@/components/svgIcon/index.vue';
/**
* element plus svg
* @param app vue
* @description 使https://element-plus.gitee.io/zh-CN/component/icon.html
*/
export function elSvg(app: App) {
const icons = svg as any;
for (const i in icons) {
app.component(`element${icons[i].name}`, icons[i]);
}
app.component('SvgIcon', SvgIcon);
}
/**
*
* @method const title = useTitle(); ==> title()
*/
export function useTitle() {
nextTick(() => {
let webTitle = '';
let globalTitle: string = store.state.themeConfig.themeConfig.globalTitle;
router.currentRoute.value.path === '/login'
? (webTitle = router.currentRoute.value.meta.title as any)
: (webTitle = i18n.global.t(router.currentRoute.value.meta.title as any));
document.title = `${webTitle} - ${globalTitle}` || globalTitle;
});
}
/**
*
* @param el dom
* @param arr
* @description data-xxx
*/
export const lazyImg = (el: any, arr: any) => {
const io = new IntersectionObserver((res) => {
res.forEach((v: any) => {
if (v.isIntersecting) {
const { img, key } = v.target.dataset;
v.target.src = img;
v.target.onload = () => {
io.unobserve(v.target);
arr[key]['loading'] = false;
};
}
});
});
nextTick(() => {
document.querySelectorAll(el).forEach((img) => io.observe(img));
});
};
/**
*
* @returns `window.localStorage` `globalComponentSize`
*/
export function globalComponentSize() {
return Local.get('themeConfig')?.globalComponentSize;
}
/**
*
* @param obj
* @returns
*/
export function deepClone(obj: any) {
let newObj: any;
try {
newObj = obj.push ? [] : {};
} catch (error) {
newObj = {};
}
for (let attr in obj) {
if (typeof obj[attr] === 'object') {
newObj[attr] = deepClone(obj[attr]);
} else {
newObj[attr] = obj[attr];
}
}
return newObj;
}
/**
*
* @method elSvg element plus svg
* @method useTitle
* @method lazyImg
* @method globalComponentSize element plus
* @method deepClone
*/
const other = {
elSvg: (app: App) => {
elSvg(app);
},
useTitle: () => {
useTitle();
},
lazyImg: (el: any, arr: any) => {
lazyImg(el, arr);
},
globalComponentSize: () => {
globalComponentSize();
},
deepClone: (obj: any) => {
deepClone(obj);
},
};
// 统一批量导出
export default other;