index.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import { h, unref } from 'vue';
  2. import type { App, Plugin } from 'vue';
  3. import { NIcon, NTag } from 'naive-ui';
  4. import { PageEnum } from '@/enums/pageEnum';
  5. import { isObject } from './is/index';
  6. import { cloneDeep } from 'lodash';
  7. import dayjs from 'dayjs';
  8. import EventEmitter from 'eventemitter3';
  9. export const eventGlobal = new EventEmitter();
  10. /**
  11. * render 图标
  12. * */
  13. export function renderIcon(icon: any) {
  14. return () => h(NIcon, null, { default: () => h(icon) });
  15. }
  16. /**
  17. * font 图标(Font class)
  18. * */
  19. export function renderFontClassIcon(icon: string, iconName = 'iconfont') {
  20. return () => h('span', { class: [iconName, icon] });
  21. }
  22. /**
  23. * font 图标(Unicode)
  24. * */
  25. export function renderUnicodeIcon(icon: string, iconName = 'iconfont') {
  26. return () => h('span', { class: [iconName], innerHTML: icon });
  27. }
  28. /**
  29. * font svg 图标
  30. * */
  31. export function renderfontsvg(icon: any) {
  32. return () =>
  33. h(NIcon, null, {
  34. default: () =>
  35. h(
  36. 'svg',
  37. { class: `icon`, 'aria-hidden': 'true' },
  38. h('use', { 'xlink:href': `#${icon}` })
  39. )
  40. });
  41. }
  42. /**
  43. * render new Tag
  44. * */
  45. const newTagColors = { color: '#f90', textColor: '#fff', borderColor: '#f90' };
  46. export function renderNew(
  47. type = 'warning',
  48. text = 'New',
  49. color: object = newTagColors
  50. ) {
  51. return () =>
  52. h(
  53. NTag as any,
  54. {
  55. type,
  56. round: true,
  57. size: 'small',
  58. color
  59. },
  60. { default: () => text }
  61. );
  62. }
  63. /**
  64. * 递归组装菜单格式
  65. */
  66. export function generatorMenu(routerMap: Array<any>) {
  67. return filterRouter(routerMap).map(item => {
  68. const isRoot = isRootRouter(item);
  69. if (isRoot) {
  70. console.log(item.children[0], 'isRoot');
  71. }
  72. const info = isRoot ? item.children[0] : item;
  73. // console.log(item)
  74. const currentMenu = {
  75. ...info,
  76. ...info.meta,
  77. label: info.meta?.title,
  78. key: info.key,
  79. icon: info.meta?.icon
  80. };
  81. // 是否有子菜单,并递归处理
  82. if (info.children && info.children.length > 0) {
  83. // Recursion
  84. currentMenu.children = generatorMenu(info.children);
  85. }
  86. return currentMenu;
  87. });
  88. }
  89. /**
  90. * 混合菜单
  91. * */
  92. export function generatorMenuMix(
  93. routerMap: Array<any>,
  94. routerName: string,
  95. location: string
  96. ) {
  97. const cloneRouterMap = cloneDeep(routerMap);
  98. const newRouter = filterRouter(cloneRouterMap);
  99. if (location === 'header') {
  100. const firstRouter: any[] = [];
  101. newRouter.forEach(item => {
  102. const isRoot = isRootRouter(item);
  103. const info = isRoot ? item.children[0] : item;
  104. info.children = undefined;
  105. const currentMenu = {
  106. ...info,
  107. ...info.meta,
  108. label: info.meta?.title,
  109. key: info.name
  110. };
  111. firstRouter.push(currentMenu);
  112. });
  113. return firstRouter;
  114. } else {
  115. // 混合菜单
  116. console.log('混合菜单');
  117. return getChildrenRouter(
  118. newRouter.filter(item => item.name === routerName)
  119. );
  120. }
  121. }
  122. /**
  123. * 递归组装子菜单
  124. * */
  125. export function getChildrenRouter(routerMap: Array<any>) {
  126. return filterRouter(routerMap).map(item => {
  127. const isRoot = isRootRouter(item);
  128. const info = isRoot ? item.children[0] : item;
  129. const currentMenu = {
  130. ...info,
  131. ...info.meta,
  132. label: info.meta?.title,
  133. key: info.name
  134. };
  135. // 是否有子菜单,并递归处理
  136. if (info.children && info.children.length > 0) {
  137. // Recursion
  138. currentMenu.children = getChildrenRouter(info.children);
  139. }
  140. return currentMenu;
  141. });
  142. }
  143. /**
  144. * 判断根路由 Router
  145. * */
  146. export function isRootRouter(item: any) {
  147. return item.meta?.isRoot === true;
  148. }
  149. /**
  150. * 排除Router
  151. * */
  152. export function filterRouter(routerMap: Array<any>) {
  153. return routerMap.filter(item => {
  154. // console.log(
  155. // (item.meta?.hidden || false) != true &&
  156. // !['/:path(.*)*', PageEnum.REDIRECT, PageEnum.BASE_LOGIN].includes(item.path),
  157. // '过滤完之后的路由'
  158. // )
  159. return (
  160. (item.meta?.hidden || false) != true &&
  161. !['/:path(.*)*', PageEnum.REDIRECT, PageEnum.BASE_LOGIN].includes(
  162. item.path
  163. )
  164. );
  165. });
  166. }
  167. export const withInstall = <T>(component: T, alias?: string) => {
  168. const comp = component as any;
  169. comp.install = (app: App) => {
  170. app.component(comp.name || comp.displayName, component as any);
  171. if (alias) {
  172. app.config.globalProperties[alias] = component;
  173. }
  174. };
  175. return component as T & Plugin;
  176. };
  177. /**
  178. * 找到对应的节点
  179. * */
  180. let result = null as any;
  181. export function getTreeItem(data: any[], key?: string | number): any {
  182. data.map(item => {
  183. if (item.key === key) {
  184. result = item;
  185. } else {
  186. if (item.children && item.children.length) {
  187. getTreeItem(item.children, key);
  188. }
  189. }
  190. });
  191. return result;
  192. }
  193. /**
  194. * 找到所有节点
  195. * */
  196. const treeAll: any[] = [];
  197. export function getTreeAll(data: any[]): any[] {
  198. data.map(item => {
  199. treeAll.push(item.key);
  200. if (item.children && item.children.length) {
  201. getTreeAll(item.children);
  202. }
  203. });
  204. return treeAll;
  205. }
  206. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  207. let key: string;
  208. for (key in target) {
  209. src[key] = isObject(src[key])
  210. ? deepMerge(src[key], target[key])
  211. : (src[key] = target[key]);
  212. }
  213. return src;
  214. }
  215. /**
  216. * Sums the passed percentage to the R, G or B of a HEX color
  217. * @param {string} color The color to change
  218. * @param {number} amount The amount to change the color by
  219. * @returns {string} The processed part of the color
  220. */
  221. function addLight(color: string, amount: number) {
  222. const cc = parseInt(color, 16) + amount;
  223. const c = cc > 255 ? 255 : cc;
  224. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
  225. }
  226. /**
  227. * Lightens a 6 char HEX color according to the passed percentage
  228. * @param {string} color The color to change
  229. * @param {number} amount The amount to change the color by
  230. * @returns {string} The processed color represented as HEX
  231. */
  232. export function lighten(color: string, amount: number) {
  233. color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
  234. amount = Math.trunc((255 * amount) / 100);
  235. return `#${addLight(color.substring(0, 2), amount)}${addLight(
  236. color.substring(2, 4),
  237. amount
  238. )}${addLight(color.substring(4, 6), amount)}`;
  239. }
  240. /**
  241. * 判断是否 url
  242. * */
  243. export function isUrl(url: string) {
  244. return /^(http|https):\/\//g.test(url);
  245. }
  246. /** 递归清除空数据 */
  247. export function clearEmtryData(list: any[], key: string) {
  248. for (let i = 0; i < list.length; i++) {
  249. if (Array.isArray(list[i][key]) && !list[i][key].length) {
  250. list[i][key] = '';
  251. } else {
  252. list[i][key] = clearEmtryData(list[i][key], key);
  253. }
  254. }
  255. return list;
  256. }
  257. // 秒转分
  258. export const getSecondRPM = (second: number, type?: string) => {
  259. if (isNaN(second)) return '00:00';
  260. const mm = Math.floor(second / 60)
  261. .toString()
  262. .padStart(2, '0');
  263. const dd = Math.floor(second % 60)
  264. .toString()
  265. .padStart(2, '0');
  266. if (type === 'cn') {
  267. return mm + '分' + dd + '秒';
  268. } else {
  269. return mm + ':' + dd;
  270. }
  271. };
  272. // 秒转分
  273. export const getSecond = (second: number, type?: string) => {
  274. if (isNaN(second)) return '0000';
  275. const mm = Math.floor(second / 60)
  276. .toString()
  277. .padStart(2, '0');
  278. const dd = Math.floor(second % 60)
  279. .toString()
  280. .padStart(2, '0');
  281. return `${mm}${dd}`;
  282. };
  283. /** 滚动到表单填写错误的地方 */
  284. export function scrollToErrorForm() {
  285. const isError =
  286. document.querySelector('.n-input--error-status') ||
  287. document.querySelector('.n-base-selection--error-status');
  288. isError?.scrollIntoView({
  289. block: 'center',
  290. behavior: 'smooth'
  291. });
  292. }
  293. export const getTimes = (
  294. times: any,
  295. keys: Array<string> = [],
  296. format = 'YYYY-MM-DD'
  297. ) => {
  298. if (times && times.length) {
  299. return format == 'YYYY-MM-DD'
  300. ? {
  301. [keys[0] || 'start']: dayjs(times[0]).isValid()
  302. ? dayjs(times[0]).format(format) + ' 00:00:00'
  303. : '',
  304. [keys[1] || 'end']: dayjs(times[1]).isValid()
  305. ? dayjs(times[1]).format(format) + ' 23:59:59'
  306. : ''
  307. }
  308. : {
  309. [keys[0] || 'start']: dayjs(times[0]).isValid()
  310. ? dayjs(times[0]).format(format)
  311. : '',
  312. [keys[1] || 'end']: dayjs(times[1]).isValid()
  313. ? dayjs(times[1]).format(format)
  314. : ''
  315. };
  316. }
  317. return {};
  318. };
  319. export const px2vw = (px: number): string => `${(px / 1920) * 100}vw`;
  320. export const px2vwH = (px: number): string =>
  321. `${(((px / 1920) * 1920) / 1188) * 100}vw`;
  322. export const fscreen = () => {
  323. const el = document.documentElement;
  324. //进入全屏
  325. (el.requestFullscreen && el.requestFullscreen()) ||
  326. (el.mozRequestFullScreen && el.mozRequestFullScreen()) ||
  327. (el.webkitRequestFullscreen && el.webkitRequestFullscreen()) ||
  328. (el.msRequestFullscreen && el.msRequestFullscreen());
  329. //退出全屏
  330. };
  331. export const exitFullscreen = () => {
  332. document.exitFullscreen
  333. ? document.exitFullscreen()
  334. : document.mozCancelFullScreen
  335. ? document.mozCancelFullScreen()
  336. : document.webkitExitFullscreen
  337. ? document.webkitExitFullscreen()
  338. : '';
  339. };