Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

apiservice.js 7.9KB

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