next-ucap-messenger/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts

177 lines
5.6 KiB
TypeScript
Raw Normal View History

2019-09-19 05:15:43 +00:00
import { Injectable } from '@angular/core';
import {
Resolve,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
2019-10-02 05:34:17 +00:00
import { Observable, throwError, of, EMPTY, forkJoin } from 'rxjs';
import {
map,
tap,
mergeMap,
catchError,
take,
exhaustMap,
concatMap,
switchMap,
withLatestFrom
} from 'rxjs/operators';
2019-09-19 05:15:43 +00:00
2019-10-02 05:34:17 +00:00
import { Store, select } from '@ngrx/store';
2019-09-19 05:15:43 +00:00
import { ProtocolService } from '@ucap-webmessenger/protocol';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
2019-10-02 05:34:17 +00:00
import {
PublicApiService,
VersionInfo2Response
} from '@ucap-webmessenger/api-public';
2019-09-19 05:15:43 +00:00
2019-09-27 03:53:21 +00:00
import {
LoginInfo,
KEY_LOGIN_INFO,
EnvironmentsInfo,
KEY_ENVIRONMENTS_INFO
} from '../types';
2019-09-19 05:15:43 +00:00
import { InnerProtocolService } from '@ucap-webmessenger/protocol-inner';
2019-09-19 06:51:42 +00:00
import {
AuthenticationProtocolService,
2019-09-24 09:42:53 +00:00
SSOMode,
LoginResponse
2019-09-19 06:51:42 +00:00
} from '@ucap-webmessenger/protocol-authentication';
2019-09-24 09:42:53 +00:00
2019-09-24 00:23:30 +00:00
import * as AuthenticationStore from '@app/store/account/authentication';
2019-10-02 05:34:17 +00:00
import { ConnResponse } from 'projects/ucap-webmessenger-protocol-inner/src/lib/models/conn';
import { PiService } from '@ucap-webmessenger/pi';
import { NGXLogger } from 'ngx-logger';
import { QueryProtocolService } from '@ucap-webmessenger/protocol-query';
import { OptionProtocolService } from '@ucap-webmessenger/protocol-option';
import * as AppStore from '@app/store';
import * as VersionInfoStore from '@app/store/setting/version-info';
import * as OptionStore from '@app/store/setting/option';
import * as QueryStore from '@app/store/setting/query';
import * as SyncStore from '@app/store/messenger/sync';
2019-09-19 05:15:43 +00:00
@Injectable()
export class AppMessengerResolver implements Resolve<void> {
2019-09-19 05:15:43 +00:00
constructor(
private store: Store<any>,
private sessionStorageService: SessionStorageService,
private publicApiService: PublicApiService,
2019-10-02 05:34:17 +00:00
private piService: PiService,
2019-09-19 05:15:43 +00:00
private protocolService: ProtocolService,
2019-10-02 05:34:17 +00:00
private queryProtocolService: QueryProtocolService,
private optionProtocolService: OptionProtocolService,
2019-09-19 06:51:42 +00:00
private authenticationProtocolService: AuthenticationProtocolService,
2019-10-02 05:34:17 +00:00
private innerProtocolService: InnerProtocolService,
private logger: NGXLogger
2019-09-19 05:15:43 +00:00
) {}
resolve(
2019-10-02 05:34:17 +00:00
activatedRouteSnapshot: ActivatedRouteSnapshot,
routerStateSnapshot: RouterStateSnapshot
2019-09-19 05:15:43 +00:00
): void | Observable<void> | Promise<void> {
2019-09-24 09:42:53 +00:00
return new Promise<void>((resolve, reject) => {
2019-10-02 05:34:17 +00:00
let versionInfo2Res: VersionInfo2Response;
let connRres: ConnResponse;
let loginRes: LoginResponse;
const loginInfo = this.sessionStorageService.get<LoginInfo>(
KEY_LOGIN_INFO
);
const environmentsInfo = this.sessionStorageService.get<EnvironmentsInfo>(
KEY_ENVIRONMENTS_INFO
);
this.publicApiService
.versionInfo2({
deviceType: environmentsInfo.deviceType,
companyGroupType: loginInfo.companyGroupType,
companyCode: loginInfo.companyCode,
loginId: loginInfo.loginId
})
.pipe(
take(1),
switchMap(res => {
versionInfo2Res = res;
return this.protocolService.connect(versionInfo2Res.serverIp);
}),
switchMap(() => this.innerProtocolService.conn({})),
switchMap(res => {
connRres = res;
return this.authenticationProtocolService.login({
loginId: loginInfo.loginId,
loginPw: loginInfo.loginPw,
deviceType: environmentsInfo.deviceType,
deviceId: ' ',
token: '',
localeCode: loginInfo.localeCode,
pushId: ' ',
companyCode: loginInfo.companyCode,
passwordEncodingType: 1,
clientVersion: '',
reconnect: false,
ip: 'localhost',
hostName: '',
ssoMode: SSOMode.AUTH,
userSpecificInformation: 'PRO_000482',
productId: 'PRO_000482',
productName: 'EZMessenger'
});
}),
switchMap(res =>
forkJoin([
this.queryProtocolService.auth({
deviceType: environmentsInfo.deviceType
}),
this.optionProtocolService.regView({})
])
),
2019-10-02 09:09:39 +00:00
map(([authRes, regViewRes]) => {
2019-10-02 05:34:17 +00:00
this.store.dispatch(
OptionStore.regViewSuccess({ res: regViewRes })
);
this.store.dispatch(QueryStore.authSuccess({ res: authRes }));
}),
withLatestFrom(
this.store.pipe(
select(AppStore.MessengerSelector.SyncSelector.buddy2SyncDate)
),
this.store.pipe(
select(AppStore.MessengerSelector.SyncSelector.group2SyncDate)
2019-10-02 09:09:39 +00:00
),
this.store.pipe(
select(AppStore.MessengerSelector.SyncSelector.roomSyncDate)
2019-10-02 05:34:17 +00:00
)
),
2019-10-02 09:09:39 +00:00
map(([_, buddy2SyncDate, group2SyncDate, roomSyncDate]) => {
2019-10-02 05:34:17 +00:00
this.store.dispatch(SyncStore.buddy2({ syncDate: buddy2SyncDate }));
this.store.dispatch(SyncStore.group2({ syncDate: group2SyncDate }));
2019-10-02 09:09:39 +00:00
this.store.dispatch(
SyncStore.room({
syncDate: roomSyncDate,
localeCode: loginInfo.localeCode
})
);
2019-10-02 05:34:17 +00:00
}),
catchError(err => {
return throwError(err);
})
)
.subscribe(
() => {
this.store.dispatch(
AuthenticationStore.loginSuccess({
loginInfo: loginRes
})
);
resolve();
},
err => {
reject(err);
}
);
2019-09-24 09:42:53 +00:00
});
2019-09-19 05:15:43 +00:00
}
}