@yysports/tools 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,13 @@
1
- import { AuthMenuItemType, CurrentUserConfig, CurrentUserInfo, LocalRouteItemType, MenuNameMapType, PathMapItemType } from '../interfaces/currentUser.interface';
1
+ import { AuthMenuItemType, CurrentUserConfig, CurrentUserInfo, LocalRouteItemType, MenuNameMapType } from '../interfaces/currentUser.interface';
2
2
  export default class CurrentUser {
3
3
  static profile: CurrentUserInfo | null;
4
4
  static pathMap: {
5
- [key: string]: PathMapItemType;
5
+ [routeStr: string]: string[];
6
+ };
7
+ static authMap: {
8
+ [authStr: string]: string;
6
9
  };
7
10
  static formatAuthMenu(authMenuData: AuthMenuItemType[]): void;
8
- static formatLocalMenu(routes: LocalRouteItemType[], menuNameMap: MenuNameMapType): void;
11
+ static formatLocalMenu(routes: LocalRouteItemType[], menuNameMap: MenuNameMapType, roles: string[]): string[];
9
12
  static init({ getAuth, menuBuild, menuNameMap, routes, }: CurrentUserConfig): Promise<CurrentUserInfo>;
10
13
  }
@@ -27,33 +27,20 @@ var CurrentUser = /*#__PURE__*/function () {
27
27
  _createClass(CurrentUser, null, [{
28
28
  key: "formatAuthMenu",
29
29
  value:
30
- // 格式化权限中心菜单、按钮数据
30
+ // 格式化权限按钮操作日志
31
31
  function formatAuthMenu(authMenuData) {
32
32
  var _this = this;
33
33
  var ergodicAuthMenu = function ergodicAuthMenu(amd) {
34
- var pathArr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
35
34
  amd.forEach(function (menu) {
36
- var path = menu.path,
37
- permission = menu.permission,
35
+ var permission = menu.permission,
38
36
  name = menu.name,
39
37
  logAction = menu.logAction,
40
38
  children = menu.children;
41
- var currPathArr = _toConsumableArray(pathArr);
42
- if (path) {
43
- currPathArr.push(path === null || path === void 0 ? void 0 : path.replaceAll('/', ''));
44
- }
45
- var currPathObj = _this.pathMap["/".concat(currPathArr.join('/'))];
46
- if (currPathObj) {
47
- currPathObj.hasAccess = true;
48
- }
49
- if (path === undefined && currPathObj) {
50
- // 按钮权限
51
- currPathObj.options[permission] = {
52
- logAction: logAction || name
53
- };
39
+ if (permission && logAction) {
40
+ _this.authMap[permission] = logAction || name;
54
41
  }
55
42
  if (Array.isArray(children) && children.length > 0) {
56
- ergodicAuthMenu(children, _toConsumableArray(currPathArr));
43
+ ergodicAuthMenu(children);
57
44
  }
58
45
  });
59
46
  };
@@ -63,8 +50,10 @@ var CurrentUser = /*#__PURE__*/function () {
63
50
  // 格式化本地菜单权限数据
64
51
  }, {
65
52
  key: "formatLocalMenu",
66
- value: function formatLocalMenu(routes, menuNameMap) {
53
+ value: function formatLocalMenu(routes, menuNameMap, roles) {
67
54
  var _this2 = this;
55
+ var isAdmin = roles.includes('admin');
56
+ var accessPage = [];
68
57
  var ergodicLocalMenu = function ergodicLocalMenu(rs, parentMenu) {
69
58
  rs.forEach(function (route) {
70
59
  var path = route.path,
@@ -73,17 +62,16 @@ var CurrentUser = /*#__PURE__*/function () {
73
62
  routes = route.routes;
74
63
  var nameArr = _toConsumableArray((parentMenu === null || parentMenu === void 0 ? void 0 : parentMenu.name) || []);
75
64
  var moduleArr = _toConsumableArray((parentMenu === null || parentMenu === void 0 ? void 0 : parentMenu.module) || []);
65
+ if (access && (isAdmin || roles.includes(access))) {
66
+ accessPage.push(access);
67
+ }
76
68
  if (name) {
77
69
  nameArr.push(name);
78
70
  var menuKey = ['menu'].concat(_toConsumableArray(nameArr)).join('.');
79
71
  moduleArr.push(menuNameMap[menuKey]);
80
72
  }
81
73
  if (path) {
82
- _this2.pathMap[path] = {
83
- module: _toConsumableArray(moduleArr),
84
- access: access,
85
- options: {}
86
- };
74
+ _this2.pathMap[path] = _toConsumableArray(moduleArr);
87
75
  }
88
76
  if (routes) {
89
77
  ergodicLocalMenu(routes, {
@@ -95,12 +83,12 @@ var CurrentUser = /*#__PURE__*/function () {
95
83
  });
96
84
  };
97
85
  ergodicLocalMenu(routes);
86
+ return accessPage;
98
87
  }
99
88
  }, {
100
89
  key: "init",
101
90
  value: function () {
102
91
  var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
103
- var _this3 = this;
104
92
  var getAuth, menuBuild, menuNameMap, routes, _yield$Promise$all, _yield$Promise$all2, _yield$Promise$all2$, authInfo, _yield$Promise$all2$2, menuData, _ref2, _ref2$user, user, _ref2$roles, roles, accessPage;
105
93
  return _regeneratorRuntime().wrap(function _callee$(_context) {
106
94
  while (1) switch (_context.prev = _context.next) {
@@ -124,26 +112,15 @@ var CurrentUser = /*#__PURE__*/function () {
124
112
  authInfo = _yield$Promise$all2$ === void 0 ? {} : _yield$Promise$all2$;
125
113
  _yield$Promise$all2$2 = _yield$Promise$all2[1];
126
114
  menuData = _yield$Promise$all2$2 === void 0 ? [] : _yield$Promise$all2$2;
127
- _ref2 = authInfo || {}, _ref2$user = _ref2.user, user = _ref2$user === void 0 ? {} : _ref2$user, _ref2$roles = _ref2.roles, roles = _ref2$roles === void 0 ? [] : _ref2$roles;
128
- this.formatLocalMenu(routes, menuNameMap);
115
+ _ref2 = authInfo || {}, _ref2$user = _ref2.user, user = _ref2$user === void 0 ? {} : _ref2$user, _ref2$roles = _ref2.roles, roles = _ref2$roles === void 0 ? [] : _ref2$roles; // 获取本地菜单配置模块名称映射,同时返沪当前用户有权限的页面
116
+ accessPage = this.formatLocalMenu(routes, menuNameMap, roles); // 获取权限中心菜单按钮logAction
129
117
  this.formatAuthMenu(menuData);
130
- accessPage = [];
131
- Object.keys(this.pathMap).forEach(function (path) {
132
- var currObj = _this3.pathMap[path];
133
- if (currObj.access) {
134
- if (user.isAdmin) {
135
- accessPage.push(currObj.access);
136
- } else {
137
- currObj.hasAccess && accessPage.push(currObj.access);
138
- }
139
- }
140
- });
141
118
  this.profile = _objectSpread(_objectSpread({}, user), {}, {
142
119
  roles: roles,
143
120
  accessPage: accessPage
144
121
  });
145
122
  return _context.abrupt("return", this.profile);
146
- case 16:
123
+ case 14:
147
124
  case "end":
148
125
  return _context.stop();
149
126
  }
@@ -161,4 +138,6 @@ var CurrentUser = /*#__PURE__*/function () {
161
138
  _defineProperty(CurrentUser, "profile", null);
162
139
  // 路径信息映射
163
140
  _defineProperty(CurrentUser, "pathMap", {});
141
+ // 权限信息映射
142
+ _defineProperty(CurrentUser, "authMap", {});
164
143
  export { CurrentUser as default };
@@ -205,8 +205,7 @@ var Track = /*#__PURE__*/function () {
205
205
  var routeNames = [];
206
206
  var pathMap = CurrentUser.pathMap;
207
207
  if (pathMap) {
208
- var _pathMap$this$getForm;
209
- routeNames = ((_pathMap$this$getForm = pathMap[this.getFormatBasePath()]) === null || _pathMap$this$getForm === void 0 ? void 0 : _pathMap$this$getForm.module) || [];
208
+ routeNames = pathMap[this.getFormatBasePath()] || [];
210
209
  }
211
210
  return routeNames;
212
211
  }
@@ -226,11 +225,11 @@ var Track = /*#__PURE__*/function () {
226
225
  key: "eventLog",
227
226
  value: function eventLog(_eventLog) {
228
227
  if (typeof _eventLog === 'string') {
229
- var _CurrentUser$pathMap$;
228
+ var _CurrentUser$authMap;
230
229
  var routeNames = this.getPageModuleName();
231
230
  _eventLog = {
232
231
  module: routeNames.join('-'),
233
- content: '触发: ' + ((_CurrentUser$pathMap$ = CurrentUser.pathMap[this.getFormatBasePath()]) === null || _CurrentUser$pathMap$ === void 0 || (_CurrentUser$pathMap$ = _CurrentUser$pathMap$.options) === null || _CurrentUser$pathMap$ === void 0 || (_CurrentUser$pathMap$ = _CurrentUser$pathMap$[_eventLog]) === null || _CurrentUser$pathMap$ === void 0 ? void 0 : _CurrentUser$pathMap$.logAction)
232
+ content: '触发: ' + ((_CurrentUser$authMap = CurrentUser.authMap) === null || _CurrentUser$authMap === void 0 ? void 0 : _CurrentUser$authMap[_eventLog])
234
233
  };
235
234
  }
236
235
  this.pushQueue({
@@ -39,13 +39,3 @@ export interface CurrentUserInfo {
39
39
  roles: string[];
40
40
  accessPage: string[];
41
41
  }
42
- export type PathMapItemType = {
43
- module?: string[];
44
- access?: string;
45
- hasAccess?: boolean;
46
- options: {
47
- [key: string]: {
48
- logAction: string;
49
- };
50
- };
51
- };
@@ -1,10 +1,13 @@
1
- import { AuthMenuItemType, CurrentUserConfig, CurrentUserInfo, LocalRouteItemType, MenuNameMapType, PathMapItemType } from '../interfaces/currentUser.interface';
1
+ import { AuthMenuItemType, CurrentUserConfig, CurrentUserInfo, LocalRouteItemType, MenuNameMapType } from '../interfaces/currentUser.interface';
2
2
  export default class CurrentUser {
3
3
  static profile: CurrentUserInfo | null;
4
4
  static pathMap: {
5
- [key: string]: PathMapItemType;
5
+ [routeStr: string]: string[];
6
+ };
7
+ static authMap: {
8
+ [authStr: string]: string;
6
9
  };
7
10
  static formatAuthMenu(authMenuData: AuthMenuItemType[]): void;
8
- static formatLocalMenu(routes: LocalRouteItemType[], menuNameMap: MenuNameMapType): void;
11
+ static formatLocalMenu(routes: LocalRouteItemType[], menuNameMap: MenuNameMapType, roles: string[]): string[];
9
12
  static init({ getAuth, menuBuild, menuNameMap, routes, }: CurrentUserConfig): Promise<CurrentUserInfo>;
10
13
  }
@@ -23,49 +23,40 @@ __export(CurrentUser_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(CurrentUser_exports);
25
25
  var CurrentUser = class {
26
- // 格式化权限中心菜单、按钮数据
26
+ // 格式化权限按钮操作日志
27
27
  static formatAuthMenu(authMenuData) {
28
- const ergodicAuthMenu = (amd, pathArr = []) => {
28
+ const ergodicAuthMenu = (amd) => {
29
29
  amd.forEach((menu) => {
30
- const { path, permission, name, logAction, children } = menu;
31
- const currPathArr = [...pathArr];
32
- if (path) {
33
- currPathArr.push(path == null ? void 0 : path.replaceAll("/", ""));
34
- }
35
- const currPathObj = this.pathMap[`/${currPathArr.join("/")}`];
36
- if (currPathObj) {
37
- currPathObj.hasAccess = true;
38
- }
39
- if (path === void 0 && currPathObj) {
40
- currPathObj.options[permission] = {
41
- logAction: logAction || name
42
- };
30
+ const { permission, name, logAction, children } = menu;
31
+ if (permission && logAction) {
32
+ this.authMap[permission] = logAction || name;
43
33
  }
44
34
  if (Array.isArray(children) && children.length > 0) {
45
- ergodicAuthMenu(children, [...currPathArr]);
35
+ ergodicAuthMenu(children);
46
36
  }
47
37
  });
48
38
  };
49
39
  ergodicAuthMenu(authMenuData);
50
40
  }
51
41
  // 格式化本地菜单权限数据
52
- static formatLocalMenu(routes, menuNameMap) {
42
+ static formatLocalMenu(routes, menuNameMap, roles) {
43
+ const isAdmin = roles.includes("admin");
44
+ const accessPage = [];
53
45
  const ergodicLocalMenu = (rs, parentMenu) => {
54
46
  rs.forEach((route) => {
55
47
  const { path, access, name, routes: routes2 } = route;
56
48
  const nameArr = [...(parentMenu == null ? void 0 : parentMenu.name) || []];
57
49
  const moduleArr = [...(parentMenu == null ? void 0 : parentMenu.module) || []];
50
+ if (access && (isAdmin || roles.includes(access))) {
51
+ accessPage.push(access);
52
+ }
58
53
  if (name) {
59
54
  nameArr.push(name);
60
55
  const menuKey = ["menu", ...nameArr].join(".");
61
56
  moduleArr.push(menuNameMap[menuKey]);
62
57
  }
63
58
  if (path) {
64
- this.pathMap[path] = {
65
- module: [...moduleArr],
66
- access,
67
- options: {}
68
- };
59
+ this.pathMap[path] = [...moduleArr];
69
60
  }
70
61
  if (routes2) {
71
62
  ergodicLocalMenu(routes2, {
@@ -77,6 +68,7 @@ var CurrentUser = class {
77
68
  });
78
69
  };
79
70
  ergodicLocalMenu(routes);
71
+ return accessPage;
80
72
  }
81
73
  static async init({
82
74
  getAuth,
@@ -99,19 +91,8 @@ var CurrentUser = class {
99
91
  })
100
92
  );
101
93
  const { user = {}, roles = [] } = authInfo || {};
102
- this.formatLocalMenu(routes, menuNameMap);
94
+ const accessPage = this.formatLocalMenu(routes, menuNameMap, roles);
103
95
  this.formatAuthMenu(menuData);
104
- const accessPage = [];
105
- Object.keys(this.pathMap).forEach((path) => {
106
- const currObj = this.pathMap[path];
107
- if (currObj.access) {
108
- if (user.isAdmin) {
109
- accessPage.push(currObj.access);
110
- } else {
111
- currObj.hasAccess && accessPage.push(currObj.access);
112
- }
113
- }
114
- });
115
96
  this.profile = {
116
97
  ...user,
117
98
  roles,
@@ -124,3 +105,5 @@ var CurrentUser = class {
124
105
  CurrentUser.profile = null;
125
106
  // 路径信息映射
126
107
  CurrentUser.pathMap = {};
108
+ // 权限信息映射
109
+ CurrentUser.authMap = {};
@@ -154,11 +154,10 @@ var Track = class {
154
154
  }
155
155
  // 获取模块名称
156
156
  static getPageModuleName() {
157
- var _a;
158
157
  let routeNames = [];
159
158
  const { pathMap } = import_CurrentUser.default;
160
159
  if (pathMap) {
161
- routeNames = ((_a = pathMap[this.getFormatBasePath()]) == null ? void 0 : _a.module) || [];
160
+ routeNames = pathMap[this.getFormatBasePath()] || [];
162
161
  }
163
162
  return routeNames;
164
163
  }
@@ -171,12 +170,12 @@ var Track = class {
171
170
  }
172
171
  // 推送用户触发日志
173
172
  static eventLog(eventLog) {
174
- var _a, _b, _c;
173
+ var _a;
175
174
  if (typeof eventLog === "string") {
176
175
  const routeNames = this.getPageModuleName();
177
176
  eventLog = {
178
177
  module: routeNames.join("-"),
179
- content: "触发: " + ((_c = (_b = (_a = import_CurrentUser.default.pathMap[this.getFormatBasePath()]) == null ? void 0 : _a.options) == null ? void 0 : _b[eventLog]) == null ? void 0 : _c.logAction)
178
+ content: "触发: " + ((_a = import_CurrentUser.default.authMap) == null ? void 0 : _a[eventLog])
180
179
  };
181
180
  }
182
181
  this.pushQueue({
@@ -39,13 +39,3 @@ export interface CurrentUserInfo {
39
39
  roles: string[];
40
40
  accessPage: string[];
41
41
  }
42
- export type PathMapItemType = {
43
- module?: string[];
44
- access?: string;
45
- hasAccess?: boolean;
46
- options: {
47
- [key: string]: {
48
- logAction: string;
49
- };
50
- };
51
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yysports/tools",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "常用工具函数库",
5
5
  "homepage": "https://git.pousheng.com/sport-service-department/pousheng-guard/yysports_tools#readme",
6
6
  "bugs": {
@@ -72,9 +72,9 @@
72
72
  "@types/pako": "^2.0.3",
73
73
  "@types/react": "^18.3.3",
74
74
  "@types/react-dom": "^18.3.0",
75
- "@umijs/lint": "^4.3.0",
75
+ "@umijs/lint": "^4.3.1",
76
76
  "antd": "^5.19.0",
77
- "dumi": "^2.4.4",
77
+ "dumi": "^2.4.5",
78
78
  "eslint": "^9.6.0",
79
79
  "father": "^4.4.4",
80
80
  "husky": "^9.0.11",
@@ -86,10 +86,7 @@
86
86
  "react-dom": "^18.3.1",
87
87
  "stylelint": "^16.6.1"
88
88
  },
89
- "peerDependencies": {
90
- "react": ">=16.9.0",
91
- "react-dom": ">=16.9.0"
92
- },
89
+ "peerDependencies": {},
93
90
  "publishConfig": {
94
91
  "access": "public"
95
92
  },