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.
- package/README.md +247 -0
- package/dist/README.md +247 -0
- package/dist/bundles/ngx-auto-logout.umd.js +864 -0
- package/dist/bundles/ngx-auto-logout.umd.js.map +1 -0
- package/dist/bundles/ngx-auto-logout.umd.min.js +16 -0
- package/dist/bundles/ngx-auto-logout.umd.min.js.map +1 -0
- package/dist/esm2015/lib/auto-logout.component.js +351 -0
- package/dist/esm2015/lib/auto-logout.module.js +26 -0
- package/dist/esm2015/lib/auto-logout.service.js +384 -0
- package/dist/esm2015/lib/models/auto-logout-config.interface.js +10 -0
- package/dist/esm2015/lib/models/auto-logout-config.token.js +7 -0
- package/dist/esm2015/lib/provide-auto-logout.js +11 -0
- package/dist/esm2015/ngx-auto-logout.js +5 -0
- package/dist/esm2015/public-api.js +10 -0
- package/dist/esm5/lib/auto-logout.component.js +196 -0
- package/dist/esm5/lib/auto-logout.module.js +30 -0
- package/dist/esm5/lib/auto-logout.service.js +391 -0
- package/dist/esm5/lib/models/auto-logout-config.interface.js +10 -0
- package/dist/esm5/lib/models/auto-logout-config.token.js +7 -0
- package/dist/esm5/lib/provide-auto-logout.js +11 -0
- package/dist/esm5/ngx-auto-logout.js +5 -0
- package/dist/esm5/public-api.js +10 -0
- package/dist/fesm2015/ngx-auto-logout.js +787 -0
- package/dist/fesm2015/ngx-auto-logout.js.map +1 -0
- package/dist/fesm5/ngx-auto-logout.js +643 -0
- package/dist/fesm5/ngx-auto-logout.js.map +1 -0
- package/dist/lib/auto-logout.component.d.ts +81 -0
- package/dist/lib/auto-logout.module.d.ts +8 -0
- package/dist/lib/auto-logout.service.d.ts +108 -0
- package/dist/lib/models/auto-logout-config.interface.d.ts +46 -0
- package/dist/lib/models/auto-logout-config.token.d.ts +7 -0
- package/dist/lib/provide-auto-logout.d.ts +6 -0
- package/dist/ngx-auto-logout.d.ts +4 -0
- package/dist/ngx-auto-logout.metadata.json +1 -0
- package/dist/package.json +41 -0
- package/dist/public-api.d.ts +6 -0
- package/package.json +53 -0
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
import { InjectionToken, NgZone, ɵɵdefineInjectable, ɵɵinject, Injectable, EventEmitter, Input, Output, Component, NgModule } from '@angular/core';
|
|
2
|
+
import { __decorate } from 'tslib';
|
|
3
|
+
import { BehaviorSubject, interval } from 'rxjs';
|
|
4
|
+
import { CommonModule } from '@angular/common';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 默认配置
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_AUTO_LOGOUT_CONFIG = {
|
|
10
|
+
mode: 'fixed',
|
|
11
|
+
timeout: 300,
|
|
12
|
+
warningTime: 30,
|
|
13
|
+
maxExtendTimes: -1,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* InjectionToken for auto logout configuration
|
|
18
|
+
* Extracted to a separate file to avoid Angular 9 Ivy compilation scope issues
|
|
19
|
+
*/
|
|
20
|
+
const AUTO_LOGOUT_CONFIG = new InjectionToken('AutoLogoutConfig');
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Auto logout service
|
|
24
|
+
* Angular 9 compatible: use providedIn: 'root' (official pattern)
|
|
25
|
+
*/
|
|
26
|
+
let AutoLogoutService = class AutoLogoutService {
|
|
27
|
+
/**
|
|
28
|
+
* Constructor
|
|
29
|
+
* Note: Do NOT use @Inject() here, it causes DI errors in Angular 9 VE
|
|
30
|
+
* Config should be passed via startMonitoring() method instead
|
|
31
|
+
*/
|
|
32
|
+
constructor(ngZone) {
|
|
33
|
+
this.ngZone = ngZone;
|
|
34
|
+
// 配置是否已初始化
|
|
35
|
+
this.isConfigInitialized = false;
|
|
36
|
+
// localStorage 键名
|
|
37
|
+
this.LOGIN_TIME_KEY = 'autoLogout_loginTime';
|
|
38
|
+
this.LAST_ACTIVITY_KEY = 'autoLogout_lastActivity';
|
|
39
|
+
this.EXTEND_COUNT_KEY = 'autoLogout_extendCount';
|
|
40
|
+
this.PAUSE_STATE_KEY = 'autoLogout_pauseState';
|
|
41
|
+
this.lastActivityTime = Date.now();
|
|
42
|
+
this.countdownValue = 0;
|
|
43
|
+
this.countdownSubject = new BehaviorSubject(0);
|
|
44
|
+
this.timerSubscription = null;
|
|
45
|
+
this.activityListeners = [];
|
|
46
|
+
this.warningShown = false;
|
|
47
|
+
// 是否启用活动监听
|
|
48
|
+
this.enableActivityTracking = false;
|
|
49
|
+
// 暂停状态
|
|
50
|
+
this.isPaused = false;
|
|
51
|
+
this.pauseStartTime = 0;
|
|
52
|
+
this.pausedRemainingTime = 0;
|
|
53
|
+
// Use default config, will be overridden by startMonitoring()
|
|
54
|
+
this.currentConfig = Object.assign({}, DEFAULT_AUTO_LOGOUT_CONFIG);
|
|
55
|
+
this.countdownValue = Math.floor(this.currentConfig.timeout);
|
|
56
|
+
this.countdownSubject = new BehaviorSubject(this.countdownValue);
|
|
57
|
+
}
|
|
58
|
+
getCountdown() {
|
|
59
|
+
return this.countdownSubject.asObservable();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 启动自动登出监控
|
|
63
|
+
*/
|
|
64
|
+
startMonitoring(config, enableTracking = false) {
|
|
65
|
+
this.stopMonitoring();
|
|
66
|
+
if (config) {
|
|
67
|
+
this.currentConfig = Object.assign(Object.assign({}, this.currentConfig), config);
|
|
68
|
+
}
|
|
69
|
+
// 恢复暂停状态
|
|
70
|
+
this.restorePauseState();
|
|
71
|
+
if (this.isPaused) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
// 根据模式初始化
|
|
75
|
+
if (this.currentConfig.mode === 'fixed') {
|
|
76
|
+
this.initFixedMode();
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this.initIdleMode(enableTracking);
|
|
80
|
+
}
|
|
81
|
+
this.warningShown = false;
|
|
82
|
+
// 启动定时器
|
|
83
|
+
this.ngZone.runOutsideAngular(() => {
|
|
84
|
+
this.timerSubscription = interval(1000).subscribe(() => {
|
|
85
|
+
this.updateCountdown();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 停止监控
|
|
91
|
+
*/
|
|
92
|
+
stopMonitoring() {
|
|
93
|
+
if (this.timerSubscription) {
|
|
94
|
+
this.timerSubscription.unsubscribe();
|
|
95
|
+
this.timerSubscription = null;
|
|
96
|
+
}
|
|
97
|
+
this.removeActivityListeners();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 清除登录时间
|
|
101
|
+
*/
|
|
102
|
+
clearLoginTime() {
|
|
103
|
+
localStorage.removeItem(this.LOGIN_TIME_KEY);
|
|
104
|
+
localStorage.removeItem(this.LAST_ACTIVITY_KEY);
|
|
105
|
+
this.resetExtendCount();
|
|
106
|
+
this.clearPauseState();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 延长会话
|
|
110
|
+
*/
|
|
111
|
+
extendSession(extendSeconds = 1800) {
|
|
112
|
+
if (this.isMaxExtendReached()) {
|
|
113
|
+
console.warn('已达到最大延长次数');
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
if (this.currentConfig.mode === 'fixed') {
|
|
117
|
+
this.lastActivityTime -= extendSeconds * 1000;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
this.resetActivityTimer();
|
|
121
|
+
}
|
|
122
|
+
this.incrementExtendCount();
|
|
123
|
+
if (this.currentConfig.onExtended) {
|
|
124
|
+
this.currentConfig.onExtended(extendSeconds);
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 检查是否可以延长
|
|
130
|
+
*/
|
|
131
|
+
canExtendSession() {
|
|
132
|
+
return !this.isMaxExtendReached() && !this.isPaused;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 获取剩余可延长次数
|
|
136
|
+
*/
|
|
137
|
+
getRemainingExtendTimes() {
|
|
138
|
+
if (this.currentConfig.maxExtendTimes === -1 || this.currentConfig.maxExtendTimes === undefined) {
|
|
139
|
+
return -1;
|
|
140
|
+
}
|
|
141
|
+
const usedCount = this.getExtendCount();
|
|
142
|
+
return Math.max(0, this.currentConfig.maxExtendTimes - usedCount);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* 获取最后操作时间显示
|
|
146
|
+
*/
|
|
147
|
+
getLastActivityDisplay() {
|
|
148
|
+
if (this.currentConfig.mode !== 'idle') {
|
|
149
|
+
return '';
|
|
150
|
+
}
|
|
151
|
+
const lastActivity = this.getLastActivityTime();
|
|
152
|
+
if (!lastActivity) {
|
|
153
|
+
return '';
|
|
154
|
+
}
|
|
155
|
+
const minutesAgo = Math.floor((Date.now() - lastActivity) / 60000);
|
|
156
|
+
if (minutesAgo < 1) {
|
|
157
|
+
return '刚刚操作';
|
|
158
|
+
}
|
|
159
|
+
return `${minutesAgo}分钟前操作`;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 暂停
|
|
163
|
+
*/
|
|
164
|
+
pause() {
|
|
165
|
+
if (this.isPaused)
|
|
166
|
+
return;
|
|
167
|
+
this.isPaused = true;
|
|
168
|
+
this.pauseStartTime = Date.now();
|
|
169
|
+
this.pausedRemainingTime = this.countdownValue;
|
|
170
|
+
this.stopMonitoring();
|
|
171
|
+
this.savePauseState();
|
|
172
|
+
if (this.currentConfig.onPaused) {
|
|
173
|
+
this.currentConfig.onPaused();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 恢复
|
|
178
|
+
*/
|
|
179
|
+
resume() {
|
|
180
|
+
if (!this.isPaused)
|
|
181
|
+
return;
|
|
182
|
+
const pauseDuration = Date.now() - this.pauseStartTime;
|
|
183
|
+
if (this.currentConfig.mode === 'fixed') {
|
|
184
|
+
this.lastActivityTime += pauseDuration;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
this.lastActivityTime += pauseDuration;
|
|
188
|
+
this.setLastActivityTime(this.lastActivityTime);
|
|
189
|
+
}
|
|
190
|
+
this.isPaused = false;
|
|
191
|
+
this.pauseStartTime = 0;
|
|
192
|
+
this.pausedRemainingTime = 0;
|
|
193
|
+
this.clearPauseState();
|
|
194
|
+
this.startMonitoring(this.currentConfig, this.enableActivityTracking);
|
|
195
|
+
if (this.currentConfig.onResumed) {
|
|
196
|
+
this.currentConfig.onResumed();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 检查是否暂停
|
|
201
|
+
*/
|
|
202
|
+
isAutoLogoutPaused() {
|
|
203
|
+
return this.isPaused;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* 格式化倒计时
|
|
207
|
+
*/
|
|
208
|
+
formatCountdown(seconds) {
|
|
209
|
+
const mins = Math.floor(seconds / 60);
|
|
210
|
+
const secs = seconds % 60;
|
|
211
|
+
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 判断是否警告状态
|
|
215
|
+
*/
|
|
216
|
+
isWarningState(seconds) {
|
|
217
|
+
const warningTime = this.currentConfig.warningTime || 30;
|
|
218
|
+
return seconds <= warningTime && seconds > 0;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 判断是否紧急状态(最后10秒)
|
|
222
|
+
*/
|
|
223
|
+
isUrgentState(seconds) {
|
|
224
|
+
return seconds <= 10 && seconds > 0;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* 获取当前模式
|
|
228
|
+
*/
|
|
229
|
+
getCurrentMode() {
|
|
230
|
+
return this.currentConfig.mode;
|
|
231
|
+
}
|
|
232
|
+
// ==================== 私有方法 ====================
|
|
233
|
+
initFixedMode() {
|
|
234
|
+
const loginTime = this.getLoginTime();
|
|
235
|
+
if (!loginTime) {
|
|
236
|
+
this.setLoginTime(Date.now());
|
|
237
|
+
this.lastActivityTime = Date.now();
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
this.lastActivityTime = loginTime;
|
|
241
|
+
}
|
|
242
|
+
this.updateCountdownValue();
|
|
243
|
+
}
|
|
244
|
+
initIdleMode(enableTracking) {
|
|
245
|
+
this.enableActivityTracking = enableTracking || true;
|
|
246
|
+
const lastActivity = this.getLastActivityTime();
|
|
247
|
+
if (!lastActivity) {
|
|
248
|
+
this.setLastActivityTime(Date.now());
|
|
249
|
+
this.lastActivityTime = Date.now();
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
this.lastActivityTime = lastActivity;
|
|
253
|
+
}
|
|
254
|
+
this.registerActivityListeners();
|
|
255
|
+
this.updateCountdownValue();
|
|
256
|
+
}
|
|
257
|
+
updateCountdownValue() {
|
|
258
|
+
const now = Date.now();
|
|
259
|
+
const elapsed = now - this.lastActivityTime;
|
|
260
|
+
const remaining = Math.max(0, Math.floor((this.currentConfig.timeout * 1000 - elapsed) / 1000));
|
|
261
|
+
this.countdownValue = remaining;
|
|
262
|
+
this.countdownSubject.next(this.countdownValue);
|
|
263
|
+
}
|
|
264
|
+
updateCountdown() {
|
|
265
|
+
const now = Date.now();
|
|
266
|
+
const elapsed = now - this.lastActivityTime;
|
|
267
|
+
const remaining = Math.max(0, Math.floor((this.currentConfig.timeout * 1000 - elapsed) / 1000));
|
|
268
|
+
if (remaining !== this.countdownValue) {
|
|
269
|
+
this.countdownValue = remaining;
|
|
270
|
+
this.ngZone.run(() => {
|
|
271
|
+
this.countdownSubject.next(this.countdownValue);
|
|
272
|
+
});
|
|
273
|
+
// 警告回调
|
|
274
|
+
const warningTime = this.currentConfig.warningTime || 30;
|
|
275
|
+
if (remaining === warningTime && !this.warningShown) {
|
|
276
|
+
this.warningShown = true;
|
|
277
|
+
if (this.currentConfig.onWarning) {
|
|
278
|
+
this.currentConfig.onWarning(remaining);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// 超时回调
|
|
282
|
+
if (remaining <= 0) {
|
|
283
|
+
this.performAutoLogout();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
performAutoLogout() {
|
|
288
|
+
this.stopMonitoring();
|
|
289
|
+
this.clearLoginTime();
|
|
290
|
+
if (this.currentConfig.onTimeout) {
|
|
291
|
+
this.currentConfig.onTimeout();
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
// 默认行为:导航到登录页
|
|
295
|
+
// Note: Router navigation removed to avoid DI issues in Angular 9 VE
|
|
296
|
+
// Use onTimeout callback for custom navigation
|
|
297
|
+
console.warn('Auto logout timeout - implement custom navigation via onTimeout callback');
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
registerActivityListeners() {
|
|
301
|
+
const events = ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart', 'click'];
|
|
302
|
+
events.forEach((event) => {
|
|
303
|
+
const listener = () => this.resetActivityTimer();
|
|
304
|
+
document.addEventListener(event, listener, true);
|
|
305
|
+
this.activityListeners.push({ event, listener });
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
removeActivityListeners() {
|
|
309
|
+
this.activityListeners.forEach(({ event, listener }) => {
|
|
310
|
+
document.removeEventListener(event, listener, true);
|
|
311
|
+
});
|
|
312
|
+
this.activityListeners = [];
|
|
313
|
+
}
|
|
314
|
+
resetActivityTimer() {
|
|
315
|
+
if (this.currentConfig.mode === 'idle') {
|
|
316
|
+
this.lastActivityTime = Date.now();
|
|
317
|
+
this.setLastActivityTime(this.lastActivityTime);
|
|
318
|
+
this.updateCountdownValue();
|
|
319
|
+
this.warningShown = false;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
isMaxExtendReached() {
|
|
323
|
+
if (this.currentConfig.maxExtendTimes === -1 || this.currentConfig.maxExtendTimes === undefined) {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
const usedCount = this.getExtendCount();
|
|
327
|
+
return usedCount >= this.currentConfig.maxExtendTimes;
|
|
328
|
+
}
|
|
329
|
+
getExtendCount() {
|
|
330
|
+
const countStr = localStorage.getItem(this.EXTEND_COUNT_KEY);
|
|
331
|
+
return countStr ? parseInt(countStr, 10) : 0;
|
|
332
|
+
}
|
|
333
|
+
incrementExtendCount() {
|
|
334
|
+
const currentCount = this.getExtendCount();
|
|
335
|
+
localStorage.setItem(this.EXTEND_COUNT_KEY, (currentCount + 1).toString());
|
|
336
|
+
}
|
|
337
|
+
resetExtendCount() {
|
|
338
|
+
localStorage.removeItem(this.EXTEND_COUNT_KEY);
|
|
339
|
+
}
|
|
340
|
+
getLoginTime() {
|
|
341
|
+
const timeStr = localStorage.getItem(this.LOGIN_TIME_KEY);
|
|
342
|
+
return timeStr ? parseInt(timeStr, 10) : null;
|
|
343
|
+
}
|
|
344
|
+
setLoginTime(time) {
|
|
345
|
+
localStorage.setItem(this.LOGIN_TIME_KEY, time.toString());
|
|
346
|
+
}
|
|
347
|
+
getLastActivityTime() {
|
|
348
|
+
const timeStr = localStorage.getItem(this.LAST_ACTIVITY_KEY);
|
|
349
|
+
return timeStr ? parseInt(timeStr, 10) : null;
|
|
350
|
+
}
|
|
351
|
+
setLastActivityTime(time) {
|
|
352
|
+
localStorage.setItem(this.LAST_ACTIVITY_KEY, time.toString());
|
|
353
|
+
}
|
|
354
|
+
savePauseState() {
|
|
355
|
+
const state = {
|
|
356
|
+
isPaused: true,
|
|
357
|
+
pauseStartTime: this.pauseStartTime,
|
|
358
|
+
pausedRemainingTime: this.pausedRemainingTime,
|
|
359
|
+
mode: this.currentConfig.mode,
|
|
360
|
+
lastActivityTime: this.lastActivityTime
|
|
361
|
+
};
|
|
362
|
+
localStorage.setItem(this.PAUSE_STATE_KEY, JSON.stringify(state));
|
|
363
|
+
}
|
|
364
|
+
clearPauseState() {
|
|
365
|
+
localStorage.removeItem(this.PAUSE_STATE_KEY);
|
|
366
|
+
}
|
|
367
|
+
restorePauseState() {
|
|
368
|
+
const stateStr = localStorage.getItem(this.PAUSE_STATE_KEY);
|
|
369
|
+
if (!stateStr)
|
|
370
|
+
return;
|
|
371
|
+
try {
|
|
372
|
+
const state = JSON.parse(stateStr);
|
|
373
|
+
if (state.isPaused) {
|
|
374
|
+
this.isPaused = true;
|
|
375
|
+
this.pauseStartTime = state.pauseStartTime;
|
|
376
|
+
this.pausedRemainingTime = state.pausedRemainingTime;
|
|
377
|
+
this.lastActivityTime = state.lastActivityTime;
|
|
378
|
+
this.currentConfig.mode = state.mode;
|
|
379
|
+
if (this.timerSubscription) {
|
|
380
|
+
this.timerSubscription.unsubscribe();
|
|
381
|
+
this.timerSubscription = null;
|
|
382
|
+
}
|
|
383
|
+
this.removeActivityListeners();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
catch (e) {
|
|
387
|
+
console.error('恢复暂停状态失败:', e);
|
|
388
|
+
this.clearPauseState();
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
AutoLogoutService.ctorParameters = () => [
|
|
393
|
+
{ type: NgZone }
|
|
394
|
+
];
|
|
395
|
+
AutoLogoutService.ɵprov = ɵɵdefineInjectable({ factory: function AutoLogoutService_Factory() { return new AutoLogoutService(ɵɵinject(NgZone)); }, token: AutoLogoutService, providedIn: "root" });
|
|
396
|
+
AutoLogoutService = __decorate([
|
|
397
|
+
Injectable({ providedIn: 'root' })
|
|
398
|
+
], AutoLogoutService);
|
|
399
|
+
|
|
400
|
+
let AutoLogoutComponent = class AutoLogoutComponent {
|
|
401
|
+
constructor(autoLogout) {
|
|
402
|
+
this.autoLogout = autoLogout;
|
|
403
|
+
// ==================== 输入属性 ====================
|
|
404
|
+
/**
|
|
405
|
+
* 是否显示延长按钮
|
|
406
|
+
*/
|
|
407
|
+
this.showExtendButton = true;
|
|
408
|
+
/**
|
|
409
|
+
* 是否显示暂停按钮
|
|
410
|
+
*/
|
|
411
|
+
this.showPauseButton = true;
|
|
412
|
+
/**
|
|
413
|
+
* 延长的秒数(默认30分钟)
|
|
414
|
+
*/
|
|
415
|
+
this.extendSeconds = 1800;
|
|
416
|
+
// ==================== 输出事件 ====================
|
|
417
|
+
/**
|
|
418
|
+
* 警告事件
|
|
419
|
+
*/
|
|
420
|
+
this.onWarning = new EventEmitter();
|
|
421
|
+
/**
|
|
422
|
+
* 超时事件
|
|
423
|
+
*/
|
|
424
|
+
this.onTimeout = new EventEmitter();
|
|
425
|
+
/**
|
|
426
|
+
* 延长成功事件
|
|
427
|
+
*/
|
|
428
|
+
this.onExtended = new EventEmitter();
|
|
429
|
+
/**
|
|
430
|
+
* 暂停事件
|
|
431
|
+
*/
|
|
432
|
+
this.onPaused = new EventEmitter();
|
|
433
|
+
/**
|
|
434
|
+
* 恢复事件
|
|
435
|
+
*/
|
|
436
|
+
this.onResumed = new EventEmitter();
|
|
437
|
+
// ==================== 内部状态 ====================
|
|
438
|
+
this.subscription = null;
|
|
439
|
+
this.countdown = '00:00';
|
|
440
|
+
this.isWarning = false;
|
|
441
|
+
this.isUrgent = false;
|
|
442
|
+
this.isPaused = false;
|
|
443
|
+
this.canExtend = true;
|
|
444
|
+
this.remainingExtends = -1;
|
|
445
|
+
}
|
|
446
|
+
ngOnInit() {
|
|
447
|
+
// 如果提供了自定义配置,启动监控
|
|
448
|
+
if (this.config) {
|
|
449
|
+
this.autoLogout.startMonitoring(this.config);
|
|
450
|
+
}
|
|
451
|
+
// 订阅倒计时
|
|
452
|
+
this.subscription = this.autoLogout.getCountdown().subscribe((seconds) => {
|
|
453
|
+
var _a;
|
|
454
|
+
this.countdown = this.autoLogout.formatCountdown(seconds);
|
|
455
|
+
this.isWarning = this.autoLogout.isWarningState(seconds);
|
|
456
|
+
this.isUrgent = this.autoLogout.isUrgentState(seconds);
|
|
457
|
+
// 触发警告事件
|
|
458
|
+
const warningTime = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.warningTime) || 30;
|
|
459
|
+
if (seconds === warningTime) {
|
|
460
|
+
this.onWarning.emit(seconds);
|
|
461
|
+
}
|
|
462
|
+
// 触发超时事件
|
|
463
|
+
if (seconds <= 0) {
|
|
464
|
+
this.onTimeout.emit();
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
// 定期检查状态
|
|
468
|
+
setInterval(() => {
|
|
469
|
+
this.updateStatus();
|
|
470
|
+
}, 1000);
|
|
471
|
+
this.updateStatus();
|
|
472
|
+
}
|
|
473
|
+
ngOnDestroy() {
|
|
474
|
+
if (this.subscription) {
|
|
475
|
+
this.subscription.unsubscribe();
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
// ==================== 公开方法 ====================
|
|
479
|
+
/**
|
|
480
|
+
* 延长会话
|
|
481
|
+
*/
|
|
482
|
+
extendSession() {
|
|
483
|
+
this.handleExtend();
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* 暂停
|
|
487
|
+
*/
|
|
488
|
+
pause() {
|
|
489
|
+
this.handleTogglePause();
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* 恢复
|
|
493
|
+
*/
|
|
494
|
+
resume() {
|
|
495
|
+
this.handleTogglePause();
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* 获取模板上下文(用于自定义模板)
|
|
499
|
+
*/
|
|
500
|
+
get templateContext() {
|
|
501
|
+
return {
|
|
502
|
+
countdown: this.countdown,
|
|
503
|
+
isWarning: this.isWarning,
|
|
504
|
+
isUrgent: this.isUrgent,
|
|
505
|
+
isPaused: this.isPaused,
|
|
506
|
+
canExtend: this.canExtend,
|
|
507
|
+
remainingExtends: this.remainingExtends,
|
|
508
|
+
extendSession: () => this.handleExtend(),
|
|
509
|
+
togglePause: () => this.handleTogglePause(),
|
|
510
|
+
formatCountdown: (seconds) => this.autoLogout.formatCountdown(seconds)
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
// ==================== 内部方法 ====================
|
|
514
|
+
updateStatus() {
|
|
515
|
+
this.isPaused = this.autoLogout.isAutoLogoutPaused();
|
|
516
|
+
this.canExtend = this.autoLogout.canExtendSession();
|
|
517
|
+
this.remainingExtends = this.autoLogout.getRemainingExtendTimes();
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* 处理延长会话(模板调用)
|
|
521
|
+
*/
|
|
522
|
+
handleExtend() {
|
|
523
|
+
const success = this.autoLogout.extendSession(this.extendSeconds);
|
|
524
|
+
if (success) {
|
|
525
|
+
this.onExtended.emit(this.extendSeconds);
|
|
526
|
+
}
|
|
527
|
+
this.updateStatus();
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* 处理暂停/恢复切换(模板调用)
|
|
531
|
+
*/
|
|
532
|
+
handleTogglePause() {
|
|
533
|
+
if (this.isPaused) {
|
|
534
|
+
this.autoLogout.resume();
|
|
535
|
+
this.onResumed.emit();
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
this.autoLogout.pause();
|
|
539
|
+
this.onPaused.emit();
|
|
540
|
+
}
|
|
541
|
+
this.updateStatus();
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
AutoLogoutComponent.ctorParameters = () => [
|
|
545
|
+
{ type: AutoLogoutService }
|
|
546
|
+
];
|
|
547
|
+
__decorate([
|
|
548
|
+
Input()
|
|
549
|
+
], AutoLogoutComponent.prototype, "showExtendButton", void 0);
|
|
550
|
+
__decorate([
|
|
551
|
+
Input()
|
|
552
|
+
], AutoLogoutComponent.prototype, "showPauseButton", void 0);
|
|
553
|
+
__decorate([
|
|
554
|
+
Input()
|
|
555
|
+
], AutoLogoutComponent.prototype, "extendSeconds", void 0);
|
|
556
|
+
__decorate([
|
|
557
|
+
Input()
|
|
558
|
+
], AutoLogoutComponent.prototype, "config", void 0);
|
|
559
|
+
__decorate([
|
|
560
|
+
Input()
|
|
561
|
+
], AutoLogoutComponent.prototype, "customTemplate", void 0);
|
|
562
|
+
__decorate([
|
|
563
|
+
Output()
|
|
564
|
+
], AutoLogoutComponent.prototype, "onWarning", void 0);
|
|
565
|
+
__decorate([
|
|
566
|
+
Output()
|
|
567
|
+
], AutoLogoutComponent.prototype, "onTimeout", void 0);
|
|
568
|
+
__decorate([
|
|
569
|
+
Output()
|
|
570
|
+
], AutoLogoutComponent.prototype, "onExtended", void 0);
|
|
571
|
+
__decorate([
|
|
572
|
+
Output()
|
|
573
|
+
], AutoLogoutComponent.prototype, "onPaused", void 0);
|
|
574
|
+
__decorate([
|
|
575
|
+
Output()
|
|
576
|
+
], AutoLogoutComponent.prototype, "onResumed", void 0);
|
|
577
|
+
AutoLogoutComponent = __decorate([
|
|
578
|
+
Component({
|
|
579
|
+
selector: 'ngx-auto-logout',
|
|
580
|
+
template: `
|
|
581
|
+
<!-- 默认UI模式(傻瓜模式) -->
|
|
582
|
+
<div *ngIf="!customTemplate" class="ngx-auto-logout-container">
|
|
583
|
+
<!-- 倒计时显示 -->
|
|
584
|
+
<div class="countdown-display" [class.warning]="isWarning" [class.urgent]="isUrgent" [class.paused]="isPaused">
|
|
585
|
+
<span class="icon">{{ isPaused ? '⏸️' : '⏱️' }}</span>
|
|
586
|
+
<span class="time">{{ countdown }}</span>
|
|
587
|
+
<span *ngIf="isPaused" class="pause-label">(已暂停)</span>
|
|
588
|
+
</div>
|
|
589
|
+
|
|
590
|
+
<!-- 控制按钮 -->
|
|
591
|
+
<div class="controls">
|
|
592
|
+
<!-- 延长会话按钮 -->
|
|
593
|
+
<button *ngIf="showExtendButton && canExtend" class="btn btn-extend" (click)="handleExtend()" [disabled]="!canExtend" title="延长30分钟">
|
|
594
|
+
➕ 延长
|
|
595
|
+
<span *ngIf="remainingExtends !== -1" class="extend-count"> ({{ remainingExtends }}) </span>
|
|
596
|
+
</button>
|
|
597
|
+
|
|
598
|
+
<!-- 暂停/恢复按钮 -->
|
|
599
|
+
<button
|
|
600
|
+
*ngIf="showPauseButton"
|
|
601
|
+
class="btn btn-pause"
|
|
602
|
+
[class.paused]="isPaused"
|
|
603
|
+
(click)="handleTogglePause()"
|
|
604
|
+
[title]="isPaused ? '恢复自动登出' : '暂停自动登出'"
|
|
605
|
+
>
|
|
606
|
+
{{ isPaused ? '▶️ 恢复' : '⏸️ 暂停' }}
|
|
607
|
+
</button>
|
|
608
|
+
</div>
|
|
609
|
+
</div>
|
|
610
|
+
|
|
611
|
+
<!-- 自定义模板模式(专家模式) -->
|
|
612
|
+
<ng-container *ngIf="customTemplate">
|
|
613
|
+
<ng-container [ngTemplateOutlet]="customTemplate" [ngTemplateOutletContext]="templateContext"></ng-container>
|
|
614
|
+
</ng-container>
|
|
615
|
+
`,
|
|
616
|
+
styles: [`
|
|
617
|
+
.ngx-auto-logout-container {
|
|
618
|
+
display: inline-flex;
|
|
619
|
+
align-items: center;
|
|
620
|
+
gap: 12px;
|
|
621
|
+
padding: 8px 12px;
|
|
622
|
+
background: #f8f9fa;
|
|
623
|
+
border-radius: 6px;
|
|
624
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
.countdown-display {
|
|
628
|
+
display: flex;
|
|
629
|
+
align-items: center;
|
|
630
|
+
gap: 8px;
|
|
631
|
+
padding: 6px 12px;
|
|
632
|
+
background: white;
|
|
633
|
+
border-radius: 4px;
|
|
634
|
+
font-weight: bold;
|
|
635
|
+
font-size: 16px;
|
|
636
|
+
transition: all 0.3s ease;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
.countdown-display.warning {
|
|
640
|
+
background: #fff3cd;
|
|
641
|
+
color: #856404;
|
|
642
|
+
animation: blink-warning 1s ease-in-out infinite;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
.countdown-display.urgent {
|
|
646
|
+
background: #f8d7da;
|
|
647
|
+
color: #721c24;
|
|
648
|
+
animation: blink-urgent 0.5s ease-in-out infinite;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
.countdown-display.paused {
|
|
652
|
+
opacity: 0.6;
|
|
653
|
+
background: #e9ecef;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
.icon {
|
|
657
|
+
font-size: 18px;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
.time {
|
|
661
|
+
min-width: 50px;
|
|
662
|
+
text-align: center;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
.pause-label {
|
|
666
|
+
font-size: 12px;
|
|
667
|
+
color: #6c757d;
|
|
668
|
+
font-weight: normal;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
.controls {
|
|
672
|
+
display: flex;
|
|
673
|
+
gap: 8px;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
.btn {
|
|
677
|
+
padding: 6px 12px;
|
|
678
|
+
font-size: 13px;
|
|
679
|
+
border: none;
|
|
680
|
+
border-radius: 4px;
|
|
681
|
+
cursor: pointer;
|
|
682
|
+
transition: all 0.2s;
|
|
683
|
+
font-weight: 500;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
.btn-extend {
|
|
687
|
+
background: #28a745;
|
|
688
|
+
color: white;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
.btn-extend:hover:not(:disabled) {
|
|
692
|
+
background: #218838;
|
|
693
|
+
transform: translateY(-1px);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
.btn-extend:disabled {
|
|
697
|
+
background: #ccc;
|
|
698
|
+
cursor: not-allowed;
|
|
699
|
+
opacity: 0.6;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
.btn-pause {
|
|
703
|
+
background: #007bff;
|
|
704
|
+
color: white;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
.btn-pause:hover {
|
|
708
|
+
background: #0056b3;
|
|
709
|
+
transform: translateY(-1px);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
.btn-pause.paused {
|
|
713
|
+
background: #ffc107;
|
|
714
|
+
color: #212529;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
.extend-count {
|
|
718
|
+
font-size: 11px;
|
|
719
|
+
opacity: 0.9;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
@keyframes blink-warning {
|
|
723
|
+
0%,
|
|
724
|
+
100% {
|
|
725
|
+
opacity: 1;
|
|
726
|
+
}
|
|
727
|
+
50% {
|
|
728
|
+
opacity: 0.7;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
@keyframes blink-urgent {
|
|
733
|
+
0%,
|
|
734
|
+
100% {
|
|
735
|
+
opacity: 1;
|
|
736
|
+
transform: scale(1);
|
|
737
|
+
}
|
|
738
|
+
50% {
|
|
739
|
+
opacity: 0.6;
|
|
740
|
+
transform: scale(1.05);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
`]
|
|
744
|
+
})
|
|
745
|
+
], AutoLogoutComponent);
|
|
746
|
+
|
|
747
|
+
var AutoLogoutModule_1;
|
|
748
|
+
/**
|
|
749
|
+
* Auto logout module
|
|
750
|
+
* Note: Service uses providedIn: 'root', module only declares/exports component
|
|
751
|
+
*/
|
|
752
|
+
let AutoLogoutModule = AutoLogoutModule_1 = class AutoLogoutModule {
|
|
753
|
+
static forRoot() {
|
|
754
|
+
return {
|
|
755
|
+
ngModule: AutoLogoutModule_1,
|
|
756
|
+
providers: []
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
AutoLogoutModule = AutoLogoutModule_1 = __decorate([
|
|
761
|
+
NgModule({
|
|
762
|
+
declarations: [AutoLogoutComponent],
|
|
763
|
+
imports: [CommonModule],
|
|
764
|
+
exports: [AutoLogoutComponent]
|
|
765
|
+
})
|
|
766
|
+
], AutoLogoutModule);
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* 提供自动登出配置(Angular 15+ Standalone)
|
|
770
|
+
*/
|
|
771
|
+
function provideAutoLogout(config) {
|
|
772
|
+
return {
|
|
773
|
+
provide: AUTO_LOGOUT_CONFIG,
|
|
774
|
+
useValue: config
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/*
|
|
779
|
+
* Public API Surface of auto-logout
|
|
780
|
+
*/
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* Generated bundle index. Do not edit.
|
|
784
|
+
*/
|
|
785
|
+
|
|
786
|
+
export { AUTO_LOGOUT_CONFIG, AutoLogoutComponent, AutoLogoutModule, AutoLogoutService, DEFAULT_AUTO_LOGOUT_CONFIG, provideAutoLogout };
|
|
787
|
+
//# sourceMappingURL=ngx-auto-logout.js.map
|