Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

apiservice.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. let SERVERNAME = 'https://app.anwisystems.com';
  2. //let SERVERNAME = 'https://anwi.bizgaze.app';
  3. //let SERVERNAME = 'http://beta.bizgaze.com';
  4. //let SERVERNAME = 'https://qa.anwisystems.com';
  5. //let SERVERNAME = 'http://localhost:3088';
  6. //template
  7. //const STAT = '5af6d904ca334734be706b86b8bfe252';
  8. //live
  9. const STAT = '484acd68fe014518b73fa54ad674f0b8';
  10. const USER_AUTH_OKAY = 'USER_AUTH_OKAY';
  11. function imgServerNameBuild(path){
  12. return `https://assets.bizgaze.com/${path}`;
  13. }
  14. async function getStatAPIService(url,data={}){
  15. let config = {
  16. url,
  17. method:'get',
  18. data:data,
  19. headers: {
  20. 'Authorization': `stat ${STAT}`,
  21. 'Content-Type': 'application/json'
  22. },
  23. }
  24. let response = await axios(config);
  25. return response;
  26. }
  27. async function getAPIService(url,data={}){
  28. const config = {
  29. url:`${SERVERNAME}/${url}`,
  30. method: "get",
  31. };
  32. let response = await axios(config);
  33. return response;
  34. }
  35. async function postAPIService(url,data={}){
  36. let config = {
  37. url:`${SERVERNAME}/${url}`,
  38. method:'post',
  39. data:data,
  40. }
  41. let response = await axios(config);
  42. return response;
  43. }
  44. async function postStatAPIService(url,data={}){
  45. let config = {
  46. url,
  47. method:'post',
  48. data:data,
  49. headers: {
  50. 'Authorization': `stat ${STAT}`,
  51. 'Content-Type': 'application/json'
  52. },
  53. }
  54. let response = await axios(config);
  55. return response;
  56. }
  57. async function postAPIServiceLocal(url,data={}){
  58. let config = {
  59. url:`${SERVERNAME}/${url}`,
  60. method:'post',
  61. data:JSON.stringify(data ),
  62. headers: {
  63. 'Authorization': `Basic b67607dd-283e-478e-b2cf-35736e8bad69`,
  64. 'Content-Type': 'application/json'
  65. },
  66. }
  67. let response = await axios(config);
  68. return response;
  69. }
  70. async function getAPIServiceLocal(url){
  71. let config = {
  72. url:`${SERVERNAME}/${url}`,
  73. method:'get',
  74. headers: {
  75. 'Authorization': `Basic b67607dd-283e-478e-b2cf-35736e8bad69`,
  76. 'Content-Type': 'application/json'
  77. },
  78. }
  79. let response = await axios(config);
  80. return response;
  81. }
  82. class API_SERVICE_CLASS{
  83. baseURL = '';
  84. token='';
  85. statToken=STAT;
  86. Instance = null;
  87. constructor(baseurl){
  88. this.Instance = this;
  89. this.baseURL = baseurl;
  90. this.getService = this.getService.bind(this);
  91. this.postService = this.postService.bind(this);
  92. this.justGetAPIService =this.justGetAPIService.bind(this);
  93. this.justPostAPIService = this.justPostAPIService.bind(this);
  94. }
  95. getService(url,isStat = false){
  96. return this.baseService(this.buildURL(url),'get',isStat);
  97. }
  98. postService(url,data,isStat = false){
  99. return this.baseService(this.buildURL(url),'post',isStat,data);
  100. }
  101. async baseService(url,method,isStat,data){
  102. let isPost = method == 'get' ? false : true;
  103. const cookieData = COOKIE_HELPER_ACTIONS.getCookie();
  104. if(!isStat){
  105. if(!cookieData) return window.location.href = "/"
  106. }
  107. let token = isStat ?`stat ${this.statToken}` : `Basic ${cookieData.token}`;
  108. this.token = token;
  109. let successResFun = this.buildSuccessResponse;
  110. let failureResFun = this.buildFailureResponse;
  111. let config = {
  112. url,
  113. method,
  114. headers:{
  115. 'Authorization': token,
  116. 'Content-Type': 'application/json'
  117. }
  118. }
  119. if(isPost){
  120. config['data'] = data;
  121. }
  122. try {
  123. let response
  124. try {
  125. response = await axios(config);
  126. if(response.data === true || response.data === false){
  127. return successResFun(response.data)
  128. }
  129. if(response.data.code == '417'){
  130. // window.location.href = '/index.html'
  131. return failureResFun(response.data)
  132. }
  133. if(response.data.code != '0'){
  134. return failureResFun(response.data)
  135. }
  136. if(response.data.errors?.length){
  137. return failureResFun(response.data)
  138. }
  139. return successResFun(response.data)
  140. } catch (error) {
  141. return failureResFun(error)
  142. }
  143. } catch (error) {
  144. return failureResFun(error)
  145. }
  146. }
  147. buildURL(url){
  148. return `${this.baseURL}/${url}`;
  149. }
  150. buildSuccessResponse(response){
  151. return {
  152. isError : false,
  153. errorMsg : null,
  154. response:response,
  155. }
  156. }
  157. buildFailureResponse(error){
  158. return {
  159. isError : true,
  160. errorMsg : error,
  161. response:null,
  162. }
  163. }
  164. async justAPI_BaseService(){
  165. let isPost = method == 'get' ? false : true;
  166. let successResFun = this.buildSuccessResponse;
  167. let failureResFun = this.buildFailureResponse;
  168. let config = {
  169. url,
  170. method,
  171. headers:{
  172. 'Content-Type': 'application/json'
  173. }
  174. }
  175. if(isPost){
  176. config['data'] = data;
  177. }
  178. try {
  179. let response
  180. try {
  181. response = await axios(config);
  182. if(response.data.code == '417'){
  183. // window.location.href = '/index.html'
  184. return failureResFun(response.data)
  185. }
  186. if(response.data.code != '0'){
  187. return failureResFun(response.data)
  188. }
  189. return successResFun(response.data)
  190. } catch (error) {
  191. return failureResFun(error)
  192. }
  193. } catch (error) {
  194. return failureResFun(error)
  195. }
  196. }
  197. justGetAPIService(url){
  198. return this.justAPI_BaseService('get',url)
  199. }
  200. justPostAPIService(url){
  201. return this.justAPI_BaseService('post',url)
  202. }
  203. isValid(){
  204. const cookieData = COOKIE_HELPER_ACTIONS.getCookie();
  205. if(!cookieData){
  206. return justGetAPIService('/Account/Session/Validate')
  207. }
  208. }
  209. }
  210. const API_SERVICES = new API_SERVICE_CLASS(SERVERNAME);
  211. const API_SERVICES_ACTIONS = {
  212. postAPIService:API_SERVICES.postService,
  213. getAPIService:API_SERVICES.getService
  214. }
  215. // cookie helper
  216. const AUTH = 'AUTH'
  217. class COOKIE_HELPER_CLASS{
  218. constructor(){
  219. this.getCookie = this.getCookie.bind(this);
  220. }
  221. getCookie(){
  222. let cookieVal = Cookies.get(AUTH);
  223. if(!cookieVal){
  224. return null;
  225. }
  226. return JSON.parse(cookieVal)
  227. };
  228. setCookie(value){
  229. debugger;
  230. return Cookies.set(AUTH,value)
  231. };
  232. removeCookie(token){
  233. Cookies.remove(token)
  234. }
  235. async validateToken(){
  236. return await API_SERVICES_ACTIONS.getAPIService(`Account/Session/Validate`)
  237. }
  238. }
  239. const COOKIE_HELPER = new COOKIE_HELPER_CLASS();
  240. const COOKIE_HELPER_ACTIONS ={
  241. getCookie : COOKIE_HELPER.getCookie,
  242. setCookie :COOKIE_HELPER.setCookie,
  243. removeAuthCookie:COOKIE_HELPER.removeCookie.bind(null,AUTH)
  244. }
  245. function setCookieManual(token){
  246. Cookies.set(AUTH,{
  247. token,
  248. userId:null
  249. })
  250. }
  251. async function checkValidAuth(cb,redirect=undefined){
  252. debugger;
  253. document.querySelector('auth-loader').show();
  254. const res = await COOKIE_HELPER.validateToken();
  255. if(!res.response){
  256. if(redirect){
  257. window.location.href =redirect;
  258. document.querySelector('auth-loader').hide();
  259. return;
  260. }
  261. window.location.href = '/';
  262. document.querySelector('auth-loader').hide();
  263. return;
  264. }
  265. setTimeout(()=>{
  266. document.querySelector('auth-loader').hide();
  267. cb();
  268. },300);
  269. }
  270. // setCookieManual('6eb70fee-35ef-4ca2-95cd-01ebdd616eb1');