-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
175 lines (147 loc) · 5.28 KB
/
index.js
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
86
87
88
89
90
91
92
93
94
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {session} from '@/library/utils/storage';
import {getNodeByPropertyAndValue, convertToTree} from '@/library/utils/tree-utils';
import pathToRegexp from "path-to-regexp/index";
import {ROUTE_BASE_NAME} from '@/router/AppRouter';
const CURRENT_USER_KEY = 'current-user';
const localStorage = window.localStorage;
const sessionStorage = window.sessionStorage;
/**
* 判断是否有权限
* @param code
*/
export function hasPermission(code) {
const loginUser = getLoginUser();
return loginUser?.permissions?.includes(code);
}
/**
* 设置当前用户信息
*/
export function setLoginUser(currentUser = {}) {
// 将用户属性在这里展开,方便查看系统都用到了那些用户属性
const {id, name, avatar, token, permissions} = currentUser;
const userStr = JSON.stringify({
id, // 用户id 必须
name, // 用户名 必须
avatar, // 用头像 非必须
token, // 登录凭证 非必须 ajax请求有可能会用到,也许是cookie
permissions, // 用户权限
});
sessionStorage.setItem(CURRENT_USER_KEY, userStr);
}
/**
* 获取当前用户信息
*/
export function getLoginUser() {
const loginUser = sessionStorage.getItem(CURRENT_USER_KEY);
return loginUser ? JSON.parse(loginUser) : null;
}
/**
* 判断用户是否登录
*/
export function isLogin() {
// 如果当前用户存在,就认为已经登录了
return !!getLoginUser();
}
/**
* 跳转到登录页面
*/
export function toLogin() {
const loginPath = '/login';
// 判断当前页面是否已经是login页面,如果是,直接返回,不进行跳转,防止出现跳转死循环
const pathname = window.location.pathname;
const isLogin = pathname.indexOf(loginPath) !== -1;
if (isLogin) return null;
// 清除相关数据
session.clear();
localStorage.setItem(CURRENT_USER_KEY, null);
sessionStorage.clear();
sessionStorage.setItem('last-href', window.location.pathname);
// 强制跳转,让浏览器刷新,重置数据
window.location.href = `${ROUTE_BASE_NAME}${loginPath}`;
return null;
}
/**
* 根据path获取对应的菜单
* @param path
* @param menuTreeData
* @returns {*}
*/
export function getSelectedMenuByPath(path, menuTreeData) {
path = path.replace(ROUTE_BASE_NAME, '');
let selectedMenu;
if (menuTreeData) {
if (path.indexOf('/_') > -1) {
path = path.substring(0, path.indexOf('/_'));
}
// 先精确匹配
selectedMenu = getNodeByPropertyAndValue(menuTreeData, 'path', path, (itemValue, value, item) => {
const isTop = item.children && item.children.length;
return itemValue === value && !isTop; // 排除父级节点
});
// 正则匹配,路由中有`:id`的情况
// fixme 容易出问题:a/b/:id,会匹配 a/b/1, a/b/detail,有可能不是期望的结果,注意路由写法
// fixme: a/b/tab/:id 具体的:id,添加一级,用来表明id是什么
if (!selectedMenu && path !== '/') {
selectedMenu = getNodeByPropertyAndValue(menuTreeData, 'path', path, (itemValue, value, item) => {
const isTop = item.children && item.children.length;
const re = pathToRegexp(itemValue);
return !!re.exec(value) && !isTop; // 排除父级节点
});
}
}
return selectedMenu;
}
/**
* 获取菜单树状结构数据 和 随菜单携带过来的权限
* @param menus 扁平化菜单数据
*/
export function getMenuTreeDataAndPermissions(menus) {
// 用户权限code,通过菜单携带过来的 1 => 菜单 2 => 功能
const permissions = menus.map(item => {
if (item.type === '1') return item.key;
if (item.type === '2') return item.code;
return null;
});
// 获取菜单,过滤掉功能码
menus = menus.filter(item => item.type !== '2');
// 处理path: 只声明了url,为iframe页面
menus = menus.map(item => {
if (item.url) {
item.path = `/iframe_page_/${window.encodeURIComponent(item.url)}`;
}
return item;
});
// 菜单根据order 排序
const orderedData = [...menus].sort((a, b) => {
const aOrder = a.order || 0;
const bOrder = b.order || 0;
// 如果order都不存在,根据 text 排序
if (!aOrder && !bOrder) {
return a.text > b.text ? 1 : -1;
}
return bOrder - aOrder;
});
// 设置顶级节点path,有的顶级没有指定path,默认设置为子孙节点的第一个path
const findPath = (node) => {
const children = orderedData.filter(item => item.parentKey === node.key);
let path = '';
if (children && children.length) {
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.path) {
path = child.path;
break;
}
path = findPath(child);
}
}
return path;
};
orderedData.forEach(item => {
if (!item.path) {
item.path = findPath(item);
}
});
const menuTreeData = convertToTree(orderedData);
return {menuTreeData, permissions}
}