request.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import store from '@/store'
  4. import {Plugin as VueAxios} from '@/plugins/axios'
  5. import {Modal, MessageBox ,Notification} from 'element-ui'
  6. import {ACCESS_TOKEN} from "@/store/mutation-types"
  7. import router from 'vue-router'
  8. // 创建 axios 实例
  9. if (process.env.NODE_ENV == 'development') {
  10. axios.defaults.baseURL = '/api'
  11. }
  12. else if (process.env.NODE_ENV == 'debug') {
  13. }
  14. else if (process.env.NODE_ENV == 'production') {
  15. // axios.defaults.baseURL = 'http://192.168.1.251/jeecg-boot';
  16. axios.defaults.baseURL = 'http://okrapi.c-top.com.cn';
  17. }
  18. const service = axios.create({
  19. // baseURL: '/jeecg-boot', // api base_url
  20. timeout: 6000000 // 请求超时时间
  21. })
  22. const err = (error) => {
  23. if (error.response) {
  24. let data = error.response.data
  25. const token = Vue.ls.get(ACCESS_TOKEN)
  26. // console.log("------异常响应------", token)
  27. // console.log("------异常响应------", error.response.status)
  28. switch (error.response.status) {
  29. case 403:
  30. Notification.error({title: '系统提示', message: '拒绝访问', duration: 4})
  31. break
  32. case 500:
  33. //notification.error({ message: '系统提示', description:'Token失效,请重新登录!',duration: 4})
  34. if (token && data.code == -1) {
  35. // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
  36. // store.dispatch('Logout').then(() => {
  37. // window.location.reload()
  38. // })
  39. // Notification.error({title: '登录已过期', message: '很抱歉,登录已过期,请重新登录', duration: 4})
  40. if (token) {
  41. store.dispatch('Logout').then(() => {
  42. setTimeout(() => {
  43. window.location.reload()
  44. // router.replace('/login')
  45. }, 1500)
  46. })
  47. }
  48. break
  49. // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
  50. }
  51. break
  52. case 404:
  53. Notification.error({title: '系统提示', message: '很抱歉,资源未找到!', duration: 4})
  54. break
  55. case 504:
  56. Notification.error({title: '系统提示', message: '网络超时'})
  57. break
  58. case 401:
  59. Notification.error({title: '系统提示', message: '未授权,请重新登录', duration: 4})
  60. if (token) {
  61. store.dispatch('Logout').then(() => {
  62. setTimeout(() => {
  63. window.location.reload()
  64. }, 1500)
  65. })
  66. }
  67. break
  68. default:
  69. Notification.error({
  70. title: '系统提示',
  71. message: data.message,
  72. duration: 4
  73. })
  74. break
  75. }
  76. }
  77. return Promise.reject(error)
  78. };
  79. // request interceptor
  80. service.interceptors.request.use(config => {
  81. const token = Vue.ls.get(ACCESS_TOKEN);
  82. // let tokens=JSON.parse(localStorage.getItem('ACCESS_TOKEN'))
  83. // console.log(token)
  84. if (token) {
  85. config.headers['X-Access-Token'] = token// 让每个请求携带自定义 token 请根据实际情况自行修改
  86. }
  87. if (config.method == 'get') {
  88. config.params = {
  89. _t: Date.parse(new Date()) / 1000,
  90. ...config.params
  91. }
  92. }
  93. return config
  94. }, (error) => {
  95. return Promise.reject(error)
  96. })
  97. // response interceptor
  98. service.interceptors.response.use((response) => {
  99. return response.data
  100. }, (error) => {
  101. console.log(error)
  102. err(error)
  103. // Notification.error({
  104. // title: '系统提示',
  105. // message: '网络超时,请刷新页面'
  106. // })
  107. return
  108. })
  109. const installer = {
  110. vm: {},
  111. install(Vue, router = {}) {
  112. Vue.use(VueAxios, router, service)
  113. }
  114. }
  115. export {
  116. installer as VueAxios,
  117. service as axios
  118. }