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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. return successResFun(response.data)
  126. } catch (error) {
  127. return failureResFun(error)
  128. }
  129. } catch (error) {
  130. return failureResFun(error)
  131. }
  132. }
  133. buildURL(url){
  134. return `${this.baseURL}/${url}`;
  135. }
  136. buildSuccessResponse(response){
  137. return {
  138. isError : false,
  139. errorMsg : null,
  140. response:response,
  141. }
  142. }
  143. buildFailureResponse(error){
  144. return {
  145. isError : true,
  146. errorMsg : error,
  147. response:null,
  148. }
  149. }
  150. async justAPI_BaseService(){
  151. let isPost = method == 'get' ? false : true;
  152. let successResFun = this.buildSuccessResponse;
  153. let failureResFun = this.buildFailureResponse;
  154. let config = {
  155. url,
  156. method,
  157. headers:{
  158. 'Content-Type': 'application/json'
  159. }
  160. }
  161. if(isPost){
  162. config['data'] = data;
  163. }
  164. try {
  165. let response
  166. try {
  167. response = await axios(config);
  168. if(response.data.code == '417'){
  169. // window.location.href = '/index.html'
  170. return failureResFun(response.data)
  171. }
  172. if(response.data.code != '0'){
  173. return failureResFun(response.data)
  174. }
  175. return successResFun(response.data)
  176. } catch (error) {
  177. return failureResFun(error)
  178. }
  179. } catch (error) {
  180. return failureResFun(error)
  181. }
  182. }
  183. justGetAPIService(url){
  184. return this.justAPI_BaseService('get',url)
  185. }
  186. justPostAPIService(url){
  187. return this.justAPI_BaseService('post',url)
  188. }
  189. isValid(){
  190. const cookieData = COOKIE_HELPER_ACTIONS.getCookie();
  191. if(!cookieData){
  192. return justGetAPIService('/Account/Session/Validate')
  193. }
  194. }
  195. }
  196. const API_SERVICES = new API_SERVICE_CLASS(SERVERNAME);
  197. const API_SERVICES_ACTIONS = {
  198. postAPIService:API_SERVICES.postService,
  199. getAPIService:API_SERVICES.getService
  200. }
  201. // cookie helper
  202. const AUTH = 'AUTH'
  203. class COOKIE_HELPER_CLASS{
  204. constructor(){
  205. this.getCookie = this.getCookie.bind(this);
  206. }
  207. getCookie(){
  208. let cookieVal = Cookies.get(AUTH);
  209. if(!cookieVal){
  210. return null;
  211. }
  212. return JSON.parse(cookieVal)
  213. };
  214. setCookie(value){
  215. debugger;
  216. return Cookies.set(AUTH,value)
  217. };
  218. removeCookie(token){
  219. Cookies.remove(token)
  220. }
  221. async validateToken(){
  222. return await API_SERVICES_ACTIONS.getAPIService(`Account/Session/Validate`)
  223. }
  224. }
  225. const COOKIE_HELPER = new COOKIE_HELPER_CLASS();
  226. const COOKIE_HELPER_ACTIONS ={
  227. getCookie : COOKIE_HELPER.getCookie,
  228. setCookie :COOKIE_HELPER.setCookie,
  229. removeAuthCookie:COOKIE_HELPER.removeCookie.bind(null,AUTH)
  230. }
  231. function setCookieManual(token){
  232. Cookies.set(AUTH,{
  233. token,
  234. userId:null
  235. })
  236. }
  237. async function checkValidAuth(cb,redirect=undefined){
  238. debugger;
  239. document.querySelector('auth-loader').show();
  240. const res = await COOKIE_HELPER.validateToken();
  241. if(!res.response){
  242. if(redirect){
  243. window.location.href =redirect;
  244. document.querySelector('auth-loader').hide();
  245. return;
  246. }
  247. window.location.href = '/';
  248. document.querySelector('auth-loader').hide();
  249. return;
  250. }
  251. setTimeout(()=>{
  252. document.querySelector('auth-loader').hide();
  253. cb();
  254. },300);
  255. }
  256. // setCookieManual('6eb70fee-35ef-4ca2-95cd-01ebdd616eb1');