Skip to content

Commit 66b4e50

Browse files
committed
perf: 优化 auth 检查,避免多次请求 api
1 parent 8384f92 commit 66b4e50

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

src/app/services/app.ts

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import {LocalStorageService, LogService} from './share';
88
import {SettingService} from '@app/services/setting';
99
import {Account, Asset, AuthInfo, ConnectData, Endpoint, Organization, View} from '@app/model';
1010
import * as CryptoJS from 'crypto-js';
11-
import {getCookie, setCookie} from '@app/utils/common';
1211
import {OrganizationService} from './organization';
1312
import {I18nService} from '@app/services/i18n';
14-
import {config} from 'rxjs';
1513

1614
declare function unescape(s: string): string;
1715

@@ -34,7 +32,7 @@ export class AppService {
3432
private protocolConnectTypesMap: object = {};
3533
private checkIntervalId: number;
3634
private newLoginHasOpen = false; // 避免多次打开新登录页
37-
private isCheckingProfile = false; // 是否在检查中
35+
private checkSecond = 120;
3836

3937
constructor(private _http: HttpService,
4038
private _router: Router,
@@ -69,33 +67,62 @@ export class AppService {
6967
}
7068
}
7169

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+
}
7583
}
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';
8090
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());
9495
}
95-
);
96+
} else {
97+
this._logger.debug('Found cache using: ', statusTime);
98+
}
99+
return status;
96100
}
97101

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+
}
99126
if (this.checkIntervalId) {
100127
clearInterval(this.checkIntervalId);
101128
}

0 commit comments

Comments
 (0)