|
@@ -11,128 +11,140 @@ let cancel; // 用于存储取消请求的方法
|
|
|
|
|
|
//POST传参序列化(添加请求拦截器)
|
|
//POST传参序列化(添加请求拦截器)
|
|
axios.interceptors.request.use((config) => {
|
|
axios.interceptors.request.use((config) => {
|
|
- //在发送请求之前做某件事
|
|
|
|
- let token = sessionStorage.getItem('access_token') || "" //获取token
|
|
|
|
- if (token != "") {
|
|
|
|
- config.headers = {
|
|
|
|
- 'access-token': token,
|
|
|
|
- 'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
- }
|
|
|
|
|
|
+ //在发送请求之前做某件事
|
|
|
|
+ let token = sessionStorage.getItem('access_token') || "" //获取token
|
|
|
|
+ if (token != "") {
|
|
|
|
+ config.headers = {
|
|
|
|
+ 'access-token': token,
|
|
|
|
+ 'Content-Type': 'application/x-www-form-urlencoded'
|
|
}
|
|
}
|
|
- // if (config.data && config.data[0].post == '1' && config.method === 'post') {
|
|
|
|
- // // config.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
|
|
|
|
- // // config.data = config.data//序列化post 参数
|
|
|
|
- // config.data = 'mode=' + (Object.values(config.data[0]).join(','))//序列化post 参数
|
|
|
|
- // } else if (config.method === 'post') {
|
|
|
|
- // config.data = qs.stringify(config.data)//序列化post 参数
|
|
|
|
- // }
|
|
|
|
- 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//序列化post 参数
|
|
|
|
- } else if (config.url.indexOf('https://r2rserver.cocorobo.cn') != -1) {
|
|
|
|
- config.headers = {
|
|
|
|
- 'Content-Type': 'application/json',
|
|
|
|
- }
|
|
|
|
- } 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 || config.url.indexOf('https://dify.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(','))//序列化post 参数
|
|
|
|
- } else if (config.method === 'post') {
|
|
|
|
|
|
+ }
|
|
|
|
+ // if (config.data && config.data[0].post == '1' && config.method === 'post') {
|
|
|
|
+ // // config.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
|
|
|
|
+ // // config.data = config.data//序列化post 参数
|
|
|
|
+ // config.data = 'mode=' + (Object.values(config.data[0]).join(','))//序列化post 参数
|
|
|
|
+ // } else if (config.method === 'post') {
|
|
|
|
+ // config.data = qs.stringify(config.data)//序列化post 参数
|
|
|
|
+ // }
|
|
|
|
+ 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//序列化post 参数
|
|
|
|
+ } else if (config.url.indexOf('https://r2rserver.cocorobo.cn') != -1) {
|
|
|
|
+ config.headers = {
|
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
|
+ }
|
|
|
|
+ } 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) {
|
|
|
|
+ config.headers = {
|
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
|
+ }
|
|
|
|
+ } else if (config.url.indexOf('https://dify.cocorobo.cn') != -1) {
|
|
|
|
+ if (config.url.indexOf('?key=role') != -1) {
|
|
|
|
+ config.headers = {
|
|
|
|
+ Authorization: `Bearer app-TonzLPv7rPG0EtnFKszOWjwt`,
|
|
|
|
+ "Content-Type": "application/json"
|
|
|
|
+ }
|
|
|
|
+ } else if (config.url.indexOf('?key=code') != -1) {
|
|
|
|
+ config.headers = {
|
|
|
|
+ Authorization: `Bearer app-zOMxBqyEKoJSvW10e5SS0kgj`,
|
|
|
|
+ "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(','))//序列化post 参数
|
|
|
|
+ } 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]);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- config.data = qs.stringify([encoded]) //序列化post 参数
|
|
|
|
- } else {
|
|
|
|
- const encoded = {};
|
|
|
|
- for (const key in config.data) {
|
|
|
|
- if (Object.hasOwnProperty.call(config.data, key)) {
|
|
|
|
- encoded[key] = encodeURIComponent(config.data[key]);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- config.data = encoded
|
|
|
|
|
|
+ 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]) //序列化post 参数
|
|
|
|
+ } else {
|
|
|
|
+ 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;
|
|
|
|
|
|
+ return config;
|
|
}, (error) => {
|
|
}, (error) => {
|
|
- console.log('错误的传参')
|
|
|
|
|
|
+ console.log('错误的传参')
|
|
|
|
|
|
- return Promise.reject(error);
|
|
|
|
|
|
+ return Promise.reject(error);
|
|
});
|
|
});
|
|
//返回状态判断(添加响应拦截器)
|
|
//返回状态判断(添加响应拦截器)
|
|
axios.interceptors.response.use((res) => {
|
|
axios.interceptors.response.use((res) => {
|
|
- //对响应数据做些事
|
|
|
|
- if (!res.data.success) {
|
|
|
|
- let newToken = res.data.token //成功后更新token
|
|
|
|
- localStorage.setItem('access_token', newToken)
|
|
|
|
|
|
+ //对响应数据做些事
|
|
|
|
+ if (!res.data.success) {
|
|
|
|
+ let newToken = res.data.token //成功后更新token
|
|
|
|
+ localStorage.setItem('access_token', newToken)
|
|
|
|
|
|
- }
|
|
|
|
- return res;
|
|
|
|
|
|
+ }
|
|
|
|
+ return res;
|
|
}, (error) => {
|
|
}, (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);
|
|
|
|
|
|
+ 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请求)
|
|
//返回一个Promise(发送post请求)
|
|
function post(url, params, source) {
|
|
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)
|
|
|
|
- })
|
|
|
|
- })
|
|
|
|
|
|
+ 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请求)
|
|
//返回一个Promise(发送put请求)
|
|
function put(url, params, source) {
|
|
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)
|
|
|
|
- })
|
|
|
|
- })
|
|
|
|
|
|
+ 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请求)
|
|
////返回一个Promise(发送get请求)
|
|
function get(url, param, source) {
|
|
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)
|
|
|
|
- })
|
|
|
|
- })
|
|
|
|
|
|
+ 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 {
|
|
export default {
|
|
- get,
|
|
|
|
- post,
|
|
|
|
- put,
|
|
|
|
- setCancelSource: () => {
|
|
|
|
- // 每次创建新的请求时,可以调用此方法以创建新的取消令牌
|
|
|
|
- cancel = CancelToken.source();
|
|
|
|
- return cancel;
|
|
|
|
- }
|
|
|
|
|
|
+ get,
|
|
|
|
+ post,
|
|
|
|
+ put,
|
|
|
|
+ setCancelSource: () => {
|
|
|
|
+ // 每次创建新的请求时,可以调用此方法以创建新的取消令牌
|
|
|
|
+ cancel = CancelToken.source();
|
|
|
|
+ return cancel;
|
|
|
|
+ }
|
|
}
|
|
}
|