request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 = Object.assign({}, config.params)
  41. } else if (config.method === 'post' || config.method === 'put') {
  42. paramsObject = Object.assign({}, config.data)
  43. JSONToString(paramsObject)
  44. }
  45. if (config.url === 'system/user/profile/updatePwd') {
  46. paramsObject = Object.assign({}, config.params)
  47. JSONToString(paramsObject)
  48. }
  49. function JSONToString(paramsObject) {
  50. Object.keys(paramsObject).forEach(key => {
  51. var item = paramsObject[key]
  52. if (item === null || item === undefined) {
  53. paramsObject[key] = ''
  54. }
  55. if (typeOf(item) === 'object' || typeOf(item) === 'array') {
  56. paramsObject[key] = JSON.stringify(item)
  57. }
  58. })
  59. }
  60. console.log('paramsObject===', paramsObject)
  61. const queryString1 = queryString.stringify(paramsObject)
  62. console.log('queryString1===', queryString1)
  63. const queryString2 = encodeURIComponent(queryString1).replace('+', '%20').replace('*', '%2A').replace('%7E', '~')
  64. console.log('queryString2===', queryString2)
  65. config.headers['signature'] = aesEncrypt_HmacSha256(queryString2, key)
  66. config.headers['timestamp'] = moment().format('YYYY-MM-DD HH:mm:ss') // 格式化输出
  67. return config
  68. },
  69. error => {
  70. // do something with request error
  71. console.log(error) // for debug
  72. return Promise.reject(error)
  73. }
  74. )
  75. // response interceptor
  76. service.interceptors.response.use(
  77. /**
  78. * If you want to get http information such as headers or status
  79. * Please return response => response
  80. */
  81. /**
  82. * Determine the request status by custom code
  83. * Here is just an example
  84. * You can also judge the status by HTTP Status Code
  85. */
  86. response => {
  87. const res = response.data
  88. // if the custom code is not 20000, it is judged as an error.
  89. if (res.code === 200) {
  90. return res
  91. } else {
  92. Message({
  93. message: res.msg || 'Error',
  94. type: 'error',
  95. duration: 5 * 1000
  96. })
  97. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  98. if (res.code === 401) {
  99. MessageBox.confirm('请重新登录', {
  100. confirmButtonText: '重新登录',
  101. cancelButtonText: '取消',
  102. type: 'warning'
  103. }).then(() => {
  104. store.dispatch('user/resetToken').then(() => {
  105. location.reload()
  106. })
  107. })
  108. }
  109. return Promise.reject(new Error(res.msg || 'Error'))
  110. }
  111. },
  112. error => {
  113. console.log('err' + error) // for debug
  114. Message({
  115. message: error.message,
  116. type: 'error',
  117. duration: 5 * 1000
  118. })
  119. return Promise.reject(error)
  120. }
  121. )
  122. export default service