index.js 4.9 KB

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