request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * @Author: your name
  3. * @Date: 2022-01-06 09:45:17
  4. * @LastEditTime: 2022-02-27 14:58:22
  5. * @LastEditors: Please set LastEditors
  6. * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  7. * @FilePath: \vue-admin-template\src\utils\request.js
  8. */
  9. import axios from 'axios'
  10. import { MessageBox, Message } from 'element-ui'
  11. import store from '@/store'
  12. import { getToken, getUserId, getCodeToken } from '@/utils/auth'
  13. import { Loading } from 'element-ui'
  14. // create an axios instance
  15. const service = axios.create({
  16. //baseURL: baseURL, // url = base url + request url
  17. //baseURL: 'http://106.14.243.117:9002',
  18. baseURL: `${PLATFROM_CONFIG.baseUrl}`,
  19. // withCredentials: true, // send cookies when cross-domain requests
  20. timeout: 30000, // request timeout
  21. headers: {
  22. 'Content-Type': 'application/json'
  23. },
  24. })
  25. const loadingInstance = Loading.service({
  26. text: '数据加载中'
  27. })
  28. // request interceptor
  29. service.interceptors.request.use(
  30. config => {
  31. // config.headers.common["content-type"] = "application/json"
  32. if (config.data) {
  33. config.data['OperatorId'] = getUserId()
  34. }
  35. if (getCodeToken() && !config.istoken) {
  36. config.headers['code'] = getCodeToken()
  37. }
  38. // do something before request is sent
  39. // config.headers['Content-Type'] = 'text/plain'
  40. if (store.getters.token) {
  41. // let each request carry token
  42. // ['X-Token'] is a custom headers key
  43. // please modify it according to the actual situation
  44. config.headers['Token'] = getToken()
  45. }
  46. return config
  47. },
  48. error => {
  49. // do something with request error
  50. console.log(error) // for debug
  51. return Promise.reject(error)
  52. }
  53. )
  54. // response interceptor
  55. service.interceptors.response.use(
  56. /**
  57. * If you want to get http information such as headers or status
  58. * Please return response => response
  59. */
  60. /**
  61. * Determine the request status by custom code
  62. * Here is just an example
  63. * You can also judge the status by HTTP Status Code
  64. */
  65. response => {
  66. const res = response.data
  67. // if the custom code is not 20000, it is judged as an error.
  68. if (res.code !== 0) {
  69. Message({
  70. message: res.message || 'Error',
  71. type: 'error',
  72. duration: 5 * 1000
  73. })
  74. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  75. if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  76. // to re-login
  77. MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  78. confirmButtonText: 'Re-Login',
  79. cancelButtonText: 'Cancel',
  80. type: 'warning'
  81. }).then(() => {
  82. store.dispatch('user/resetToken').then(() => {
  83. location.reload()
  84. })
  85. })
  86. }
  87. loadingInstance.close()
  88. return Promise.reject(new Error(res.message || 'Error'))
  89. } else {
  90. loadingInstance.close()
  91. return res
  92. }
  93. },
  94. error => {
  95. console.log('err' + error) // for debug
  96. Message({
  97. message: error.message,
  98. type: 'error',
  99. duration: 5 * 1000
  100. })
  101. loadingInstance.close()
  102. return Promise.reject(error)
  103. }
  104. )
  105. export default service