utils.js 4.29 KB
Newer Older
1
import Cookies from 'js-cookie';
邓晓峰's avatar
邓晓峰 committed
2 3
import pathRegexp from 'path-to-regexp';
import { parse } from 'querystring';
4

5
import pkg from '../../package.json';
6

邓晓峰's avatar
邓晓峰 committed
7
const { toString } = Object.prototype;
邓晓峰's avatar
邓晓峰 committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/* eslint no-useless-escape:0 import/prefer-default-export:0 */
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

export const isUrl = path => reg.test(path);

export const getPageQuery = () => parse(window.location.href.split('?')[1]);

export const getAuthorityFromRouter = (router, pathname) => {
  const authority = router.find(
    ({ routes, path = '/', target = '_self' }) =>
      (path && target !== '_blank' && pathRegexp(path).exec(pathname)) ||
      (routes && getAuthorityFromRouter(routes, pathname)),
  );
  if (authority) return authority;
  return undefined;
};

export const getRouteAuthority = (path, routeData) => {
  let authorities;
  routeData.forEach(route => {
    if (pathRegexp(`${route.path}/(.*)`).test(`${path}/`)) {
      if (route.authority) {
        authorities = route.authority;
      }
      if (route.path === path) {
        authorities = route.authority || authorities;
      }

      if (route.routes) {
        authorities = getRouteAuthority(path, route.routes) || authorities;
      }
    }
  });
  return authorities;
};

export function isPromise(obj) {
  return (
邓晓峰's avatar
邓晓峰 committed
46
    !!obj && // 有实际含义的变量才执行方法,变量null,undefined和''空串都为false
邓晓峰's avatar
邓晓峰 committed
47 48 49 50
    (typeof obj === 'object' || typeof obj === 'function') && // 初始promise 或 promise.then返回的
    typeof obj.then === 'function'
  );
}
邓晓峰's avatar
邓晓峰 committed
51 52 53 54

export function getBaseName() {
  return pkg.name.toLocaleLowerCase();
}
55 56
let cache = {};
export function findPathByLeafId(leafId, nodes, path, key, clear) {
邓晓峰's avatar
邓晓峰 committed
57 58 59
  if (path === undefined) {
    path = {};
  }
邓晓峰's avatar
邓晓峰 committed
60
  let tmpPath = path;
61 62 63
  if(clear) {
    cache = {};
  }
邓晓峰's avatar
邓晓峰 committed
64 65 66
  if (cache[leafId]) {
    return cache[leafId];
  }
邓晓峰's avatar
邓晓峰 committed
67 68 69
  // eslint-disable-next-line no-plusplus
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i] && nodes[i][key] && leafId === nodes[i][key]) {
邓晓峰's avatar
邓晓峰 committed
70
      tmpPath = nodes[i];
71
      cache[leafId] = tmpPath;
邓晓峰's avatar
邓晓峰 committed
72
      return tmpPath;
邓晓峰's avatar
邓晓峰 committed
73 74
    }
    if (nodes[i] && nodes[i].routes) {
邓晓峰's avatar
邓晓峰 committed
75
      const findResult = findPathByLeafId(
邓晓峰's avatar
邓晓峰 committed
76 77 78 79 80 81
        leafId,
        nodes[i].routes,
        tmpPath,
        key,
      );
      if (findResult) {
82
        cache[leafId] = findResult;
邓晓峰's avatar
邓晓峰 committed
83 84 85 86 87
        return findResult;
      }
    }
  }
}
邓晓峰's avatar
邓晓峰 committed
88 89

export function transformURL(path) {
邓晓峰's avatar
邓晓峰 committed
90
  const el = document.createElement('input');
邓晓峰's avatar
邓晓峰 committed
91 92
  el.value = path;
  String.fromCharCode(92);
邓晓峰's avatar
邓晓峰 committed
93 94
  return el.value.replace(/\\/g, '/');
}
邓晓峰's avatar
邓晓峰 committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

export function findPathByWidget(leafId, nodes, path, key) {
  if (path === undefined) {
    path = {};
  }
  let tmpPath = path;
  // eslint-disable-next-line no-plusplus
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i] && nodes[i][key] && nodes[i][key].indexOf(leafId) > -1) {
      tmpPath = nodes[i];
      return tmpPath;
    }
    if (nodes[i] && nodes[i].widgets) {
      const findResult = findPathByWidget(
        leafId,
        nodes[i].widgets,
        tmpPath,
        key,
        // eslint-disable-next-line no-restricted-globals
        location,
      );
      if (findResult) {
        return findResult;
      }
    }
  }
  return tmpPath;
}
邓晓峰's avatar
邓晓峰 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136

export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      const obj = JSON.parse(str);
      if (typeof obj === 'object' && obj) {
        return true;
      }
      return false;
    } catch (e) {
      return false;
    }
  }
}
邓晓峰's avatar
邓晓峰 committed
137 138 139 140

export function isString(str) {
  return toString.call(str) === '[object String]';
}
邓晓峰's avatar
邓晓峰 committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

export const getKeyName = path => {
  const truePath = path.split('?')[0];
  const curRoute = [].filter(item => item.path.includes(truePath));

  if (!curRoute[0]) {
    return {
      title: '暂无权限',
      tabKey: '403',
    };
  }
  const { name, key, component } = curRoute[0];
  return { title: name, tabKey: key, component };
};

export const asyncAction = action => {
  const wait = new Promise(resolve => resolve(action));
  return callback => {
    wait.then(() => setTimeout(() => callback()));
  };
};
162

163 164
export function getToken() {
  return Cookies.get('token');
165
}
166
export function setToken(token) {
167
  Cookies.set(token);
168
}
邓晓峰's avatar
邓晓峰 committed
169
export const closeTabAction = (history, returnUrl, callback) => {};