123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- //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 = 'd8c3dc7ce4d2404484004c02d1d3145f';
-
- //live
- //const STAT = '23bdb3c6d08d492c939a53daec34e89a';
-
- const USER_AUTH_OKAY = 'USER_AUTH_OKAY';
-
-
- function imgServerNameBuild(path){
-
- return `https://appassets.bizgaze.app/${path}`;
- }
-
-
- function buildLinkWithServerName(link){
- return `${SERVERNAME}/${link}`
- }
-
-
- 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 = "login.html"
- }
- 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){
- debugger;
- 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){
-
-
- 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');
|