@@ -8,10 +8,8 @@ import {LocalStorageService, LogService} from './share';
8
8
import { SettingService } from '@app/services/setting' ;
9
9
import { Account , Asset , AuthInfo , ConnectData , Endpoint , Organization , View } from '@app/model' ;
10
10
import * as CryptoJS from 'crypto-js' ;
11
- import { getCookie , setCookie } from '@app/utils/common' ;
12
11
import { OrganizationService } from './organization' ;
13
12
import { I18nService } from '@app/services/i18n' ;
14
- import { config } from 'rxjs' ;
15
13
16
14
declare function unescape ( s : string ) : string ;
17
15
@@ -34,7 +32,7 @@ export class AppService {
34
32
private protocolConnectTypesMap : object = { } ;
35
33
private checkIntervalId : number ;
36
34
private newLoginHasOpen = false ; // 避免多次打开新登录页
37
- private isCheckingProfile = false ; // 是否在检查中
35
+ private checkSecond = 120 ;
38
36
39
37
constructor ( private _http : HttpService ,
40
38
private _router : Router ,
@@ -69,33 +67,62 @@ export class AppService {
69
67
}
70
68
}
71
69
72
- doCheckProfile ( ) {
73
- if ( this . isCheckingProfile ) {
74
- return ;
70
+ async getProfileStatus ( recheck = false ) {
71
+ let status = '' ;
72
+ let statusTime = '' ;
73
+ // From local storage
74
+ if ( ! recheck ) {
75
+ statusTime = localStorage . getItem ( 'CheckProfile' ) ;
76
+ if ( statusTime && statusTime . split ( ' ' ) . length === 2 ) {
77
+ const time = statusTime . split ( ' ' ) [ 1 ] ;
78
+ const expired = new Date ( ) . getTime ( ) - parseInt ( time , 10 ) > 1000 * this . checkSecond ;
79
+ if ( ! expired ) {
80
+ status = statusTime . split ( ' ' ) [ 0 ] ;
81
+ }
82
+ }
75
83
}
76
- this . isCheckingProfile = true ;
77
- User . logined = false ;
78
- this . _http . get ( `/api/v1/users/profile/?fields_size=mini` ) . subscribe (
79
- ( res ) => {
84
+
85
+ if ( ! status ) {
86
+ User . logined = false ;
87
+ try {
88
+ await this . _http . get ( `/api/v1/users/profile/?fields_size=mini` ) . toPromise ( ) ;
89
+ status = 'ok' ;
80
90
User . logined = true ;
81
- this . newLoginHasOpen = false ;
82
- this . isCheckingProfile = false ;
83
- } ,
84
- ( err ) => {
85
- const ok = confirm ( this . _i18n . instant ( 'LoginExpireMsg' ) ) ;
86
- if ( ok && ! this . newLoginHasOpen ) {
87
- window . open ( '/core/auth/login/?next=/luna/' , '_self' ) ;
88
- this . newLoginHasOpen = true ;
89
- }
90
- this . isCheckingProfile = false ;
91
- setTimeout ( ( ) => {
92
- this . doCheckProfile ( ) ;
93
- } , 5000 ) ;
91
+ } catch ( err ) {
92
+ status = 'error' ;
93
+ } finally {
94
+ localStorage . setItem ( 'CheckProfile' , status + ' ' + new Date ( ) . getTime ( ) ) ;
94
95
}
95
- ) ;
96
+ } else {
97
+ this . _logger . debug ( 'Found cache using: ' , statusTime ) ;
98
+ }
99
+ return status ;
96
100
}
97
101
98
- intervalCheckLogin ( second : number = 60 * 2 , clear : boolean = false ) {
102
+ async doCheckProfile ( recheck = false ) {
103
+ const status = await this . getProfileStatus ( recheck ) ;
104
+ if ( status === 'ok' ) {
105
+ this . newLoginHasOpen = false ;
106
+ // 重新检查时,如果好了,重启 check
107
+ if ( recheck ) {
108
+ this . intervalCheckLogin ( ) . then ( ) ;
109
+ }
110
+ } else {
111
+ clearInterval ( this . checkIntervalId ) ;
112
+ const ok = confirm ( this . _i18n . instant ( 'LoginExpireMsg' ) ) ;
113
+ if ( ok && ! this . newLoginHasOpen ) {
114
+ window . open ( '/core/auth/login/?next=/luna/' , '_self' ) ;
115
+ this . newLoginHasOpen = true ;
116
+ }
117
+ setTimeout ( ( ) => this . doCheckProfile ( true ) , 5 * 1000 ) ;
118
+ }
119
+ return status ;
120
+ }
121
+
122
+ async intervalCheckLogin ( second = null , clear : boolean = false ) {
123
+ if ( second == null ) {
124
+ second = this . checkSecond ;
125
+ }
99
126
if ( this . checkIntervalId ) {
100
127
clearInterval ( this . checkIntervalId ) ;
101
128
}
0 commit comments