-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathacl-dialog.component.ts
More file actions
492 lines (462 loc) · 14.9 KB
/
Copy pathacl-dialog.component.ts
File metadata and controls
492 lines (462 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import {writeText} from 'clipboard-polyfill';
import {I18nService} from '@app/services/i18n';
import {HttpService} from '@app/services/http';
import {FaceService} from '@app/services/face';
import {HttpErrorResponse} from '@angular/common/http';
import {Component, Inject, OnInit} from '@angular/core';
import {Asset, ConnectData, ConnectionToken} from '@app/model';
import {NzNotificationService} from 'ng-zorro-antd/notification';
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import {NZ_MODAL_DATA, NzModalRef, NzModalService} from 'ng-zorro-antd/modal';
import {ActivatedRoute} from '@angular/router';
import {withUIBase} from '@app/utils/path';
interface DialogAction {
text: string;
type?: 'primary' | 'default';
callback: () => void;
}
interface DialogContent {
title: string;
message: string;
isError?: boolean;
actions: DialogAction[];
customContent?: any;
}
@Component({
standalone: false,
selector: 'elements-acl-dialog',
templateUrl: 'acl-dialog.component.html',
styleUrls: ['acl-dialog.component.scss']
})
export class ElementACLDialogComponent implements OnInit {
public asset: Asset;
public connectInfo: ConnectData;
public code: string;
public connectionToken: ConnectionToken = null;
public error: HttpErrorResponse;
public otherError: string;
public ticketAssignees: string = '-';
// Token 的行为,创建或者兑换 Token, create, exchange
public tokenAction: string = 'create';
public tokenID: string;
public faceVerifyUrl: SafeResourceUrl;
content: DialogContent;
private timerCheckTicket: number;
constructor(
@Inject(NZ_MODAL_DATA) public data: any,
private _i18n: I18nService,
private _http: HttpService,
public _dialog: NzModalService,
private _toastr: NzNotificationService,
private sanitizer: DomSanitizer,
private faceService: FaceService,
public dialogRef: NzModalRef<ElementACLDialogComponent>,
private _route: ActivatedRoute
) {
this.data = data;
}
get ticketDetailPageURL(): string {
return this.connectionToken.from_ticket_info.ticket_detail_page_url;
}
get errorDetail() {
let error = this.data.error.error;
if (this.data.error.status === 500) {
error = this.data.error.message;
}
if (Array.isArray(error)) {
error = error.join(' ');
} else if (typeof error === 'object') {
if (error.detail) {
error = error.detail;
} else {
error = JSON.stringify(error);
}
}
return error;
}
ngOnInit() {
// 创建 Token 的时候,需要传入 Asset 和 ConnectInfo
this.content = this.getDialogContent(this.data.code);
this.asset = this.data.asset;
this.connectInfo = this.data.connectInfo;
this.code = this.data.code;
// 兑换 Token 的时候,需要传入 Token ID
this.tokenID = this.data.tokenID;
// 控制 token 的行为, 创建还是兑换
this.tokenAction = this.data.tokenAction;
}
async onCopySuccess(evt) {
const msg = await this._i18n.t('Copied');
this._toastr.success(msg, '', {nzClass: 'custom-success-notification'});
writeText(evt);
}
onCancelReview() {
this.closeDialog();
}
onConfirmFaceOnline() {
let faceMonitorToken = this._route.snapshot.queryParamMap.get('face_monitor_token');
console.log('get token from queryParamMap:', faceMonitorToken);
if (!faceMonitorToken) {
faceMonitorToken = this.faceService.getToken();
console.log('get token from faceService:', faceMonitorToken);
}
const successCallback = (connToken: ConnectionToken) => {
if (connToken && connToken.face_token) {
this.faceVerifyUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
'/facelive/capture?token=' + connToken.face_token
);
this.code = 'face_verify_capture';
this.content = this.getDialogContent(this.code);
const timer = setInterval(() => {
this._http.getFaceVerifyState(connToken.face_token).subscribe(async data => {
if (data.is_finished) {
clearInterval(timer);
if (!data.success) {
this.code = 'other';
this.otherError = data.error_message;
this.content = this.getDialogContent(this.code);
} else {
const msg = await this._i18n.t('Face verify success');
this._toastr.success(msg, '', {nzClass: 'custom-success-notification'});
this.dialogRef.close(connToken);
this.faceService.openFaceMonitor();
}
}
});
}, 1000);
}
};
const errorCallback = error => {
if (error.error.code === 'no_face_feature') {
this.code = error.error.code;
} else {
this.code = 'other';
this.otherError = error.error.detail;
}
this.content = this.getDialogContent(this.code);
};
if (this.tokenAction === 'exchange') {
this._http
.exchangeConnectToken(this.tokenID, false, true, faceMonitorToken)
.subscribe(successCallback, errorCallback);
} else {
if (this.data.connectData && this.data.connectData.direct) {
this._http
.adminConnectToken(this.asset, this.data.connectData, false, true, faceMonitorToken)
.subscribe(successCallback, errorCallback);
return;
}
this._http
.createConnectToken(this.asset, this.connectInfo, false, true, faceMonitorToken)
.subscribe(successCallback, errorCallback);
}
}
onConfirmFaceVerify() {
const successCallback = (connToken: ConnectionToken) => {
if (connToken && connToken.face_token) {
this.faceVerifyUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
'/facelive/capture?token=' + connToken.face_token
);
this.code = 'face_verify_capture';
this.content = this.getDialogContent(this.code);
const timer = setInterval(() => {
this._http.getFaceVerifyState(connToken.face_token).subscribe(async data => {
if (data.is_finished) {
clearInterval(timer);
if (!data.success) {
this.code = 'other';
this.otherError = data.error_message;
this.content = this.getDialogContent(this.code);
} else {
const msg = await this._i18n.t('Face verify success');
this._toastr.success(msg, '', {nzClass: 'custom-success-notification'});
this.dialogRef.close(connToken);
}
}
});
}, 1000);
}
};
const errorCallback = error => {
if (error.error.code === 'no_face_feature') {
this.code = error.error.code;
} else {
this.code = 'other';
this.otherError = error.error.detail;
}
this.content = this.getDialogContent(this.code);
};
if (this.tokenAction === 'exchange') {
this._http
.exchangeConnectToken(this.tokenID, false, true)
.subscribe(successCallback, errorCallback);
} else {
if (this.data.connectData && this.data.connectData.direct) {
this._http
.adminConnectToken(this.asset, this.data.connectData, false, true)
.subscribe(successCallback, errorCallback);
return;
}
this._http
.createConnectToken(this.asset, this.connectInfo, false, true)
.subscribe(successCallback, errorCallback);
}
}
onCancelFaceVerify() {
this.closeDialog();
}
onConfirmReview() {
const successCallback = (connToken: ConnectionToken) => {
if (connToken && connToken.from_ticket) {
this.connectionToken = connToken;
this.code = 'ticket_review_pending';
this.content = this.getDialogContent(this.code); // 重新生成 content
this.checkTicket();
}
};
const errorCallback = error => {
if (error.error.code.startsWith('acl_')) {
this.code = error.error.code;
} else {
this.code = 'other';
this.otherError = error.error.detail;
}
this.content = this.getDialogContent(this.code);
};
if (this.tokenAction === 'exchange') {
this._http.exchangeConnectToken(this.tokenID, true).subscribe(successCallback, errorCallback);
} else {
if (this.data.connectData && this.data.connectData.direct) {
this._http
.adminConnectToken(this.asset, this.data.connectData, true)
.subscribe(successCallback, errorCallback);
return;
}
this._http
.createConnectToken(this.asset, this.connectInfo, true)
.subscribe(successCallback, errorCallback);
}
}
onCancelWait() {
const closeMethod = this.connectionToken.from_ticket_info.close_ticket_api.method.toLowerCase();
const closeURL = this.connectionToken.from_ticket_info.close_ticket_api.url;
this._http[closeMethod](closeURL).subscribe();
this.closeDialog();
}
closeDialog() {
clearInterval(this.timerCheckTicket);
this.dialogRef.close(null);
}
checkTicket() {
const checkMethod = this.connectionToken.from_ticket_info.check_ticket_api.method.toLowerCase();
const checkURL = this.connectionToken.from_ticket_info.check_ticket_api.url;
const ticketAssignees = this.connectionToken.from_ticket_info.assignees.join(', ');
// @ts-ignore
this.timerCheckTicket = setInterval(() => {
this._http[checkMethod](checkURL).subscribe(
async ticket => {
if (ticket.status.value !== 'closed') {
// 工单未关闭
this.ticketAssignees = ticketAssignees;
this.code = 'ticket_review_pending';
return;
}
const state = ticket.state.value;
if (state === 'approved') {
const msg = await this._i18n.t('Login review approved');
this._toastr.success(msg, '', {nzClass: 'custom-success-notification'});
this.dialogRef.close(this.connectionToken);
} else if (state === 'rejected') {
this.code = 'ticket_review_rejected';
this.content = this.getDialogContent(this.code);
} else if (state === 'closed') {
this.code = 'ticket_review_closed';
this.content = this.getDialogContent(this.code);
}
clearInterval(this.timerCheckTicket);
},
error => {
this.code = 'other';
this.otherError = error.error.detail + `(${checkURL})`;
this.content = this.getDialogContent(this.code);
clearInterval(this.timerCheckTicket);
}
);
}, 3000);
}
private getDialogContent(code: string): DialogContent {
const vm = this;
const contentMap: { [key: string]: DialogContent } = {
acl_reject: {
title: 'Login reminder',
message: 'ACL reject login asset',
isError: true,
actions: [
{
text: 'Close',
type: 'primary',
callback: () => vm.closeDialog()
}
]
},
acl_review: {
title: 'Login reminder',
message: 'Need review for login asset',
actions: [
{
text: 'Cancel',
callback: () => vm.onCancelReview()
},
{
text: 'Confirm',
type: 'primary',
callback: () => vm.onConfirmReview()
}
]
},
acl_face_online: {
title: 'Login reminder',
message: 'Face online required',
actions: [
{
text: 'Cancel',
callback: () => vm.onCancelFaceVerify()
},
{
text: 'Confirm',
type: 'primary',
callback: () => vm.onConfirmFaceOnline()
}
]
},
acl_face_verify: {
title: 'Login reminder',
message: 'Face verify required',
actions: [
{
text: 'Cancel',
callback: () => vm.onCancelFaceVerify()
},
{
text: 'Confirm',
type: 'primary',
callback: () => vm.onConfirmFaceVerify()
}
]
},
acl_face_online_not_supported: {
title: 'Login reminder',
message: this.errorDetail,
isError: true,
actions: [
{
text: 'Close',
callback: () => vm.closeDialog()
}
]
},
face_verify_capture: {
title: this._i18n.instant('Face Verify'),
message: 'Please complete the face verification',
actions: [],
},
no_face_feature: {
title: 'Login reminder',
message: 'No facial features',
isError: true,
customContent: {
type: 'link',
link: withUIBase('#/profile/index'),
linkText: this._i18n.instant('Go to profile')
},
actions: [
{
text: 'Close',
callback: () => vm.closeDialog()
}
]
},
ticket_review_pending: {
title: 'Login reminder',
message: 'Ticket review pending for login asset',
customContent: {
type: 'ticket',
assignees:
this.connectionToken?.from_ticket_info?.assignees?.join(', ') ||
this.ticketAssignees ||
'-',
ticketUrl: this.connectionToken?.from_ticket_info?.ticket_detail_page_url
},
actions: [
{
text: 'Cancel',
callback: () => vm.onCancelWait()
},
{
text: 'Copy link',
type: 'primary',
callback: () => {
vm.onCopySuccess(this.connectionToken?.from_ticket_info?.ticket_detail_page_url);
}
}
]
},
ticket_review_rejected: {
title: 'Login reminder',
message: 'Ticket review rejected for login asset',
isError: true,
actions: [
{
text: 'Close',
callback: () => vm.closeDialog()
}
]
},
ticket_review_closed: {
title: 'Login reminder',
message: 'Ticket review closed for login asset',
isError: true,
actions: [
{
text: 'Close',
callback: () => vm.closeDialog()
}
]
},
perm_account_invalid: {
title: 'Login reminder',
message: 'Account not found',
isError: true,
actions: [
{
text: 'Close',
callback: () => vm.closeDialog()
}
]
},
other: {
title: 'Login reminder',
message: this.otherError,
isError: true,
actions: [
{
text: 'Close',
callback: () => vm.closeDialog()
}
]
},
unknown: {
title: 'Unknown error',
message: this.errorDetail,
isError: true,
actions: [
{
text: 'Close',
callback: () => vm.closeDialog()
}
]
}
};
return contentMap[code] || contentMap.unknown;
}
}