request.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import axios from 'axios'
  2. import Cookie from 'js-cookie'
  3. // 跨域认证信息 header 名
  4. const xsrfHeaderName = 'Authorization'
  5. axios.defaults.timeout = 5000
  6. axios.defaults.withCredentials= true
  7. axios.defaults.xsrfHeaderName= xsrfHeaderName
  8. axios.defaults.xsrfCookieName= xsrfHeaderName
  9. // 认证类型
  10. const AUTH_TYPE = {
  11. BEARER: 'Bearer',
  12. BASIC: 'basic',
  13. AUTH1: 'auth1',
  14. AUTH2: 'auth2',
  15. }
  16. // http method
  17. const METHOD = {
  18. GET: 'get',
  19. POST: 'post'
  20. }
  21. /**
  22. * axios请求
  23. * @param url 请求地址
  24. * @param method {METHOD} http method
  25. * @param params 请求参数
  26. * @returns {Promise<AxiosResponse<T>>}
  27. */
  28. async function request(url, method, params, config) {
  29. switch (method) {
  30. case METHOD.GET:
  31. return axios.get(url, {params, ...config})
  32. case METHOD.POST:
  33. console.info('request url',url);
  34. return axios.post(url, params, config)
  35. default:
  36. return axios.get(url, {params, ...config})
  37. }
  38. }
  39. /**
  40. * 设置认证信息
  41. * @param auth {Object}
  42. * @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
  43. */
  44. function setAuthorization(auth, authType = AUTH_TYPE.BEARER) {
  45. switch (authType) {
  46. case AUTH_TYPE.BEARER:
  47. Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token, {expires: auth.expireAt})
  48. break
  49. case AUTH_TYPE.BASIC:
  50. case AUTH_TYPE.AUTH1:
  51. case AUTH_TYPE.AUTH2:
  52. default:
  53. break
  54. }
  55. }
  56. /**
  57. * 移出认证信息
  58. * @param authType {AUTH_TYPE} 认证类型
  59. */
  60. function removeAuthorization(authType = AUTH_TYPE.BEARER) {
  61. switch (authType) {
  62. case AUTH_TYPE.BEARER:
  63. Cookie.remove(xsrfHeaderName)
  64. break
  65. case AUTH_TYPE.BASIC:
  66. case AUTH_TYPE.AUTH1:
  67. case AUTH_TYPE.AUTH2:
  68. default:
  69. break
  70. }
  71. }
  72. /**
  73. * 检查认证信息
  74. * @param authType
  75. * @returns {boolean}
  76. */
  77. function checkAuthorization(authType = AUTH_TYPE.BEARER) {
  78. switch (authType) {
  79. case AUTH_TYPE.BEARER:
  80. if (Cookie.get(xsrfHeaderName)) {
  81. return true
  82. }
  83. break
  84. case AUTH_TYPE.BASIC:
  85. case AUTH_TYPE.AUTH1:
  86. case AUTH_TYPE.AUTH2:
  87. default:
  88. break
  89. }
  90. return false
  91. }
  92. /**
  93. * 加载 axios 拦截器
  94. * @param interceptors
  95. * @param options
  96. */
  97. function loadInterceptors(interceptors, options) {
  98. const {request, response} = interceptors
  99. // 加载请求拦截器
  100. request.forEach(item => {
  101. let {onFulfilled, onRejected} = item
  102. if (!onFulfilled || typeof onFulfilled !== 'function') {
  103. onFulfilled = config => config
  104. }
  105. if (!onRejected || typeof onRejected !== 'function') {
  106. onRejected = error => Promise.reject(error)
  107. }
  108. axios.interceptors.request.use(
  109. config => onFulfilled(config, options),
  110. error => onRejected(error, options)
  111. )
  112. })
  113. // 加载响应拦截器
  114. response.forEach(item => {
  115. let {onFulfilled, onRejected} = item
  116. if (!onFulfilled || typeof onFulfilled !== 'function') {
  117. onFulfilled = response => response
  118. }
  119. if (!onRejected || typeof onRejected !== 'function') {
  120. onRejected = error => Promise.reject(error)
  121. }
  122. axios.interceptors.response.use(
  123. response => onFulfilled(response, options),
  124. error => onRejected(error, options)
  125. )
  126. })
  127. }
  128. /**
  129. * 解析 url 中的参数
  130. * @param url
  131. * @returns {Object}
  132. */
  133. function parseUrlParams(url) {
  134. const params = {}
  135. if (!url || url === '' || typeof url !== 'string') {
  136. return params
  137. }
  138. const paramsStr = url.split('?')[1]
  139. if (!paramsStr) {
  140. return params
  141. }
  142. const paramsArr = paramsStr.replace(/&|=/g, ' ').split(' ')
  143. for (let i = 0; i < paramsArr.length / 2; i++) {
  144. const value = paramsArr[i * 2 + 1]
  145. params[paramsArr[i * 2]] = value === 'true' ? true : (value === 'false' ? false : value)
  146. }
  147. return params
  148. }
  149. export {
  150. METHOD,
  151. AUTH_TYPE,
  152. request,
  153. setAuthorization,
  154. removeAuthorization,
  155. checkAuthorization,
  156. loadInterceptors,
  157. parseUrlParams
  158. }