修复使用生产模式打包后,调用set-cookie后,document.cookie仍然为空的问题。
This commit is contained in:
parent
77ef0139fc
commit
950d589f0f
BIN
assets/list.png
BIN
assets/list.png
Binary file not shown.
Before Width: | Height: | Size: 628 B |
18
src/index.ts
18
src/index.ts
@ -30,8 +30,8 @@ interface ApiResponse<T> {
|
||||
|
||||
// 检查登录状态
|
||||
function checkLoginStatus() {
|
||||
const auth = document.cookie.split('; ').find(row => row.startsWith('EACToken=') || row.startsWith('MssSsoToken='));
|
||||
if (!auth) {
|
||||
const cookie = window.electronAPI.getSessionStorage('cookie');
|
||||
if (!cookie) {
|
||||
window.location.href = 'login.html';
|
||||
return;
|
||||
}
|
||||
@ -45,7 +45,7 @@ function checkLoginStatus() {
|
||||
|
||||
// 处理退出登录
|
||||
function handleLogout() {
|
||||
document.cookie = 'expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
|
||||
window.electronAPI.removeSessionStorage('cookie');
|
||||
window.location.href = 'login.html';
|
||||
}
|
||||
|
||||
@ -53,7 +53,10 @@ function handleLogout() {
|
||||
async function getMenuList(): Promise<MenuItem[]> {
|
||||
try {
|
||||
const response = await fetch('http://1.12.73.211:8848/EIAC_Desktop_Api/api/Menu/GetMenu', {
|
||||
method: 'POST'
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8'
|
||||
}
|
||||
});
|
||||
|
||||
const result = await response.json() as ApiResponse<MenuItem[]>;
|
||||
@ -97,14 +100,13 @@ function createMenuItem(item: MenuItem, menuList: MenuItem[]): HTMLLIElement {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 添加当前菜单项的选中状态和图标
|
||||
li.classList.add('active');
|
||||
icon.src = item.IconConfig._1x.Selected;
|
||||
|
||||
const url: string = item.Url.startsWith("http") ? item.Url : `http://${item.Url}`;
|
||||
|
||||
const result = await window.electronAPI.checkUrl(url);
|
||||
const result = await window.electronAPI.checkUrlAvailable(url);
|
||||
if (result.ok && result.status >= 200 && result.status < 400) {
|
||||
console.log('✅ URL 可访问:', result.status);
|
||||
(document.querySelector("webview") as WebviewTag).src = url;
|
||||
@ -165,7 +167,7 @@ async function initialize() {
|
||||
const errorModal = document.getElementById('errorModal') as HTMLDivElement;
|
||||
const closeErrorModal = document.getElementById('closeErrorModal') as HTMLButtonElement;
|
||||
// 关闭按钮点击事件
|
||||
closeErrorModal.addEventListener('click', (event)=>{
|
||||
closeErrorModal.addEventListener('click', (event) => {
|
||||
errorModal.style.display = 'none';
|
||||
});
|
||||
|
||||
|
13
src/login.ts
13
src/login.ts
@ -19,17 +19,6 @@ interface ApiResponse<T = any> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
|
||||
// 设置Cookie的示例
|
||||
const setCookies = async (cookieString: string) => {
|
||||
try {
|
||||
await window.electronAPI.setCookie(cookieString);
|
||||
console.log('Cookies设置成功');
|
||||
} catch (error) {
|
||||
console.error('设置Cookies失败:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// 使用示例
|
||||
// setCookies('key1=value1; key2=value2; key3=value3');
|
||||
|
||||
@ -63,7 +52,7 @@ async function handleLogin(account: string, password: string): Promise<void> {
|
||||
} else {
|
||||
// 登录成功,将data.Cookies拼接为字符串,存储cookie
|
||||
const cookies = rspBody.data.Cookies.map(cookie => `${cookie.Key.trim()}=${cookie.Value.trim()}`).join('; ');
|
||||
await setCookies(cookies);
|
||||
window.electronAPI.setSessionStorage('cookie', cookies);
|
||||
// 跳转到主页面
|
||||
window.location.href = 'index.html';
|
||||
}
|
||||
|
40
src/main.ts
40
src/main.ts
@ -10,18 +10,7 @@ if (started) {
|
||||
app.quit();
|
||||
}
|
||||
|
||||
// 处理设置Cookie的IPC请求
|
||||
ipcMain.handle('set-cookie', async (event, cookie) => {
|
||||
try {
|
||||
await event.sender.session.cookies.set(cookie);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('设置Cookie失败:', error);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('check-url', async (event, rawUrl: string) => {
|
||||
ipcMain.handle('check-url-available', async (event, rawUrl: string) => {
|
||||
try {
|
||||
const url = new URL(rawUrl);
|
||||
const lib = url.protocol === 'https:' ? https : http;
|
||||
@ -73,12 +62,34 @@ const createWindow = () => {
|
||||
nodeIntegration: false, // 禁用 Node.js 集成,提高安全性。
|
||||
preload: path.join(__dirname, 'preload.js'), // 预加载脚本
|
||||
webviewTag: true, // 启用webview标签
|
||||
//webSecurity: false, // 禁用web安全策略
|
||||
//webSecurity: false, // 禁用web安全策略,允许跨域请求
|
||||
//allowRunningInsecureContent: true, // 允许运行不安全的内容
|
||||
//sandbox: false // 禁用沙箱
|
||||
},
|
||||
});
|
||||
|
||||
// // 配置 webview 的权限
|
||||
// win.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
|
||||
// callback({
|
||||
// requestHeaders: {
|
||||
// ...details.requestHeaders,
|
||||
// 'Origin': 'http://1.12.73.211:8848'
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
// // 配置 webview 的响应头
|
||||
// win.webContents.session.webRequest.onHeadersReceived((details, callback) => {
|
||||
// callback({
|
||||
// responseHeaders: {
|
||||
// ...details.responseHeaders,
|
||||
// 'Access-Control-Allow-Origin': ['*'],
|
||||
// 'Access-Control-Allow-Methods': ['GET, POST, PUT, DELETE, OPTIONS'],
|
||||
// 'Access-Control-Allow-Headers': ['Content-Type, Authorization']
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
// 隐藏顶部菜单栏
|
||||
win.setMenuBarVisibility(false);
|
||||
win.setAutoHideMenuBar(true);
|
||||
@ -113,7 +124,7 @@ const createWindow = () => {
|
||||
|
||||
// 监听窗口关闭(用户点击右上角X)
|
||||
win.on('close', (event) => {
|
||||
event.preventDefault();
|
||||
event.preventDefault();
|
||||
win.hide();
|
||||
});
|
||||
|
||||
@ -134,7 +145,6 @@ const createWindow = () => {
|
||||
tray.on('click', () => {
|
||||
win.show();
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
// This method will be called when Electron has finished
|
||||
|
@ -4,35 +4,26 @@
|
||||
import { contextBridge, ipcRenderer } from 'electron';
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
setCookie: async (cookieString: string) => {
|
||||
const cookies = cookieString.split(';').map(cookie => {
|
||||
const [name, value] = cookie.trim().split('=');
|
||||
return {
|
||||
name: name.trim(),
|
||||
value: value.trim(),
|
||||
path: '/',
|
||||
secure: false,
|
||||
httpOnly: false,
|
||||
expirationDate: Math.floor(Date.now() / 1000) + 86400 // 24小时后过期
|
||||
};
|
||||
});
|
||||
|
||||
// 为每个域名设置Cookie
|
||||
const domains = ['localhost', '1.12.73.211'];
|
||||
const ports = ['', ':8848'];
|
||||
|
||||
for (const cookie of cookies) {
|
||||
for (const domain of domains) {
|
||||
for (const port of ports) {
|
||||
const url = `http://${domain}${port}`;
|
||||
await ipcRenderer.invoke('set-cookie', {
|
||||
...cookie,
|
||||
url,
|
||||
domain
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// 检查URL是否可用
|
||||
checkUrlAvailable: (url: string) => ipcRenderer.invoke('check-url-available', url),
|
||||
|
||||
// 设置 sessionStorage
|
||||
setSessionStorage: (key: string, value: string) => {
|
||||
window.sessionStorage.setItem(key, value);
|
||||
},
|
||||
checkUrl: (url: string) => ipcRenderer.invoke('check-url', url)
|
||||
|
||||
// 获取 sessionStorage
|
||||
getSessionStorage: (key: string) => {
|
||||
return window.sessionStorage.getItem(key);
|
||||
},
|
||||
|
||||
// 删除 sessionStorage
|
||||
removeSessionStorage: (key: string) => {
|
||||
window.sessionStorage.removeItem(key);
|
||||
},
|
||||
|
||||
// 清空 sessionStorage
|
||||
clearSessionStorage: () => {
|
||||
window.sessionStorage.clear();
|
||||
}
|
||||
});
|
||||
|
7
src/types/electron.d.ts
vendored
7
src/types/electron.d.ts
vendored
@ -1,6 +1,9 @@
|
||||
export interface ElectronAPI {
|
||||
setCookie: (cookieString: string) => Promise<void>;
|
||||
checkUrl: (url: string) => Promise<{ ok: boolean; status: number; error?: string }>;
|
||||
checkUrlAvailable: (url: string) => Promise<{ ok: boolean; status: number; error?: string }>;
|
||||
setSessionStorage: (key: string, value: string) => void;
|
||||
getSessionStorage: (key: string) => string | null;
|
||||
removeSessionStorage: (key: string) => void;
|
||||
clearSessionStorage: () => void;
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
Loading…
x
Reference in New Issue
Block a user