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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // let SERVERNAME = 'https://anwi.bizgaze.app';
  2. let SERVERNAME = 'https://beta.bizgaze.app';
  3. //let SERVERNAME = 'http://localhost:3088';
  4. const STAT = 'b276960fddf84e8cb63de6e32d31529b';
  5. async function getStatAPIService(url,data={}){
  6. let config = {
  7. url,
  8. method:'get',
  9. data:data,
  10. headers: {
  11. 'Authorization': `stat ${STAT}`,
  12. 'Content-Type': 'application/json'
  13. },
  14. }
  15. let response = await axios(config);
  16. return response;
  17. }
  18. async function postStatAPIService(url,data={}){
  19. let config = {
  20. url,
  21. method:'post',
  22. data:data,
  23. headers: {
  24. 'Authorization': `stat ${STAT}`,
  25. 'Content-Type': 'application/json'
  26. },
  27. }
  28. let response = await axios(config);
  29. return response;
  30. }
  31. async function postAPIServiceWarranty(url,data={}){
  32. let SERVERURL = 'https://anwi.bizgaze.app';
  33. let config = {
  34. url:`${SERVERURL}/${url}`,
  35. method:'post',
  36. data:data,
  37. headers: {
  38. 'Authorization': `Basic 6cdcfe22-1623-4740-97e0-363d518c0e8a`,
  39. 'Content-Type': 'application/json'
  40. },
  41. }
  42. let response = await axios(config);
  43. return response;
  44. }
  45. async function postAPIService(url,data={}){
  46. let SERVERURL = 'https://beta.bizgaze.app';
  47. //let SERVERURL = 'http://localhost:3088';
  48. let config = {
  49. url:`${SERVERURL}/${url}`,
  50. method:'post',
  51. data:data,
  52. }
  53. let response = await axios(config);
  54. return response;
  55. }
  56. async function postAPIServiceLocal(url,data={}){
  57. let config = {
  58. url:`${SERVERNAME}/${url}`,
  59. method:'post',
  60. data:JSON.stringify(data),
  61. headers: {
  62. 'Authorization': `Basic c86af480-b5ef-43af-8ce9-503e5b831e2e`,
  63. 'Content-Type': 'application/json'
  64. },
  65. }
  66. let response = await axios(config);
  67. return response;
  68. }
  69. async function getAPIServiceLocal(url){
  70. let SERVERURL = 'https://anwi.bizgaze.app';
  71. let config = {
  72. url:`${SERVERURL}/${url}`,
  73. method:'get',
  74. headers: {
  75. 'Authorization': `Basic ed40b74d-561a-47af-b03b-4f29c5ff6937`,
  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. // let token = isStat ?`stat ${this.statToken}` : `Basic ${this.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. 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. debugger
  253. if(!res.response){
  254. if(redirect){
  255. window.location.href =redirect;
  256. document.querySelector('auth-loader').hide();
  257. return;
  258. }
  259. window.location.href = '/';
  260. document.querySelector('auth-loader').hide();
  261. return;
  262. }
  263. setTimeout(()=>{
  264. document.querySelector('auth-loader').hide();
  265. cb();
  266. },300);
  267. }