request-new.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import axios from 'axios'
  2. import { MessageBox, Message } from 'element-ui'
  3. import { getToken, getUserId, getCodeToken } from '@/utils/auth'
  4. import store from '@/store'
  5. let isMttoken
  6. // create an axios instance
  7. const service = axios.create({
  8. //baseURL: baseURL, // url = base url + request url
  9. // baseURL: 'http://106.14.243.117:9112',
  10. //baseURL: 'http://106.14.243.117:9111',
  11. baseURL: `${PLATFROM_CONFIG.baseNewUrl}`,
  12. // withCredentials: true, // send cookies when cross-domain requests
  13. timeout: 30000, // request timeout
  14. headers: {
  15. 'Content-Type': 'application/json'
  16. },
  17. })
  18. // request interceptor
  19. service.interceptors.request.use(
  20. config => {
  21. // config.headers.common["content-type"] = "application/json"
  22. if (config.istoken) {
  23. config.headers['appSecret'] = PLATFROM_CONFIG.appSecret
  24. isMttoken = config.istoken
  25. }
  26. if (config.data) {
  27. config.data['OperatorId'] = getUserId()
  28. }
  29. // do something before request is sent
  30. // config.headers['Content-Type'] = 'text/plain'
  31. if (getCodeToken() && !config.istoken) {
  32. config.headers['Authorization'] = getCodeToken()
  33. }
  34. if (store.getters.token) {
  35. // let each request carry token
  36. // ['X-Token'] is a custom headers key
  37. // please modify it according to the actual situation
  38. config.headers['Authorization'] = getToken()
  39. }
  40. return config
  41. },
  42. error => {
  43. // do something with request error
  44. console.log(error) // for debug
  45. return Promise.reject(error)
  46. }
  47. )
  48. // response interceptor
  49. service.interceptors.response.use(
  50. /**
  51. * If you want to get http information such as headers or status
  52. * Please return response => response
  53. */
  54. /**
  55. * Determine the request status by custom code
  56. * Here is just an example
  57. * You can also judge the status by HTTP Status Code
  58. */
  59. response => {
  60. const res = response.data
  61. // if the custom code is not 20000, it is judged as an error.
  62. if (res.code != 0) {
  63. Message({
  64. message: res.message || 'Error',
  65. type: 'error',
  66. duration: 5 * 1000
  67. })
  68. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  69. if (!isMttoken) {
  70. if (res.code == 500) {
  71. // to re-login
  72. Message({
  73. message: '身份令牌过期或失效,即将重新登录',
  74. type: 'error',
  75. duration: 5 * 1000,
  76. onClose: () => {
  77. store.dispatch('user/resetToken').then(() => {
  78. location.reload()
  79. })
  80. }
  81. })
  82. }
  83. }
  84. return Promise.reject(new Error(res.message || 'Error'))
  85. } else {
  86. return res
  87. }
  88. },
  89. error => {
  90. if (isMttoken) {
  91. store.dispatch("user/setIsLogin", true);
  92. }
  93. console.log('err' + error) // for debug
  94. // const des = `${error}`.split(" ").includes('500')
  95. // if (des) {
  96. // Message({
  97. // message: '身份令牌过期或失效,即将重新登录',
  98. // type: 'error',
  99. // duration: 5 * 1000,
  100. // onClose: () => {
  101. // store.dispatch('user/resetToken').then(() => {
  102. // location.reload()
  103. // })
  104. // }
  105. // })
  106. // // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  107. // // confirmButtonText: 'Re-Login',
  108. // // cancelButtonText: 'Cancel',
  109. // // type: 'warning'
  110. // // }).then(() => {
  111. // // store.dispatch('user/resetToken').then(() => {
  112. // // location.reload()
  113. // // })
  114. // // })
  115. // } else {
  116. // Message({
  117. // message: error.message,
  118. // type: 'error',
  119. // duration: 5 * 1000
  120. // })
  121. // }
  122. //loadingInstance.close()
  123. return Promise.reject(error)
  124. }
  125. )
  126. export default service