import { Injectable, Injector } from '@angular/core'; import { IRoutesInfo } from './../Authorization/routes.info'; //import { environment } from '../environments/environment'; import { CbmsService } from './../cbms.services'; import { CbmsSharedService } from './cbms-shared.service' import { ActivatedRoute } from '@angular/router'; @Injectable({ providedIn: 'root' }) export class CbmsBaseUtilService { public userInfoId: number; public lstRoutes: Array; public IsAuthenticate: boolean; public isuidValid: boolean; xid: any; public uid: string; constructor(public injector: Injector, private cbmsService: CbmsService, private cbmsSharedService: CbmsSharedService, private router: ActivatedRoute) { } get RoutesInfo(): IRoutesInfo[] { return this.lstRoutes; } public authenticateUser(obj): any { if (obj != undefined && obj != null) { return obj; } else if (localStorage.getItem('UserID') !== null && localStorage.getItem('UserID') !== "") { let userInfo = localStorage.getItem('UserID') this.uid = JSON.parse(userInfo); if (this.uid !== null && this.uid !== undefined) { return this.uid; } else { // this.IsAuthenticate = false; localStorage.removeItem('UserID'); location.href = this.injector.get("config").appLoginURL; return false; } } else { location.href = this.injector.get("config").appLoginURL; return false; } } public async UidValidation(userInfo) { if (userInfo == null && userInfo == undefined) { this.IsAuthenticate = false; localStorage.removeItem('UserID'); location.href = this.injector.get("config").appLoginURL; return false; } else { let res; if (userInfo.type == "uid") { res = await this.cbmsService.Validateuid(userInfo.id); // Normalize response: support either plain number/string or object { userInfoId: n } if (res && typeof res === 'object' && (res as any).userInfoId != null) { res = (res as any).userInfoId; } } else if (userInfo.type == "xid") { res = await this.cbmsService.Validatexid(userInfo.id); } if (!Number(res)) { localStorage.removeItem('UserID'); location.href = this.injector.get("config").appLoginURL; return this.IsAuthenticate; } else { this.userInfoId = res; localStorage.setItem('UserID', JSON.stringify(userInfo)); this.IsAuthenticate = true; return res; } } } public setRoutesInfo(lstRoutes: Array) { this.lstRoutes = lstRoutes; return true; } }