axios.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. console.log(process.env)
  8. const CancelToken = axios.CancelToken;
  9. let cancel; // 用于存储取消请求的方法
  10. //POST传参序列化(添加请求拦截器)
  11. axios.interceptors.request.use((config) => {
  12. //在发送请求之前做某件事
  13. let token = sessionStorage.getItem('access_token') || "" //获取token
  14. if (token != "") {
  15. config.headers = {
  16. 'access-token': token,
  17. 'Content-Type': 'application/x-www-form-urlencoded'
  18. }
  19. }
  20. // if (config.data && config.data[0].post == '1' && config.method === 'post') {
  21. // // config.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
  22. // // config.data = config.data//序列化post 参数
  23. // config.data = 'mode=' + (Object.values(config.data[0]).join(','))//序列化post 参数
  24. // } else if (config.method === 'post') {
  25. // config.data = qs.stringify(config.data)//序列化post 参数
  26. // }
  27. if (config.url.includes('getUser') || config.url.includes('selectUser')) {
  28. config.withCredentials = true
  29. }
  30. if (config.url.includes('knowledge-base') || config.url === 'https://gpt.cocorobo.cn/search_image' || config.url === 'https://gpt.cocorobo.cn/chat' || config.url === 'https://gpt4.cocorobo.cn/create_free_assistants' || config.url === 'https://gpt4.cocorobo.cn/assistants_completion_response') {
  31. config.data = config.data//序列化post 参数
  32. } else if (config.url.indexOf('http://localhost:7004') != -1 || config.url.indexOf('https://r2rserver.cocorobo.cn') != -1) {
  33. config.headers = {
  34. 'Content-Type': 'application/json',
  35. }
  36. } else if (config.url.indexOf('http://localhost:8000') != -1 || config.url.indexOf('https://gpt4.cocorobo.cn/') != -1 || config.url.indexOf('https://claude3.cocorobo.cn/') != -1 || config.url.indexOf('llm.cocorobo.cn/') != -1 || config.url.indexOf('https://appapi.cocorobo.cn/')!=-1) {
  37. config.headers = {
  38. 'Content-Type': 'application/json',
  39. }
  40. } else if (Array.isArray(config.data) && config.data[0] && config.data[0].post == '1' && config.method === 'post' ) {
  41. config.data = 'mode=' + (Object.values(config.data[0]).join(','))//序列化post 参数
  42. } else if (config.method === 'post') {
  43. const encoded = {};
  44. for (const key in config.data[0]) {
  45. if (Object.hasOwnProperty.call(config.data[0], key)) {
  46. encoded[key] = encodeURIComponent(config.data[0][key]);
  47. }
  48. }
  49. config.data = qs.stringify([encoded]) //序列化post 参数
  50. } else {
  51. const encoded = {};
  52. for (const key in config.data) {
  53. if (Object.hasOwnProperty.call(config.data, key)) {
  54. encoded[key] = encodeURIComponent(config.data[key]);
  55. }
  56. }
  57. config.data = encoded
  58. }
  59. return config;
  60. }, (error) => {
  61. console.log('错误的传参')
  62. return Promise.reject(error);
  63. });
  64. //返回状态判断(添加响应拦截器)
  65. axios.interceptors.response.use((res) => {
  66. //对响应数据做些事
  67. if (!res.data.success) {
  68. let newToken = res.data.token //成功后更新token
  69. localStorage.setItem('access_token', newToken)
  70. }
  71. return res;
  72. }, (error) => {
  73. if (axios.isCancel(error)) {
  74. console.log('请求已取消', error.message);
  75. } else if (error.response.data.status == '401') { //如果token 过期 则跳转到登录页面
  76. this.$router.push('/login');
  77. }
  78. return Promise.reject(error);
  79. });
  80. //返回一个Promise(发送post请求)
  81. function post(url, params, source) {
  82. return new Promise((resolve, reject) => {
  83. axios.post(url, params, source ? { cancelToken: source.token, responseType: source.responseType } : '')
  84. .then(response => {
  85. resolve(response);
  86. }, err => {
  87. reject(err);
  88. })
  89. .catch((error) => {
  90. reject(error)
  91. })
  92. })
  93. }
  94. //返回一个Promise(发送put请求)
  95. function put(url, params, source) {
  96. return new Promise((resolve, reject) => {
  97. axios.put(url, params, source ? { cancelToken: source.token } : '')
  98. .then(response => {
  99. resolve(response);
  100. }, err => {
  101. reject(err);
  102. })
  103. .catch((error) => {
  104. reject(error)
  105. })
  106. })
  107. }
  108. ////返回一个Promise(发送get请求)
  109. function get(url, param, source) {
  110. return new Promise((resolve, reject) => {
  111. let cancelToken = source ? source.token : ''
  112. axios.get(url, { params: param, cancelToken })
  113. .then(response => {
  114. resolve(response)
  115. }, err => {
  116. reject(err)
  117. })
  118. .catch((error) => {
  119. reject(error)
  120. })
  121. })
  122. }
  123. export default {
  124. get,
  125. post,
  126. put,
  127. setCancelSource: () => {
  128. // 每次创建新的请求时,可以调用此方法以创建新的取消令牌
  129. cancel = CancelToken.source();
  130. return cancel;
  131. }
  132. }