1
2
3
4
5
6
7
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import Cookies from 'js-cookie';
import pathRegexp from 'path-to-regexp';
import { parse } from 'querystring';
import pkg from '../../package.json';
/* 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 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 (
!!obj && // 有实际含义的变量才执行方法,变量null,undefined和''空串都为false
(typeof obj === 'object' || typeof obj === 'function') && // 初始promise 或 promise.then返回的
typeof obj.then === 'function'
);
}
export function getBaseName() {
return pkg.name.toLocaleLowerCase();
}
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()));
};
};
export function getToken() {
return Cookies.get('token');
}
export function setToken(token) {
Cookies.set(token);
}
export function getVideoUrl() {
const { protocol } = window.location;
const address =
protocol === 'https:'
? window.location.origin.replace(protocol, 'wss:')
: window.location.origin.replace(protocol, 'ws:');
return `${address}/`;
}