@yysports/tools 1.1.0 → 2.0.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.
Files changed (56) hide show
  1. package/dist/Track/core/auth.d.ts +16 -0
  2. package/dist/Track/core/auth.js +83 -0
  3. package/dist/Track/core/constants.d.ts +14 -0
  4. package/dist/Track/core/constants.js +25 -0
  5. package/dist/Track/core/envelope.d.ts +9 -0
  6. package/dist/Track/core/envelope.js +54 -0
  7. package/dist/Track/core/instance.d.ts +108 -0
  8. package/dist/Track/core/instance.js +365 -0
  9. package/dist/Track/core/lifecycle.d.ts +18 -0
  10. package/dist/Track/core/lifecycle.js +59 -0
  11. package/dist/Track/core/profile.d.ts +5 -0
  12. package/dist/Track/core/profile.js +30 -0
  13. package/dist/Track/core/queue.d.ts +13 -0
  14. package/dist/Track/core/queue.js +36 -0
  15. package/dist/Track/core/replay.d.ts +17 -0
  16. package/dist/Track/core/replay.js +117 -0
  17. package/dist/Track/core/router.d.ts +6 -0
  18. package/dist/Track/core/router.js +15 -0
  19. package/dist/Track/core/system.d.ts +14 -0
  20. package/dist/Track/core/system.js +36 -0
  21. package/dist/Track/core/transport.d.ts +18 -0
  22. package/dist/Track/core/transport.js +151 -0
  23. package/dist/Track/index.d.ts +19 -28
  24. package/dist/Track/index.js +144 -396
  25. package/dist/Utils/common.js +5 -6
  26. package/dist/Utils/route.js +1 -6
  27. package/dist/Utils/url.js +1 -3
  28. package/dist/Utils/wx.js +2 -7
  29. package/dist/interfaces/track.interface.d.ts +14 -4
  30. package/lib/Track/core/auth.d.ts +16 -0
  31. package/lib/Track/core/auth.js +67 -0
  32. package/lib/Track/core/constants.d.ts +14 -0
  33. package/lib/Track/core/constants.js +56 -0
  34. package/lib/Track/core/envelope.d.ts +9 -0
  35. package/lib/Track/core/envelope.js +61 -0
  36. package/lib/Track/core/instance.d.ts +108 -0
  37. package/lib/Track/core/instance.js +257 -0
  38. package/lib/Track/core/lifecycle.d.ts +18 -0
  39. package/lib/Track/core/lifecycle.js +74 -0
  40. package/lib/Track/core/profile.d.ts +5 -0
  41. package/lib/Track/core/profile.js +47 -0
  42. package/lib/Track/core/queue.d.ts +13 -0
  43. package/lib/Track/core/queue.js +49 -0
  44. package/lib/Track/core/replay.d.ts +17 -0
  45. package/lib/Track/core/replay.js +131 -0
  46. package/lib/Track/core/router.d.ts +6 -0
  47. package/lib/Track/core/router.js +39 -0
  48. package/lib/Track/core/system.d.ts +14 -0
  49. package/lib/Track/core/system.js +53 -0
  50. package/lib/Track/core/transport.d.ts +18 -0
  51. package/lib/Track/core/transport.js +115 -0
  52. package/lib/Track/index.d.ts +19 -28
  53. package/lib/Track/index.js +93 -317
  54. package/lib/Utils/common.js +4 -6
  55. package/lib/interfaces/track.interface.d.ts +14 -4
  56. package/package.json +11 -10
@@ -0,0 +1,16 @@
1
+ import { AuthCenterUserProfile, RouteMapValueType, TrackConfig } from '../../interfaces/track.interface';
2
+ export interface AuthInitResult {
3
+ authCenterUserProfile: AuthCenterUserProfile;
4
+ routesMap: Map<string, RouteMapValueType>;
5
+ authMap: Map<string, string>;
6
+ }
7
+ /**
8
+ * 初始化权限 / 菜单 / 路由配置信息
9
+ * 1. 并行请求:用户权限(getAuth) / 菜单(menuBuild) / 数据域权限(getDataScopeZonePermissions)
10
+ * 2. 生成 routesMap 以及可访问页面 accessPage
11
+ * 3. 生成 authMap(按钮/操作编码 -> 中文描述)
12
+ * 4. 组织返回 AuthCenterUserProfile
13
+ * @param config TrackConfig
14
+ * @returns {AuthInitResult}
15
+ */
16
+ export declare function initAuthCenterData(config: TrackConfig): Promise<AuthInitResult>;
@@ -0,0 +1,83 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
5
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
7
+ var _excluded = ["roles"];
8
+ /**
9
+ * 权限中心 / 菜单 / 数据权限初始化逻辑
10
+ */
11
+ import { getAuthMapByAuthCenter, getRoutesMap } from "../../Utils";
12
+ /**
13
+ * 初始化权限 / 菜单 / 路由配置信息
14
+ * 1. 并行请求:用户权限(getAuth) / 菜单(menuBuild) / 数据域权限(getDataScopeZonePermissions)
15
+ * 2. 生成 routesMap 以及可访问页面 accessPage
16
+ * 3. 生成 authMap(按钮/操作编码 -> 中文描述)
17
+ * 4. 组织返回 AuthCenterUserProfile
18
+ * @param config TrackConfig
19
+ * @returns {AuthInitResult}
20
+ */
21
+ export function initAuthCenterData(_x) {
22
+ return _initAuthCenterData.apply(this, arguments);
23
+ }
24
+ function _initAuthCenterData() {
25
+ _initAuthCenterData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
26
+ var getAuth, menuBuild, getDataScopeZonePermissions, menuNameMap, routes, tasks, _yield$Promise$all, _yield$Promise$all2, _yield$Promise$all2$, authInfo, _yield$Promise$all2$2, menuData, dataScopeZonePermissions, _ref, _ref$user, user, _ref$roles, roles, _ref2, dataScopeRoles, restUser, isAdmin, accessPage, routesMap, authMap, profile;
27
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
28
+ while (1) switch (_context.prev = _context.next) {
29
+ case 0:
30
+ getAuth = config.getAuth, menuBuild = config.menuBuild, getDataScopeZonePermissions = config.getDataScopeZonePermissions, menuNameMap = config.menuNameMap, routes = config.routes; // 并行触发三个可选任务(若未提供则使用 resolved Promise)
31
+ tasks = [getAuth === null || getAuth === void 0 ? void 0 : getAuth(), menuBuild === null || menuBuild === void 0 ? void 0 : menuBuild(), getDataScopeZonePermissions === null || getDataScopeZonePermissions === void 0 ? void 0 : getDataScopeZonePermissions()];
32
+ _context.next = 4;
33
+ return Promise.all(tasks.map(function (t) {
34
+ return t !== null && t !== void 0 ? t : Promise.resolve();
35
+ }));
36
+ case 4:
37
+ _yield$Promise$all = _context.sent;
38
+ _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 3);
39
+ _yield$Promise$all2$ = _yield$Promise$all2[0];
40
+ authInfo = _yield$Promise$all2$ === void 0 ? {} : _yield$Promise$all2$;
41
+ _yield$Promise$all2$2 = _yield$Promise$all2[1];
42
+ menuData = _yield$Promise$all2$2 === void 0 ? [] : _yield$Promise$all2$2;
43
+ dataScopeZonePermissions = _yield$Promise$all2[2];
44
+ _ref = authInfo || {}, _ref$user = _ref.user, user = _ref$user === void 0 ? {} : _ref$user, _ref$roles = _ref.roles, roles = _ref$roles === void 0 ? [] : _ref$roles;
45
+ _ref2 = user, dataScopeRoles = _ref2.roles, restUser = _objectWithoutProperties(_ref2, _excluded);
46
+ isAdmin = roles.includes('admin');
47
+ accessPage = [];
48
+ routesMap = new Map();
49
+ if (routes) {
50
+ routesMap = getRoutesMap(routes, menuNameMap);
51
+ routesMap.forEach(function (_ref3) {
52
+ var access = _ref3.access;
53
+ if (!access.length) return;
54
+ if (isAdmin) accessPage.push.apply(accessPage, _toConsumableArray(access));else access.forEach(function (k) {
55
+ return roles.includes(k) && accessPage.push(k);
56
+ });
57
+ });
58
+ }
59
+ authMap = getAuthMapByAuthCenter(menuData);
60
+ profile = _objectSpread(_objectSpread({}, restUser), {}, {
61
+ dataScopeRoles: dataScopeRoles,
62
+ roles: roles,
63
+ accessPage: accessPage,
64
+ isAdmin: isAdmin
65
+ });
66
+ if (dataScopeZonePermissions)
67
+ // 合并数据域区权限结构
68
+ Object.assign(profile, {
69
+ dataScopeZonePermissions: dataScopeZonePermissions
70
+ });
71
+ return _context.abrupt("return", {
72
+ authCenterUserProfile: profile,
73
+ routesMap: routesMap,
74
+ authMap: authMap
75
+ });
76
+ case 21:
77
+ case "end":
78
+ return _context.stop();
79
+ }
80
+ }, _callee);
81
+ }));
82
+ return _initAuthCenterData.apply(this, arguments);
83
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Track 常量定义
3
+ * 集中维护埋点内部所需的可配置默认值、端点等信息。
4
+ */
5
+ export declare const DEFAULT_FLUSH_THRESHOLD = 5;
6
+ export declare const DEFAULT_MAX_WAIT_MS = 5000;
7
+ export declare const TRACK_ENDPOINT = "/pousheng-guard/api/track";
8
+ export declare const APP_VERSION_ENDPOINT = "/pousheng-guard/api/apps/version/update";
9
+ export declare const ERROR_TRACK_ENDPOINT = "/pousheng-guard/api/track/error";
10
+ export declare const ERROR_REPLAY_ENDPOINT = "/pousheng-guard/api/track/error-replay/";
11
+ export declare const DEFAULT_REPLAY_CHECKOUT_NMS: number;
12
+ export declare const DEFAULT_REPLAY_MAX_EVENTS = 1000;
13
+ export declare const DEFAULT_REPLAY_MAX_BYTES: number;
14
+ export declare const DEFAULT_ERROR_THROTTLE_MS = 3000;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Track 常量定义
3
+ * 集中维护埋点内部所需的可配置默认值、端点等信息。
4
+ */
5
+
6
+ // 队列长度达到该阈值触发自动 flush
7
+ export var DEFAULT_FLUSH_THRESHOLD = 5;
8
+
9
+ // 最大等待时间(毫秒)默认值,用于定时 flush
10
+ export var DEFAULT_MAX_WAIT_MS = 5000;
11
+
12
+ // 后端统一接口路径片段
13
+ export var TRACK_ENDPOINT = '/pousheng-guard/api/track';
14
+ export var APP_VERSION_ENDPOINT = '/pousheng-guard/api/apps/version/update';
15
+ export var ERROR_TRACK_ENDPOINT = '/pousheng-guard/api/track/error';
16
+ export var ERROR_REPLAY_ENDPOINT = '/pousheng-guard/api/track/error-replay/';
17
+
18
+ // rrweb 默认参数
19
+ export var DEFAULT_REPLAY_CHECKOUT_NMS = 5 * 60 * 1000; // 5min
20
+ export var DEFAULT_REPLAY_MAX_EVENTS = 1000;
21
+ // 默认最大携带体积:1MB(避免截断 FullSnapshot 导致无法回放)
22
+ export var DEFAULT_REPLAY_MAX_BYTES = 1024 * 1024; // 1MB
23
+
24
+ // 错误节流默认窗口(毫秒)
25
+ export var DEFAULT_ERROR_THROTTLE_MS = 3000;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 事件包构建与采样控制
3
+ */
4
+ import { TrackConfig, TrackEventEnvelope, TrackType, UserProfile } from '../../interfaces/track.interface';
5
+ /**
6
+ * 构建事件信封(统一字段拼装)
7
+ * - systemProfile + user + type + appId + 序列化后的log
8
+ */
9
+ export declare function buildEnvelope(config: TrackConfig, user: UserProfile, type: TrackType, log: any): TrackEventEnvelope;
@@ -0,0 +1,54 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
3
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4
+ var _excluded = ["module", "content"];
5
+ /**
6
+ * 事件包构建与采样控制
7
+ */
8
+
9
+ import { getSystemProfile } from "./system";
10
+
11
+ // 全量采样:不再基于采样率过滤事件
12
+
13
+ /**
14
+ * 构建事件信封(统一字段拼装)
15
+ * - systemProfile + user + type + appId + 序列化后的log
16
+ */
17
+ export function buildEnvelope(config, user, type, log) {
18
+ var trackData = _objectSpread(_objectSpread(_objectSpread({}, getSystemProfile()), user), {}, {
19
+ type: type,
20
+ appId: config.appId
21
+ });
22
+ if (typeof log === 'string') {
23
+ trackData.log = log; // 字符串直接赋值
24
+ }
25
+ if (_typeof(log) === 'object') {
26
+ var copyLog = {};
27
+ switch (type) {
28
+ case 'visit':
29
+ case 'event':
30
+ var module = log.module,
31
+ content = log.content,
32
+ rest = _objectWithoutProperties(log, _excluded);
33
+ copyLog = rest; // 解构后移除 module 字段
34
+ Object.assign(trackData, {
35
+ module: module,
36
+ content: content
37
+ });
38
+ break;
39
+ case 'error':
40
+ Object.assign(trackData, {
41
+ log: JSON.stringify(log)
42
+ });
43
+ break;
44
+ default:
45
+ // 对于其他类型,保持 log 不变
46
+ copyLog = log;
47
+ break;
48
+ }
49
+ if (Object.keys(copyLog).length > 0) {
50
+ trackData.log = JSON.stringify(copyLog);
51
+ }
52
+ }
53
+ return trackData;
54
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * TrackerInstance - 多实例核心组合:权限初始化 + 队列 + 采样 + 生命周期
3
+ */
4
+ import { AuthCenterUserProfile, CustomizeProfile, ErrorProfile, TrackConfig, UserProfile } from '../../interfaces/track.interface';
5
+ export declare class TrackerInstance {
6
+ private config;
7
+ private userProfile;
8
+ private authCenterUserProfile;
9
+ private routesMap;
10
+ private authMap;
11
+ private queue;
12
+ private internal;
13
+ private replay?;
14
+ private lifecycle?;
15
+ /**
16
+ * 构造函数:合并配置 + 参数校验
17
+ */
18
+ constructor(cfg: TrackConfig);
19
+ /**
20
+ * 初始化:
21
+ * 1. 拉取权限/菜单/数据域信息
22
+ * 2. 合并用户画像(优先 AuthCenter,再叠加外部传入)
23
+ * 3. 挂载生命周期监听(visibilitychange / error)
24
+ * 4. 上报版本信息 & 启动定时 flush
25
+ */
26
+ init(): Promise<AuthCenterUserProfile>;
27
+ /**
28
+ * Flush:
29
+ * - 取出队列中全部事件
30
+ */
31
+ flush(): void;
32
+ /**
33
+ * 销毁:清理定时器 & 事件监听,并 flush 剩余数据
34
+ */
35
+ destroy(): void;
36
+ /**
37
+ * 事件日志:
38
+ * - 支持传入字符串(自动映射为 authMap 描述)或对象
39
+ * - 自动补全 module(基于当前路由)
40
+ */
41
+ eventLog(eventLog: CustomizeProfile | string): void;
42
+ /**
43
+ * 访问日志:
44
+ * - 默认按当前路由模块组装文案
45
+ * - 若路由标记 isRedirect 则跳过(避免中转页统计)
46
+ */
47
+ visitLog(visitLog?: CustomizeProfile): void;
48
+ /**
49
+ * 错误日志:force = true 不受采样率影响
50
+ */
51
+ errorLog(error: ErrorProfile): Promise<void>;
52
+ /**
53
+ * API 日志:直接即时上报(不进入公共队列)
54
+ */
55
+ apiLog(apiLog: any): void;
56
+ /**
57
+ * 动态更新用户画像(不会重置已有字段)
58
+ */
59
+ setUserProfile(patch: Partial<UserProfile>): void;
60
+ /** 获取当前实例配置副本(避免外部直接修改内部对象) */
61
+ getConfig(): {
62
+ appId: string;
63
+ userProfile?: UserProfile | undefined;
64
+ base?: string | undefined;
65
+ getAuth?: (() => Promise<any>) | undefined;
66
+ menuBuild?: (() => Promise<import("../../interfaces/track.interface").AuthMenuItemType[]>) | undefined;
67
+ menuNameMap?: import("../../interfaces/track.interface").MenuNameMapType | undefined;
68
+ routes?: import("../../interfaces/track.interface").RouteType[] | undefined;
69
+ getDataScopeZonePermissions?: (() => Promise<{
70
+ zoneId: number;
71
+ zoneName: string;
72
+ }[]>) | undefined;
73
+ domain: string;
74
+ enableReplay?: boolean | undefined;
75
+ };
76
+ /** 配置校验:缺少关键字段直接抛错 */
77
+ private validate;
78
+ /**
79
+ * 合并用户画像:
80
+ * - 若权限中心未返回 userName,则只使用外部提供
81
+ * - 否则使用 AuthCenter 信息并叠加外部 patch
82
+ */
83
+ private mergeUserProfile;
84
+ /**
85
+ * 入队封装:
86
+ * - 全量采样
87
+ * - 生成 envelope
88
+ * - 触发阈值自动 flush
89
+ */
90
+ private enqueue;
91
+ /** 获取当前路径(剥离 base 前缀) */
92
+ private getFormatPath;
93
+ /** 获取当前页面的模块层级(由 routesMap 提供) */
94
+ private getPageModules;
95
+ /** 挂载浏览器事件监听 */
96
+ private mountLifecycle;
97
+ /** 启动/重置周期性 flush 计时器 */
98
+ private scheduleFlushTimer;
99
+ /** 校验 module 是否有效(非空且非纯空白) */
100
+ private isValidModule;
101
+ /** 获取过滤后的模块层级(去除空项和空白) */
102
+ private getFilteredModules;
103
+ /** 若日志缺少 module,则尝试用路由模块补齐;返回是否有效 */
104
+ private fillModuleIfMissing;
105
+ private maybeStartReplay;
106
+ private stopReplay;
107
+ private buildReplayAttachment;
108
+ }
@@ -0,0 +1,365 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
5
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
6
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
7
+ /**
8
+ * TrackerInstance - 多实例核心组合:权限初始化 + 队列 + 采样 + 生命周期
9
+ */
10
+ // rrweb 录制管理
11
+
12
+ import { initAuthCenterData } from "./auth";
13
+ import { DEFAULT_ERROR_THROTTLE_MS, DEFAULT_FLUSH_THRESHOLD, DEFAULT_MAX_WAIT_MS } from "./constants";
14
+ import { buildEnvelope } from "./envelope";
15
+ import { LifecycleManager } from "./lifecycle";
16
+ import { mergeUserProfile as _mergeUserProfile } from "./profile";
17
+ import { TrackQueue } from "./queue";
18
+ import { ReplayManager } from "./replay";
19
+ import { getFormattedPath, getPageModules as _getPageModules } from "./router";
20
+ import { reportApiLog, reportAppVersion, reportBatch, reportError } from "./transport";
21
+ export var TrackerInstance = /*#__PURE__*/function () {
22
+ /**
23
+ * 构造函数:合并配置 + 参数校验
24
+ */
25
+ function TrackerInstance(cfg) {
26
+ _classCallCheck(this, TrackerInstance);
27
+ _defineProperty(this, "config", void 0);
28
+ _defineProperty(this, "userProfile", {
29
+ userId: '',
30
+ userName: ''
31
+ });
32
+ _defineProperty(this, "authCenterUserProfile", {});
33
+ _defineProperty(this, "routesMap", new Map());
34
+ _defineProperty(this, "authMap", new Map());
35
+ _defineProperty(this, "queue", new TrackQueue());
36
+ _defineProperty(this, "internal", {
37
+ initialized: false,
38
+ destroyed: false
39
+ });
40
+ // 录制管理器
41
+ _defineProperty(this, "replay", void 0);
42
+ // 生命周期与错误监听
43
+ _defineProperty(this, "lifecycle", void 0);
44
+ this.config = _objectSpread({
45
+ // 回放默认开启
46
+ enableReplay: true
47
+ }, cfg);
48
+ this.validate();
49
+ }
50
+
51
+ /**
52
+ * 初始化:
53
+ * 1. 拉取权限/菜单/数据域信息
54
+ * 2. 合并用户画像(优先 AuthCenter,再叠加外部传入)
55
+ * 3. 挂载生命周期监听(visibilitychange / error)
56
+ * 4. 上报版本信息 & 启动定时 flush
57
+ */
58
+ _createClass(TrackerInstance, [{
59
+ key: "init",
60
+ value: (function () {
61
+ var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
62
+ var _yield$initAuthCenter, authCenterUserProfile, authMap, routesMap;
63
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
64
+ while (1) switch (_context.prev = _context.next) {
65
+ case 0:
66
+ if (!this.internal.initialized) {
67
+ _context.next = 2;
68
+ break;
69
+ }
70
+ return _context.abrupt("return", this.authCenterUserProfile);
71
+ case 2:
72
+ _context.next = 4;
73
+ return initAuthCenterData(this.config);
74
+ case 4:
75
+ _yield$initAuthCenter = _context.sent;
76
+ authCenterUserProfile = _yield$initAuthCenter.authCenterUserProfile;
77
+ authMap = _yield$initAuthCenter.authMap;
78
+ routesMap = _yield$initAuthCenter.routesMap;
79
+ this.authCenterUserProfile = authCenterUserProfile;
80
+ this.authMap = authMap;
81
+ this.routesMap = routesMap;
82
+ this.mergeUserProfile();
83
+ this.mountLifecycle();
84
+ this.maybeStartReplay();
85
+ reportAppVersion(this.config);
86
+ this.internal.initialized = true;
87
+ this.scheduleFlushTimer();
88
+ return _context.abrupt("return", this.authCenterUserProfile);
89
+ case 18:
90
+ case "end":
91
+ return _context.stop();
92
+ }
93
+ }, _callee, this);
94
+ }));
95
+ function init() {
96
+ return _init.apply(this, arguments);
97
+ }
98
+ return init;
99
+ }()
100
+ /**
101
+ * Flush:
102
+ * - 取出队列中全部事件
103
+ */
104
+ )
105
+ }, {
106
+ key: "flush",
107
+ value: function flush() {
108
+ if (!this.queue.size()) return;
109
+ var batch = this.queue.drain();
110
+ reportBatch(this.config, batch);
111
+ }
112
+
113
+ /**
114
+ * 销毁:清理定时器 & 事件监听,并 flush 剩余数据
115
+ */
116
+ }, {
117
+ key: "destroy",
118
+ value: function destroy() {
119
+ var _this$lifecycle;
120
+ if (this.internal.destroyed) return;
121
+ this.flush();
122
+ (_this$lifecycle = this.lifecycle) === null || _this$lifecycle === void 0 || _this$lifecycle.unmount();
123
+ this.stopReplay();
124
+ clearInterval(this.internal.flushTimer);
125
+ this.internal.destroyed = true;
126
+ }
127
+
128
+ /**
129
+ * 事件日志:
130
+ * - 支持传入字符串(自动映射为 authMap 描述)或对象
131
+ * - 自动补全 module(基于当前路由)
132
+ */
133
+ }, {
134
+ key: "eventLog",
135
+ value: function eventLog(_eventLog) {
136
+ var data = {
137
+ module: '',
138
+ content: ''
139
+ };
140
+ if (typeof _eventLog === 'string') {
141
+ var action = this.authMap.get(_eventLog);
142
+ data.content = action ? '触发: ' + action : _eventLog;
143
+ } else Object.assign(data, _eventLog);
144
+
145
+ // 统一的模块补齐与校验
146
+ var modules = this.getFilteredModules();
147
+ if (!this.fillModuleIfMissing(data, modules)) return;
148
+ this.enqueue('event', data);
149
+ }
150
+
151
+ /**
152
+ * 访问日志:
153
+ * - 默认按当前路由模块组装文案
154
+ * - 若路由标记 isRedirect 则跳过(避免中转页统计)
155
+ */
156
+ }, {
157
+ key: "visitLog",
158
+ value: function visitLog(_visitLog) {
159
+ var routeInfo = this.routesMap.get(this.getFormatPath());
160
+ if (routeInfo !== null && routeInfo !== void 0 && routeInfo.isRedirect) return;
161
+ var mods = this.getFilteredModules();
162
+ if (!_visitLog) {
163
+ if (!mods.length) return;
164
+ _visitLog = {
165
+ module: mods.join('-'),
166
+ content: "\u8BBF\u95EE\u9875\u9762: ".concat(mods[mods.length - 1])
167
+ };
168
+ }
169
+ // 统一的模块补齐与校验
170
+ if (!this.fillModuleIfMissing(_visitLog, mods)) return;
171
+ this.enqueue('visit', _visitLog);
172
+ }
173
+
174
+ /**
175
+ * 错误日志:force = true 不受采样率影响
176
+ */
177
+ }, {
178
+ key: "errorLog",
179
+ value: (function () {
180
+ var _errorLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(error) {
181
+ var _this$replay;
182
+ var throttle, now, replayPayload, env;
183
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
184
+ while (1) switch (_context2.prev = _context2.next) {
185
+ case 0:
186
+ // 简单节流:同一窗口只发送首条
187
+ throttle = DEFAULT_ERROR_THROTTLE_MS;
188
+ now = Date.now();
189
+ if (!(this.internal.lastErrorSentAt && now - this.internal.lastErrorSentAt < throttle)) {
190
+ _context2.next = 4;
191
+ break;
192
+ }
193
+ return _context2.abrupt("return");
194
+ case 4:
195
+ // 先把报错信息写入回放流,便于回放中定位
196
+ (_this$replay = this.replay) === null || _this$replay === void 0 || _this$replay.markError(error);
197
+
198
+ // 构建回放附件(包含刚刚注入的错误标记)
199
+ replayPayload = this.buildReplayAttachment();
200
+ env = buildEnvelope(this.config, this.userProfile, 'error', error);
201
+ reportError(this.config, env, replayPayload);
202
+
203
+ // 上报完成后再更新节流时间戳
204
+ this.internal.lastErrorSentAt = now;
205
+ case 9:
206
+ case "end":
207
+ return _context2.stop();
208
+ }
209
+ }, _callee2, this);
210
+ }));
211
+ function errorLog(_x) {
212
+ return _errorLog.apply(this, arguments);
213
+ }
214
+ return errorLog;
215
+ }()
216
+ /**
217
+ * API 日志:直接即时上报(不进入公共队列)
218
+ */
219
+ )
220
+ }, {
221
+ key: "apiLog",
222
+ value: function apiLog(_apiLog) {
223
+ var env = buildEnvelope(this.config, this.userProfile, 'api', _apiLog);
224
+ reportApiLog(this.config, _apiLog, env);
225
+ }
226
+
227
+ /**
228
+ * 动态更新用户画像(不会重置已有字段)
229
+ */
230
+ }, {
231
+ key: "setUserProfile",
232
+ value: function setUserProfile(patch) {
233
+ Object.assign(this.userProfile, patch);
234
+ }
235
+
236
+ /** 获取当前实例配置副本(避免外部直接修改内部对象) */
237
+ }, {
238
+ key: "getConfig",
239
+ value: function getConfig() {
240
+ return _objectSpread({}, this.config);
241
+ }
242
+
243
+ /** 配置校验:缺少关键字段直接抛错 */
244
+ }, {
245
+ key: "validate",
246
+ value: function validate() {
247
+ if (!this.config.domain) throw new Error('缺少 domain');
248
+ if (!this.config.appId) throw new Error('缺少 appId');
249
+ }
250
+
251
+ /**
252
+ * 合并用户画像:
253
+ * - 若权限中心未返回 userName,则只使用外部提供
254
+ * - 否则使用 AuthCenter 信息并叠加外部 patch
255
+ */
256
+ }, {
257
+ key: "mergeUserProfile",
258
+ value: function mergeUserProfile() {
259
+ this.userProfile = _mergeUserProfile(this.authCenterUserProfile, this.config.userProfile);
260
+ }
261
+
262
+ /**
263
+ * 入队封装:
264
+ * - 全量采样
265
+ * - 生成 envelope
266
+ * - 触发阈值自动 flush
267
+ */
268
+ }, {
269
+ key: "enqueue",
270
+ value: function enqueue(type, log, opts) {
271
+ var env = buildEnvelope(this.config, this.userProfile, type, log);
272
+ this.queue.push(env);
273
+ if (this.queue.size() >= DEFAULT_FLUSH_THRESHOLD) this.flush();
274
+ }
275
+
276
+ /** 获取当前路径(剥离 base 前缀) */
277
+ }, {
278
+ key: "getFormatPath",
279
+ value: function getFormatPath() {
280
+ return getFormattedPath(this.config);
281
+ }
282
+
283
+ /** 获取当前页面的模块层级(由 routesMap 提供) */
284
+ }, {
285
+ key: "getPageModules",
286
+ value: function getPageModules() {
287
+ return _getPageModules(this.config, this.routesMap);
288
+ }
289
+
290
+ /** 挂载浏览器事件监听 */
291
+ }, {
292
+ key: "mountLifecycle",
293
+ value: function mountLifecycle() {
294
+ var _this = this;
295
+ this.lifecycle = new LifecycleManager({
296
+ onHidden: function onHidden() {
297
+ return _this.flush();
298
+ },
299
+ onError: function onError(err) {
300
+ return _this.errorLog(err);
301
+ }
302
+ });
303
+ this.lifecycle.mount();
304
+ }
305
+
306
+ /** 启动/重置周期性 flush 计时器 */
307
+ }, {
308
+ key: "scheduleFlushTimer",
309
+ value: function scheduleFlushTimer() {
310
+ var _this2 = this;
311
+ this.internal.flushTimer && clearInterval(this.internal.flushTimer);
312
+ this.internal.flushTimer = setInterval(function () {
313
+ return _this2.flush();
314
+ }, DEFAULT_MAX_WAIT_MS);
315
+ }
316
+
317
+ /** 校验 module 是否有效(非空且非纯空白) */
318
+ }, {
319
+ key: "isValidModule",
320
+ value: function isValidModule(mod) {
321
+ return !!(mod && mod.trim());
322
+ }
323
+
324
+ /** 获取过滤后的模块层级(去除空项和空白) */
325
+ }, {
326
+ key: "getFilteredModules",
327
+ value: function getFilteredModules() {
328
+ return (this.getPageModules() || []).filter(function (m) {
329
+ return m && m.trim();
330
+ });
331
+ }
332
+
333
+ /** 若日志缺少 module,则尝试用路由模块补齐;返回是否有效 */
334
+ }, {
335
+ key: "fillModuleIfMissing",
336
+ value: function fillModuleIfMissing(profile, modules) {
337
+ if (!profile.module && modules.length) {
338
+ profile.module = modules.join('-');
339
+ }
340
+ return this.isValidModule(profile.module);
341
+ }
342
+
343
+ /* ================= rrweb 录制相关 ================= */
344
+ }, {
345
+ key: "maybeStartReplay",
346
+ value: function maybeStartReplay() {
347
+ if (!this.config.enableReplay) return;
348
+ this.replay = new ReplayManager(this.config);
349
+ this.replay.start();
350
+ }
351
+ }, {
352
+ key: "stopReplay",
353
+ value: function stopReplay() {
354
+ var _this$replay2;
355
+ (_this$replay2 = this.replay) === null || _this$replay2 === void 0 || _this$replay2.stop();
356
+ }
357
+ }, {
358
+ key: "buildReplayAttachment",
359
+ value: function buildReplayAttachment() {
360
+ var _this$replay3;
361
+ return (_this$replay3 = this.replay) === null || _this$replay3 === void 0 ? void 0 : _this$replay3.buildAttachment();
362
+ }
363
+ }]);
364
+ return TrackerInstance;
365
+ }();