index.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import { h, unref } from 'vue';
  2. import type { App, Plugin } from 'vue';
  3. import { ElIcon, ElTag } from 'element-plus';
  4. import { PageEnum } from '@/enums/pageEnum';
  5. import { isObject, isString, isNumber } from './is/index';
  6. import { cloneDeep } from 'lodash-es';
  7. import { TargetContext } from '/#/index';
  8. /**
  9. * render 图标
  10. * */
  11. export function renderIcon(icon) {
  12. return () => h(ElIcon, null, { default: () => h(icon) });
  13. }
  14. /**
  15. * render new Tag
  16. * */
  17. const newTagColors = { color: '#f90', textColor: '#fff', borderColor: '#f90' };
  18. export function renderNew(type = 'warning', text = 'New', color: object = newTagColors) {
  19. return () =>
  20. h(
  21. ElTag as any,
  22. {
  23. type,
  24. round: true,
  25. size: 'small',
  26. color,
  27. },
  28. { default: () => text },
  29. );
  30. }
  31. /**
  32. * 递归组装菜单格式
  33. */
  34. export function generatorMenu(routerMap: Array<any>) {
  35. return filterRouter(routerMap).map((item) => {
  36. const isRoot = isRootRouter(item);
  37. const info = isRoot ? item.children[0] : item;
  38. const currentMenu = {
  39. ...info,
  40. ...info.meta,
  41. title: info.meta?.title,
  42. key: info.name,
  43. icon: isRoot ? item.meta?.icon : info.meta?.icon,
  44. };
  45. // 是否有子菜单,并递归处理
  46. if (info.children && info.children.length > 0) {
  47. // Recursion
  48. currentMenu.children = generatorMenu(info.children);
  49. }
  50. return currentMenu;
  51. });
  52. }
  53. /**
  54. * 混合菜单
  55. * */
  56. export function generatorMenuMix(routerMap: Array<any>, routerName: string, location: string) {
  57. const cloneRouterMap = cloneDeep(routerMap);
  58. const newRouter = filterRouter(cloneRouterMap);
  59. if (location === 'header') {
  60. const firstRouter: any[] = [];
  61. newRouter.forEach((item) => {
  62. const isRoot = isRootRouter(item);
  63. const info = isRoot ? item.children[0] : item;
  64. info.children = undefined;
  65. const currentMenu = {
  66. ...info,
  67. ...info.meta,
  68. title: info.meta?.title,
  69. key: info.name,
  70. };
  71. firstRouter.push(currentMenu);
  72. });
  73. console.log(firstRouter);
  74. return firstRouter;
  75. } else {
  76. const currentRouters = newRouter.filter((item) => item.name === routerName);
  77. const childrenRouter = currentRouters.length ? currentRouters[0].children || [] : [];
  78. return getChildrenRouter(childrenRouter);
  79. }
  80. }
  81. /**
  82. * 递归组装子菜单
  83. * */
  84. export function getChildrenRouter(routerMap: Array<any>) {
  85. return filterRouter(routerMap).map((item) => {
  86. const isRoot = isRootRouter(item);
  87. const info = isRoot ? item.children[0] : item;
  88. const currentMenu = {
  89. ...info,
  90. ...info.meta,
  91. title: info.meta?.title,
  92. key: info.name,
  93. };
  94. // 是否有子菜单,并递归处理
  95. if (info.children && info.children.length > 0) {
  96. // Recursion
  97. currentMenu.children = getChildrenRouter(info.children);
  98. }
  99. return currentMenu;
  100. });
  101. }
  102. /**
  103. * 判断根路由 Router
  104. * */
  105. export function isRootRouter(item) {
  106. return item.meta?.alwaysShow === true && item.children?.length === 1;
  107. }
  108. /**
  109. * 排除Router
  110. * */
  111. export function filterRouter(routerMap: Array<any>) {
  112. return routerMap.filter((item) => {
  113. return (
  114. (item.meta?.hidden || false) != true &&
  115. !['/:path(.*)*', '/', PageEnum.REDIRECT, PageEnum.BASE_LOGIN].includes(item.path)
  116. );
  117. });
  118. }
  119. export const withInstall = <T>(component: T, alias?: string) => {
  120. const comp = component as any;
  121. comp.install = (app: App) => {
  122. app.component(comp.name || comp.displayName, component);
  123. if (alias) {
  124. app.config.globalProperties[alias] = component;
  125. }
  126. };
  127. return component as T & Plugin;
  128. };
  129. /**
  130. * 找到对应的节点
  131. * */
  132. let result = null;
  133. export function getTreeItem(data: any[], key?: string | number, keyField = 'key'): any {
  134. data.map((item) => {
  135. if (item[keyField] === key) {
  136. result = item;
  137. } else {
  138. if (item.children && item.children.length) {
  139. getTreeItem(item.children, key, keyField);
  140. }
  141. }
  142. });
  143. return result;
  144. }
  145. /**
  146. * 找到所有节点
  147. * */
  148. const treeAll: any[] = [];
  149. export function getTreeAll(data: any[]): any[] {
  150. data.map((item) => {
  151. treeAll.push(item.key);
  152. if (item.children && item.children.length) {
  153. getTreeAll(item.children);
  154. }
  155. });
  156. return treeAll;
  157. }
  158. // dynamic use hook props
  159. export function getDynamicProps<T, U>(props: T): Partial<U> {
  160. const ret: Recordable = {};
  161. Object.keys(props).map((key) => {
  162. ret[key] = unref((props as Recordable)[key]);
  163. });
  164. return ret as Partial<U>;
  165. }
  166. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  167. let key: string;
  168. for (key in target) {
  169. src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
  170. }
  171. return src;
  172. }
  173. /**
  174. * Sums the passed percentage to the R, G or B of a HEX color
  175. * @param {string} color The color to change
  176. * @param {number} amount The amount to change the color by
  177. * @returns {string} The processed part of the color
  178. */
  179. function addLight(color: string, amount: number) {
  180. const cc = parseInt(color, 16) + amount;
  181. const c = cc > 255 ? 255 : cc;
  182. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
  183. }
  184. /**
  185. * Lightens a 6 char HEX color according to the passed percentage
  186. * @param {string} color The color to change
  187. * @param {number} amount The amount to change the color by
  188. * @returns {string} The processed color represented as HEX
  189. */
  190. export function lighten(color: string, amount: number) {
  191. color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
  192. amount = Math.trunc((255 * amount) / 100);
  193. return `#${addLight(color.substring(0, 2), amount)}${addLight(
  194. color.substring(2, 4),
  195. amount,
  196. )}${addLight(color.substring(4, 6), amount)}`;
  197. }
  198. export function openWindow(
  199. url: string,
  200. opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
  201. ) {
  202. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  203. const feature: string[] = [];
  204. noopener && feature.push('noopener=yes');
  205. noreferrer && feature.push('noreferrer=yes');
  206. window.open(url, target, feature.join(','));
  207. }
  208. /**
  209. * 处理css单位
  210. * */
  211. export function cssUnit(value: string | number, unit = 'px') {
  212. return isNumber(value) || (isString(value) && value.indexOf(unit as string) === -1)
  213. ? `${value}${unit}`
  214. : value;
  215. }
  216. /**
  217. * 判断是否 url
  218. * */
  219. export function isUrl(url: string) {
  220. return /^(http|https):\/\//g.test(url);
  221. }
  222. /*
  223. * 模拟a下载一个文件
  224. * @params res 结果集
  225. * @params filename 文件名
  226. */
  227. export const downloadFile = (res, filename?) => {
  228. const blob = new Blob([res.data]);
  229. let fileName = filename || res.headers['content-disposition'].split('filename=').pop();
  230. fileName = decodeURIComponent(fileName);
  231. if (window.navigator && window.navigator.msSaveOrOpenBlob) {
  232. // IE
  233. window.navigator.msSaveOrOpenBlob(blob, fileName);
  234. } else {
  235. const objectUrl = (window.URL || window.webkitURL).createObjectURL(blob);
  236. const downFile = document.createElement('a');
  237. downFile.style.display = 'none';
  238. downFile.href = objectUrl;
  239. downFile.download = fileName; // 下载后文件名
  240. document.body.appendChild(downFile);
  241. downFile.click();
  242. document.body.removeChild(downFile); // 下载完成移除元素
  243. window.URL.revokeObjectURL(objectUrl); // 释放掉blob对象。
  244. }
  245. };