分包
This commit is contained in:
@@ -4,31 +4,49 @@
|
||||
*/
|
||||
import axios from 'axios'
|
||||
import { getToken, getUser } from '@/utils/storage'
|
||||
import { useRouter } from 'vue-router';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import router from '@/router'
|
||||
|
||||
const router = useRouter();
|
||||
// axios.defaults.withCredentials = true;
|
||||
import { ElMessage } from 'element-plus';
|
||||
// import { usePythonBridge, } from '@/utils/pythonBridge'
|
||||
|
||||
// const { stopScript } = usePythonBridge();
|
||||
|
||||
const specialBaseURL = 'http://120.26.251.180:15330'
|
||||
const specialUrlPrefixes = ['chat', 'translation'] // 示例路径前缀
|
||||
// 请求地址前缀
|
||||
let baseURL = ''
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
// 生产环境
|
||||
// baseURL = "http://120.26.251.180:8085/"
|
||||
baseURL = "http://120.26.251.180:15330"
|
||||
// baseURL = "https://api.tkpage.yolozs.com"
|
||||
// baseURL = "http://192.168.1.174:8101"
|
||||
baseURL = "http://47.79.98.113:8101"
|
||||
} else {
|
||||
// 测试环境
|
||||
// baseURL = "http://120.26.251.180:8085/"
|
||||
// 开发环境
|
||||
baseURL = "http://120.26.251.180:8085/"
|
||||
|
||||
baseURL = "http://47.79.98.113:8101"
|
||||
// baseURL = "http://api.tkpage.vvtiktok.cn"
|
||||
}
|
||||
|
||||
// 请求拦截器
|
||||
axios.interceptors.request.use((config) => {
|
||||
// if (getToken()) {
|
||||
// config.headers['token'] = getToken();
|
||||
// }
|
||||
console.log("config", config)
|
||||
const url = sliceUrl(config.url)
|
||||
console.log("url", url)
|
||||
if (!(config.url == 'doLogin' || config.url == 'get-id-by-name')) {
|
||||
config.headers['vvtoken'] = getToken();
|
||||
}
|
||||
|
||||
// 判断是否是特殊接口
|
||||
const isSpecial = specialUrlPrefixes.some(prefix => config.url.includes(prefix));
|
||||
console.log("isSpecial", isSpecial)
|
||||
// 根据接口设置 baseURL
|
||||
config.baseURL = isSpecial ? specialBaseURL : baseURL;
|
||||
console.log("config.baseURL", config.baseURL)
|
||||
// 请求超时时间 - 毫秒
|
||||
config.timeout = 60000
|
||||
config.baseURL = baseURL
|
||||
// config.baseURL = baseURL
|
||||
// 自定义Content-type
|
||||
config.headers['Content-type'] = 'application/json'
|
||||
return config;
|
||||
@@ -38,7 +56,20 @@ axios.interceptors.request.use((config) => {
|
||||
|
||||
// 响应拦截器
|
||||
axios.interceptors.response.use((response) => {
|
||||
return response
|
||||
console.log("response", response.data)
|
||||
if (response.data.code == 0 || response.data.code == 200) {
|
||||
console.log("response", response.data.data)
|
||||
return response.data.data
|
||||
} else if (response.data.code == 40400) {
|
||||
// stopScript();
|
||||
router.push('/')
|
||||
ElMessage.error(response.data.code + '' + response.data.message + 1);
|
||||
} else {
|
||||
|
||||
ElMessage.error(response.data.code + '' + response.data.message + 2);
|
||||
Promise.reject(response.data.code + '' + response.data.message)
|
||||
}
|
||||
|
||||
}, (error) => {
|
||||
// 可添加请求失败后的处理逻辑
|
||||
return Promise.reject(error)
|
||||
@@ -54,24 +85,18 @@ export function getAxios({ url, params }) {
|
||||
params
|
||||
// 请求成功,将返回的数据传递给resolve函数
|
||||
}).then(res => {
|
||||
resolve(res.data)
|
||||
resolve(res)
|
||||
// 请求失败,将错误信息传递给reject函数
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
ElMessage.error(err + 3);
|
||||
reject(err)
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// axios的post请求
|
||||
export function postAxios({ url, data }) {
|
||||
// if (url != 'api/account/login') {
|
||||
|
||||
// throttledCheekalive();
|
||||
|
||||
// }
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.post(
|
||||
url,
|
||||
@@ -82,20 +107,10 @@ export function postAxios({ url, data }) {
|
||||
}
|
||||
}
|
||||
).then(res => {
|
||||
resolve(res.data)
|
||||
resolve(res)
|
||||
}).catch(err => {
|
||||
if (err.message == "Network Error") {
|
||||
// alert("网络错误,请检查网络连接")
|
||||
// ElMessage.error('网络连接错误');
|
||||
reject('网络连接错误')
|
||||
|
||||
} else {
|
||||
ElMessage.error(err.message);
|
||||
reject(err.message)
|
||||
|
||||
}
|
||||
// console.log(err)
|
||||
// reject(err)
|
||||
ElMessage.error(err + 4);
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -131,28 +146,29 @@ export const downFile = async (urlstr, data) => {
|
||||
|
||||
}
|
||||
//请求前验证
|
||||
function cheekalive() {
|
||||
axios.post('api/account/cheekalive', {
|
||||
userId: getUser().userId,
|
||||
currcode: getToken(),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
}
|
||||
).then(res => {
|
||||
console.log(res.data)
|
||||
if (res.data) {
|
||||
// function cheekalive() {
|
||||
// axios.post('api/account/cheekalive', {
|
||||
// userId: getUser().userId,
|
||||
// currcode: getToken(),
|
||||
// },
|
||||
// {
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/x-www-form-urlencoded'
|
||||
// }
|
||||
// }
|
||||
// ).then(res => {
|
||||
// console.log(res.data)
|
||||
// if (res.data) {
|
||||
|
||||
} else {
|
||||
alert("账号在其他地方登录!")
|
||||
window.location.href = '/';
|
||||
}
|
||||
// } else {
|
||||
// stopScript();
|
||||
// alert("账号在其他地方登录!")
|
||||
// window.location.href = '/';
|
||||
// }
|
||||
|
||||
|
||||
})
|
||||
}
|
||||
// })
|
||||
// }
|
||||
//节流函数
|
||||
function throttle(func, limit) {
|
||||
let inThrottle;
|
||||
@@ -167,6 +183,18 @@ function throttle(func, limit) {
|
||||
}
|
||||
}
|
||||
|
||||
const throttledCheekalive = throttle(cheekalive, 5000);
|
||||
function sliceUrl(url) {
|
||||
const lastSlash = url.lastIndexOf('/');
|
||||
const questionMark = url.indexOf('?');
|
||||
if (questionMark == -1) {
|
||||
const result = url.slice(lastSlash + 1, url.length);
|
||||
return result;
|
||||
} else {
|
||||
const result = url.slice(lastSlash + 1, questionMark);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default axios
|
||||
Reference in New Issue
Block a user