Files
37duAutomation/desktop/electron-shell/electron/notify-toast.js
T
puddy 3dcedf95fd feat: 新增通知推送功能及离线登录,优化重连逻辑
- 新增通知推送模块:支持 WebSocket 实时推送与轮询兜底,新增通知管理界面
- 新增离线登录功能:断网时凭本地 7 天授权票据自动登录或手动登录
- 优化 Electron 客户端 WebSocket 重连逻辑,避免死循环
- 调整工作流脚本,拆分重构合同相关分支逻辑
- 新增设备上报客户端版本字段,用于升级追踪与关旧依据
2026-10-13 17:46:49 +08:00

93 lines
3.9 KiB
JavaScript

// 自定义通知弹窗(右下角 toast):无边框、置顶、不抢焦点、按级别配色。
// 替代系统原生通知(原生样式跟随 Windows 主题,深色模式下发黑、不可控)。
"use strict";
const { makeLogger } = require("./logger");
const logger = makeLogger("notify-toast");
const W = 380, H = 132, GAP = 10, MARGIN = 16, TTL = 9000;
let lastBottom = null;
function levelColor(level) {
return { info: "#2f5cf0", warning: "#d97706", important: "#d6453d" }[level] || "#2f5cf0";
}
function levelLabel(level) {
return { info: "通知", warning: "提醒", important: "重要" }[level] || "通知";
}
function esc(s) {
return String(s || "")
.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}
function buildHTML(title, body, level) {
const color = levelColor(level);
const label = levelLabel(level);
return `<!doctype html><html><head><meta charset="utf-8"><style>
* { margin:0; padding:0; box-sizing:border-box; font-family:"Segoe UI","Microsoft YaHei",sans-serif; }
html,body { background:transparent; }
.toast {
position:fixed; inset:0; border-radius:14px; overflow:hidden;
background:#fff; border:1px solid #e6eaf2;
box-shadow:0 12px 32px -14px rgba(20,26,40,.45);
display:flex; flex-direction:column;
animation: in .28s ease;
}
@keyframes in { from { transform:translateY(14px); opacity:0 } to { transform:none; opacity:1 } }
.bar { padding:9px 12px 2px; display:flex; align-items:center; gap:8px; }
.logo { width:20px;height:20px;border-radius:6px;background:linear-gradient(135deg,#2f5cf0,#0f9f9b);
color:#fff;font-size:11px;font-weight:800;display:flex;align-items:center;justify-content:center; }
.lvl { font-size:11px;font-weight:700;color:${color};letter-spacing:.3px; }
.close { margin-left:auto;cursor:pointer;color:#9aa2b4;font-size:14px;line-height:1;padding:2px 5px; }
.close:hover { color:#4a5468; }
.ttl { padding:0 14px; font-size:14px;font-weight:700;color:#232a3a; }
.bdy { padding:4px 14px 12px; font-size:12.5px;color:#3b4358;line-height:1.45;
overflow:hidden; display:-webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; }
</style></head><body>
<div class="toast">
<div class="bar">
<span class="logo">37</span>
<span class="lvl">${label}</span>
<span class="close" onclick="window.close()">✕</span>
</div>
<div class="ttl">${esc(title)}</div>
<div class="bdy">${esc(body)}</div>
</div>
</body></html>`;
}
function show(title, body, level) {
try {
const { BrowserWindow, screen } = require("electron");
const display = screen.getPrimaryDisplay();
const wa = display.workArea;
const x = wa.x + wa.width - W - MARGIN;
if (lastBottom === null) lastBottom = wa.y + wa.height - H - MARGIN;
const y = lastBottom;
lastBottom = lastBottom - H - GAP;
if (lastBottom < wa.y + MARGIN) lastBottom = null; // 满屏后下一轮回到底部
const win = new BrowserWindow({
width: W, height: H, x, y,
frame: false, transparent: true, resizable: false, movable: false,
alwaysOnTop: true, skipTaskbar: true, hasShadow: false, show: false, focusable: false,
backgroundColor: "#00000000",
webPreferences: { contextIsolation: true, nodeIntegration: false, sandbox: true },
});
win.setAlwaysOnTop(true, "pop-up-menu");
win.loadURL("data:text/html;charset=utf-8," + encodeURIComponent(buildHTML(title, body, level)));
win.once("ready-to-show", () => {
try { win.showInactive(); } catch (e) { win.show(); }
});
const t = setTimeout(() => { try { win.destroy(); } catch (e) { /* 忽略 */ } }, TTL);
win.on("closed", () => { clearTimeout(t); });
return true;
} catch (e) {
logger.warning(`自定义通知弹窗失败:${e.message}`);
return false;
}
}
module.exports = { show };