config(vite): 更新 Vite配置以支持特定主机

- 在 Vite 配置中添加 allowedHosts 选项,允许 yiligpt.x.digitalyili.com 主机
- 优化代码格式,调整导入语句的分号
This commit is contained in:
陈昱达
2025-05-27 21:16:02 +08:00
parent 94330b1f01
commit 13d30a51a8
2 changed files with 50 additions and 42 deletions

View File

@@ -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>

View File

@@ -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: {