index.ts 1023 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * @Author: wjc
  3. * @Date: 2023-11-17 11:46:48
  4. * @LastEditors: LiZhiWei
  5. * @LastEditTime: 2026-01-09 15:40:43
  6. * @Description:
  7. */
  8. import type { FetchContext, FetchResponse } from 'ofetch'
  9. import { apiBase } from '~/constants'
  10. import Request from './request'
  11. import { checkStatus } from './checkStatus'
  12. import whiteList from './whiteList'
  13. const http = new Request({
  14. baseURL: `${apiBase}`,
  15. interceptor: {
  16. // 请求前钩子函数
  17. onRequest({ request, options }: FetchContext) {
  18. const isHas = whiteList.find((item) => (request as string).includes(item))
  19. if (isHas) {
  20. options.headers = {
  21. ...options.headers,
  22. Authorization: '',
  23. } as any
  24. }
  25. },
  26. // 响应错误拦截
  27. onResponseError({ response }: FetchContext & { response: FetchResponse<any> }) {
  28. if (response?.status && response?.status !== 401) {
  29. checkStatus(response?.status, response._data?.message || '服务不可用,请稍候再试')
  30. }
  31. },
  32. },
  33. })
  34. export { http }