config.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import axios from 'axios'
  2. import message from '@/utils/message'
  3. import qs from 'qs'
  4. const instance = axios.create({ timeout: 1000 * 300 })
  5. const cancelToken = axios.CancelToken
  6. axios.defaults.withCredentials = true
  7. // POST传参序列化(添加请求拦截器)
  8. instance.interceptors.request.use(
  9. (config) => {
  10. // 修复 config.url 可能为 undefined 的问题
  11. const url = config.url ?? ''
  12. // 修复 config.data 可能为 undefined 的问题
  13. const data = config.data ?? {}
  14. // 确保每个请求都带上cookie
  15. config.withCredentials = true
  16. if (url.includes('https://gpt4.cocorobo.cn') || url.includes('https://appapi.cocorobo.cn') || url.includes('https://ccrb.s3.cn-northwest-1.amazonaws.com.cn')) {
  17. config.withCredentials = false
  18. }
  19. // 需要 form-urlencoded 且 data 为数组的情况
  20. if (
  21. config.method === 'post' &&
  22. (
  23. url.includes('http://localhost:7003/api/pbl/') ||
  24. url.includes('https://pbl.cocorobo.cn/api/mongo/') ||
  25. url.includes('https://pbl.cocorobo.cn/api/pbl/') ||
  26. url.includes('https://r2rapi.cocorobo.cn/') ||
  27. url.includes('http://10.1.82.64:7004/file/')
  28. )
  29. ) {
  30. // 修复 headers 类型问题
  31. if (config.headers) {
  32. config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  33. }
  34. else {
  35. // @ts-ignore
  36. config.headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
  37. }
  38. // 处理 data[0] 为对象的情况
  39. const encoded: Record<string, any> = {}
  40. if (Array.isArray(data) && data.length > 0 && typeof data[0] === 'object') {
  41. for (const key in data[0]) {
  42. if (Object.prototype.hasOwnProperty.call(data[0], key)) {
  43. encoded[key] = encodeURIComponent(data[0][key])
  44. }
  45. }
  46. config.data = qs.stringify([encoded]) // 序列化post参数
  47. }
  48. }
  49. // 需要对 data 进行 encode 的情况
  50. else if (
  51. url.includes('http://localhost:7003/api/pbl/') ||
  52. url.includes('https://pbl.cocorobo.cn/api/mongo/') ||
  53. url.includes('https://pbl.cocorobo.cn/api/pbl/') ||
  54. url.includes('https://r2rapi.cocorobo.cn/')
  55. ) {
  56. const encoded: Record<string, any> = {}
  57. for (const key in data) {
  58. if (Object.prototype.hasOwnProperty.call(data, key)) {
  59. encoded[key] = encodeURIComponent(data[key])
  60. }
  61. }
  62. config.data = encoded
  63. }
  64. return config
  65. },
  66. (error) => {
  67. console.log('错误的传参')
  68. return Promise.reject(error)
  69. }
  70. )
  71. instance.interceptors.response.use(
  72. (response) => {
  73. if (response.status >= 200 && response.status < 400) {
  74. return Promise.resolve(response.data)
  75. }
  76. message.error(response.config.url || '')
  77. message.error('未知的请求错误!')
  78. return Promise.reject(response)
  79. },
  80. (error) => {
  81. // 处理请求取消的情况
  82. if (axios.isCancel(error)) {
  83. console.log('请求被取消:', error.message)
  84. return Promise.reject(error)
  85. }
  86. const config = error.config
  87. let fullUrl = '未知请求'
  88. if (config) {
  89. // 拼接 baseURL 和 url
  90. const baseURL = config.baseURL || ''
  91. const url = config.url || ''
  92. fullUrl = baseURL + url
  93. // 如果有查询参数,添加到 URL 中
  94. if (config.params) {
  95. const params = new URLSearchParams(config.params).toString()
  96. if (params) {
  97. fullUrl += '?' + params
  98. }
  99. }
  100. // 检查是否需要显示错误信息
  101. const showError = config.showError !== false
  102. if (!showError) {
  103. return Promise.reject(error)
  104. }
  105. }
  106. if (error && error.response) {
  107. if (error.response.status >= 400 && error.response.status < 500) {
  108. return Promise.reject(error.message)
  109. }
  110. else if (error.response.status >= 500) {
  111. return Promise.reject(error.message)
  112. }
  113. message.error('服务器遇到未知错误!')
  114. return Promise.reject(error.message)
  115. }
  116. if (fullUrl !== 'https://r2rserver.cocorobo.cn/v3/documents') {
  117. message.error(fullUrl)
  118. message.error(error)
  119. }
  120. else {
  121. console.error(fullUrl)
  122. console.error(error)
  123. }
  124. return Promise.reject(error)
  125. }
  126. )
  127. export default instance
  128. export { cancelToken }