Bez popisu
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.7KB

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