request.js 3.8 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. //关闭断网时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. cookies.remove('accessToken')
  59. userStore().clearUserInfo()
  60. Message({
  61. duration: 5000,
  62. message: 'Your session has expired. Please log in again.',
  63. type: "error",
  64. })
  65. window.location.href = '/login'
  66. }
  67. return Promise.reject(error);
  68. }
  69. const { message } = error;
  70. //断网情况下,弹出框
  71. if(error.request){
  72. msg = Message({
  73. duration: 0,
  74. showClose: true,
  75. message: 'Internet Connection Error',
  76. type: "error",
  77. })
  78. }
  79. console.error(message);
  80. return Promise.reject(error);
  81. },
  82. );
  83. // 导出常用函数
  84. /**
  85. * @param {string} url
  86. * @param {object} data
  87. * @param {object} params
  88. */
  89. export function post(url, data = {}, params = {}) {
  90. return instance({
  91. method: 'post',
  92. url,
  93. data,
  94. params,
  95. });
  96. }
  97. /**
  98. * @param {string} url
  99. * @param {object} params
  100. */
  101. export function get(url, params = {}) {
  102. return instance({
  103. method: 'get',
  104. url,
  105. params,
  106. });
  107. }
  108. export function downLoad(url, params = {}) {
  109. return instance({
  110. method: 'get',
  111. url,
  112. params,
  113. responseType:"arraybuffer"
  114. });
  115. }
  116. /**
  117. * @param {string} url
  118. * @param {object} data
  119. * @param {object} params
  120. */
  121. export function put(url, data = {}, params = {}) {
  122. return instance({
  123. method: 'put',
  124. url,
  125. params,
  126. data,
  127. });
  128. }
  129. /**
  130. * @param {string} url
  131. * @param {object} params
  132. */
  133. export function _delete(url, params = {}) {
  134. return instance({
  135. method: 'delete',
  136. url,
  137. params,
  138. });
  139. }
  140. /**
  141. * @param {string} url
  142. * @param {object} data
  143. */
  144. export function postWithHeader(url, data = {}) {
  145. return instance({
  146. method: 'post',
  147. url,
  148. data,
  149. headers: {
  150. 'Content-Type': 'application/json'
  151. },
  152. dataType: "json",
  153. });
  154. }
  155. /**
  156. * @param {string} url
  157. * @param {object} data
  158. */
  159. export function putWithHeader(url, data = {}) {
  160. return instance({
  161. method: 'put',
  162. url,
  163. data,
  164. headers: {
  165. 'Content-Type': 'application/json'
  166. },
  167. dataType: "json",
  168. });
  169. }
  170. export default instance;