other.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { nextTick } from 'vue';
  2. import type { App } from 'vue';
  3. import * as svg from '@element-plus/icons-vue';
  4. import router from '/@/router/index';
  5. import { store } from '/@/store/index';
  6. import { i18n } from '/@/i18n/index';
  7. import { Local } from '/@/utils/storage';
  8. import SvgIcon from '/@/components/svgIcon/index.vue';
  9. /**
  10. * 导出全局注册 element plus svg 图标
  11. * @param app vue 实例
  12. * @description 使用:https://element-plus.gitee.io/zh-CN/component/icon.html
  13. */
  14. export function elSvg(app: App) {
  15. const icons = svg as any;
  16. for (const i in icons) {
  17. app.component(`ele-${icons[i].name}`, icons[i]);
  18. }
  19. app.component('SvgIcon', <any>SvgIcon);
  20. }
  21. /**
  22. * 设置浏览器标题国际化
  23. * @method const title = useTitle(); ==> title()
  24. */
  25. export function useTitle() {
  26. nextTick(() => {
  27. let webTitle = '';
  28. let globalTitle: string = store.state.themeConfig.themeConfig.globalTitle;
  29. router.currentRoute.value.path === '/login'
  30. ? (webTitle = router.currentRoute.value.meta?.title as any)
  31. : (webTitle = i18n.global.t(router.currentRoute.value.meta?.title as any));
  32. document.title = `${webTitle} - ${globalTitle}` || globalTitle;
  33. });
  34. }
  35. /**
  36. * 图片懒加载
  37. * @param el dom 目标元素
  38. * @param arr 列表数据
  39. * @description data-xxx 属性用于存储页面或应用程序的私有自定义数据
  40. */
  41. export const lazyImg = (el: any, arr: any) => {
  42. const io = new IntersectionObserver((res) => {
  43. res.forEach((v: any) => {
  44. if (v.isIntersecting) {
  45. const { img, key } = v.target.dataset;
  46. v.target.src = img;
  47. v.target.onload = () => {
  48. io.unobserve(v.target);
  49. arr[key]['loading'] = false;
  50. };
  51. }
  52. });
  53. });
  54. nextTick(() => {
  55. document.querySelectorAll(el).forEach((img) => io.observe(img));
  56. });
  57. };
  58. /**
  59. * 全局组件大小
  60. * @returns 返回 `window.localStorage` 中读取的缓存值 `globalComponentSize`
  61. */
  62. export const globalComponentSize: string = Local.get('themeConfig')?.globalComponentSize || store.state.themeConfig.themeConfig?.globalComponentSize;
  63. /**
  64. * 对象深克隆
  65. * @param obj 源对象
  66. * @returns 克隆后的对象
  67. */
  68. export function deepClone(obj: any) {
  69. let newObj: any;
  70. try {
  71. newObj = obj.push ? [] : {};
  72. } catch (error) {
  73. newObj = {};
  74. }
  75. for (let attr in obj) {
  76. if (typeof obj[attr] === 'object') {
  77. newObj[attr] = deepClone(obj[attr]);
  78. } else {
  79. newObj[attr] = obj[attr];
  80. }
  81. }
  82. return newObj;
  83. }
  84. /**
  85. * 判断是否是移动端
  86. */
  87. export function isMobile() {
  88. if (
  89. navigator.userAgent.match(
  90. /('phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone')/i
  91. )
  92. ) {
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. /**
  99. * 判断数组对象中所有属性是否为空,为空则删除当前行对象
  100. * @description @感谢大黄
  101. * @param list 数组对象
  102. * @returns 删除空值后的数组对象
  103. */
  104. export function handleEmpty(list: any) {
  105. const arr = [];
  106. for (const i in list) {
  107. const d = [];
  108. for (const j in list[i]) {
  109. d.push(list[i][j]);
  110. }
  111. const leng = d.filter((item) => item === '').length;
  112. if (leng !== d.length) {
  113. arr.push(list[i]);
  114. }
  115. }
  116. return arr;
  117. }
  118. /**
  119. * 统一批量导出
  120. * @method elSvg 导出全局注册 element plus svg 图标
  121. * @method useTitle 设置浏览器标题国际化
  122. * @method lazyImg 图片懒加载
  123. * @method globalComponentSize element plus 全局组件大小
  124. * @method deepClone 对象深克隆
  125. * @method isMobile 判断是否是移动端
  126. * @method handleEmpty 判断数组对象中所有属性是否为空,为空则删除当前行对象
  127. */
  128. const other = {
  129. elSvg: (app: App) => {
  130. elSvg(app);
  131. },
  132. useTitle: () => {
  133. useTitle();
  134. },
  135. lazyImg: (el: any, arr: any) => {
  136. lazyImg(el, arr);
  137. },
  138. globalComponentSize,
  139. deepClone: (obj: any) => {
  140. deepClone(obj);
  141. },
  142. isMobile: () => {
  143. return isMobile();
  144. },
  145. handleEmpty: (list: any) => {
  146. return handleEmpty(list);
  147. },
  148. };
  149. // 统一批量导出
  150. export default other;