request.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import i18n from '@/locale'
  2. import { getEnvBaseUrl } from '@/utils'
  3. import { platform } from '@/utils/platform'
  4. import { stringifyQuery } from '@/utils/queryString'
  5. export type CustomRequestOptions = UniApp.RequestOptions & {
  6. query?: Record<string, any>
  7. /** 出错时是否隐藏错误提示 */
  8. hideErrorToast?: boolean
  9. } & IUniUploadFileOptions // 添加uni.uploadFile参数类型
  10. // 请求基准地址
  11. const baseUrl = getEnvBaseUrl()
  12. // 获取对应前缀的请求地址和处理前缀替换
  13. function getBaseUrlAndProcessPrefix(url: string): { baseUrl: string, processedUrl: string } {
  14. const mallPrefix = import.meta.env.VITE_SERVER_BASEURL_PREFIX || '/mall'
  15. const cifPrefix = import.meta.env.VITE_API_SECONDARY_URL_PREFIX || '/cif'
  16. const operatingPrefix = import.meta.env.VITE_API_THIRD_URL_PREFIX || '/operating'
  17. let targetBaseUrl = baseUrl
  18. let processedUrl = url
  19. if (url.startsWith(mallPrefix)) {
  20. targetBaseUrl = import.meta.env.VITE_SERVER_BASEURL || baseUrl
  21. // 检查是否需要替换前缀
  22. if (import.meta.env.VITE_SERVER_BASEURL_PREFIX_REPLACE === 'true') {
  23. const replaceValue = import.meta.env.VITE_SERVER_BASEURL_PREFIX_REPLACE_VALUE || ''
  24. processedUrl = url.replace(mallPrefix, replaceValue)
  25. }
  26. }
  27. else if (url.startsWith(cifPrefix)) {
  28. targetBaseUrl = import.meta.env.VITE_API_SECONDARY_URL || baseUrl
  29. // 检查是否需要替换前缀
  30. if (import.meta.env.VITE_API_SECONDARY_URL_PREFIX_REPLACE === 'true') {
  31. const replaceValue = import.meta.env.VITE_API_SECONDARY_URL_PREFIX_REPLACE_VALUE || ''
  32. processedUrl = url.replace(cifPrefix, replaceValue)
  33. }
  34. }
  35. else if (url.startsWith(operatingPrefix)) {
  36. targetBaseUrl = import.meta.env.VITE_API_THIRD_URL || baseUrl
  37. // 检查是否需要替换前缀
  38. if (import.meta.env.VITE_API_THIRD_URL_PREFIX_REPLACE === 'true') {
  39. const replaceValue = import.meta.env.VITE_API_THIRD_URL_PREFIX_REPLACE_VALUE || ''
  40. processedUrl = url.replace(operatingPrefix, replaceValue)
  41. }
  42. }
  43. return { baseUrl: targetBaseUrl, processedUrl }
  44. }
  45. // 拦截器配置
  46. const httpInterceptor = {
  47. // 拦截前触发
  48. invoke(options: CustomRequestOptions) {
  49. // 接口请求支持通过 query 参数配置 queryString
  50. if (options.query) {
  51. const queryStr = stringifyQuery(options.query)
  52. if (options.url.includes('?')) {
  53. options.url += `&${queryStr}`
  54. }
  55. else {
  56. options.url += `?${queryStr}`
  57. }
  58. }
  59. // 非 http 开头需拼接地址
  60. if (!options.url.startsWith('http')) {
  61. // 根据URL前缀获取对应的基础地址和处理前缀替换
  62. const { baseUrl: targetBaseUrl, processedUrl } = getBaseUrlAndProcessPrefix(options.url)
  63. // #ifdef H5
  64. // console.log(__VITE_APP_PROXY__)
  65. if (JSON.parse(__VITE_APP_PROXY__)) {
  66. // 自动拼接代理前缀
  67. options.url = import.meta.env.VITE_APP_PROXY_PREFIX + processedUrl
  68. }
  69. else {
  70. options.url = targetBaseUrl + processedUrl
  71. }
  72. // #endif
  73. // 非H5正常拼接
  74. // #ifndef H5
  75. options.url = targetBaseUrl + processedUrl
  76. // #endif
  77. }
  78. // 1. 请求超时
  79. options.timeout = 10000 // 10s
  80. // 2. (可选)添加小程序端请求头标识
  81. options.header = {
  82. platform, // 可选,与 uniapp 定义的平台一致,告诉后台来源
  83. ...options.header,
  84. }
  85. options.header.language = i18n.global.locale
  86. // 判断是否为登录接口
  87. const isLoginApi = options.url?.includes('/login') || options.url?.includes('/register')
  88. // 如果不是登录接口,添加 token 请求头
  89. if (!isLoginApi) {
  90. const token = uni.getStorageSync('token')
  91. if (token) {
  92. options.header.Token = token
  93. }
  94. }
  95. },
  96. }
  97. export const requestInterceptor = {
  98. install() {
  99. // 拦截 request 请求
  100. uni.addInterceptor('request', httpInterceptor)
  101. // 拦截 uploadFile 文件上传
  102. uni.addInterceptor('uploadFile', httpInterceptor)
  103. },
  104. }