utils.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { parse } from 'querystring';
  2. import pathRegexp from 'path-to-regexp';
  3. /* eslint no-useless-escape:0 import/prefer-default-export:0 */
  4. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
  5. export const isUrl = (path) => reg.test(path);
  6. export const isAntDesignPro = () => {
  7. if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
  8. return true;
  9. }
  10. return window.location.hostname === 'preview.pro.ant.design';
  11. }; // 给官方演示站点用,用于关闭真实开发环境不需要使用的特性
  12. export const isAntDesignProOrDev = () => {
  13. const { NODE_ENV } = process.env;
  14. if (NODE_ENV === 'development') {
  15. return true;
  16. }
  17. return isAntDesignPro();
  18. };
  19. export const getPageQuery = () => parse(window.location.href.split('?')[1]);
  20. /**
  21. * props.route.routes
  22. * @param router [{}]
  23. * @param pathname string
  24. */
  25. export const getAuthorityFromRouter = (router = [], pathname) => {
  26. // const authority = router.find(
  27. // ({ routes, path = '/' }) =>
  28. // (path && pathRegexp(path).exec(pathname)) ||
  29. // (routes && getAuthorityFromRouter(routes, pathname)),
  30. // );
  31. // if (authority) return authority;
  32. // return undefined;
  33. for (let r of router) {
  34. if (pathRegexp(r.path || '/').exec(pathname)) {
  35. return r
  36. }
  37. if (r.routes && r.routes.length) {
  38. const f = getAuthorityFromRouter(r.routes, pathname)
  39. if (f) {
  40. return f
  41. }
  42. }
  43. }
  44. };
  45. export const getRouteAuthority = (path, routeData) => {
  46. let authorities;
  47. routeData.forEach((route) => {
  48. // match prefix
  49. if (pathRegexp(`${route.path}/(.*)`).test(`${path}/`)) {
  50. if (route.authority) {
  51. authorities = route.authority;
  52. } // exact match
  53. if (route.path === path) {
  54. authorities = route.authority || authorities;
  55. } // get children authority recursively
  56. if (route.routes) {
  57. authorities = getRouteAuthority(path, route.routes) || authorities;
  58. }
  59. }
  60. });
  61. return authorities;
  62. };