index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { routerMode } from '@/config/env';
  2. export function deepMerge(a, b) {
  3. let k;
  4. for (k in b) {
  5. a[k] =
  6. a[k] && a[k].toString() === '[object Object]' ? deepMerge(a[k], b[k]) : (a[k] = b[k]);
  7. }
  8. return a;
  9. }
  10. export function numberGap(value, options) {
  11. const { max = 10000000, min = 0 } = options || {};
  12. let [a, b] = value || [];
  13. if (a === undefined && b === undefined) {
  14. return [];
  15. } else {
  16. if (a != undefined && b == undefined) {
  17. value = [a, max];
  18. }
  19. if (a == undefined && b != undefined) {
  20. value = [min, b];
  21. }
  22. }
  23. return value;
  24. }
  25. export function orderBy(list, key) {
  26. return list.sort((a, b) => a[key] - b[key]);
  27. }
  28. export function deepTree(list) {
  29. let newList = [];
  30. let map = {};
  31. list.forEach(e => (map[e.id] = e));
  32. list.forEach(e => {
  33. let parent = map[e.parentId];
  34. if (parent) {
  35. (parent.children || (parent.children = [])).push(e);
  36. } else {
  37. if (!e.parentId) {
  38. newList.push(e);
  39. }
  40. }
  41. });
  42. const fn = list => {
  43. list.map(e => {
  44. if (e.children instanceof Array) {
  45. e.children = orderBy(e.children, 'orderNum');
  46. fn(e.children);
  47. }
  48. });
  49. };
  50. fn(newList);
  51. return orderBy(newList, 'orderNum');
  52. }
  53. export function firstMenu(list) {
  54. let path = '';
  55. const fn = arr => {
  56. arr.forEach(e => {
  57. if (e.type == 1) {
  58. if (!path) {
  59. path = e.path;
  60. }
  61. } else {
  62. fn(e.children);
  63. }
  64. });
  65. };
  66. fn(list);
  67. return path || '/404';
  68. }
  69. export const revisePath = path => {
  70. if (!path) {
  71. return '';
  72. }
  73. if (path[0] == '/') {
  74. return path;
  75. } else {
  76. return `/${path}`;
  77. }
  78. };
  79. export function getUrlParam(name) {
  80. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  81. var r = window.location.search.substr(1).match(reg);
  82. if (r != null) return decodeURIComponent(r[2]);
  83. return null;
  84. }
  85. export function href(path, newWindow) {
  86. let { search, origin } = window.location;
  87. let url = '';
  88. if (routerMode == 'history') {
  89. url = origin + path;
  90. } else {
  91. url = origin + search + '#' + path;
  92. }
  93. if (newWindow) {
  94. window.open(url);
  95. } else {
  96. window.location.href = url;
  97. }
  98. }
  99. export function contains(parent, node) {
  100. if (document.documentElement.contains) {
  101. return parent !== node && parent.contains(node);
  102. } else {
  103. while (node && (node = node.parentNode)) if (node === parent) return true;
  104. return false;
  105. }
  106. }
  107. export function isPc() {
  108. const userAgentInfo = navigator.userAgent;
  109. const Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
  110. let flag = true;
  111. for (var v = 0; v < Agents.length; v++) {
  112. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  113. flag = false;
  114. break;
  115. }
  116. }
  117. return flag;
  118. }