request.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import axios from 'axios'
  2. import {MessageBox, Message} from 'element-ui'
  3. import store from '@/store'
  4. import {getToken} from '@/utils/auth'
  5. import {aesEncrypt_HmacSha256} from './encryption/utils.js'
  6. import * as queryString from 'query-string'
  7. import moment from 'moment'
  8. // create an axios instance
  9. const service = axios.create({
  10. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  11. // withCredentials: true, // send cookies when cross-domain requests
  12. timeout: 600000 // request timeout
  13. })
  14. function typeOf(obj) {
  15. const toString = Object.prototype.toString
  16. const map = {
  17. '[object Boolean]': 'boolean',
  18. '[object Number]': 'number',
  19. '[object String]': 'string',
  20. '[object Function]': 'function',
  21. '[object Array]': 'array',
  22. '[object Date]': 'date',
  23. '[object RegExp]': 'regExp',
  24. '[object Undefined]': 'undefined',
  25. '[object Null]': 'null',
  26. '[object Object]': 'object',
  27. '[object Blob]': 'blob'
  28. }
  29. return map[toString.call(obj)]
  30. }
  31. const key = 's#e@5f98H*^his%t'
  32. // request interceptor
  33. service.interceptors.request.use(
  34. config => {
  35. if (store.getters.token) {
  36. config.headers['Authorization'] = getToken()
  37. }
  38. var paramsObject = ''
  39. if (config.method === 'get' || config.method === 'delete') {
  40. paramsObject = config.params
  41. } else if (config.method === 'post' || config.method === 'put') {
  42. paramsObject = config.data
  43. Object.keys(paramsObject).forEach(key => {
  44. const item = paramsObject[key]
  45. if (typeOf(item) === 'object' || typeOf(item) === 'array') {
  46. paramsObject[key] = JSON.stringify(item)
  47. }
  48. })
  49. }
  50. const queryString1 = queryString.stringify(paramsObject)
  51. const queryString2 = encodeURIComponent(queryString1).replace('+', '%20').replace('*', '%2A').replace('%7E', '~')
  52. config.headers['signature'] = aesEncrypt_HmacSha256(queryString2, key)
  53. config.headers['timestamp'] = moment().format('YYYY-MM-DD HH:mm:ss') // 格式化输出
  54. return config
  55. },
  56. error => {
  57. // do something with request error
  58. console.log(error) // for debug
  59. return Promise.reject(error)
  60. }
  61. )
  62. // response interceptor
  63. service.interceptors.response.use(
  64. /**
  65. * If you want to get http information such as headers or status
  66. * Please return response => response
  67. */
  68. /**
  69. * Determine the request status by custom code
  70. * Here is just an example
  71. * You can also judge the status by HTTP Status Code
  72. */
  73. response => {
  74. const res = response.data
  75. // if the custom code is not 20000, it is judged as an error.
  76. if (res.code === 200) {
  77. return res
  78. } else {
  79. Message({
  80. message: res.msg || 'Error',
  81. type: 'error',
  82. duration: 5 * 1000
  83. })
  84. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  85. if (res.code === 401) {
  86. MessageBox.confirm('请重新登录', {
  87. confirmButtonText: '重新登录',
  88. cancelButtonText: '取消',
  89. type: 'warning'
  90. }).then(() => {
  91. store.dispatch('user/resetToken').then(() => {
  92. location.reload()
  93. })
  94. })
  95. }
  96. return Promise.reject(new Error(res.msg || 'Error'))
  97. }
  98. },
  99. error => {
  100. console.log('err' + error) // for debug
  101. Message({
  102. message: error.message,
  103. type: 'error',
  104. duration: 5 * 1000
  105. })
  106. return Promise.reject(error)
  107. }
  108. )
  109. export default service