axios.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import axios from "axios"
  2. import qs from "qs"
  3. // axios.defaults.timeout = 180000 //响应时间
  4. axios.defaults.timeout = 600000 //响应时间
  5. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; //配置请求头
  6. axios.defaults.baseURL = process.env.NODE_HOST; //配置接口地址
  7. axios.defaults.withCredentials = true; // 设置为 true,允许跨域请求携带凭据
  8. console.log(process.env)
  9. const CancelToken = axios.CancelToken;
  10. let cancel; // 用于存储取消请求的方法
  11. //POST传参序列化(添加请求拦截器)
  12. axios.interceptors.request.use((config) => {
  13. // 判断请求的 URL,执行不同的逻辑
  14. if (config.url === 'https://gpt.cocorobo.cn/search_image' ||
  15. config.url === 'https://gpt.cocorobo.cn/chat' ||
  16. config.url === 'https://gpt4.cocorobo.cn/create_free_assistants' ||
  17. config.url === 'https://gpt4.cocorobo.cn/assistants_completion_response') {
  18. // 如果是特定的 URL,可以进行 post 参数的序列化
  19. // 注意:此处不需要 config.data = config.data; 去掉这个无意义的赋值
  20. config.data = serializePostData(config.data);
  21. } else if (config.url.indexOf('https://gpt4.cocorobo.cn/') !== -1 ||
  22. config.url.indexOf('https://claude3.cocorobo.cn/') !== -1 ||
  23. config.url.indexOf('https://llm.cocorobo.cn/') !== -1) {
  24. // 如果是其他 URL,则设置请求头为 application/json
  25. config.headers = {
  26. 'Content-Type': 'application/json',
  27. };
  28. } else if (config.data && config.data[0].post === '1' && config.method === 'post') {
  29. // 如果 data 的第一个元素 post 值为 '1',则进行自定义序列化
  30. config.data = 'mode=' + Object.values(config.data[0]).join(',');
  31. } else if (config.method === 'post' && config.data) {
  32. // 处理其他 post 请求的参数序列化
  33. const encoded = {};
  34. for (const key in config.data[0]) {
  35. if (Object.hasOwnProperty.call(config.data[0], key)) {
  36. encoded[key] = encodeURIComponent(config.data[0][key]);
  37. }
  38. }
  39. config.data = qs.stringify([encoded]); // 使用 qs 序列化数据
  40. } else {
  41. // 对于其他请求类型,进行标准的 URL 编码
  42. const encoded = {};
  43. for (const key in config.data) {
  44. if (Object.hasOwnProperty.call(config.data, key)) {
  45. encoded[key] = encodeURIComponent(config.data[key]);
  46. }
  47. }
  48. config.data = encoded;
  49. }
  50. return config; // 确保返回修改后的 config
  51. }, (error) => {
  52. // 错误处理
  53. return Promise.reject(error);
  54. });
  55. // 序列化函数
  56. function serializePostData(data) {
  57. // 你可以根据需求,写一个自定义的序列化方法
  58. // 比如这里假设我们只是将数据简单地序列化
  59. return JSON.stringify(data);
  60. }
  61. //返回状态判断(添加响应拦截器)
  62. axios.interceptors.response.use((res) => {
  63. //对响应数据做些事
  64. if (!res.data.success) {
  65. let newToken = res.data.token //成功后更新token
  66. localStorage.setItem('access_token', newToken)
  67. }
  68. return res;
  69. }, (error) => {
  70. if (axios.isCancel(error)) {
  71. console.log('请求已取消', error.message);
  72. } else if (error.response.data.status == '401') { //如果token 过期 则跳转到登录页面
  73. this.$router.push('/login');
  74. }
  75. return Promise.reject(error);
  76. });
  77. //返回一个Promise(发送post请求)
  78. function post(url, params, source) {
  79. return new Promise((resolve, reject) => {
  80. axios.post(url, params, source ? { cancelToken: source.token } : '')
  81. .then(response => {
  82. resolve(response);
  83. }, err => {
  84. reject(err);
  85. })
  86. .catch((error) => {
  87. reject(error)
  88. })
  89. })
  90. }
  91. //返回一个Promise(发送put请求)
  92. function put(url, params, source) {
  93. return new Promise((resolve, reject) => {
  94. axios.put(url, params, source ? { cancelToken: source.token } : '')
  95. .then(response => {
  96. resolve(response);
  97. }, err => {
  98. reject(err);
  99. })
  100. .catch((error) => {
  101. reject(error)
  102. })
  103. })
  104. }
  105. ////返回一个Promise(发送get请求)
  106. function get(url, param, source) {
  107. return new Promise((resolve, reject) => {
  108. let cancelToken = source ? source.token : ''
  109. axios.get(url, { params: param, cancelToken })
  110. .then(response => {
  111. resolve(response)
  112. }, err => {
  113. reject(err)
  114. })
  115. .catch((error) => {
  116. reject(error)
  117. })
  118. })
  119. }
  120. export default {
  121. get,
  122. post,
  123. put,
  124. setCancelSource: () => {
  125. // 每次创建新的请求时,可以调用此方法以创建新的取消令牌
  126. cancel = CancelToken.source();
  127. return cancel;
  128. }
  129. }