axios.config.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import axios from "axios"
  2. import qs from "qs"
  3. axios.defaults.timeout = 900000 //响应时间
  4. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; //配置请求头
  5. axios.defaults.baseURL = process.env.NODE_HOST; //配置接口地址
  6. console.log(process.env)
  7. //POST传参序列化(添加请求拦截器)
  8. axios.interceptors.request.use((config) => {
  9. //在发送请求之前做某件事
  10. let token = sessionStorage.getItem('access_token') || "" //获取token
  11. console.log(token)
  12. if (token != "") {
  13. config.headers = {
  14. 'access-token': token,
  15. 'Content-Type': 'application/x-www-form-urlencoded'
  16. }
  17. }
  18. if (config.url === 'https://gpt.cocorobo.cn/chat' || config.url === 'https://gpt4.cocorobo.cn/imageAnalyse' || config.url === 'https://gpt4.cocorobo.cn/create_free_assistants' || config.url === 'https://gpt4.cocorobo.cn/assistants_completion_response') {
  19. config.data = config.data //序列化post 参数
  20. }else if(config.url.indexOf('https://gpt4.cocorobo.cn/')!=-1 || config.url.indexOf('https://claude3.cocorobo.cn/')!=-1 || config.url.indexOf('https://appapi.cocorobo.cn/')!=-1){
  21. config.headers = {
  22. 'Content-Type': 'application/json',
  23. }
  24. }else if(config.url.indexOf('https://gpt.cocorobo.cn/')!=-1){
  25. config.headers = {
  26. 'Content-Type': 'application/json',
  27. }
  28. } else if (config.data && config.data[0].post == '1' && config.method === 'post') {
  29. config.data = 'mode=' + (Object.values(config.data[0]).join(',')) //序列化post 参数
  30. } else if (config.method === 'post') {
  31. const encoded = {};
  32. for (const key in config.data[0]) {
  33. if (Object.hasOwnProperty.call(config.data[0], key)) {
  34. encoded[key] = encodeURIComponent(config.data[0][key]);
  35. }
  36. }
  37. config.data = qs.stringify([encoded]) //序列化post 参数
  38. }else {
  39. const encoded = {};
  40. for (const key in config.data) {
  41. if (Object.hasOwnProperty.call(config.data, key)) {
  42. encoded[key] = encodeURIComponent(config.data[key]);
  43. }
  44. }
  45. config.data = encoded
  46. }
  47. return config;
  48. }, (error) => {
  49. console.log('错误的传参')
  50. return Promise.reject(error);
  51. });
  52. //返回状态判断(添加响应拦截器)
  53. axios.interceptors.response.use((res) => {
  54. //对响应数据做些事
  55. if (!res.data.success) {
  56. let newToken = res.data.token //成功后更新token
  57. localStorage.setItem('access_token', newToken)
  58. }
  59. return res;
  60. }, (error) => {
  61. if (error.response.data.status == '401') { //如果token 过期 则跳转到登录页面
  62. this.$router.push('/login');
  63. }
  64. return Promise.reject(error);
  65. });
  66. //返回一个Promise(发送post请求)
  67. function post(url, params) {
  68. return new Promise((resolve, reject) => {
  69. axios.post(url, params)
  70. .then(response => {
  71. resolve(response);
  72. }, err => {
  73. reject(err);
  74. })
  75. .catch((error) => {
  76. reject(error)
  77. })
  78. })
  79. }
  80. ////返回一个Promise(发送get请求)
  81. function get(url, param) {
  82. return new Promise((resolve, reject) => {
  83. axios.get(url, { params: param })
  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) {
  96. return new Promise((resolve, reject) => {
  97. axios.put(url, params)
  98. .then(response => {
  99. resolve(response);
  100. }, err => {
  101. reject(err);
  102. })
  103. .catch((error) => {
  104. reject(error)
  105. })
  106. })
  107. }
  108. export default {
  109. get,
  110. post,
  111. put
  112. }