import { Injectable } from '@angular/core'; import { AppUtil } from '../app-util/app-util'; import { AppParam, ADSysConfig } from '../../app/app.constant'; import { Platform } from 'ionic-angular'; import { Device } from '@ionic-native/device' @Injectable() export class AppConf { private myAppCache: any = null; private defaultConfig: any = null; private IMEI: string = null; private appParam = new AppParam(); private adSysConfig = new ADSysConfig(); constructor(public device: Device, private platform: Platform, public appUtil: AppUtil) { } public init() { var me = this; let promise = new Promise((resolve, reject) => { if (me.myAppCache === null) { me.myAppCache = me.adSysConfig.sysConfig; me.defaultConfig = me.adSysConfig.sysConfig; resolve(); } else { me.myAppCache = me.myAppCache; me.defaultConfig = me.adSysConfig.sysConfig; resolve(); } me.platform.ready().then(() => { if (me.platform.is('android')) { me.setIMEI(me.device.uuid); } }) }); return promise; } public isLogin() { if (this.appUtil.isNotNull(this.getUserName()) && this.appUtil.isNotNull(this.getUserToken()) && this.appUtil.isNotNull(this.getUserID()) && this.appUtil.isNotNull(this.getAppInstance())) { return true; } else { return false; } } public getLoginRequest() { let param = null; if (this.appUtil.isNotNull(this.getUserToken()) && this.appUtil.isNotNull(this.getUserName())) { param = { "user": this.getUserName(), "Token": this.getUserToken() } } if (param) { return param; } else { return null; } } public removeLoginParam() { this.setLSItems("driverapp.app.Username", null); this.setLSItems("driverapp.app.Token", null); } public clearLocalStorage() { localStorage.clear(); } public getFullUrl() { return (this.getByName("driverapp.app.AppServerUrl").trim() + ":" + parseInt(this.getByName("driverapp.app.AppServerPort")) + this.getByName("driverapp.app.AppServerWSURL").trim()); } getTheLoggerLevel() { return this.getByName("driverapp.app.LogLevel"); } public setLSItems(name, value) { if (name) { localStorage.setItem(name, value); } } public getLSItems(name) { if (name) { return localStorage.getItem(name); } return null; } public setRouteId(value) { return this.setLSItems("driverapp.app.RouteID", value) } public getRouteId() { return this.getLSItems("driverapp.app.RouteID"); } public getAppInstance() { return this.getLSItems("driverapp.app.APP_INSTANCE_ID"); } public setAppInstance(value) { return this.setLSItems("driverapp.app.APP_INSTANCE_ID", value); } public setUserName(value) { return this.setLSItems("driverapp.app.Username", value); } public setUserToken(value) { return this.setLSItems("driverapp.app.Token", value); } public setUserID(value) { return this.setLSItems("driverapp.app.UserID", value); } public getUserID() { return this.getLSItems("driverapp.app.UserID"); } public getUserName() { return this.getLSItems("driverapp.app.Username"); } public getUserToken() { return this.getLSItems("driverapp.app.Token"); } public isBeta() { return this.appParam.driverapp.app.isBeta } public getAppName() { return this.appParam.driverapp.app.APPID; } public getDBName() { return this.appParam.driverapp.app.DBName; } public getVersion() { let installedVersion = this.getLSItems("driverapp.app.Version"); if (installedVersion) { return installedVersion; } else { return this.appParam.driverapp.app.Version; } } public getAppVersion() { return this.appParam.driverapp.app.Version; } public getRole() { return parseInt(this.getByName("driverapp.app.Role")); } public getLang() { return this.getByName("driverapp.app.Lang"); } public getClient() { return parseInt(this.getByName("driverapp.app.Client")); } public getWarehouse() { return parseInt(this.getByName("driverapp.app.Warehouse")); } public getOrg() { return parseInt(this.getByName("driverapp.app.Org")); } public getStage() { return this.getByName("driverapp.app.Stage"); } public getNodeHost() { return this.getByName("driverapp.app.NodeServerUrl"); } public getNodePort() { return parseInt(this.getByName("driverapp.app.NodeServerPort")); } public getNodeFullUrl() { return this.getNodeHost() + ":" + this.getNodePort(); } public getByName(Name) { let obj = []; if (Name != null) { this.myAppCache.forEach(Obj => { if (Obj.name == Name) { obj.push(Obj.value); } }); if (obj.length > 0) { return obj[0]; } else return null; } } public getByID(ID) { let obj = []; if (ID != null) { this.myAppCache.forEach(Obj => { if (Obj.ad_sysconfig_id == ID) { obj.push(Obj.value); } }); if (obj.length > 0) return obj[0]; else return null; } else { return null; } } public getDefaultSysConf() { return this.myAppCache; } public setDefaultSysCong(list) { this.myAppCache = list; } public setIMEI(imei) { this.IMEI = imei; } public getIMEI() { if (this.appUtil.isNotNull(this.IMEI)) { // put self imei as a service let and remove angular.isdefined with app util return this.IMEI; } else { return ('Dev02'); } } public getCordovaAppVersion() { return this.getAppVersion(); } public setWarehouse(value) { if (this.appUtil.isNotNull(value)) { this.setByName("driverapp.app.Warehouse", value); } }; public getDefaultWarehouse() { for (let i in this.defaultConfig) { if (this.defaultConfig[i].name == "driverapp.app.Warehouse") { return this.defaultConfig[i].value; } } }; public setByName(Name, value) { //let obj = []; if (Name != null && value != null) { this.myAppCache.forEach(Obj => { if (Obj.name == Name) { Obj.value = value; } }); } } }