| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- // 全局 Loading 控制器
- window.GlobalLoading = (function() {
- let overlay = null;
- let loadingText = null;
-
- function init() {
- overlay = document.getElementById('globalLoadingOverlay');
- loadingText = overlay ? overlay.querySelector('.global-loading-text') : null;
- }
-
- function show(text = '正在处理...') {
- // console.log('[GlobalLoading] show() 被调用');
- // console.log('[GlobalLoading] 文本:', text);
-
- if (!overlay) {
- // console.log('[GlobalLoading] → 初始化overlay元素...');
- init();
- }
-
- if (!overlay) {
- // console.error('[GlobalLoading] ✗ overlay元素未找到!');
- return;
- }
-
- // console.log('[GlobalLoading] ✓ overlay元素存在');
-
- if (loadingText) {
- loadingText.textContent = text;
- // console.log('[GlobalLoading] ✓ 设置Loading文本:', text);
- }
-
- overlay.classList.add('is-visible');
- // console.log('[GlobalLoading] ✓ 添加is-visible类,Loading应该可见了');
- }
-
- function hide() {
- // console.log('[GlobalLoading] hide() 被调用');
-
- if (!overlay) {
- // console.error('[GlobalLoading] ✗ overlay元素不存在');
- return;
- }
-
- overlay.classList.remove('is-visible');
- // console.log('[GlobalLoading] ✓ 移除is-visible类,Loading已隐藏');
- }
-
- return {
- show,
- hide
- };
- })();
- // 全局 Alert 控制器
- window.GlobalAlert = (function() {
- let alertContainer = null;
- let alertMessage = null;
- let hideTimer = null;
-
- function init() {
- alertContainer = document.getElementById('globalAlert');
- alertMessage = alertContainer ? alertContainer.querySelector('#alertMessage') : null;
- }
-
- function show(text, duration = 1500) {
- console.log('[GlobalAlert] show() 被调用:', { text, duration });
-
- if (!alertContainer) {
- console.log('[GlobalAlert] 初始化 alertContainer');
- init();
- }
-
- if (!alertContainer) {
- console.error('[GlobalAlert] alertContainer 未找到!');
- return;
- }
-
- if (!alertMessage) {
- console.error('[GlobalAlert] alertMessage 未找到!');
- return;
- }
-
- console.log('[GlobalAlert] 设置消息:', text);
-
- // 清除之前的自动隐藏定时器
- if (hideTimer) {
- clearTimeout(hideTimer);
- hideTimer = null;
- }
-
- alertMessage.textContent = text;
- alertContainer.classList.add('show');
-
- console.log('[GlobalAlert] 已添加 show 类,alert 应该可见了');
-
- // 自动隐藏
- if (duration > 0) {
- hideTimer = setTimeout(() => {
- hide();
- }, duration);
- }
- }
-
- function hide() {
- if (!alertContainer) return;
- alertContainer.classList.remove('show');
- if (hideTimer) {
- clearTimeout(hideTimer);
- hideTimer = null;
- }
- }
-
- return {
- show,
- hide
- };
- })();
- // 页面管理:负责切换 iframe 中的子页面,并保持与导航栏状态同步
- (function () {
- console.log('[Index] index.js 已加载');
- const DEFAULT_PAGE = "store";
- function getPageFrame() {
- return document.getElementById("pageFrame");
- }
- function getNavigationFrame() {
- return document.getElementById("navigationFrame");
- }
- function switchPage(page) {
- const frame = getPageFrame();
- if (!frame) {
- return;
- }
- // 只处理实际的页面切换,不处理login/register
- if (page === "login" || page === "register") {
- return;
- }
- switch (page) {
- case "store":
- frame.src = "page/store/store.html";
- break;
- case "assets":
- default:
- frame.src = "page/assets/assets.html";
- break;
- }
- // 同步导航栏状态
- syncNavigationState(page);
- }
- // 同步导航栏状态
- function syncNavigationState(page) {
- const navigationFrame = getNavigationFrame();
- if (navigationFrame && navigationFrame.contentWindow) {
- // 使用setTimeout确保iframe已加载
- setTimeout(() => {
- navigationFrame.contentWindow.postMessage(
- { type: "navigation", page },
- "*"
- );
- }, 100);
- }
- }
- window.addEventListener("message", (event) => {
- // 调试:记录所有收到的消息
- console.log('[Index] 收到message事件:', {
- origin: event.origin,
- expectedOrigin: window.location.origin,
- data: event.data,
- source: event.source,
- type: event.data?.type
- });
-
- // 检查 origin(允许同源或 localhost,或者来自 iframe)
- const isSameOrigin = event.origin === window.location.origin;
- const isLocalhost = event.origin === 'http://localhost:3000' ||
- event.origin === 'http://127.0.0.1:3000' ||
- event.origin.startsWith('http://localhost:') ||
- event.origin.startsWith('http://127.0.0.1:');
- // 允许来自同源的 iframe(即使 origin 不完全匹配)
- const isFromIframe = event.source && event.source !== window;
-
- console.log('[Index] Origin 检查:', {
- eventOrigin: event.origin,
- windowOrigin: window.location.origin,
- isSameOrigin,
- isLocalhost,
- isFromIframe,
- willPass: isSameOrigin || isLocalhost || isFromIframe
- });
-
- // 对于 global-alert 消息,放宽 origin 检查(允许来自任何同源 iframe)
- if (event.data && event.data.type === 'global-alert') {
- console.log('[Index] 这是 global-alert 消息,放宽 origin 检查');
- // 允许来自任何 iframe 的消息(只要 source 存在且不是 window 本身)
- if (isFromIframe || isSameOrigin || isLocalhost) {
- console.log('[Index] global-alert 消息通过检查');
- } else {
- console.warn('[Index] global-alert 消息被 origin 检查过滤:', event.origin);
- // 即使 origin 不匹配,也允许 global-alert 消息通过(因为来自同源 iframe)
- console.log('[Index] 但允许通过(来自 iframe)');
- }
- } else if (!isSameOrigin && !isLocalhost && !isFromIframe) {
- console.log('[Index] 消息被 origin 检查过滤:', event.origin);
- return;
- }
-
- console.log('[Index] 消息通过 origin 检查,继续处理');
-
- const { data } = event;
- if (data && data.type === "navigation" && (data.page === "login" || data.page === "register")) {
- // console.log('[2-Index] 收到login/register消息');
- const loginFrame = document.getElementById('loginViewFrame');
- if (loginFrame) {
- const mode = data.page === "register" ? "register" : "login";
- loginFrame.style.display = 'block';
- // console.log('[3-Index] iframe已显示');
-
- const sendMode = () => {
- if (loginFrame.contentWindow) {
- loginFrame.contentWindow.postMessage({
- type: 'open-login-view',
- mode: mode
- }, '*');
- // console.log('[4-Index] 消息已发送到login iframe');
- } else {
- setTimeout(sendMode, 100);
- }
- };
-
- sendMode();
-
- const handleLoad = () => {
- setTimeout(() => {
- sendMode();
- }, 50);
- };
-
- if (loginFrame.contentDocument && loginFrame.contentDocument.readyState === 'complete') {
- handleLoad();
- } else {
- loginFrame.addEventListener('load', handleLoad, { once: true });
- }
- }
- } else if (data && data.type === "navigation" && data.page) {
- switchPage(data.page);
- }
- // 注意:global-alert、global-loading、global-confirm 消息不再通过 index.js 处理
- // 各个 view 现在直接调用父窗口的 GlobalAlert/GlobalLoading/GlobalConfirm
- else if (data && data.type === "open-export-view") {
- // 处理打开导出弹出框
- console.log('[Index] 收到open-export-view消息:', data);
- if (!data.folderName) {
- console.error('[Index] 缺少文件夹名称');
- return;
- }
- if (!window.ExportViewManager) {
- console.error('[Index] ExportViewManager 未初始化');
- return;
- }
- // 直接打开弹出框,传递文件夹名称
- window.ExportViewManager.show(data.folderName).then((confirmed) => {
- console.log('[Index] 用户选择:', confirmed ? '确认导出' : '取消');
- // 如果用户确认,可以在这里处理实际的导出下载逻辑
- if (confirmed) {
- // TODO: 处理实际的导出下载逻辑
- console.log('[Index] 用户确认导出,文件夹:', data.folderName);
- }
- }).catch(error => {
- console.error('[Index] ExportViewManager显示失败:', error);
- });
- } else if (data && data.type === "close-login-view") {
- const loginFrame = document.getElementById('loginViewFrame');
- if (loginFrame) {
- loginFrame.style.display = 'none';
- }
- } else if (data && data.type === "login-success" && data.user) {
- // 处理登录成功消息,转发给 navigation iframe 和 pageFrame
- const navigationFrame = getNavigationFrame();
- if (navigationFrame && navigationFrame.contentWindow) {
- navigationFrame.contentWindow.postMessage({
- type: 'login-success',
- user: data.user
- }, '*');
- }
-
- // 也转发给 pageFrame(store 页面)
- const pageFrame = getPageFrame();
- if (pageFrame && pageFrame.contentWindow) {
- pageFrame.contentWindow.postMessage({
- type: 'login-success',
- user: data.user
- }, '*');
- }
-
- // 转发给 assets iframe(如果存在),它会再转发给 disk iframe
- const assetsFrame = document.getElementById('assetsFrame');
- if (assetsFrame && assetsFrame.contentWindow) {
- assetsFrame.contentWindow.postMessage({
- type: 'login-success',
- user: data.user
- }, '*');
- }
- } else if (data && data.type === "logout") {
- // 处理登出消息,转发给所有 iframe
- const navigationFrame = getNavigationFrame();
- if (navigationFrame && navigationFrame.contentWindow) {
- navigationFrame.contentWindow.postMessage({
- type: 'logout'
- }, '*');
- }
-
- const pageFrame = getPageFrame();
- if (pageFrame && pageFrame.contentWindow) {
- pageFrame.contentWindow.postMessage({
- type: 'logout'
- }, '*');
- }
- }
- });
- window.addEventListener("DOMContentLoaded", () => {
- switchPage(DEFAULT_PAGE);
- syncNavigationState(DEFAULT_PAGE);
- });
- })();
|