| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import axios from "axios"
- import qs from "qs"
- // axios.defaults.timeout = 180000 //响应时间
- axios.defaults.timeout = 600000 //响应时间
- axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; //配置请求头
- axios.defaults.baseURL = process.env.NODE_HOST; //配置接口地址
- axios.defaults.withCredentials = true; // 设置为 true,允许跨域请求携带凭据
- console.log(process.env)
- const CancelToken = axios.CancelToken;
- let cancel; // 用于存储取消请求的方法
- //POST传参序列化(添加请求拦截器)
- axios.interceptors.request.use((config) => {
- // 判断请求的 URL,执行不同的逻辑
- 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') {
- // 如果是特定的 URL,可以进行 post 参数的序列化
- // 注意:此处不需要 config.data = config.data; 去掉这个无意义的赋值
- config.data = serializePostData(config.data);
- } else if (config.url.indexOf('https://gpt4.cocorobo.cn/') !== -1 ||
- config.url.indexOf('https://claude3.cocorobo.cn/') !== -1 ||
- config.url.indexOf('https://llm.cocorobo.cn/') !== -1) {
- // 如果是其他 URL,则设置请求头为 application/json
- config.headers = {
- 'Content-Type': 'application/json',
- };
- } else if (config.data && config.data[0].post === '1' && config.method === 'post') {
- // 如果 data 的第一个元素 post 值为 '1',则进行自定义序列化
- config.data = 'mode=' + Object.values(config.data[0]).join(',');
- } else if (config.method === 'post' && config.data) {
- // 处理其他 post 请求的参数序列化
- const encoded = {};
- for (const key in config.data[0]) {
- if (Object.hasOwnProperty.call(config.data[0], key)) {
- encoded[key] = encodeURIComponent(config.data[0][key]);
- }
- }
- config.data = qs.stringify([encoded]); // 使用 qs 序列化数据
- } else {
- // 对于其他请求类型,进行标准的 URL 编码
- const encoded = {};
- for (const key in config.data) {
- if (Object.hasOwnProperty.call(config.data, key)) {
- encoded[key] = encodeURIComponent(config.data[key]);
- }
- }
- config.data = encoded;
- }
- return config; // 确保返回修改后的 config
- }, (error) => {
- // 错误处理
- return Promise.reject(error);
- });
- // 序列化函数
- function serializePostData(data) {
- // 你可以根据需求,写一个自定义的序列化方法
- // 比如这里假设我们只是将数据简单地序列化
- return JSON.stringify(data);
- }
- //返回状态判断(添加响应拦截器)
- axios.interceptors.response.use((res) => {
- //对响应数据做些事
- if (!res.data.success) {
- let newToken = res.data.token //成功后更新token
- localStorage.setItem('access_token', newToken)
- }
- return res;
- }, (error) => {
- if (axios.isCancel(error)) {
- console.log('请求已取消', error.message);
- } else if (error.response.data.status == '401') { //如果token 过期 则跳转到登录页面
- this.$router.push('/login');
- }
- return Promise.reject(error);
- });
- //返回一个Promise(发送post请求)
- function post(url, params, source) {
- return new Promise((resolve, reject) => {
- axios.post(url, params, source ? { cancelToken: source.token } : '')
- .then(response => {
- resolve(response);
- }, err => {
- reject(err);
- })
- .catch((error) => {
- reject(error)
- })
- })
- }
- //返回一个Promise(发送put请求)
- function put(url, params, source) {
- return new Promise((resolve, reject) => {
- axios.put(url, params, source ? { cancelToken: source.token } : '')
- .then(response => {
- resolve(response);
- }, err => {
- reject(err);
- })
- .catch((error) => {
- reject(error)
- })
- })
- }
- ////返回一个Promise(发送get请求)
- function get(url, param, source) {
- return new Promise((resolve, reject) => {
- let cancelToken = source ? source.token : ''
- axios.get(url, { params: param, cancelToken })
- .then(response => {
- resolve(response)
- }, err => {
- reject(err)
- })
- .catch((error) => {
- reject(error)
- })
- })
- }
- export default {
- get,
- post,
- put,
- setCancelSource: () => {
- // 每次创建新的请求时,可以调用此方法以创建新的取消令牌
- cancel = CancelToken.source();
- return cancel;
- }
- }
|