request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import axios from 'axios'
  2. import { Notification, MessageBox, Message } from 'element-ui'
  3. import store from '@/store'
  4. import { getToken } from '@/utils/auth'
  5. import errorCode from '@/utils/errorCode'
  6. axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  7. // 创建axios实例
  8. const service = axios.create({
  9. // axios中请求配置有baseURL选项,表示请求URL公共部分
  10. baseURL: process.env.VUE_APP_BASE_API,
  11. // 超时
  12. timeout: 10000
  13. })
  14. // request拦截器
  15. service.interceptors.request.use(config => {
  16. // 是否需要设置 token
  17. const isToken = (config.headers || {}).isToken === false
  18. if (getToken() && !isToken) {
  19. config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  20. }
  21. // get请求映射params参数
  22. if (config.method === 'get' && config.params) {
  23. let url = config.url + '?';
  24. for (const propName of Object.keys(config.params)) {
  25. const value = config.params[propName];
  26. var part = encodeURIComponent(propName) + "=";
  27. if (value !== null && typeof(value) !== "undefined") {
  28. if (typeof value === 'object') {
  29. for (const key of Object.keys(value)) {
  30. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  31. let params = propName + '[' + key + ']';
  32. let subPart = encodeURIComponent(params) + '=';
  33. url += subPart + encodeURIComponent(value[key]) + '&';
  34. }
  35. }
  36. } else {
  37. url += part + encodeURIComponent(value) + "&";
  38. }
  39. }
  40. }
  41. url = url.slice(0, -1);
  42. config.params = {};
  43. config.url = url;
  44. }
  45. return config
  46. }, error => {
  47. console.log(error)
  48. Promise.reject(error)
  49. })
  50. // 响应拦截器
  51. service.interceptors.response.use(res => {
  52. // 未设置状态码则默认成功状态
  53. const code = res.data.code || 200;
  54. // 获取错误信息
  55. const msg = errorCode[code] || res.data.msg || errorCode['default']
  56. if (code === 401) {
  57. MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
  58. confirmButtonText: '重新登录',
  59. cancelButtonText: '取消',
  60. type: 'warning'
  61. }
  62. ).then(() => {
  63. store.dispatch('LogOut').then(() => {
  64. location.href = '/index';
  65. })
  66. }).catch(() => {});
  67. return Promise.reject('令牌验证失败')
  68. } else if (code === 500) {
  69. Message({
  70. message: msg,
  71. type: 'error'
  72. })
  73. return Promise.reject(new Error(msg))
  74. } else if (code !== 200) {
  75. Notification.error({
  76. title: msg
  77. })
  78. return Promise.reject('error')
  79. } else {
  80. return res.data
  81. }
  82. },
  83. error => {
  84. console.log('err' + error)
  85. let { message } = error;
  86. if (message == "Network Error") {
  87. message = "后端接口连接异常";
  88. }
  89. else if (message.includes("timeout")) {
  90. message = "系统接口请求超时";
  91. }
  92. else if (message.includes("Request failed with status code")) {
  93. message = "系统接口" + message.substr(message.length - 3) + "异常";
  94. }
  95. Message({
  96. message: message,
  97. type: 'error',
  98. duration: 5 * 1000
  99. })
  100. return Promise.reject(error)
  101. }
  102. )
  103. export default service