index.js 5.0 KB

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