request.js 3.8 KB

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