You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

101 lines
2.9 KiB

  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import qs from 'qs'
  4. import 'vue2-toast/lib/toast.css'
  5. import Toast from 'vue2-toast'
  6. import store from '@/store'
  7. import {guid} from "./util";
  8. import md5 from 'js-md5';
  9. // import { Spin } from 'iview'
  10. Vue.use(Toast)
  11. class HttpRequest {
  12. constructor (baseUrl = baseURL) {
  13. this.baseUrl = baseUrl
  14. this.queue = {}
  15. }
  16. getInsideConfig () {
  17. const config = {
  18. baseURL: this.baseUrl,
  19. /*transformRequest: [function (data) {
  20. // 对 data 进行任意转换处理
  21. return qs.stringify(data, {arrayFormat: 'indices'})
  22. }],*/
  23. headers: {
  24. // 'Content-Type':'application/x-www-form-urlencoded'
  25. },
  26. paramsSerializer: function (params) {
  27. return qs.stringify(params, {arrayFormat: 'repeat'})
  28. }
  29. }
  30. return config
  31. }
  32. distroy (url) {
  33. delete this.queue[url]
  34. if (!Object.keys(this.queue).length) {
  35. // Spin.hide()
  36. }
  37. }
  38. interceptors (instance, url) {
  39. // 请求拦截
  40. instance.interceptors.request.use(config => {
  41. // 添加全局的loading...
  42. if (!Object.keys(this.queue).length) {
  43. // Spin.show() // 不建议开启,因为界面不友好
  44. }
  45. let accesstoken = 'aaaa09f1bc6305d06a7307c698ca2403'
  46. let timestamp = new Date().getTime()
  47. let nonce = guid()
  48. let secret = 'a7d1c64a82e5105e7a9e6713478a2a66'
  49. config.headers['x-dj-accesstoken'] = accesstoken
  50. config.headers['x-dj-timestamp'] = timestamp
  51. config.headers['x-dj-nonce'] = nonce
  52. config.headers['x-dj-signature'] = md5(accesstoken+secret+timestamp+nonce)
  53. config.headers['X-Requested-With'] = 'XMLHttpRequest'
  54. /*if (store.state.user.id_token) {
  55. config.headers['Authorization'] = 'Bearer ' + store.state.user.id_token
  56. }*/
  57. this.queue[url] = true
  58. return config
  59. }, error => {
  60. return Promise.reject(error)
  61. })
  62. // 响应拦截
  63. instance.interceptors.response.use(res => {
  64. this.distroy(url)
  65. // console.log(res)
  66. if (res.status === 200 || res.data.success) {
  67. return Promise.resolve(res.data)
  68. } else {
  69. return Promise.reject(res)
  70. }
  71. }, error => {
  72. this.distroy(url)
  73. console.log(error)
  74. if (error && error.response) {
  75. switch (error.response.status) {
  76. case 400:
  77. error.message = '请求错误'
  78. break
  79. case 401:
  80. error.message = '未授权,请重新登录'
  81. location.href = ''
  82. break
  83. default:
  84. Vue.prototype.$toast.center(error.response.data.message || '未知错误')
  85. }
  86. } else {
  87. Vue.prototype.$toast.center('未知错误')
  88. }
  89. return Promise.reject(error.response)
  90. })
  91. }
  92. request (options) {
  93. const instance = axios.create()
  94. options = Object.assign(this.getInsideConfig(), options)
  95. this.interceptors(instance, options.url)
  96. return instance(options)
  97. }
  98. }
  99. export default HttpRequest