http.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import Axios from 'axios';
  2. import { PageList } from '../pages/Router';
  3. import app from '../../app.json';
  4. //config
  5. //const hostUrl = 'http://161.117.183.142/';
  6. const hostUrl = app.product ? 'https://csms.chargeco.global/' : 'https://uat.chargeco.global/';
  7. //const hostUrl = app.product ? 'https://csms.chargeco.global/' : 'https://test.internal.chargeco.global/'
  8. const service = 'chargeco/'
  9. export const host = hostUrl;
  10. //在生产模式显示日志
  11. const DEBUG_PROD = false;
  12. //日志开关
  13. const DEBUG = DEBUG_PROD || (app.debug && !app.product);
  14. Axios.defaults.timeout = 30000;
  15. Axios.defaults.timeoutErrorMessage = "Connection timeout, please check your network.";
  16. Axios.interceptors.response.use((response) => {
  17. if (DEBUG) {
  18. console.log('-------', response.config.method, response.config.url);
  19. console.log('-------', response.status, JSON.stringify(response.data));
  20. }
  21. if (response.data.code == '401' || response.data.code == '402') {
  22. setAccessToken('');
  23. startPage(PageList.login, {action: response.data.code});
  24. return Promise.reject('Need sign in');
  25. }
  26. if (response.data.code == '500' || response.data.code == '502') {
  27. setAccessToken('');
  28. return Promise.reject('Sever error');
  29. }
  30. return response.data;
  31. }, (error) => {
  32. console.info('-------error', error);
  33. return Promise.reject(error);
  34. });
  35. export const get = (path, params) => {
  36. return new Promise((resolve, reject) => {
  37. Axios.get(host + service + path, {
  38. params: params,
  39. method: 'GET',
  40. headers: {
  41. 'Accept': 'application/json',
  42. 'lang': global.currentLocale,
  43. 'accessToken': global.accessToken ?? ''
  44. }
  45. }).then(res => {
  46. if (res.success) {
  47. resolve(res);
  48. } else if (res.msg) {
  49. reject({err: res.msg, ...res});
  50. } else {
  51. reject('Request Failed');
  52. }
  53. }).catch(error => {
  54. console.info('HTTP-ERROR', error);
  55. reject(error);
  56. });
  57. });
  58. }
  59. export const post = (path, params) => {
  60. return new Promise((resolve, reject) => {
  61. Axios.post(host + service + path, params, {
  62. method: 'POST',
  63. headers: {
  64. 'Accept': 'application/json',
  65. 'lang': global.currentLocale,
  66. 'accessToken': global.accessToken ?? ''
  67. }
  68. }).then(res => {
  69. if (res.success) {
  70. resolve(res);
  71. } else if (res.msg) {
  72. reject({err: res.msg, ...res});
  73. } else {
  74. reject('Request Failed');
  75. }
  76. }).catch(error => {
  77. console.info('HTTP-ERROR', error);
  78. reject(error);
  79. });
  80. });
  81. }
  82. export const upload = (path, params, header={}) => {
  83. return new Promise((resolve, reject) => {
  84. Axios.post(host + service + path, params, {
  85. method: 'POST',
  86. headers: {
  87. 'lang': global.currentLocale,
  88. 'Accept': 'application/json',
  89. 'Content-Type': 'multipart/form-data',
  90. 'accessToken': global.accessToken ?? '',
  91. ...header
  92. }
  93. }).then(res => {
  94. if (res.success) {
  95. resolve(res);
  96. } else if (res.msg) {
  97. reject({err: res.msg, ...res});
  98. } else {
  99. reject('Request Failed');
  100. }
  101. }).catch(error => {
  102. console.info('HTTP-ERROR', error);
  103. reject(error);
  104. });
  105. })
  106. }
  107. export const del = (path) => {
  108. return new Promise((resolve, reject) => {
  109. Axios.delete(host + service + path, {
  110. method: 'DELETE',
  111. headers: {
  112. 'Accept': 'application/json',
  113. 'lang': global.currentLocale,
  114. 'accessToken': global.accessToken ?? ''
  115. }
  116. }).then(res => {
  117. if (res.success) {
  118. resolve(res);
  119. } else if (res.msg) {
  120. reject({err: res.msg, ...res});
  121. } else {
  122. reject('Request Failed');
  123. }
  124. }).catch(error => {
  125. console.info('HTTP-ERROR', error);
  126. reject(error);
  127. });
  128. });
  129. }
  130. export const GET = (url, params) => {
  131. var request = host + service + url;
  132. if (params) {
  133. var keys = ''
  134. for (let key in params) {
  135. if (keys != '') {
  136. keys += '&';
  137. }
  138. keys += key + '=' + encodeURI(params[key]);
  139. }
  140. if (keys !== '') {
  141. request += '?' + keys;
  142. }
  143. }
  144. return new Promise((resolve, reject) => {
  145. fetch(request, {
  146. method: 'GET',
  147. headers: {
  148. 'lang': global.currentLocale,
  149. 'Accept': 'application/json',
  150. 'accessToken': global.accessToken ?? ''
  151. }.then((response) => {
  152. if (response.ok) {
  153. return response.json();
  154. } else {
  155. reject(response);
  156. }
  157. }).then(data => {
  158. resolve(data);
  159. }).catch((error) => {
  160. reject(error);
  161. })
  162. });
  163. });
  164. }
  165. export const POST = (url, params) => {
  166. return new Promise((resolve, reject) => {
  167. fetch(host + service + url, {
  168. method: 'POST',
  169. headers: {
  170. 'lang': global.currentLocale,
  171. 'Accept': 'application/json',
  172. 'content-type': 'application/json',
  173. 'accessToken': global.accessToken ?? ''
  174. },
  175. body: JSON.stringify(params)
  176. }).then((response) => {
  177. if (response.ok) {
  178. return response.json();
  179. } else {
  180. reject(response);
  181. }
  182. }).then(data => {
  183. resolve(data);
  184. }).catch((error) => {
  185. reject(error);
  186. })
  187. });
  188. }
  189. export const SOCKET = (url) => {
  190. const ws = new WebSocket(url); // like 'wss://vbea.com/path'
  191. ws.onopen = () => {
  192. // connection opened
  193. ws.send('something'); // send a message
  194. };
  195. ws.onmessage = (e) => {
  196. // a message was received
  197. console.log(e.data);
  198. };
  199. ws.onerror = (e) => {
  200. // an error occurred
  201. console.log(e.message);
  202. };
  203. ws.onclose = (e) => {
  204. // connection closed
  205. console.log(e.code, e.reason);
  206. };
  207. }
  208. export const setAccessToken = token => {
  209. global.accessToken = token;
  210. }