@yysports/tools 0.0.10 → 0.1.1

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); }); }; }
@@ -19,10 +23,8 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
19
23
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
20
24
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
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
- import Cookies from 'js-cookie';
23
26
  import pako from 'pako';
24
- import CurrentUser from "../CurrentUser";
25
- import { generateRandomString } from "../Utils";
27
+ import { generateRandomString, getAuthMapByAuthCenter, getRoutesMap } from "../Utils";
26
28
  var Track = /*#__PURE__*/function () {
27
29
  function Track() {
28
30
  _classCallCheck(this, Track);
@@ -46,16 +48,18 @@ var Track = /*#__PURE__*/function () {
46
48
  key: "send",
47
49
  value: function () {
48
50
  var _send = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
49
- var data, compressed;
51
+ var debug, data, compressed;
50
52
  return _regeneratorRuntime().wrap(function _callee$(_context) {
51
53
  while (1) switch (_context.prev = _context.next) {
52
54
  case 0:
55
+ debug = this.config.debug;
53
56
  data = this.trackQueue.splice(0, this.trackQueue.length);
54
57
  compressed = pako.deflate(JSON.stringify(data));
55
58
  if (data.length) {
59
+ debug && console.log('[track:debug] push data', data);
56
60
  navigator.sendBeacon("https://track.yysports.com/pousheng-guard/api/analysis/track", compressed);
57
61
  }
58
- case 3:
62
+ case 4:
59
63
  case "end":
60
64
  return _context.stop();
61
65
  }
@@ -72,7 +76,6 @@ var Track = /*#__PURE__*/function () {
72
76
  var _this = this;
73
77
  var autoReportError = this.config.autoReportError;
74
78
  if (autoReportError) {
75
- // TODO 短时间内推送超过10个错误,关闭监听
76
79
  addEventListener('error', function (event) {
77
80
  var message = event.message,
78
81
  filename = event.filename;
@@ -82,16 +85,6 @@ var Track = /*#__PURE__*/function () {
82
85
  };
83
86
  _this.errorLog(errorInfo);
84
87
  });
85
-
86
- // 监听点击事件
87
- // addEventListener(
88
- // 'click',
89
- // (event) => {
90
- // const { target } = event;
91
- // console.log(target.parentElement);
92
- // },
93
- // true
94
- // );
95
88
  }
96
89
 
97
90
  //关闭页面时,发送剩余的埋点数据
@@ -106,18 +99,16 @@ var Track = /*#__PURE__*/function () {
106
99
  }, {
107
100
  key: "getSystemProfile",
108
101
  value: function getSystemProfile() {
109
- var IMEI = Cookies.get('__IMEI__');
102
+ var IMEI = localStorage.getItem('__IMEI__');
110
103
  if (!IMEI) {
111
- Cookies.set('__IMEI__', generateRandomString(16), {
112
- expires: 90
113
- });
104
+ localStorage.setItem('__IMEI__', generateRandomString(16));
114
105
  }
115
106
  return {
116
107
  innerHeight: innerHeight,
117
108
  innerWidth: innerWidth,
118
109
  href: location.href,
119
110
  systemTime: new Date(),
120
- IMEI: Cookies.get('__IMEI__')
111
+ IMEI: localStorage.getItem('__IMEI__')
121
112
  };
122
113
  }
123
114
 
@@ -142,59 +133,111 @@ var Track = /*#__PURE__*/function () {
142
133
  this.userProfile = userProfile;
143
134
  }
144
135
 
145
- // 挂载初始配置
136
+ // 初始化权限中心数据
146
137
  }, {
147
- key: "init",
138
+ key: "initAuthCenterData",
148
139
  value: function () {
149
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref) {
150
- var _CurrentUser$profile;
151
- var userProfile, menuNameMap, routes, getAuth, menuBuild, restConfig;
140
+ var _initAuthCenterData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref) {
141
+ 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, accessPage, routesMap;
152
142
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
153
143
  while (1) switch (_context2.prev = _context2.next) {
154
144
  case 0:
155
- userProfile = _ref.userProfile, menuNameMap = _ref.menuNameMap, routes = _ref.routes, getAuth = _ref.getAuth, menuBuild = _ref.menuBuild, restConfig = _objectWithoutProperties(_ref, _excluded);
145
+ getAuth = _ref.getAuth, menuBuild = _ref.menuBuild, menuNameMap = _ref.menuNameMap, routes = _ref.routes;
146
+ _context2.next = 3;
147
+ return Promise.all([getAuth,
148
+ // 获取用户权限
149
+ menuBuild // 获取权限中心配置的菜单及按钮
150
+ ].map(function (v) {
151
+ if (v) {
152
+ return v();
153
+ } else {
154
+ return Promise.resolve();
155
+ }
156
+ }));
157
+ case 3:
158
+ _yield$Promise$all = _context2.sent;
159
+ _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
160
+ _yield$Promise$all2$ = _yield$Promise$all2[0];
161
+ authInfo = _yield$Promise$all2$ === void 0 ? {} : _yield$Promise$all2$;
162
+ _yield$Promise$all2$2 = _yield$Promise$all2[1];
163
+ menuData = _yield$Promise$all2$2 === void 0 ? [] : _yield$Promise$all2$2;
164
+ _ref2 = authInfo || {}, _ref2$user = _ref2.user, user = _ref2$user === void 0 ? {} : _ref2$user, _ref2$roles = _ref2.roles, roles = _ref2$roles === void 0 ? [] : _ref2$roles;
165
+ isAdmin = roles.includes('admin'); // 当前用户所拥有的所有页面权限
166
+ accessPage = []; // 获取本地菜单信息映射
167
+ if (routes) {
168
+ routesMap = getRoutesMap(routes, menuNameMap);
169
+ routesMap.forEach(function (value, key) {
170
+ var access = value.access;
171
+ if (access && (isAdmin || roles.includes(access))) {
172
+ accessPage.push(access);
173
+ }
174
+ });
175
+ this.routesMap = routesMap;
176
+ }
177
+ this.authMap = getAuthMapByAuthCenter(menuData);
178
+ this.authCenterUserProfile = _objectSpread(_objectSpread({}, user), {}, {
179
+ roles: roles,
180
+ accessPage: accessPage
181
+ });
182
+ return _context2.abrupt("return", this.authCenterUserProfile);
183
+ case 16:
184
+ case "end":
185
+ return _context2.stop();
186
+ }
187
+ }, _callee2, this);
188
+ }));
189
+ function initAuthCenterData(_x) {
190
+ return _initAuthCenterData.apply(this, arguments);
191
+ }
192
+ return initAuthCenterData;
193
+ }() // 挂载初始配置
194
+ }, {
195
+ key: "init",
196
+ value: function () {
197
+ var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref3) {
198
+ var userProfile, menuNameMap, routes, getAuth, menuBuild, restConfig;
199
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
200
+ while (1) switch (_context3.prev = _context3.next) {
201
+ case 0:
202
+ userProfile = _ref3.userProfile, menuNameMap = _ref3.menuNameMap, routes = _ref3.routes, getAuth = _ref3.getAuth, menuBuild = _ref3.menuBuild, restConfig = _objectWithoutProperties(_ref3, _excluded);
156
203
  this.checkConfig(restConfig);
157
204
  this.config = restConfig;
158
- if (!(routes && menuNameMap)) {
159
- _context2.next = 6;
160
- break;
161
- }
162
- _context2.next = 6;
163
- return CurrentUser.init({
205
+ _context3.next = 5;
206
+ return this.initAuthCenterData({
164
207
  getAuth: getAuth,
165
208
  menuBuild: menuBuild,
166
209
  menuNameMap: menuNameMap,
167
210
  routes: routes
168
211
  });
169
- case 6:
212
+ case 5:
170
213
  // 如果对接了权限中心,取权限中心的用户信息
171
- if ((_CurrentUser$profile = CurrentUser.profile) !== null && _CurrentUser$profile !== void 0 && _CurrentUser$profile.email) {
214
+ if (this.authCenterUserProfile.email) {
172
215
  this.setUserProfile({
173
- userId: CurrentUser.profile.userName,
174
- userName: CurrentUser.profile.nickName
216
+ userId: this.authCenterUserProfile.userName,
217
+ userName: this.authCenterUserProfile.nickName
175
218
  });
176
219
  }
177
220
  if (userProfile) {
178
221
  this.setUserProfile(userProfile);
179
222
  }
180
223
  this.mountAutoTask();
181
- return _context2.abrupt("return", CurrentUser.profile);
182
- case 10:
224
+ return _context3.abrupt("return", this.authCenterUserProfile);
225
+ case 9:
183
226
  case "end":
184
- return _context2.stop();
227
+ return _context3.stop();
185
228
  }
186
- }, _callee2, this);
229
+ }, _callee3, this);
187
230
  }));
188
- function init(_x) {
231
+ function init(_x2) {
189
232
  return _init.apply(this, arguments);
190
233
  }
191
234
  return init;
192
235
  }() // 推送埋点信息进入队列
193
236
  }, {
194
237
  key: "pushQueue",
195
- value: function pushQueue(_ref2) {
196
- var type = _ref2.type,
197
- log = _ref2.log;
238
+ value: function pushQueue(_ref4) {
239
+ var type = _ref4.type,
240
+ log = _ref4.log;
198
241
  var config = this.config;
199
242
  this.checkConfig(config);
200
243
  var reqData = _objectSpread(_objectSpread(_objectSpread({}, this.getSystemProfile()), this.getUserProfile()), {}, {
@@ -223,12 +266,8 @@ var Track = /*#__PURE__*/function () {
223
266
  }, {
224
267
  key: "getPageModuleName",
225
268
  value: function getPageModuleName() {
226
- var routeNames = [];
227
- var pathMap = CurrentUser.pathMap;
228
- if (pathMap) {
229
- routeNames = pathMap[this.getFormatBasePath()] || [];
230
- }
231
- return routeNames;
269
+ var routeInfo = this.routesMap.get(this.getFormatBasePath());
270
+ return (routeInfo === null || routeInfo === void 0 ? void 0 : routeInfo.module) || [];
232
271
  }
233
272
 
234
273
  // 推送错误日志
@@ -250,8 +289,7 @@ var Track = /*#__PURE__*/function () {
250
289
  content: ''
251
290
  };
252
291
  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];
292
+ var actionLog = this.authMap.get(_eventLog);
255
293
  if (actionLog) {
256
294
  copyEventLog.content = '触发: ' + actionLog;
257
295
  } else {
@@ -303,8 +341,16 @@ var Track = /*#__PURE__*/function () {
303
341
  }]);
304
342
  return Track;
305
343
  }();
344
+ // 配置信息
306
345
  _defineProperty(Track, "config", {});
346
+ // 需要传入到后端的用户信息
307
347
  _defineProperty(Track, "userProfile", {});
348
+ // 权限中心用户信息
349
+ _defineProperty(Track, "authCenterUserProfile", {});
350
+ // 路径信息映射
351
+ _defineProperty(Track, "routesMap", new Map());
352
+ // 权限信息映射
353
+ _defineProperty(Track, "authMap", new Map());
308
354
  // 埋点队列
309
355
  _defineProperty(Track, "trackQueue", []);
310
356
  export default Track;
@@ -0,0 +1,10 @@
1
+ export type AuthDataType = {
2
+ children: AuthDataType[];
3
+ name?: string;
4
+ logAction?: string;
5
+ permission?: string;
6
+ meta?: {
7
+ title: string;
8
+ };
9
+ };
10
+ export declare const getAuthMapByAuthCenter: (authData: AuthDataType[]) => Map<string, string>;
@@ -0,0 +1,22 @@
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
+ meta = menu.meta;
11
+ if (permission) {
12
+ var authLog = logAction || (meta === null || meta === void 0 ? void 0 : meta.title) || name || '权限校验';
13
+ authMap.set(permission, authLog);
14
+ }
15
+ if (Array.isArray(children) && children.length > 0) {
16
+ ergodicAuthMenu(children);
17
+ }
18
+ });
19
+ };
20
+ ergodicAuthMenu(authData);
21
+ return authMap;
22
+ };
@@ -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;