Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

apiservice.js 7.4KB

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