Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

apiservice.js 7.6KB

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