request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. let params = propName + '[' + key + ']';
  31. var subPart = encodeURIComponent(params) + "=";
  32. url += subPart + encodeURIComponent(value[key]) + "&";
  33. }
  34. } else {
  35. url += part + encodeURIComponent(value) + "&";
  36. }
  37. }
  38. }
  39. url = url.slice(0, -1);
  40. config.params = {};
  41. config.url = url;
  42. }
  43. return config
  44. }, error => {
  45. console.log(error)
  46. Promise.reject(error)
  47. })
  48. // 响应拦截器
  49. service.interceptors.response.use(res => {
  50. // 未设置状态码则默认成功状态
  51. const code = res.data.code || 200;
  52. // 获取错误信息
  53. const msg = errorCode[code] || res.data.msg || errorCode['default']
  54. if (code === 401) {
  55. MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
  56. confirmButtonText: '重新登录',
  57. cancelButtonText: '取消',
  58. type: 'warning'
  59. }
  60. ).then(() => {
  61. store.dispatch('LogOut').then(() => {
  62. location.href = '/index';
  63. })
  64. }).catch(() => {});
  65. return Promise.reject('令牌验证失败')
  66. } else if (code === 500) {
  67. Message({
  68. message: msg,
  69. type: 'error'
  70. })
  71. return Promise.reject(new Error(msg))
  72. } else if (code !== 200) {
  73. Notification.error({
  74. title: msg
  75. })
  76. return Promise.reject('error')
  77. } else {
  78. return res.data
  79. }
  80. },
  81. error => {
  82. console.log('err' + error)
  83. let { message } = error;
  84. if (message == "Network Error") {
  85. message = "后端接口连接异常";
  86. }
  87. else if (message.includes("timeout")) {
  88. message = "系统接口请求超时";
  89. }
  90. else if (message.includes("Request failed with status code")) {
  91. message = "系统接口" + message.substr(message.length - 3) + "异常";
  92. }
  93. Message({
  94. message: message,
  95. type: 'error',
  96. duration: 5 * 1000
  97. })
  98. return Promise.reject(error)
  99. }
  100. )
  101. export default service