request.js 3.9 KB

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