let SERVERNAME = 'https://app.anwisystems.com'; //let SERVERNAME = 'https://anwi.bizgaze.app'; //let SERVERNAME = 'http://beta.bizgaze.com'; //let SERVERNAME = 'https://qa.anwisystems.com'; //let SERVERNAME = 'http://localhost:3088'; //template //const STAT = '5af6d904ca334734be706b86b8bfe252'; //live const STAT = '484acd68fe014518b73fa54ad674f0b8'; const USER_AUTH_OKAY = 'USER_AUTH_OKAY'; function imgServerNameBuild(path){ return `https://assets.bizgaze.com/${path}`; } async function getStatAPIService(url,data={}){ let config = { url, method:'get', data:data, headers: { 'Authorization': `stat ${STAT}`, 'Content-Type': 'application/json' }, } let response = await axios(config); return response; } async function getAPIService(url,data={}){ const config = { url:`${SERVERNAME}/${url}`, method: "get", }; let response = await axios(config); return response; } async function postAPIService(url,data={}){ let config = { url:`${SERVERNAME}/${url}`, method:'post', data:data, } let response = await axios(config); return response; } async function postStatAPIService(url,data={}){ let config = { url, method:'post', data:data, headers: { 'Authorization': `stat ${STAT}`, 'Content-Type': 'application/json' }, } let response = await axios(config); return response; } async function postAPIServiceLocal(url,data={}){ let config = { url:`${SERVERNAME}/${url}`, method:'post', data:JSON.stringify(data ), headers: { 'Authorization': `Basic b67607dd-283e-478e-b2cf-35736e8bad69`, 'Content-Type': 'application/json' }, } let response = await axios(config); return response; } async function getAPIServiceLocal(url){ let config = { url:`${SERVERNAME}/${url}`, method:'get', headers: { 'Authorization': `Basic b67607dd-283e-478e-b2cf-35736e8bad69`, 'Content-Type': 'application/json' }, } let response = await axios(config); return response; } class API_SERVICE_CLASS{ baseURL = ''; token=''; statToken=STAT; Instance = null; constructor(baseurl){ this.Instance = this; this.baseURL = baseurl; this.getService = this.getService.bind(this); this.postService = this.postService.bind(this); this.justGetAPIService =this.justGetAPIService.bind(this); this.justPostAPIService = this.justPostAPIService.bind(this); } getService(url,isStat = false){ return this.baseService(this.buildURL(url),'get',isStat); } postService(url,data,isStat = false){ return this.baseService(this.buildURL(url),'post',isStat,data); } async baseService(url,method,isStat,data){ let isPost = method == 'get' ? false : true; const cookieData = COOKIE_HELPER_ACTIONS.getCookie(); if(!isStat){ if(!cookieData) return window.location.href = "/" } let token = isStat ?`stat ${this.statToken}` : `Basic ${cookieData.token}`; this.token = token; let successResFun = this.buildSuccessResponse; let failureResFun = this.buildFailureResponse; let config = { url, method, headers:{ 'Authorization': token, 'Content-Type': 'application/json' } } if(isPost){ config['data'] = data; } try { let response try { response = await axios(config); if(response.data === true || response.data === false){ return successResFun(response.data) } if(response.data.code == '417'){ // window.location.href = '/index.html' return failureResFun(response.data) } if(response.data.code != '0'){ return failureResFun(response.data) } if(response.data.errors?.length){ return failureResFun(response.data) } return successResFun(response.data) } catch (error) { return failureResFun(error) } } catch (error) { return failureResFun(error) } } buildURL(url){ return `${this.baseURL}/${url}`; } buildSuccessResponse(response){ return { isError : false, errorMsg : null, response:response, } } buildFailureResponse(error){ return { isError : true, errorMsg : error, response:null, } } async justAPI_BaseService(){ let isPost = method == 'get' ? false : true; let successResFun = this.buildSuccessResponse; let failureResFun = this.buildFailureResponse; let config = { url, method, headers:{ 'Content-Type': 'application/json' } } if(isPost){ config['data'] = data; } try { let response try { response = await axios(config); if(response.data.code == '417'){ // window.location.href = '/index.html' return failureResFun(response.data) } if(response.data.code != '0'){ return failureResFun(response.data) } return successResFun(response.data) } catch (error) { return failureResFun(error) } } catch (error) { return failureResFun(error) } } justGetAPIService(url){ return this.justAPI_BaseService('get',url) } justPostAPIService(url){ return this.justAPI_BaseService('post',url) } isValid(){ const cookieData = COOKIE_HELPER_ACTIONS.getCookie(); if(!cookieData){ return justGetAPIService('/Account/Session/Validate') } } } const API_SERVICES = new API_SERVICE_CLASS(SERVERNAME); const API_SERVICES_ACTIONS = { postAPIService:API_SERVICES.postService, getAPIService:API_SERVICES.getService } // cookie helper const AUTH = 'AUTH' class COOKIE_HELPER_CLASS{ constructor(){ this.getCookie = this.getCookie.bind(this); } getCookie(){ let cookieVal = Cookies.get(AUTH); if(!cookieVal){ return null; } return JSON.parse(cookieVal) }; setCookie(value){ debugger; return Cookies.set(AUTH,value) }; removeCookie(token){ Cookies.remove(token) } async validateToken(){ return await API_SERVICES_ACTIONS.getAPIService(`Account/Session/Validate`) } } const COOKIE_HELPER = new COOKIE_HELPER_CLASS(); const COOKIE_HELPER_ACTIONS ={ getCookie : COOKIE_HELPER.getCookie, setCookie :COOKIE_HELPER.setCookie, removeAuthCookie:COOKIE_HELPER.removeCookie.bind(null,AUTH) } function setCookieManual(token){ Cookies.set(AUTH,{ token, userId:null }) } async function checkValidAuth(cb,redirect=undefined){ debugger; document.querySelector('auth-loader').show(); const res = await COOKIE_HELPER.validateToken(); if(!res.response){ if(redirect){ window.location.href =redirect; document.querySelector('auth-loader').hide(); return; } window.location.href = '/'; document.querySelector('auth-loader').hide(); return; } setTimeout(()=>{ document.querySelector('auth-loader').hide(); cb(); },300); } // setCookieManual('6eb70fee-35ef-4ca2-95cd-01ebdd616eb1');