config(vite): 更新 Vite配置以支持特定主机
- 在 Vite 配置中添加 allowedHosts 选项,允许 yiligpt.x.digitalyili.com 主机 - 优化代码格式,调整导入语句的分号
This commit is contained in:
@@ -1,53 +1,60 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
const host = window.location.origin;
|
||||
const path = '/aiagent/assistant/78907182-cc42-4072-abae-86ef67c1ecd3/share?';
|
||||
const param = `token=${encodeURIComponent(localStorage.getItem('plantToken') as string)}&source=app`;
|
||||
// const url = host + path + param;
|
||||
const url = host + path + param;
|
||||
console.log(url);
|
||||
// const iframe = ref<HTMLIFrameElement | null>(null);
|
||||
const iframe = ref<HTMLIFrameElement | null>(null);
|
||||
const router = useRouter();
|
||||
onMounted(() => {
|
||||
// 保存原始的window.open方法
|
||||
const originalOpen = window.open;
|
||||
|
||||
// onMounted(() => {
|
||||
// // 保存原始的window.open方法
|
||||
// const originalOpen = window.open;
|
||||
// 监听iframe的load事件,确保iframe已完全加载
|
||||
iframe.value?.addEventListener('load', () => {
|
||||
try {
|
||||
const iframeWindow = iframe.value?.contentWindow;
|
||||
|
||||
// // 监听iframe的load事件,确保iframe已完全加载
|
||||
// iframe.value?.addEventListener('load', () => {
|
||||
// try {
|
||||
// const iframeWindow = iframe.value?.contentWindow;
|
||||
|
||||
// // 尝试覆盖iframe的open方法
|
||||
// if (iframeWindow) {
|
||||
// // 方法一:直接覆盖
|
||||
// try {
|
||||
// iframeWindow.open = function (...args: any[]) {
|
||||
// // console.log('iframe中的open方法被调用,参数:', args);
|
||||
// // 使用父窗口的open方法打开
|
||||
// return originalOpen.apply(window, args as any);
|
||||
// };
|
||||
// } catch (e) {
|
||||
// // 方法二:如果直接覆盖失败,尝试使用defineProperty
|
||||
// try {
|
||||
// Object.defineProperty(iframeWindow, 'open', {
|
||||
// value: function (...args: any[]) {
|
||||
// // console.log('iframe中的open方法被调用,参数:', args);
|
||||
// return originalOpen.apply(window, args as any);
|
||||
// },
|
||||
// writable: true,
|
||||
// configurable: true
|
||||
// });
|
||||
// } catch (e2) {
|
||||
// // console.error('无法覆盖iframe的open方法(defineProperty):', e2);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error('无法覆盖iframe的open方法:', error);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
// 尝试覆盖iframe的open方法
|
||||
if (iframeWindow) {
|
||||
// 方法一:直接覆盖
|
||||
try {
|
||||
iframeWindow.open = function (...args: any[]) {
|
||||
const url = new URL(args[0]); // 使用 URL API 解析
|
||||
const path = url.pathname; // 获取路径部分
|
||||
const query = Object.fromEntries(url.searchParams); // 将 search 转换为对象
|
||||
// 如果需要去除特定 host 前缀(如本地调试)
|
||||
if (url.host === window.location.host) {
|
||||
args[0] = args[0].replace(`https://${url.host}`, '');
|
||||
}
|
||||
router.push({
|
||||
path,
|
||||
query
|
||||
});
|
||||
};
|
||||
} catch (e) {
|
||||
// 方法二:如果直接覆盖失败,尝试使用defineProperty
|
||||
try {
|
||||
Object.defineProperty(iframeWindow, 'open', {
|
||||
value: function (...args: any[]) {
|
||||
// console.log('iframe中的open方法被调用,参数:', args);
|
||||
return originalOpen.apply(window, args as any);
|
||||
},
|
||||
writable: true,
|
||||
configurable: true
|
||||
});
|
||||
} catch (e2) {
|
||||
console.error('无法覆盖iframe的open方法(defineProperty):', e2);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('无法覆盖iframe的open方法:', error);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
@@ -11,7 +11,7 @@ import legacy from '@vitejs/plugin-legacy';
|
||||
// shift + alt 快速定位到对应组件
|
||||
import { codeInspectorPlugin } from 'code-inspector-plugin';
|
||||
// 导入 dev tools
|
||||
import vueDevTools from 'vite-plugin-vue-devtools'
|
||||
import vueDevTools from 'vite-plugin-vue-devtools';
|
||||
|
||||
export default defineConfig(({ mode }) => {
|
||||
// 接收 mode 参数
|
||||
@@ -24,6 +24,7 @@ export default defineConfig(({ mode }) => {
|
||||
return {
|
||||
// 必须 return 配置对象
|
||||
server: {
|
||||
allowedHosts: ['yiligpt.x.digitalyili.com'],
|
||||
host: '0.0.0.0',
|
||||
port: 3000,
|
||||
proxy: {
|
||||
|
||||
Reference in New Issue
Block a user