index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { routerMode } from '@/config/env';
  2. export const getFirstHost = () => {
  3. let host = window.location.host;
  4. host = host.split('.');
  5. host = host.length == 3 ? host[1] : host[0];
  6. return process.env.NODE_ENV == 'development' ? 'huaml' : host;
  7. };
  8. export const revisePath = path => {
  9. if (!path) {
  10. return '';
  11. }
  12. if (path[0] == '/') {
  13. return path;
  14. } else {
  15. return `/${path}`;
  16. }
  17. };
  18. export function getUrlParam(name) {
  19. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
  20. var r = window.location.search.substr(1).match(reg);
  21. if (r != null) return decodeURIComponent(r[2]);
  22. return null;
  23. }
  24. export function firstMenu(list) {
  25. let path = '';
  26. const fn = arr => {
  27. arr.forEach(e => {
  28. if (e.type == 1) {
  29. if (!path) {
  30. path = e.path;
  31. }
  32. } else {
  33. fn(e.children);
  34. }
  35. });
  36. };
  37. fn(list);
  38. return path || '/404';
  39. }
  40. export function isPc() {
  41. const userAgentInfo = navigator.userAgent;
  42. const Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
  43. let flag = true;
  44. for (var v = 0; v < Agents.length; v++) {
  45. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  46. flag = false;
  47. break;
  48. }
  49. }
  50. return flag;
  51. }
  52. export function href(path, newWindow) {
  53. let { search, origin } = window.location;
  54. let url = '';
  55. if (routerMode == 'history') {
  56. url = origin + path;
  57. } else {
  58. url = origin + search + '#' + path;
  59. }
  60. if (newWindow) {
  61. window.open(url);
  62. } else {
  63. window.location.href = url;
  64. }
  65. }
  66. export function orderBy(list, key) {
  67. return list.sort((a, b) => a[key] - b[key]);
  68. }
  69. export function deepTree(list) {
  70. let newList = [];
  71. let map = {};
  72. list.forEach(e => (map[e.id] = e));
  73. list.forEach(e => {
  74. let parent = map[e.parentId];
  75. if (parent) {
  76. (parent.children || (parent.children = [])).push(e);
  77. } else {
  78. if (!e.parentId) {
  79. newList.push(e);
  80. }
  81. }
  82. });
  83. const fn = list => {
  84. list.map(e => {
  85. if (e.children instanceof Array) {
  86. e.children = orderBy(e.children, 'orderNum');
  87. fn(e.children);
  88. }
  89. });
  90. };
  91. fn(newList);
  92. return orderBy(newList, 'orderNum');
  93. }
  94. export function revDeepTree(list = []) {
  95. let d = [];
  96. const deep = list => {
  97. list.forEach(e => {
  98. d.push(e);
  99. if (e.children && isArray(e.children)) {
  100. deep(e.children);
  101. }
  102. });
  103. };
  104. deep(list || []);
  105. return d;
  106. }
  107. export function debounce(fn, delay) {
  108. let timer = null;
  109. return function() {
  110. let args = arguments;
  111. let context = this;
  112. if (timer) {
  113. clearTimeout(timer);
  114. timer = setTimeout(function() {
  115. fn.apply(context, args);
  116. }, delay);
  117. } else {
  118. timer = setTimeout(function() {
  119. fn.apply(context, args);
  120. }, delay);
  121. }
  122. };
  123. }
  124. export function isArray(value) {
  125. if (typeof Array.isArray === 'function') {
  126. return Array.isArray(value);
  127. } else {
  128. return Object.prototype.toString.call(value) === '[object Array]';
  129. }
  130. }
  131. export function isObject(value) {
  132. return Object.prototype.toString.call(value) === '[object Object]';
  133. }
  134. export function isNumber(value) {
  135. return !isNaN(Number(value));
  136. }
  137. export function isFunction(value) {
  138. return typeof value == 'function';
  139. }
  140. export function isString(value) {
  141. return typeof value == 'string';
  142. }
  143. export function isEmpty(value) {
  144. if (isArray(value)) {
  145. return value.length === 0;
  146. }
  147. if (isObject(value)) {
  148. return Object.keys(value).length === 0;
  149. }
  150. return value === '' || value === undefined || value === null;
  151. }
  152. export function cloneDeep(obj) {
  153. let d = isArray(obj) ? obj : {};
  154. if (isObject(obj)) {
  155. for (let key in obj) {
  156. if (obj.hasOwnProperty && obj.hasOwnProperty(key)) {
  157. if (obj[key] && typeof obj[key] === 'object') {
  158. d[key] = cloneDeep(obj[key]);
  159. } else {
  160. d[key] = obj[key];
  161. }
  162. }
  163. }
  164. }
  165. return d;
  166. }
  167. export function clone(obj) {
  168. return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
  169. }
  170. export function certainProperty(obj, keys) {
  171. return keys.reduce((result, key) => {
  172. if (obj.hasOwnProperty(key)) {
  173. result[key] = obj[key];
  174. }
  175. return result;
  176. }, {});
  177. }
  178. export function deepMerge(a, b) {
  179. let k;
  180. for (k in b) {
  181. a[k] =
  182. a[k] && a[k].toString() === '[object Object]' ? deepMerge(a[k], b[k]) : (a[k] = b[k]);
  183. }
  184. return a;
  185. }
  186. export function contains(parent, node) {
  187. if (document.documentElement.contains) {
  188. return parent !== node && parent.contains(node);
  189. } else {
  190. while (node && (node = node.parentNode)) if (node === parent) return true;
  191. return false;
  192. }
  193. }
  194. export function moreList(res, { list, pagination }) {
  195. const { page, size } = res.pagination;
  196. const len = res.list.length;
  197. const max = list.length;
  198. if (page == 1) {
  199. list.splice(0, max, ...res.list);
  200. } else {
  201. let start = max - (max % size);
  202. let end = start + len;
  203. list.splice(start, end, ...res.list);
  204. }
  205. if (len == size) {
  206. res.pagination.page += 1;
  207. }
  208. Object.assign(pagination, res.pagination);
  209. return page != 1;
  210. }