集成后台重构版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
  1. import axios from "axios"
  2. import router from '@/router/routers'
  3. import { Notification } from 'element-ui' //elementUI 提示框组件
  4. import store from '../store'
  5. import { getToken } from '@/utils/auth'
  6. import Config from '@/settings'
  7. import Cookies from "js-cookie"
  8. //创建axios 实例
  9. const service = axios.create({
  10. //api 的 base_url在.env.development有配置
  11. baseURL: process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_API : '/',
  12. //请求超时时间
  13. timeout: Config.timeout
  14. })
  15. //添加请求拦截器
  16. service.interceptors.request.use(
  17. // 在发送请求之前做些什么
  18. config => {
  19. //如果获取token
  20. if (getToken()) {
  21. //每次请求都附带上header的Authorization
  22. config.headers['Authorization'] = getToken()
  23. }
  24. //请求格式
  25. config.headers['Content-Type'] = 'application/json'
  26. return config
  27. },
  28. error => {
  29. //对请求错误的处理
  30. Promise.reject(error)
  31. }
  32. )
  33. //添加响应拦截器
  34. service.interceptors.response.use(
  35. //响应后要做的事情
  36. response => {
  37. //返回响应数据
  38. return response.data
  39. },
  40. //响应错误处理
  41. error => {
  42. //兼容blob下载出错json提示
  43. if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
  44. //创建文件读取对象
  45. const reader = new FileReader()
  46. //读取指定的Blob中错误信息内容
  47. reader.readAsText(error.response.data, 'utf-8')
  48. //读取文件完成后触发onload事件
  49. reader.onload = function (e) {
  50. //转换错误信息
  51. const errorMsg = JSON.parse(reader.result).message
  52. //通知提醒框返回错误信息
  53. Notification.error({
  54. title: errorMsg,
  55. duration: 5000
  56. })
  57. }
  58. } else {
  59. let code = 0
  60. //捕获错误信息
  61. try {
  62. //获取响应错误状态码
  63. code = error.response.data.status
  64. } catch (e) {
  65. //做请求超时判断
  66. if (error.toString().indexOf('Error:timeout') !== -1) {
  67. Notification.error({
  68. title: '网络请求超时',
  69. duration: 5000
  70. })
  71. //拿到回调信息并返回
  72. return Promise.reject(error)
  73. }
  74. }
  75. console.log(code)
  76. //错误代码判断
  77. if (code) {
  78. if (code === 401) {
  79. //跳转401并写入Cookies
  80. store.dispatch('LogOut').then(() => {
  81. // 用户登录界面提示
  82. Cookies.set('point', 401)
  83. //重新加载
  84. location.reload()
  85. })
  86. } else if (code === 403) {
  87. //如果是403直接返回401页面路径
  88. router.push({
  89. path: '/401'
  90. })
  91. } else {
  92. //获取错误信息
  93. const errorMsg = error.response.data.message
  94. if (errorMsg !== undefined) {
  95. //告知提示框错误信息
  96. Notification.error({
  97. title: errorMsg,
  98. duration: 5000
  99. })
  100. }
  101. }
  102. } else {
  103. //否则把请求接口失败告知提示框
  104. Notification.error({
  105. title: '接口请求失败',
  106. duration: 5000
  107. })
  108. }
  109. }
  110. //返回错误
  111. return Promise.reject(error)
  112. }
  113. )
  114. export default service