request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import axios from 'axios';
  2. import cookies from 'vue-cookies'
  3. import { userStore } from '@/store/userInfo'
  4. import { Message } from 'element-ui'
  5. import Qs from 'qs'
  6. //关闭断网时toast
  7. let msg = {
  8. close(){}
  9. }
  10. // 创建请求实例
  11. const instance = axios.create({
  12. baseURL: 'http://124.223.7.184:8032',
  13. // 指定请求超时的毫秒数
  14. timeout: 10000,
  15. // 表示跨域请求时是否需要使用凭证
  16. withCredentials: false,
  17. });
  18. // 前置拦截器(发起请求之前的拦截)
  19. instance.interceptors.request.use(
  20. (config) => {
  21. /**
  22. * 在这里一般会携带前台的参数发送给后台,比如下面这段代码:
  23. * const token = getToken()
  24. * if (token) {
  25. * config.headers.token = token
  26. * }
  27. */
  28. msg.close()
  29. if(!(config.url.indexOf("pdf-tech/vppMember/create") !== -1 || config.url.indexOf("/pdf-tech/login") !== -1)){
  30. const token = cookies.get('accessToken')
  31. config.headers.Authorization = 'Bearer ' + token
  32. }
  33. return config;
  34. },
  35. (error) => {
  36. return Promise.reject(error);
  37. },
  38. );
  39. // 后置拦截器(获取到响应时的拦截)
  40. instance.interceptors.response.use(
  41. (response) => {
  42. /**
  43. * 根据你的项目实际情况来对 response 和 error 做处理
  44. * 这里对 response 和 error 不做任何处理,直接返回
  45. */
  46. if(response.data.msg === "当前用户不具备操作权限" || response.data.msg === "Insufficient permissions"){
  47. window.location.href = '/non-admin-user'
  48. this.$cookies.remove('accessToken')
  49. userStore().clearUserInfo()
  50. }
  51. return response;
  52. },
  53. (error) => {
  54. const { response } = error;
  55. msg.close()
  56. if (response && response.data) {
  57. //没有登录或者登录过期跳到登录页面
  58. if(response.data.code === 310 && response.data.msg === "无效的token或者token已过期"){
  59. window.location.href = '/login'
  60. this.$cookies.remove('accessToken')
  61. userStore().clearUserInfo()
  62. Message({
  63. duration: 5000,
  64. message: 'Your session has expired. Please log in again.',
  65. type: "error",
  66. })
  67. }
  68. return Promise.reject(error);
  69. }
  70. const { message } = error;
  71. //断网情况下,弹出框
  72. if(error.request){
  73. msg = Message({
  74. duration: 0,
  75. showClose: true,
  76. message: 'Internet Connection Error',
  77. type: "error",
  78. })
  79. }
  80. console.error(message);
  81. return Promise.reject(error);
  82. },
  83. );
  84. // 导出常用函数
  85. /**
  86. * @param {string} url
  87. * @param {object} data
  88. * @param {object} params
  89. */
  90. export function post(url, data = {}, params = {}) {
  91. return instance({
  92. method: 'post',
  93. url,
  94. data,
  95. params,
  96. });
  97. }
  98. /**
  99. * @param {string} url
  100. * @param {object} params
  101. */
  102. export function get(url, params = {}) {
  103. return instance({
  104. method: 'get',
  105. url,
  106. params,
  107. });
  108. }
  109. export function downLoad(url, params = {}) {
  110. return instance({
  111. method: 'get',
  112. url,
  113. params,
  114. responseType:"arraybuffer"
  115. });
  116. }
  117. /**
  118. * @param {string} url
  119. * @param {object} data
  120. * @param {object} params
  121. */
  122. export function put(url, data = {}, params = {}) {
  123. return instance({
  124. method: 'put',
  125. url,
  126. params,
  127. data,
  128. });
  129. }
  130. /**
  131. * @param {string} url
  132. * @param {object} params
  133. */
  134. export function _delete(url, params = {}) {
  135. return instance({
  136. method: 'delete',
  137. url,
  138. params,
  139. });
  140. }
  141. /**
  142. * @param {string} url
  143. * @param {object} data
  144. */
  145. export function postWithHeader(url, data = {}) {
  146. return instance({
  147. method: 'post',
  148. url,
  149. data,
  150. headers: {
  151. 'Content-Type': 'application/json'
  152. },
  153. dataType: "json",
  154. });
  155. }
  156. /**
  157. * @param {string} url
  158. * @param {object} data
  159. */
  160. export function putWithHeader(url, data = {}) {
  161. return instance({
  162. method: 'put',
  163. url,
  164. data,
  165. headers: {
  166. 'Content-Type': 'application/json'
  167. },
  168. dataType: "json",
  169. });
  170. }
  171. export default instance;