index.ts 7.9 KB

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