ms.http.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. (function() {
  2. axios.defaults.timeout = 1000 * 60;
  3. axios.defaults.baseURL = '';
  4. //http request 拦截器
  5. axios.interceptors.request.use(
  6. function(config) {
  7. config.headers = {
  8. 'Content-Type': 'application/x-www-form-urlencoded',
  9. 'X-Requested-With': 'XMLHttpRequest'
  10. }
  11. if (config.method === 'post' && config.headers["Content-Type"] === "application/x-www-form-urlencoded") {
  12. config.data = Qs.stringify(config.data, {
  13. allowDots: true
  14. });
  15. }
  16. return config;
  17. },
  18. function(error) {
  19. return Promise.reject(err);
  20. }
  21. );
  22. //http response 拦截器
  23. axios.interceptors.response.use(
  24. function(response) {
  25. //登录失效
  26. if (response.data.bizCode == "401") {
  27. window.parent.location.href = ms.base + "/" + ms.login + "?backurl=" + encodeURIComponent(window.parent.location.href);
  28. return;
  29. }
  30. return response;
  31. },
  32. function(error) {
  33. return Promise.reject(error)
  34. }
  35. )
  36. function ajax(conf) {
  37. if (conf != undefined) {
  38. var _axios = axios.create({
  39. baseURL: conf.baseURL == undefined ? axios.defaults.baseURL : conf.baseURL,
  40. timeout: conf.timeout == undefined ? axios.defaults.timeout : conf.timeout,
  41. headers: conf.headers == undefined ? null : conf.headers,
  42. });
  43. _axios.interceptors.request.use(
  44. function(config) {
  45. if (config.method === 'post' && config.headers["Content-Type"] === "application/x-www-form-urlencoded") {
  46. config.data = Qs.stringify(config.data, {
  47. allowDots: true
  48. });
  49. }
  50. return config;
  51. },
  52. function(error) {
  53. return Promise.reject(err);
  54. }
  55. );
  56. return _axios;
  57. }
  58. return axios;
  59. }
  60. /**
  61. * 封装get方法
  62. * @param url
  63. * @param data
  64. * @returns {Promise}
  65. */
  66. function get(url, params) {
  67. if (params == undefined) {
  68. params = {}
  69. }
  70. return new Promise(function(resolve, reject) {
  71. ajax().get(url, {
  72. params: params
  73. })
  74. .then(function(response) {
  75. resolve(response.data);
  76. })
  77. .catch(function(err) {
  78. reject(err)
  79. })
  80. })
  81. }
  82. /**
  83. * 封装post请求
  84. * @param url
  85. * @param data
  86. * @returns {Promise}
  87. */
  88. function post(url, data, conf) {
  89. if (data == undefined) {
  90. data = {}
  91. }
  92. return new Promise(function(resolve, reject) {
  93. ajax(conf).post(url, data, conf)
  94. .then(function(response) {
  95. resolve(response.data);
  96. }, function(err) {
  97. reject(err)
  98. })
  99. })
  100. }
  101. /**
  102. * 封装patch请求
  103. * @param url
  104. * @param data
  105. * @returns {Promise}
  106. */
  107. function patch(url, data, conf) {
  108. if (data == undefined) {
  109. data = {}
  110. }
  111. return new Promise(function(resolve, reject) {
  112. ajax(conf).patch(url, data, conf)
  113. .then(function(response) {
  114. resolve(response);
  115. }, function(err) {
  116. reject(err)
  117. })
  118. })
  119. }
  120. /**
  121. * 封装put请求
  122. * @param url
  123. * @param data
  124. * @returns {Promise}
  125. */
  126. function put(url, data, conf) {
  127. if (data == undefined) {
  128. data = {}
  129. }
  130. return new Promise(function(resolve, reject) {
  131. ajax(conf).put(url, data, conf)
  132. .then(function(response) {
  133. resolve(response.data);
  134. }, function(err) {
  135. reject(err)
  136. })
  137. })
  138. }
  139. var http = {
  140. get: get,
  141. post: post,
  142. put: put,
  143. patch: patch
  144. }
  145. if (typeof ms != "object") {
  146. window.ms = {};
  147. }
  148. window.ms.http = http;
  149. }());