axios.config.js 5.3 KB

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