request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import axios from "axios"; //引入axios
  2. import sysConfig from '@/config' //引入配置文件
  3. import store from "../store/index"
  4. import qs from "qs"
  5. // console.log(process.env.NODE_ENV)
  6. axios.defaults.baseURL = sysConfig.baseUrl //设置请求时间
  7. axios.defaults.timeout = sysConfig.timeout //设置请求超时时间
  8. const ContentType = sysConfig.ContentType
  9. //axios.defaults.headers["Content-Type"] = "application/json";
  10. // axios.defaults.withCredentials = true
  11. axios.defaults.headers = { 'Content-Type': 'application/json; charset=utf-8' };
  12. // axios.defaults.headers["Content-Type"] = "application/x-www-form-urlencoded";
  13. /** 请求发送前拦截,用于配置求情内容,如headers参数等 */
  14. axios.interceptors.request.use(
  15. (config) => {
  16. config.headers.token = window.localStorage.getItem('MZ_TOKEN');
  17. return config
  18. },
  19. (error) => {
  20. return error
  21. }
  22. )
  23. function error1(res) {
  24. if (res.status == 408 || res.status == 401) { // 需要重新登录
  25. store.commit('setPcToken', "");
  26. console.log(res.data);
  27. // window.xm.showToast({
  28. // message:res.data
  29. // })
  30. } else if (res.status === 404) {
  31. console.log("404,路径找不到:" + res.data.path);
  32. // window.xm.showToast({
  33. // message:"404,路径找不到:"+res.data.path
  34. // })
  35. } else if (res.status === 503) {
  36. console.log("服务不可用");
  37. // window.xm.showToast({
  38. // message:"服务不可用"
  39. // })
  40. } else if (res.status === 504) {
  41. console.log("网络连接错误");
  42. // window.xm.showToast({
  43. // message:"网络连接错误"
  44. // })
  45. } else {
  46. console.log(res.data);
  47. // window.xm.showToast({
  48. // message:res.data
  49. // })
  50. }
  51. }
  52. /** 请求发送后返回拦截,用于处理数据格式等 */
  53. axios.interceptors.response.use(
  54. (response) => {
  55. return response
  56. },
  57. (error) => {
  58. if (error.request) {
  59. error1(error.request);
  60. } else if (error.response) {
  61. }
  62. return error
  63. }
  64. )
  65. //封装http请求
  66. var http = {
  67. /**
  68. * get请求
  69. * @param {*} url 请求地址
  70. * @param {*} params 请求参数,拼接再url地址栏后面,无需求时忽略该参数
  71. * @param {*} config 请求的config相关配置
  72. * @returns
  73. */
  74. get: function(url, params = {}, config = {}) {
  75. return new Promise((resolve, reject) => {
  76. axios({
  77. url: url,
  78. method: 'get',
  79. params: params,
  80. config: config,
  81. }).then((response) => {
  82. resolve(response.data)
  83. }).catch((error) => {
  84. reject(error)
  85. })
  86. })
  87. },
  88. /**
  89. * post请求
  90. * @param {*} url 请求地址
  91. * @param {*} data 请求参数
  92. * @param {*} params 请求参数,拼接再url地址栏后面,无需求时忽略该参数
  93. * @param {*} config 请求的config相关配置
  94. * @returns
  95. */
  96. post: function(url, data = {}, params = {}, config = {}) {
  97. return new Promise((resolve, reject) => {
  98. axios({
  99. url: url,
  100. method: 'post',
  101. data: data,
  102. // params: params,
  103. params: qs.stringify(params),
  104. config: config
  105. }).then((response) => {
  106. //if(response.data && response.data.statusCode !== 200){
  107. // error(response.data);
  108. // reject(response.data)
  109. // }
  110. // resolve(response.data);
  111. resolve(response);
  112. }).catch((error) => {
  113. reject(error)
  114. })
  115. })
  116. },
  117. /**
  118. * 文件上传
  119. * @param {*} url 请求地址
  120. * @param {*} data 请求参数
  121. * @returns
  122. */
  123. uploadFile: function(url, data = {}) {
  124. return new Promise((resolve, reject) => {
  125. axios({
  126. url: url,
  127. method: 'post',
  128. data: data,
  129. headers: { 'Content-Type': 'multipart/form-data' }
  130. }).then((response) => {
  131. resolve(response);
  132. }).catch((error) => {
  133. reject(error)
  134. })
  135. })
  136. },
  137. /**
  138. * 删除
  139. * @param {*} url 请求地址
  140. * @param {*} data 请求参数
  141. * @returns
  142. */
  143. delete: function(url, data = {}) {
  144. return new Promise((resolve, reject) => {
  145. axios({
  146. url: url,
  147. method: 'delete',
  148. params: data,
  149. }).then((response) => {
  150. resolve(response);
  151. }).catch((error) => {
  152. reject(error)
  153. })
  154. })
  155. }
  156. }
  157. export default http;