ngx-auto-logout 1.0.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.
Files changed (37) hide show
  1. package/README.md +247 -0
  2. package/dist/README.md +247 -0
  3. package/dist/bundles/ngx-auto-logout.umd.js +864 -0
  4. package/dist/bundles/ngx-auto-logout.umd.js.map +1 -0
  5. package/dist/bundles/ngx-auto-logout.umd.min.js +16 -0
  6. package/dist/bundles/ngx-auto-logout.umd.min.js.map +1 -0
  7. package/dist/esm2015/lib/auto-logout.component.js +351 -0
  8. package/dist/esm2015/lib/auto-logout.module.js +26 -0
  9. package/dist/esm2015/lib/auto-logout.service.js +384 -0
  10. package/dist/esm2015/lib/models/auto-logout-config.interface.js +10 -0
  11. package/dist/esm2015/lib/models/auto-logout-config.token.js +7 -0
  12. package/dist/esm2015/lib/provide-auto-logout.js +11 -0
  13. package/dist/esm2015/ngx-auto-logout.js +5 -0
  14. package/dist/esm2015/public-api.js +10 -0
  15. package/dist/esm5/lib/auto-logout.component.js +196 -0
  16. package/dist/esm5/lib/auto-logout.module.js +30 -0
  17. package/dist/esm5/lib/auto-logout.service.js +391 -0
  18. package/dist/esm5/lib/models/auto-logout-config.interface.js +10 -0
  19. package/dist/esm5/lib/models/auto-logout-config.token.js +7 -0
  20. package/dist/esm5/lib/provide-auto-logout.js +11 -0
  21. package/dist/esm5/ngx-auto-logout.js +5 -0
  22. package/dist/esm5/public-api.js +10 -0
  23. package/dist/fesm2015/ngx-auto-logout.js +787 -0
  24. package/dist/fesm2015/ngx-auto-logout.js.map +1 -0
  25. package/dist/fesm5/ngx-auto-logout.js +643 -0
  26. package/dist/fesm5/ngx-auto-logout.js.map +1 -0
  27. package/dist/lib/auto-logout.component.d.ts +81 -0
  28. package/dist/lib/auto-logout.module.d.ts +8 -0
  29. package/dist/lib/auto-logout.service.d.ts +108 -0
  30. package/dist/lib/models/auto-logout-config.interface.d.ts +46 -0
  31. package/dist/lib/models/auto-logout-config.token.d.ts +7 -0
  32. package/dist/lib/provide-auto-logout.d.ts +6 -0
  33. package/dist/ngx-auto-logout.d.ts +4 -0
  34. package/dist/ngx-auto-logout.metadata.json +1 -0
  35. package/dist/package.json +41 -0
  36. package/dist/public-api.d.ts +6 -0
  37. package/package.json +53 -0
@@ -0,0 +1,864 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('@angular/common')) :
3
+ typeof define === 'function' && define.amd ? define('ngx-auto-logout', ['exports', '@angular/core', 'rxjs', '@angular/common'], factory) :
4
+ (global = global || self, factory(global.NgxAutoLogout = {}, global.ng.core, global.rxjs, global.ng.common));
5
+ }(this, (function (exports, core, rxjs, common) { 'use strict';
6
+
7
+ /*! *****************************************************************************
8
+ Copyright (c) Microsoft Corporation.
9
+
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
+ ***************************************************************************** */
21
+ /* global Reflect, Promise */
22
+
23
+ var extendStatics = function(d, b) {
24
+ extendStatics = Object.setPrototypeOf ||
25
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
26
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
27
+ return extendStatics(d, b);
28
+ };
29
+
30
+ function __extends(d, b) {
31
+ extendStatics(d, b);
32
+ function __() { this.constructor = d; }
33
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
34
+ }
35
+
36
+ var __assign = function() {
37
+ __assign = Object.assign || function __assign(t) {
38
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
39
+ s = arguments[i];
40
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
41
+ }
42
+ return t;
43
+ };
44
+ return __assign.apply(this, arguments);
45
+ };
46
+
47
+ function __rest(s, e) {
48
+ var t = {};
49
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
50
+ t[p] = s[p];
51
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
52
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
53
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
54
+ t[p[i]] = s[p[i]];
55
+ }
56
+ return t;
57
+ }
58
+
59
+ function __decorate(decorators, target, key, desc) {
60
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
61
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
62
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
63
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
64
+ }
65
+
66
+ function __param(paramIndex, decorator) {
67
+ return function (target, key) { decorator(target, key, paramIndex); }
68
+ }
69
+
70
+ function __metadata(metadataKey, metadataValue) {
71
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
72
+ }
73
+
74
+ function __awaiter(thisArg, _arguments, P, generator) {
75
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
76
+ return new (P || (P = Promise))(function (resolve, reject) {
77
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
78
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
79
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
80
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
81
+ });
82
+ }
83
+
84
+ function __generator(thisArg, body) {
85
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
86
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
87
+ function verb(n) { return function (v) { return step([n, v]); }; }
88
+ function step(op) {
89
+ if (f) throw new TypeError("Generator is already executing.");
90
+ while (_) try {
91
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
92
+ if (y = 0, t) op = [op[0] & 2, t.value];
93
+ switch (op[0]) {
94
+ case 0: case 1: t = op; break;
95
+ case 4: _.label++; return { value: op[1], done: false };
96
+ case 5: _.label++; y = op[1]; op = [0]; continue;
97
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
98
+ default:
99
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
100
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
101
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
102
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
103
+ if (t[2]) _.ops.pop();
104
+ _.trys.pop(); continue;
105
+ }
106
+ op = body.call(thisArg, _);
107
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
108
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
109
+ }
110
+ }
111
+
112
+ function __createBinding(o, m, k, k2) {
113
+ if (k2 === undefined) k2 = k;
114
+ o[k2] = m[k];
115
+ }
116
+
117
+ function __exportStar(m, exports) {
118
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) exports[p] = m[p];
119
+ }
120
+
121
+ function __values(o) {
122
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
123
+ if (m) return m.call(o);
124
+ if (o && typeof o.length === "number") return {
125
+ next: function () {
126
+ if (o && i >= o.length) o = void 0;
127
+ return { value: o && o[i++], done: !o };
128
+ }
129
+ };
130
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
131
+ }
132
+
133
+ function __read(o, n) {
134
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
135
+ if (!m) return o;
136
+ var i = m.call(o), r, ar = [], e;
137
+ try {
138
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
139
+ }
140
+ catch (error) { e = { error: error }; }
141
+ finally {
142
+ try {
143
+ if (r && !r.done && (m = i["return"])) m.call(i);
144
+ }
145
+ finally { if (e) throw e.error; }
146
+ }
147
+ return ar;
148
+ }
149
+
150
+ function __spread() {
151
+ for (var ar = [], i = 0; i < arguments.length; i++)
152
+ ar = ar.concat(__read(arguments[i]));
153
+ return ar;
154
+ }
155
+
156
+ function __spreadArrays() {
157
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
158
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
159
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
160
+ r[k] = a[j];
161
+ return r;
162
+ };
163
+
164
+ function __await(v) {
165
+ return this instanceof __await ? (this.v = v, this) : new __await(v);
166
+ }
167
+
168
+ function __asyncGenerator(thisArg, _arguments, generator) {
169
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
170
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
171
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
172
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
173
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
174
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
175
+ function fulfill(value) { resume("next", value); }
176
+ function reject(value) { resume("throw", value); }
177
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
178
+ }
179
+
180
+ function __asyncDelegator(o) {
181
+ var i, p;
182
+ return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
183
+ function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
184
+ }
185
+
186
+ function __asyncValues(o) {
187
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
188
+ var m = o[Symbol.asyncIterator], i;
189
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
190
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
191
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
192
+ }
193
+
194
+ function __makeTemplateObject(cooked, raw) {
195
+ if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
196
+ return cooked;
197
+ };
198
+
199
+ function __importStar(mod) {
200
+ if (mod && mod.__esModule) return mod;
201
+ var result = {};
202
+ if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
203
+ result.default = mod;
204
+ return result;
205
+ }
206
+
207
+ function __importDefault(mod) {
208
+ return (mod && mod.__esModule) ? mod : { default: mod };
209
+ }
210
+
211
+ function __classPrivateFieldGet(receiver, privateMap) {
212
+ if (!privateMap.has(receiver)) {
213
+ throw new TypeError("attempted to get private field on non-instance");
214
+ }
215
+ return privateMap.get(receiver);
216
+ }
217
+
218
+ function __classPrivateFieldSet(receiver, privateMap, value) {
219
+ if (!privateMap.has(receiver)) {
220
+ throw new TypeError("attempted to set private field on non-instance");
221
+ }
222
+ privateMap.set(receiver, value);
223
+ return value;
224
+ }
225
+
226
+ /**
227
+ * 默认配置
228
+ */
229
+ var DEFAULT_AUTO_LOGOUT_CONFIG = {
230
+ mode: 'fixed',
231
+ timeout: 300,
232
+ warningTime: 30,
233
+ maxExtendTimes: -1,
234
+ };
235
+
236
+ /**
237
+ * InjectionToken for auto logout configuration
238
+ * Extracted to a separate file to avoid Angular 9 Ivy compilation scope issues
239
+ */
240
+ var AUTO_LOGOUT_CONFIG = new core.InjectionToken('AutoLogoutConfig');
241
+
242
+ /**
243
+ * Auto logout service
244
+ * Angular 9 compatible: use providedIn: 'root' (official pattern)
245
+ */
246
+ var AutoLogoutService = /** @class */ (function () {
247
+ /**
248
+ * Constructor
249
+ * Note: Do NOT use @Inject() here, it causes DI errors in Angular 9 VE
250
+ * Config should be passed via startMonitoring() method instead
251
+ */
252
+ function AutoLogoutService(ngZone) {
253
+ this.ngZone = ngZone;
254
+ // 配置是否已初始化
255
+ this.isConfigInitialized = false;
256
+ // localStorage 键名
257
+ this.LOGIN_TIME_KEY = 'autoLogout_loginTime';
258
+ this.LAST_ACTIVITY_KEY = 'autoLogout_lastActivity';
259
+ this.EXTEND_COUNT_KEY = 'autoLogout_extendCount';
260
+ this.PAUSE_STATE_KEY = 'autoLogout_pauseState';
261
+ this.lastActivityTime = Date.now();
262
+ this.countdownValue = 0;
263
+ this.countdownSubject = new rxjs.BehaviorSubject(0);
264
+ this.timerSubscription = null;
265
+ this.activityListeners = [];
266
+ this.warningShown = false;
267
+ // 是否启用活动监听
268
+ this.enableActivityTracking = false;
269
+ // 暂停状态
270
+ this.isPaused = false;
271
+ this.pauseStartTime = 0;
272
+ this.pausedRemainingTime = 0;
273
+ // Use default config, will be overridden by startMonitoring()
274
+ this.currentConfig = __assign({}, DEFAULT_AUTO_LOGOUT_CONFIG);
275
+ this.countdownValue = Math.floor(this.currentConfig.timeout);
276
+ this.countdownSubject = new rxjs.BehaviorSubject(this.countdownValue);
277
+ }
278
+ AutoLogoutService.prototype.getCountdown = function () {
279
+ return this.countdownSubject.asObservable();
280
+ };
281
+ /**
282
+ * 启动自动登出监控
283
+ */
284
+ AutoLogoutService.prototype.startMonitoring = function (config, enableTracking) {
285
+ var _this = this;
286
+ if (enableTracking === void 0) { enableTracking = false; }
287
+ this.stopMonitoring();
288
+ if (config) {
289
+ this.currentConfig = __assign(__assign({}, this.currentConfig), config);
290
+ }
291
+ // 恢复暂停状态
292
+ this.restorePauseState();
293
+ if (this.isPaused) {
294
+ return;
295
+ }
296
+ // 根据模式初始化
297
+ if (this.currentConfig.mode === 'fixed') {
298
+ this.initFixedMode();
299
+ }
300
+ else {
301
+ this.initIdleMode(enableTracking);
302
+ }
303
+ this.warningShown = false;
304
+ // 启动定时器
305
+ this.ngZone.runOutsideAngular(function () {
306
+ _this.timerSubscription = rxjs.interval(1000).subscribe(function () {
307
+ _this.updateCountdown();
308
+ });
309
+ });
310
+ };
311
+ /**
312
+ * 停止监控
313
+ */
314
+ AutoLogoutService.prototype.stopMonitoring = function () {
315
+ if (this.timerSubscription) {
316
+ this.timerSubscription.unsubscribe();
317
+ this.timerSubscription = null;
318
+ }
319
+ this.removeActivityListeners();
320
+ };
321
+ /**
322
+ * 清除登录时间
323
+ */
324
+ AutoLogoutService.prototype.clearLoginTime = function () {
325
+ localStorage.removeItem(this.LOGIN_TIME_KEY);
326
+ localStorage.removeItem(this.LAST_ACTIVITY_KEY);
327
+ this.resetExtendCount();
328
+ this.clearPauseState();
329
+ };
330
+ /**
331
+ * 延长会话
332
+ */
333
+ AutoLogoutService.prototype.extendSession = function (extendSeconds) {
334
+ if (extendSeconds === void 0) { extendSeconds = 1800; }
335
+ if (this.isMaxExtendReached()) {
336
+ console.warn('已达到最大延长次数');
337
+ return false;
338
+ }
339
+ if (this.currentConfig.mode === 'fixed') {
340
+ this.lastActivityTime -= extendSeconds * 1000;
341
+ }
342
+ else {
343
+ this.resetActivityTimer();
344
+ }
345
+ this.incrementExtendCount();
346
+ if (this.currentConfig.onExtended) {
347
+ this.currentConfig.onExtended(extendSeconds);
348
+ }
349
+ return true;
350
+ };
351
+ /**
352
+ * 检查是否可以延长
353
+ */
354
+ AutoLogoutService.prototype.canExtendSession = function () {
355
+ return !this.isMaxExtendReached() && !this.isPaused;
356
+ };
357
+ /**
358
+ * 获取剩余可延长次数
359
+ */
360
+ AutoLogoutService.prototype.getRemainingExtendTimes = function () {
361
+ if (this.currentConfig.maxExtendTimes === -1 || this.currentConfig.maxExtendTimes === undefined) {
362
+ return -1;
363
+ }
364
+ var usedCount = this.getExtendCount();
365
+ return Math.max(0, this.currentConfig.maxExtendTimes - usedCount);
366
+ };
367
+ /**
368
+ * 获取最后操作时间显示
369
+ */
370
+ AutoLogoutService.prototype.getLastActivityDisplay = function () {
371
+ if (this.currentConfig.mode !== 'idle') {
372
+ return '';
373
+ }
374
+ var lastActivity = this.getLastActivityTime();
375
+ if (!lastActivity) {
376
+ return '';
377
+ }
378
+ var minutesAgo = Math.floor((Date.now() - lastActivity) / 60000);
379
+ if (minutesAgo < 1) {
380
+ return '刚刚操作';
381
+ }
382
+ return minutesAgo + "\u5206\u949F\u524D\u64CD\u4F5C";
383
+ };
384
+ /**
385
+ * 暂停
386
+ */
387
+ AutoLogoutService.prototype.pause = function () {
388
+ if (this.isPaused)
389
+ return;
390
+ this.isPaused = true;
391
+ this.pauseStartTime = Date.now();
392
+ this.pausedRemainingTime = this.countdownValue;
393
+ this.stopMonitoring();
394
+ this.savePauseState();
395
+ if (this.currentConfig.onPaused) {
396
+ this.currentConfig.onPaused();
397
+ }
398
+ };
399
+ /**
400
+ * 恢复
401
+ */
402
+ AutoLogoutService.prototype.resume = function () {
403
+ if (!this.isPaused)
404
+ return;
405
+ var pauseDuration = Date.now() - this.pauseStartTime;
406
+ if (this.currentConfig.mode === 'fixed') {
407
+ this.lastActivityTime += pauseDuration;
408
+ }
409
+ else {
410
+ this.lastActivityTime += pauseDuration;
411
+ this.setLastActivityTime(this.lastActivityTime);
412
+ }
413
+ this.isPaused = false;
414
+ this.pauseStartTime = 0;
415
+ this.pausedRemainingTime = 0;
416
+ this.clearPauseState();
417
+ this.startMonitoring(this.currentConfig, this.enableActivityTracking);
418
+ if (this.currentConfig.onResumed) {
419
+ this.currentConfig.onResumed();
420
+ }
421
+ };
422
+ /**
423
+ * 检查是否暂停
424
+ */
425
+ AutoLogoutService.prototype.isAutoLogoutPaused = function () {
426
+ return this.isPaused;
427
+ };
428
+ /**
429
+ * 格式化倒计时
430
+ */
431
+ AutoLogoutService.prototype.formatCountdown = function (seconds) {
432
+ var mins = Math.floor(seconds / 60);
433
+ var secs = seconds % 60;
434
+ return mins.toString().padStart(2, '0') + ":" + secs.toString().padStart(2, '0');
435
+ };
436
+ /**
437
+ * 判断是否警告状态
438
+ */
439
+ AutoLogoutService.prototype.isWarningState = function (seconds) {
440
+ var warningTime = this.currentConfig.warningTime || 30;
441
+ return seconds <= warningTime && seconds > 0;
442
+ };
443
+ /**
444
+ * 判断是否紧急状态(最后10秒)
445
+ */
446
+ AutoLogoutService.prototype.isUrgentState = function (seconds) {
447
+ return seconds <= 10 && seconds > 0;
448
+ };
449
+ /**
450
+ * 获取当前模式
451
+ */
452
+ AutoLogoutService.prototype.getCurrentMode = function () {
453
+ return this.currentConfig.mode;
454
+ };
455
+ // ==================== 私有方法 ====================
456
+ AutoLogoutService.prototype.initFixedMode = function () {
457
+ var loginTime = this.getLoginTime();
458
+ if (!loginTime) {
459
+ this.setLoginTime(Date.now());
460
+ this.lastActivityTime = Date.now();
461
+ }
462
+ else {
463
+ this.lastActivityTime = loginTime;
464
+ }
465
+ this.updateCountdownValue();
466
+ };
467
+ AutoLogoutService.prototype.initIdleMode = function (enableTracking) {
468
+ this.enableActivityTracking = enableTracking || true;
469
+ var lastActivity = this.getLastActivityTime();
470
+ if (!lastActivity) {
471
+ this.setLastActivityTime(Date.now());
472
+ this.lastActivityTime = Date.now();
473
+ }
474
+ else {
475
+ this.lastActivityTime = lastActivity;
476
+ }
477
+ this.registerActivityListeners();
478
+ this.updateCountdownValue();
479
+ };
480
+ AutoLogoutService.prototype.updateCountdownValue = function () {
481
+ var now = Date.now();
482
+ var elapsed = now - this.lastActivityTime;
483
+ var remaining = Math.max(0, Math.floor((this.currentConfig.timeout * 1000 - elapsed) / 1000));
484
+ this.countdownValue = remaining;
485
+ this.countdownSubject.next(this.countdownValue);
486
+ };
487
+ AutoLogoutService.prototype.updateCountdown = function () {
488
+ var _this = this;
489
+ var now = Date.now();
490
+ var elapsed = now - this.lastActivityTime;
491
+ var remaining = Math.max(0, Math.floor((this.currentConfig.timeout * 1000 - elapsed) / 1000));
492
+ if (remaining !== this.countdownValue) {
493
+ this.countdownValue = remaining;
494
+ this.ngZone.run(function () {
495
+ _this.countdownSubject.next(_this.countdownValue);
496
+ });
497
+ // 警告回调
498
+ var warningTime = this.currentConfig.warningTime || 30;
499
+ if (remaining === warningTime && !this.warningShown) {
500
+ this.warningShown = true;
501
+ if (this.currentConfig.onWarning) {
502
+ this.currentConfig.onWarning(remaining);
503
+ }
504
+ }
505
+ // 超时回调
506
+ if (remaining <= 0) {
507
+ this.performAutoLogout();
508
+ }
509
+ }
510
+ };
511
+ AutoLogoutService.prototype.performAutoLogout = function () {
512
+ this.stopMonitoring();
513
+ this.clearLoginTime();
514
+ if (this.currentConfig.onTimeout) {
515
+ this.currentConfig.onTimeout();
516
+ }
517
+ else {
518
+ // 默认行为:导航到登录页
519
+ // Note: Router navigation removed to avoid DI issues in Angular 9 VE
520
+ // Use onTimeout callback for custom navigation
521
+ console.warn('Auto logout timeout - implement custom navigation via onTimeout callback');
522
+ }
523
+ };
524
+ AutoLogoutService.prototype.registerActivityListeners = function () {
525
+ var _this = this;
526
+ var events = ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart', 'click'];
527
+ events.forEach(function (event) {
528
+ var listener = function () { return _this.resetActivityTimer(); };
529
+ document.addEventListener(event, listener, true);
530
+ _this.activityListeners.push({ event: event, listener: listener });
531
+ });
532
+ };
533
+ AutoLogoutService.prototype.removeActivityListeners = function () {
534
+ this.activityListeners.forEach(function (_a) {
535
+ var event = _a.event, listener = _a.listener;
536
+ document.removeEventListener(event, listener, true);
537
+ });
538
+ this.activityListeners = [];
539
+ };
540
+ AutoLogoutService.prototype.resetActivityTimer = function () {
541
+ if (this.currentConfig.mode === 'idle') {
542
+ this.lastActivityTime = Date.now();
543
+ this.setLastActivityTime(this.lastActivityTime);
544
+ this.updateCountdownValue();
545
+ this.warningShown = false;
546
+ }
547
+ };
548
+ AutoLogoutService.prototype.isMaxExtendReached = function () {
549
+ if (this.currentConfig.maxExtendTimes === -1 || this.currentConfig.maxExtendTimes === undefined) {
550
+ return false;
551
+ }
552
+ var usedCount = this.getExtendCount();
553
+ return usedCount >= this.currentConfig.maxExtendTimes;
554
+ };
555
+ AutoLogoutService.prototype.getExtendCount = function () {
556
+ var countStr = localStorage.getItem(this.EXTEND_COUNT_KEY);
557
+ return countStr ? parseInt(countStr, 10) : 0;
558
+ };
559
+ AutoLogoutService.prototype.incrementExtendCount = function () {
560
+ var currentCount = this.getExtendCount();
561
+ localStorage.setItem(this.EXTEND_COUNT_KEY, (currentCount + 1).toString());
562
+ };
563
+ AutoLogoutService.prototype.resetExtendCount = function () {
564
+ localStorage.removeItem(this.EXTEND_COUNT_KEY);
565
+ };
566
+ AutoLogoutService.prototype.getLoginTime = function () {
567
+ var timeStr = localStorage.getItem(this.LOGIN_TIME_KEY);
568
+ return timeStr ? parseInt(timeStr, 10) : null;
569
+ };
570
+ AutoLogoutService.prototype.setLoginTime = function (time) {
571
+ localStorage.setItem(this.LOGIN_TIME_KEY, time.toString());
572
+ };
573
+ AutoLogoutService.prototype.getLastActivityTime = function () {
574
+ var timeStr = localStorage.getItem(this.LAST_ACTIVITY_KEY);
575
+ return timeStr ? parseInt(timeStr, 10) : null;
576
+ };
577
+ AutoLogoutService.prototype.setLastActivityTime = function (time) {
578
+ localStorage.setItem(this.LAST_ACTIVITY_KEY, time.toString());
579
+ };
580
+ AutoLogoutService.prototype.savePauseState = function () {
581
+ var state = {
582
+ isPaused: true,
583
+ pauseStartTime: this.pauseStartTime,
584
+ pausedRemainingTime: this.pausedRemainingTime,
585
+ mode: this.currentConfig.mode,
586
+ lastActivityTime: this.lastActivityTime
587
+ };
588
+ localStorage.setItem(this.PAUSE_STATE_KEY, JSON.stringify(state));
589
+ };
590
+ AutoLogoutService.prototype.clearPauseState = function () {
591
+ localStorage.removeItem(this.PAUSE_STATE_KEY);
592
+ };
593
+ AutoLogoutService.prototype.restorePauseState = function () {
594
+ var stateStr = localStorage.getItem(this.PAUSE_STATE_KEY);
595
+ if (!stateStr)
596
+ return;
597
+ try {
598
+ var state = JSON.parse(stateStr);
599
+ if (state.isPaused) {
600
+ this.isPaused = true;
601
+ this.pauseStartTime = state.pauseStartTime;
602
+ this.pausedRemainingTime = state.pausedRemainingTime;
603
+ this.lastActivityTime = state.lastActivityTime;
604
+ this.currentConfig.mode = state.mode;
605
+ if (this.timerSubscription) {
606
+ this.timerSubscription.unsubscribe();
607
+ this.timerSubscription = null;
608
+ }
609
+ this.removeActivityListeners();
610
+ }
611
+ }
612
+ catch (e) {
613
+ console.error('恢复暂停状态失败:', e);
614
+ this.clearPauseState();
615
+ }
616
+ };
617
+ AutoLogoutService.ctorParameters = function () { return [
618
+ { type: core.NgZone }
619
+ ]; };
620
+ AutoLogoutService.ɵprov = core.ɵɵdefineInjectable({ factory: function AutoLogoutService_Factory() { return new AutoLogoutService(core.ɵɵinject(core.NgZone)); }, token: AutoLogoutService, providedIn: "root" });
621
+ AutoLogoutService = __decorate([
622
+ core.Injectable({ providedIn: 'root' })
623
+ ], AutoLogoutService);
624
+ return AutoLogoutService;
625
+ }());
626
+
627
+ var AutoLogoutComponent = /** @class */ (function () {
628
+ function AutoLogoutComponent(autoLogout) {
629
+ this.autoLogout = autoLogout;
630
+ // ==================== 输入属性 ====================
631
+ /**
632
+ * 是否显示延长按钮
633
+ */
634
+ this.showExtendButton = true;
635
+ /**
636
+ * 是否显示暂停按钮
637
+ */
638
+ this.showPauseButton = true;
639
+ /**
640
+ * 延长的秒数(默认30分钟)
641
+ */
642
+ this.extendSeconds = 1800;
643
+ // ==================== 输出事件 ====================
644
+ /**
645
+ * 警告事件
646
+ */
647
+ this.onWarning = new core.EventEmitter();
648
+ /**
649
+ * 超时事件
650
+ */
651
+ this.onTimeout = new core.EventEmitter();
652
+ /**
653
+ * 延长成功事件
654
+ */
655
+ this.onExtended = new core.EventEmitter();
656
+ /**
657
+ * 暂停事件
658
+ */
659
+ this.onPaused = new core.EventEmitter();
660
+ /**
661
+ * 恢复事件
662
+ */
663
+ this.onResumed = new core.EventEmitter();
664
+ // ==================== 内部状态 ====================
665
+ this.subscription = null;
666
+ this.countdown = '00:00';
667
+ this.isWarning = false;
668
+ this.isUrgent = false;
669
+ this.isPaused = false;
670
+ this.canExtend = true;
671
+ this.remainingExtends = -1;
672
+ }
673
+ AutoLogoutComponent.prototype.ngOnInit = function () {
674
+ var _this = this;
675
+ // 如果提供了自定义配置,启动监控
676
+ if (this.config) {
677
+ this.autoLogout.startMonitoring(this.config);
678
+ }
679
+ // 订阅倒计时
680
+ this.subscription = this.autoLogout.getCountdown().subscribe(function (seconds) {
681
+ var _a;
682
+ _this.countdown = _this.autoLogout.formatCountdown(seconds);
683
+ _this.isWarning = _this.autoLogout.isWarningState(seconds);
684
+ _this.isUrgent = _this.autoLogout.isUrgentState(seconds);
685
+ // 触发警告事件
686
+ var warningTime = ((_a = _this.config) === null || _a === void 0 ? void 0 : _a.warningTime) || 30;
687
+ if (seconds === warningTime) {
688
+ _this.onWarning.emit(seconds);
689
+ }
690
+ // 触发超时事件
691
+ if (seconds <= 0) {
692
+ _this.onTimeout.emit();
693
+ }
694
+ });
695
+ // 定期检查状态
696
+ setInterval(function () {
697
+ _this.updateStatus();
698
+ }, 1000);
699
+ this.updateStatus();
700
+ };
701
+ AutoLogoutComponent.prototype.ngOnDestroy = function () {
702
+ if (this.subscription) {
703
+ this.subscription.unsubscribe();
704
+ }
705
+ };
706
+ // ==================== 公开方法 ====================
707
+ /**
708
+ * 延长会话
709
+ */
710
+ AutoLogoutComponent.prototype.extendSession = function () {
711
+ this.handleExtend();
712
+ };
713
+ /**
714
+ * 暂停
715
+ */
716
+ AutoLogoutComponent.prototype.pause = function () {
717
+ this.handleTogglePause();
718
+ };
719
+ /**
720
+ * 恢复
721
+ */
722
+ AutoLogoutComponent.prototype.resume = function () {
723
+ this.handleTogglePause();
724
+ };
725
+ Object.defineProperty(AutoLogoutComponent.prototype, "templateContext", {
726
+ /**
727
+ * 获取模板上下文(用于自定义模板)
728
+ */
729
+ get: function () {
730
+ var _this = this;
731
+ return {
732
+ countdown: this.countdown,
733
+ isWarning: this.isWarning,
734
+ isUrgent: this.isUrgent,
735
+ isPaused: this.isPaused,
736
+ canExtend: this.canExtend,
737
+ remainingExtends: this.remainingExtends,
738
+ extendSession: function () { return _this.handleExtend(); },
739
+ togglePause: function () { return _this.handleTogglePause(); },
740
+ formatCountdown: function (seconds) { return _this.autoLogout.formatCountdown(seconds); }
741
+ };
742
+ },
743
+ enumerable: true,
744
+ configurable: true
745
+ });
746
+ // ==================== 内部方法 ====================
747
+ AutoLogoutComponent.prototype.updateStatus = function () {
748
+ this.isPaused = this.autoLogout.isAutoLogoutPaused();
749
+ this.canExtend = this.autoLogout.canExtendSession();
750
+ this.remainingExtends = this.autoLogout.getRemainingExtendTimes();
751
+ };
752
+ /**
753
+ * 处理延长会话(模板调用)
754
+ */
755
+ AutoLogoutComponent.prototype.handleExtend = function () {
756
+ var success = this.autoLogout.extendSession(this.extendSeconds);
757
+ if (success) {
758
+ this.onExtended.emit(this.extendSeconds);
759
+ }
760
+ this.updateStatus();
761
+ };
762
+ /**
763
+ * 处理暂停/恢复切换(模板调用)
764
+ */
765
+ AutoLogoutComponent.prototype.handleTogglePause = function () {
766
+ if (this.isPaused) {
767
+ this.autoLogout.resume();
768
+ this.onResumed.emit();
769
+ }
770
+ else {
771
+ this.autoLogout.pause();
772
+ this.onPaused.emit();
773
+ }
774
+ this.updateStatus();
775
+ };
776
+ AutoLogoutComponent.ctorParameters = function () { return [
777
+ { type: AutoLogoutService }
778
+ ]; };
779
+ __decorate([
780
+ core.Input()
781
+ ], AutoLogoutComponent.prototype, "showExtendButton", void 0);
782
+ __decorate([
783
+ core.Input()
784
+ ], AutoLogoutComponent.prototype, "showPauseButton", void 0);
785
+ __decorate([
786
+ core.Input()
787
+ ], AutoLogoutComponent.prototype, "extendSeconds", void 0);
788
+ __decorate([
789
+ core.Input()
790
+ ], AutoLogoutComponent.prototype, "config", void 0);
791
+ __decorate([
792
+ core.Input()
793
+ ], AutoLogoutComponent.prototype, "customTemplate", void 0);
794
+ __decorate([
795
+ core.Output()
796
+ ], AutoLogoutComponent.prototype, "onWarning", void 0);
797
+ __decorate([
798
+ core.Output()
799
+ ], AutoLogoutComponent.prototype, "onTimeout", void 0);
800
+ __decorate([
801
+ core.Output()
802
+ ], AutoLogoutComponent.prototype, "onExtended", void 0);
803
+ __decorate([
804
+ core.Output()
805
+ ], AutoLogoutComponent.prototype, "onPaused", void 0);
806
+ __decorate([
807
+ core.Output()
808
+ ], AutoLogoutComponent.prototype, "onResumed", void 0);
809
+ AutoLogoutComponent = __decorate([
810
+ core.Component({
811
+ selector: 'ngx-auto-logout',
812
+ template: "\n <!-- \u9ED8\u8BA4UI\u6A21\u5F0F\uFF08\u50BB\u74DC\u6A21\u5F0F\uFF09 -->\n <div *ngIf=\"!customTemplate\" class=\"ngx-auto-logout-container\">\n <!-- \u5012\u8BA1\u65F6\u663E\u793A -->\n <div class=\"countdown-display\" [class.warning]=\"isWarning\" [class.urgent]=\"isUrgent\" [class.paused]=\"isPaused\">\n <span class=\"icon\">{{ isPaused ? '\u23F8\uFE0F' : '\u23F1\uFE0F' }}</span>\n <span class=\"time\">{{ countdown }}</span>\n <span *ngIf=\"isPaused\" class=\"pause-label\">(\u5DF2\u6682\u505C)</span>\n </div>\n\n <!-- \u63A7\u5236\u6309\u94AE -->\n <div class=\"controls\">\n <!-- \u5EF6\u957F\u4F1A\u8BDD\u6309\u94AE -->\n <button *ngIf=\"showExtendButton && canExtend\" class=\"btn btn-extend\" (click)=\"handleExtend()\" [disabled]=\"!canExtend\" title=\"\u5EF6\u957F30\u5206\u949F\">\n \u2795 \u5EF6\u957F\n <span *ngIf=\"remainingExtends !== -1\" class=\"extend-count\"> ({{ remainingExtends }}) </span>\n </button>\n\n <!-- \u6682\u505C/\u6062\u590D\u6309\u94AE -->\n <button\n *ngIf=\"showPauseButton\"\n class=\"btn btn-pause\"\n [class.paused]=\"isPaused\"\n (click)=\"handleTogglePause()\"\n [title]=\"isPaused ? '\u6062\u590D\u81EA\u52A8\u767B\u51FA' : '\u6682\u505C\u81EA\u52A8\u767B\u51FA'\"\n >\n {{ isPaused ? '\u25B6\uFE0F \u6062\u590D' : '\u23F8\uFE0F \u6682\u505C' }}\n </button>\n </div>\n </div>\n\n <!-- \u81EA\u5B9A\u4E49\u6A21\u677F\u6A21\u5F0F\uFF08\u4E13\u5BB6\u6A21\u5F0F\uFF09 -->\n <ng-container *ngIf=\"customTemplate\">\n <ng-container [ngTemplateOutlet]=\"customTemplate\" [ngTemplateOutletContext]=\"templateContext\"></ng-container>\n </ng-container>\n ",
813
+ styles: ["\n .ngx-auto-logout-container {\n display: inline-flex;\n align-items: center;\n gap: 12px;\n padding: 8px 12px;\n background: #f8f9fa;\n border-radius: 6px;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n }\n\n .countdown-display {\n display: flex;\n align-items: center;\n gap: 8px;\n padding: 6px 12px;\n background: white;\n border-radius: 4px;\n font-weight: bold;\n font-size: 16px;\n transition: all 0.3s ease;\n }\n\n .countdown-display.warning {\n background: #fff3cd;\n color: #856404;\n animation: blink-warning 1s ease-in-out infinite;\n }\n\n .countdown-display.urgent {\n background: #f8d7da;\n color: #721c24;\n animation: blink-urgent 0.5s ease-in-out infinite;\n }\n\n .countdown-display.paused {\n opacity: 0.6;\n background: #e9ecef;\n }\n\n .icon {\n font-size: 18px;\n }\n\n .time {\n min-width: 50px;\n text-align: center;\n }\n\n .pause-label {\n font-size: 12px;\n color: #6c757d;\n font-weight: normal;\n }\n\n .controls {\n display: flex;\n gap: 8px;\n }\n\n .btn {\n padding: 6px 12px;\n font-size: 13px;\n border: none;\n border-radius: 4px;\n cursor: pointer;\n transition: all 0.2s;\n font-weight: 500;\n }\n\n .btn-extend {\n background: #28a745;\n color: white;\n }\n\n .btn-extend:hover:not(:disabled) {\n background: #218838;\n transform: translateY(-1px);\n }\n\n .btn-extend:disabled {\n background: #ccc;\n cursor: not-allowed;\n opacity: 0.6;\n }\n\n .btn-pause {\n background: #007bff;\n color: white;\n }\n\n .btn-pause:hover {\n background: #0056b3;\n transform: translateY(-1px);\n }\n\n .btn-pause.paused {\n background: #ffc107;\n color: #212529;\n }\n\n .extend-count {\n font-size: 11px;\n opacity: 0.9;\n }\n\n @keyframes blink-warning {\n 0%,\n 100% {\n opacity: 1;\n }\n 50% {\n opacity: 0.7;\n }\n }\n\n @keyframes blink-urgent {\n 0%,\n 100% {\n opacity: 1;\n transform: scale(1);\n }\n 50% {\n opacity: 0.6;\n transform: scale(1.05);\n }\n }\n "]
814
+ })
815
+ ], AutoLogoutComponent);
816
+ return AutoLogoutComponent;
817
+ }());
818
+
819
+ /**
820
+ * Auto logout module
821
+ * Note: Service uses providedIn: 'root', module only declares/exports component
822
+ */
823
+ var AutoLogoutModule = /** @class */ (function () {
824
+ function AutoLogoutModule() {
825
+ }
826
+ AutoLogoutModule_1 = AutoLogoutModule;
827
+ AutoLogoutModule.forRoot = function () {
828
+ return {
829
+ ngModule: AutoLogoutModule_1,
830
+ providers: []
831
+ };
832
+ };
833
+ var AutoLogoutModule_1;
834
+ AutoLogoutModule = AutoLogoutModule_1 = __decorate([
835
+ core.NgModule({
836
+ declarations: [AutoLogoutComponent],
837
+ imports: [common.CommonModule],
838
+ exports: [AutoLogoutComponent]
839
+ })
840
+ ], AutoLogoutModule);
841
+ return AutoLogoutModule;
842
+ }());
843
+
844
+ /**
845
+ * 提供自动登出配置(Angular 15+ Standalone)
846
+ */
847
+ function provideAutoLogout(config) {
848
+ return {
849
+ provide: AUTO_LOGOUT_CONFIG,
850
+ useValue: config
851
+ };
852
+ }
853
+
854
+ exports.AUTO_LOGOUT_CONFIG = AUTO_LOGOUT_CONFIG;
855
+ exports.AutoLogoutComponent = AutoLogoutComponent;
856
+ exports.AutoLogoutModule = AutoLogoutModule;
857
+ exports.AutoLogoutService = AutoLogoutService;
858
+ exports.DEFAULT_AUTO_LOGOUT_CONFIG = DEFAULT_AUTO_LOGOUT_CONFIG;
859
+ exports.provideAutoLogout = provideAutoLogout;
860
+
861
+ Object.defineProperty(exports, '__esModule', { value: true });
862
+
863
+ })));
864
+ //# sourceMappingURL=ngx-auto-logout.umd.js.map