request.js 4.3 KB

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