|
@@ -5,6 +5,10 @@ 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;
|
|
|
console.log(process.env)
|
|
|
+
|
|
|
+const CancelToken = axios.CancelToken;
|
|
|
+let cancel;
|
|
|
+
|
|
|
|
|
|
axios.interceptors.request.use((config) => {
|
|
|
|
|
@@ -22,32 +26,33 @@ axios.interceptors.request.use((config) => {
|
|
|
|
|
|
|
|
|
|
|
|
- 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') {
|
|
|
+ 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') {
|
|
|
config.data = config.data
|
|
|
- }else if(config.url.indexOf('https://gpt4.cocorobo.cn/')!=-1 || config.url.indexOf('https://claude3.cocorobo.cn/')!=-1){
|
|
|
- config.headers = {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- }
|
|
|
- }else if (config.data && config.data[0].post == '1' && config.method === 'post') {
|
|
|
+ } else if (config.url.indexOf('https://gpt4.cocorobo.cn/') != -1 || config.url.indexOf('https://claude3.cocorobo.cn/') != -1) {
|
|
|
+ config.headers = {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ }
|
|
|
+ } else if (config.data && config.data[0].post == '1' && config.method === 'post') {
|
|
|
config.data = 'mode=' + (Object.values(config.data[0]).join(','))
|
|
|
} else if (config.method === '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]);
|
|
|
+ encoded[key] = encodeURIComponent(config.data[0][key]);
|
|
|
}
|
|
|
}
|
|
|
config.data = qs.stringify([encoded])
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
const encoded = {};
|
|
|
for (const key in config.data) {
|
|
|
if (Object.hasOwnProperty.call(config.data, key)) {
|
|
|
- encoded[key] = encodeURIComponent(config.data[key]);
|
|
|
+ encoded[key] = encodeURIComponent(config.data[key]);
|
|
|
}
|
|
|
}
|
|
|
config.data = encoded
|
|
|
}
|
|
|
+
|
|
|
return config;
|
|
|
}, (error) => {
|
|
|
console.log('错误的传参')
|
|
@@ -64,15 +69,17 @@ axios.interceptors.response.use((res) => {
|
|
|
}
|
|
|
return res;
|
|
|
}, (error) => {
|
|
|
- if (error.response.data.status == '401') {
|
|
|
+ if (axios.isCancel(error)) {
|
|
|
+ console.log('请求已取消', error.message);
|
|
|
+ } else if (error.response.data.status == '401') {
|
|
|
this.$router.push('/login');
|
|
|
}
|
|
|
return Promise.reject(error);
|
|
|
});
|
|
|
|
|
|
-function post(url, params) {
|
|
|
+function post(url, params, source) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- axios.post(url, params)
|
|
|
+ axios.post(url, params, source ? { cancelToken: source.token } : '')
|
|
|
.then(response => {
|
|
|
resolve(response);
|
|
|
}, err => {
|
|
@@ -85,25 +92,26 @@ function post(url, params) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-function put(url, params) {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- axios.put(url, params)
|
|
|
- .then(response => {
|
|
|
- resolve(response);
|
|
|
- }, err => {
|
|
|
- reject(err);
|
|
|
- })
|
|
|
- .catch((error) => {
|
|
|
- reject(error)
|
|
|
- })
|
|
|
- })
|
|
|
+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)
|
|
|
+ })
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-function get(url, param) {
|
|
|
+function get(url, param, source) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- axios.get(url, { params: param })
|
|
|
+ let cancelToken = source ? source.token : ''
|
|
|
+ axios.get(url, { params: param, cancelToken })
|
|
|
.then(response => {
|
|
|
resolve(response)
|
|
|
}, err => {
|
|
@@ -117,5 +125,10 @@ function get(url, param) {
|
|
|
export default {
|
|
|
get,
|
|
|
post,
|
|
|
- put,
|
|
|
+ put,
|
|
|
+ setCancelSource: () => {
|
|
|
+
|
|
|
+ cancel = CancelToken.source();
|
|
|
+ return cancel;
|
|
|
+ }
|
|
|
}
|