@yysports/tools 0.0.10 → 0.1.0

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,8 +1,11 @@
1
- import { CurrentUserInfo } from '../interfaces/currentUser.interface';
2
- import { CustomizeProfile, ErrorProfile, SystemProfile, TrackConfig, TrackType, UserProfile } from '../interfaces/track.interface';
1
+ import { RouteMapValueType } from '../Utils';
2
+ import { AuthCenterUserProfile, CustomizeProfile, ErrorProfile, SystemProfile, TrackConfig, TrackType, UserProfile } from '../interfaces/track.interface';
3
3
  declare class Track {
4
4
  static config: TrackConfig;
5
5
  static userProfile: UserProfile;
6
+ static authCenterUserProfile: AuthCenterUserProfile;
7
+ static routesMap: Map<string, RouteMapValueType>;
8
+ static authMap: Map<string, string>;
6
9
  static trackQueue: any[];
7
10
  static checkConfig(config?: TrackConfig): void;
8
11
  static send(): Promise<void>;
@@ -10,7 +13,8 @@ declare class Track {
10
13
  static getSystemProfile(): SystemProfile;
11
14
  static getUserProfile(): UserProfile;
12
15
  static setUserProfile(userProfile: UserProfile): void;
13
- static init({ userProfile, menuNameMap, routes, getAuth, menuBuild, ...restConfig }: TrackConfig): Promise<CurrentUserInfo>;
16
+ static initAuthCenterData({ getAuth, menuBuild, menuNameMap, routes, }: Pick<TrackConfig, 'getAuth' | 'menuBuild' | 'menuNameMap' | 'routes'>): Promise<AuthCenterUserProfile>;
17
+ static init({ userProfile, menuNameMap, routes, getAuth, menuBuild, ...restConfig }: TrackConfig): Promise<AuthCenterUserProfile>;
14
18
  static pushQueue({ type, log, }: {
15
19
  type: TrackType;
16
20
  log: ErrorProfile | CustomizeProfile;
@@ -2,14 +2,18 @@ var _excluded = ["userProfile", "menuNameMap", "routes", "getAuth", "menuBuild"]
2
2
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
3
3
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
4
4
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
5
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
5
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
7
6
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
8
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
9
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
10
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
11
7
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
12
8
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
9
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
10
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
11
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
12
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
14
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
15
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
16
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
17
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
14
18
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
15
19
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
@@ -21,8 +25,7 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
21
25
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
22
26
  import Cookies from 'js-cookie';
23
27
  import pako from 'pako';
24
- import CurrentUser from "../CurrentUser";
25
- import { generateRandomString } from "../Utils";
28
+ import { generateRandomString, getAuthMapByAuthCenter, getRoutesMap } from "../Utils";
26
29
  var Track = /*#__PURE__*/function () {
27
30
  function Track() {
28
31
  _classCallCheck(this, Track);
@@ -72,7 +75,6 @@ var Track = /*#__PURE__*/function () {
72
75
  var _this = this;
73
76
  var autoReportError = this.config.autoReportError;
74
77
  if (autoReportError) {
75
- // TODO 短时间内推送超过10个错误,关闭监听
76
78
  addEventListener('error', function (event) {
77
79
  var message = event.message,
78
80
  filename = event.filename;
@@ -142,25 +144,79 @@ var Track = /*#__PURE__*/function () {
142
144
  this.userProfile = userProfile;
143
145
  }
144
146
 
145
- // 挂载初始配置
147
+ // 初始化权限中心数据
146
148
  }, {
147
- key: "init",
149
+ key: "initAuthCenterData",
148
150
  value: function () {
149
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref) {
150
- var _CurrentUser$profile;
151
- var userProfile, menuNameMap, routes, getAuth, menuBuild, restConfig;
151
+ var _initAuthCenterData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref) {
152
+ 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, isAdmin, routesMap, accessPage;
152
153
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
153
154
  while (1) switch (_context2.prev = _context2.next) {
154
155
  case 0:
155
- userProfile = _ref.userProfile, menuNameMap = _ref.menuNameMap, routes = _ref.routes, getAuth = _ref.getAuth, menuBuild = _ref.menuBuild, restConfig = _objectWithoutProperties(_ref, _excluded);
156
+ getAuth = _ref.getAuth, menuBuild = _ref.menuBuild, menuNameMap = _ref.menuNameMap, routes = _ref.routes;
157
+ _context2.next = 3;
158
+ return Promise.all([getAuth,
159
+ // 获取用户权限
160
+ menuBuild // 获取权限中心配置的菜单及按钮
161
+ ].map(function (v) {
162
+ if (v) {
163
+ return v();
164
+ } else {
165
+ return Promise.resolve();
166
+ }
167
+ }));
168
+ case 3:
169
+ _yield$Promise$all = _context2.sent;
170
+ _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
171
+ _yield$Promise$all2$ = _yield$Promise$all2[0];
172
+ authInfo = _yield$Promise$all2$ === void 0 ? {} : _yield$Promise$all2$;
173
+ _yield$Promise$all2$2 = _yield$Promise$all2[1];
174
+ menuData = _yield$Promise$all2$2 === void 0 ? [] : _yield$Promise$all2$2;
175
+ _ref2 = authInfo || {}, _ref2$user = _ref2.user, user = _ref2$user === void 0 ? {} : _ref2$user, _ref2$roles = _ref2.roles, roles = _ref2$roles === void 0 ? [] : _ref2$roles;
176
+ isAdmin = roles.includes('admin'); // 获取本地菜单信息映射
177
+ routesMap = getRoutesMap(routes, menuNameMap); // 当前用户所拥有的所有页面权限
178
+ accessPage = [];
179
+ routesMap.forEach(function (value, key) {
180
+ var access = value.access;
181
+ if (access && (isAdmin || roles.includes(access))) {
182
+ accessPage.push(access);
183
+ }
184
+ });
185
+ this.routesMap = routesMap;
186
+ this.authMap = getAuthMapByAuthCenter(menuData);
187
+ this.authCenterUserProfile = _objectSpread(_objectSpread({}, user), {}, {
188
+ roles: roles,
189
+ accessPage: accessPage
190
+ });
191
+ return _context2.abrupt("return", this.authCenterUserProfile);
192
+ case 18:
193
+ case "end":
194
+ return _context2.stop();
195
+ }
196
+ }, _callee2, this);
197
+ }));
198
+ function initAuthCenterData(_x) {
199
+ return _initAuthCenterData.apply(this, arguments);
200
+ }
201
+ return initAuthCenterData;
202
+ }() // 挂载初始配置
203
+ }, {
204
+ key: "init",
205
+ value: function () {
206
+ var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref3) {
207
+ var userProfile, menuNameMap, routes, getAuth, menuBuild, restConfig;
208
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
209
+ while (1) switch (_context3.prev = _context3.next) {
210
+ case 0:
211
+ userProfile = _ref3.userProfile, menuNameMap = _ref3.menuNameMap, routes = _ref3.routes, getAuth = _ref3.getAuth, menuBuild = _ref3.menuBuild, restConfig = _objectWithoutProperties(_ref3, _excluded);
156
212
  this.checkConfig(restConfig);
157
213
  this.config = restConfig;
158
214
  if (!(routes && menuNameMap)) {
159
- _context2.next = 6;
215
+ _context3.next = 6;
160
216
  break;
161
217
  }
162
- _context2.next = 6;
163
- return CurrentUser.init({
218
+ _context3.next = 6;
219
+ return this.initAuthCenterData({
164
220
  getAuth: getAuth,
165
221
  menuBuild: menuBuild,
166
222
  menuNameMap: menuNameMap,
@@ -168,33 +224,33 @@ var Track = /*#__PURE__*/function () {
168
224
  });
169
225
  case 6:
170
226
  // 如果对接了权限中心,取权限中心的用户信息
171
- if ((_CurrentUser$profile = CurrentUser.profile) !== null && _CurrentUser$profile !== void 0 && _CurrentUser$profile.email) {
227
+ if (this.authCenterUserProfile.email) {
172
228
  this.setUserProfile({
173
- userId: CurrentUser.profile.userName,
174
- userName: CurrentUser.profile.nickName
229
+ userId: this.authCenterUserProfile.userName,
230
+ userName: this.authCenterUserProfile.nickName
175
231
  });
176
232
  }
177
233
  if (userProfile) {
178
234
  this.setUserProfile(userProfile);
179
235
  }
180
236
  this.mountAutoTask();
181
- return _context2.abrupt("return", CurrentUser.profile);
237
+ return _context3.abrupt("return", this.authCenterUserProfile);
182
238
  case 10:
183
239
  case "end":
184
- return _context2.stop();
240
+ return _context3.stop();
185
241
  }
186
- }, _callee2, this);
242
+ }, _callee3, this);
187
243
  }));
188
- function init(_x) {
244
+ function init(_x2) {
189
245
  return _init.apply(this, arguments);
190
246
  }
191
247
  return init;
192
248
  }() // 推送埋点信息进入队列
193
249
  }, {
194
250
  key: "pushQueue",
195
- value: function pushQueue(_ref2) {
196
- var type = _ref2.type,
197
- log = _ref2.log;
251
+ value: function pushQueue(_ref4) {
252
+ var type = _ref4.type,
253
+ log = _ref4.log;
198
254
  var config = this.config;
199
255
  this.checkConfig(config);
200
256
  var reqData = _objectSpread(_objectSpread(_objectSpread({}, this.getSystemProfile()), this.getUserProfile()), {}, {
@@ -223,12 +279,8 @@ var Track = /*#__PURE__*/function () {
223
279
  }, {
224
280
  key: "getPageModuleName",
225
281
  value: function getPageModuleName() {
226
- var routeNames = [];
227
- var pathMap = CurrentUser.pathMap;
228
- if (pathMap) {
229
- routeNames = pathMap[this.getFormatBasePath()] || [];
230
- }
231
- return routeNames;
282
+ var routeInfo = this.routesMap.get(this.getFormatBasePath());
283
+ return (routeInfo === null || routeInfo === void 0 ? void 0 : routeInfo.module) || [];
232
284
  }
233
285
 
234
286
  // 推送错误日志
@@ -250,8 +302,7 @@ var Track = /*#__PURE__*/function () {
250
302
  content: ''
251
303
  };
252
304
  if (typeof _eventLog === 'string') {
253
- var _CurrentUser$authMap;
254
- var actionLog = (_CurrentUser$authMap = CurrentUser.authMap) === null || _CurrentUser$authMap === void 0 ? void 0 : _CurrentUser$authMap[_eventLog];
305
+ var actionLog = this.authMap.get(_eventLog);
255
306
  if (actionLog) {
256
307
  copyEventLog.content = '触发: ' + actionLog;
257
308
  } else {
@@ -305,6 +356,12 @@ var Track = /*#__PURE__*/function () {
305
356
  }();
306
357
  _defineProperty(Track, "config", {});
307
358
  _defineProperty(Track, "userProfile", {});
359
+ // 权限中心用户信息
360
+ _defineProperty(Track, "authCenterUserProfile", {});
361
+ // 路径信息映射
362
+ _defineProperty(Track, "routesMap", new Map());
363
+ // 权限信息映射
364
+ _defineProperty(Track, "authMap", new Map());
308
365
  // 埋点队列
309
366
  _defineProperty(Track, "trackQueue", []);
310
367
  export default Track;
@@ -0,0 +1,7 @@
1
+ export type AuthDataType = {
2
+ children: AuthDataType[];
3
+ name?: string;
4
+ logAction?: string;
5
+ permission?: string;
6
+ };
7
+ export declare const getAuthMapByAuthCenter: (authData: AuthDataType[]) => Map<string, string>;
@@ -0,0 +1,20 @@
1
+ // 根据权限中心返回的数据,生成路由映射及权限数据映射
2
+ export var getAuthMapByAuthCenter = function getAuthMapByAuthCenter(authData) {
3
+ var authMap = new Map();
4
+ var ergodicAuthMenu = function ergodicAuthMenu(authCenterData) {
5
+ authCenterData.forEach(function (menu) {
6
+ var permission = menu.permission,
7
+ name = menu.name,
8
+ logAction = menu.logAction,
9
+ children = menu.children;
10
+ if (permission && logAction) {
11
+ authMap.set(permission, logAction || name || '权限校验');
12
+ }
13
+ if (Array.isArray(children) && children.length > 0) {
14
+ ergodicAuthMenu(children);
15
+ }
16
+ });
17
+ };
18
+ ergodicAuthMenu(authData);
19
+ return authMap;
20
+ };
@@ -2,3 +2,5 @@ export * from './common';
2
2
  export * from './file';
3
3
  export * from './url';
4
4
  export * from './wx';
5
+ export * from './route';
6
+ export * from './auth';
@@ -1,4 +1,6 @@
1
1
  export * from "./common";
2
2
  export * from "./file";
3
3
  export * from "./url";
4
- export * from "./wx";
4
+ export * from "./wx";
5
+ export * from "./route";
6
+ export * from "./auth";
@@ -0,0 +1,15 @@
1
+ export type RouteType = {
2
+ path: string;
3
+ redirect?: string;
4
+ name?: string;
5
+ access?: string;
6
+ routes?: RouteType[];
7
+ };
8
+ export type RouteMapValueType = {
9
+ access?: string;
10
+ name?: string[];
11
+ module?: string[];
12
+ };
13
+ export declare const getRoutesMap: (routes: RouteType[], menuNameMap?: {
14
+ [key: string]: string;
15
+ } | undefined) => Map<string, RouteMapValueType>;
@@ -0,0 +1,46 @@
1
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
5
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
6
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
7
+ // 根据本地路由配置文件、路由地址名称映射、当前用户权限,生成路由映射关系表
8
+
9
+ export var getRoutesMap = function getRoutesMap(routes, menuNameMap) {
10
+ var routesMap = new Map();
11
+ var ergodicLocalMenu = function ergodicLocalMenu(rs, parentMenu) {
12
+ rs.forEach(function (route) {
13
+ var path = route.path,
14
+ access = route.access,
15
+ name = route.name,
16
+ routes = route.routes,
17
+ redirect = route.redirect;
18
+ if (redirect || !name) {
19
+ // 不统计重定向路由、无名称路由
20
+ return;
21
+ }
22
+ var nameArr = _toConsumableArray((parentMenu === null || parentMenu === void 0 ? void 0 : parentMenu.name) || []);
23
+ var moduleArr = _toConsumableArray((parentMenu === null || parentMenu === void 0 ? void 0 : parentMenu.module) || []);
24
+ if (name) {
25
+ nameArr.push(name);
26
+ if (menuNameMap) {
27
+ var menuKey = ['menu'].concat(_toConsumableArray(nameArr)).join('.');
28
+ moduleArr.push(menuNameMap[menuKey]);
29
+ }
30
+ }
31
+ var routeInfo = {
32
+ access: access,
33
+ name: nameArr,
34
+ module: moduleArr
35
+ };
36
+ if (path) {
37
+ routesMap.set(path, routeInfo);
38
+ }
39
+ if (routes) {
40
+ ergodicLocalMenu(routes, routeInfo);
41
+ }
42
+ });
43
+ };
44
+ ergodicLocalMenu(routes);
45
+ return routesMap;
46
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- export { default as CurrentUser } from './CurrentUser';
2
1
  export { default as Track } from './Track';
3
2
  export * as Utils from './Utils';
4
- export * from './interfaces/currentUser.interface';
5
3
  export * from './interfaces/track.interface';
package/dist/index.js CHANGED
@@ -1,6 +1,4 @@
1
- export { default as CurrentUser } from "./CurrentUser";
2
1
  export { default as Track } from "./Track";
3
2
  import * as _Utils from "./Utils";
4
3
  export { _Utils as Utils };
5
- export * from "./interfaces/currentUser.interface";
6
4
  export * from "./interfaces/track.interface";
@@ -1,10 +1,35 @@
1
- import { CurrentUserConfig } from './currentUser.interface';
2
- export interface TrackConfig extends Partial<CurrentUserConfig> {
1
+ import { RouteType } from '../Utils';
2
+ export type LocalRouteItemType = {
3
+ access?: string;
4
+ name?: string;
5
+ parentPath?: string;
6
+ path?: string;
7
+ routes?: LocalRouteItemType[];
8
+ };
9
+ export type AuthMenuItemType = {
10
+ logAction: any;
11
+ name: string;
12
+ meta?: {
13
+ title: string;
14
+ };
15
+ permission?: string;
16
+ children?: AuthMenuItemType[];
17
+ path?: string;
18
+ builtIn?: boolean;
19
+ };
20
+ export type MenuNameMapType = {
21
+ [key: string]: string;
22
+ };
23
+ export interface TrackConfig {
3
24
  appId: string;
4
25
  debug?: boolean;
5
26
  autoReportError?: boolean;
6
27
  userProfile?: UserProfile;
7
28
  base?: string;
29
+ getAuth?: () => Promise<any>;
30
+ menuBuild?: () => Promise<AuthMenuItemType[]>;
31
+ menuNameMap?: MenuNameMapType;
32
+ routes?: RouteType[];
8
33
  }
9
34
  export declare enum TrackTypeEnum {
10
35
  error = "\u9519\u8BEF\u4FE1\u606F",
@@ -17,6 +42,20 @@ export interface UserProfile {
17
42
  userId: string;
18
43
  userName: string;
19
44
  }
45
+ export interface AuthCenterUserProfile {
46
+ userName: string;
47
+ department?: string;
48
+ email?: string;
49
+ enabled?: boolean;
50
+ gender?: string;
51
+ isAdmin?: boolean;
52
+ jobs?: string[];
53
+ nickName?: string;
54
+ phone?: string;
55
+ status?: number;
56
+ roles?: string[];
57
+ accessPage?: string[];
58
+ }
20
59
  export interface SystemProfile {
21
60
  IMEI: string;
22
61
  innerHeight: number;
@@ -9,6 +9,8 @@ export var TrackTypeEnum = /*#__PURE__*/function (TrackTypeEnum) {
9
9
 
10
10
  // 用户信息
11
11
 
12
+ // 权限中心用户信息
13
+
12
14
  // 系统信息
13
15
 
14
16
  // 自定义埋点信息
@@ -1,8 +1,11 @@
1
- import { CurrentUserInfo } from '../interfaces/currentUser.interface';
2
- import { CustomizeProfile, ErrorProfile, SystemProfile, TrackConfig, TrackType, UserProfile } from '../interfaces/track.interface';
1
+ import { RouteMapValueType } from '../Utils';
2
+ import { AuthCenterUserProfile, CustomizeProfile, ErrorProfile, SystemProfile, TrackConfig, TrackType, UserProfile } from '../interfaces/track.interface';
3
3
  declare class Track {
4
4
  static config: TrackConfig;
5
5
  static userProfile: UserProfile;
6
+ static authCenterUserProfile: AuthCenterUserProfile;
7
+ static routesMap: Map<string, RouteMapValueType>;
8
+ static authMap: Map<string, string>;
6
9
  static trackQueue: any[];
7
10
  static checkConfig(config?: TrackConfig): void;
8
11
  static send(): Promise<void>;
@@ -10,7 +13,8 @@ declare class Track {
10
13
  static getSystemProfile(): SystemProfile;
11
14
  static getUserProfile(): UserProfile;
12
15
  static setUserProfile(userProfile: UserProfile): void;
13
- static init({ userProfile, menuNameMap, routes, getAuth, menuBuild, ...restConfig }: TrackConfig): Promise<CurrentUserInfo>;
16
+ static initAuthCenterData({ getAuth, menuBuild, menuNameMap, routes, }: Pick<TrackConfig, 'getAuth' | 'menuBuild' | 'menuNameMap' | 'routes'>): Promise<AuthCenterUserProfile>;
17
+ static init({ userProfile, menuNameMap, routes, getAuth, menuBuild, ...restConfig }: TrackConfig): Promise<AuthCenterUserProfile>;
14
18
  static pushQueue({ type, log, }: {
15
19
  type: TrackType;
16
20
  log: ErrorProfile | CustomizeProfile;
@@ -34,7 +34,6 @@ __export(Track_exports, {
34
34
  module.exports = __toCommonJS(Track_exports);
35
35
  var import_js_cookie = __toESM(require("js-cookie"));
36
36
  var import_pako = __toESM(require("pako"));
37
- var import_CurrentUser = __toESM(require("../CurrentUser"));
38
37
  var import_Utils = require("../Utils");
39
38
  var Track = class {
40
39
  // 检查必填配置项
@@ -108,6 +107,46 @@ var Track = class {
108
107
  static setUserProfile(userProfile) {
109
108
  this.userProfile = userProfile;
110
109
  }
110
+ // 初始化权限中心数据
111
+ static async initAuthCenterData({
112
+ getAuth,
113
+ menuBuild,
114
+ menuNameMap,
115
+ routes
116
+ }) {
117
+ const [authInfo = {}, menuData = []] = await Promise.all(
118
+ [
119
+ getAuth,
120
+ // 获取用户权限
121
+ menuBuild
122
+ // 获取权限中心配置的菜单及按钮
123
+ ].map((v) => {
124
+ if (v) {
125
+ return v();
126
+ } else {
127
+ return Promise.resolve();
128
+ }
129
+ })
130
+ );
131
+ const { user = {}, roles = [] } = authInfo || {};
132
+ const isAdmin = roles.includes("admin");
133
+ const routesMap = (0, import_Utils.getRoutesMap)(routes, menuNameMap);
134
+ const accessPage = [];
135
+ routesMap.forEach((value, key) => {
136
+ const { access } = value;
137
+ if (access && (isAdmin || roles.includes(access))) {
138
+ accessPage.push(access);
139
+ }
140
+ });
141
+ this.routesMap = routesMap;
142
+ this.authMap = (0, import_Utils.getAuthMapByAuthCenter)(menuData);
143
+ this.authCenterUserProfile = {
144
+ ...user,
145
+ roles,
146
+ accessPage
147
+ };
148
+ return this.authCenterUserProfile;
149
+ }
111
150
  // 挂载初始配置
112
151
  static async init({
113
152
  userProfile,
@@ -117,28 +156,27 @@ var Track = class {
117
156
  menuBuild,
118
157
  ...restConfig
119
158
  }) {
120
- var _a;
121
159
  this.checkConfig(restConfig);
122
160
  this.config = restConfig;
123
161
  if (routes && menuNameMap) {
124
- await import_CurrentUser.default.init({
162
+ await this.initAuthCenterData({
125
163
  getAuth,
126
164
  menuBuild,
127
165
  menuNameMap,
128
166
  routes
129
167
  });
130
168
  }
131
- if ((_a = import_CurrentUser.default.profile) == null ? void 0 : _a.email) {
169
+ if (this.authCenterUserProfile.email) {
132
170
  this.setUserProfile({
133
- userId: import_CurrentUser.default.profile.userName,
134
- userName: import_CurrentUser.default.profile.nickName
171
+ userId: this.authCenterUserProfile.userName,
172
+ userName: this.authCenterUserProfile.nickName
135
173
  });
136
174
  }
137
175
  if (userProfile) {
138
176
  this.setUserProfile(userProfile);
139
177
  }
140
178
  this.mountAutoTask();
141
- return import_CurrentUser.default.profile;
179
+ return this.authCenterUserProfile;
142
180
  }
143
181
  // 推送埋点信息进入队列
144
182
  static pushQueue({
@@ -167,12 +205,8 @@ var Track = class {
167
205
  }
168
206
  // 获取模块名称
169
207
  static getPageModuleName() {
170
- let routeNames = [];
171
- const { pathMap } = import_CurrentUser.default;
172
- if (pathMap) {
173
- routeNames = pathMap[this.getFormatBasePath()] || [];
174
- }
175
- return routeNames;
208
+ const routeInfo = this.routesMap.get(this.getFormatBasePath());
209
+ return (routeInfo == null ? void 0 : routeInfo.module) || [];
176
210
  }
177
211
  // 推送错误日志
178
212
  static errorLog(errorLog) {
@@ -183,13 +217,12 @@ var Track = class {
183
217
  }
184
218
  // 推送用户触发日志
185
219
  static eventLog(eventLog) {
186
- var _a;
187
220
  const copyEventLog = {
188
221
  module: "",
189
222
  content: ""
190
223
  };
191
224
  if (typeof eventLog === "string") {
192
- const actionLog = (_a = import_CurrentUser.default.authMap) == null ? void 0 : _a[eventLog];
225
+ const actionLog = this.authMap.get(eventLog);
193
226
  if (actionLog) {
194
227
  copyEventLog.content = "触发: " + actionLog;
195
228
  } else {
@@ -232,6 +265,12 @@ var Track = class {
232
265
  };
233
266
  Track.config = {};
234
267
  Track.userProfile = {};
268
+ // 权限中心用户信息
269
+ Track.authCenterUserProfile = {};
270
+ // 路径信息映射
271
+ Track.routesMap = /* @__PURE__ */ new Map();
272
+ // 权限信息映射
273
+ Track.authMap = /* @__PURE__ */ new Map();
235
274
  // 埋点队列
236
275
  Track.trackQueue = [];
237
276
  var Track_default = Track;
@@ -0,0 +1,7 @@
1
+ export type AuthDataType = {
2
+ children: AuthDataType[];
3
+ name?: string;
4
+ logAction?: string;
5
+ permission?: string;
6
+ };
7
+ export declare const getAuthMapByAuthCenter: (authData: AuthDataType[]) => Map<string, string>;