feat(license): 接入授权平台客户端门禁(license-v2/lease-v2)并烧入交付版信任根

This commit is contained in:
lianjianwei2024
2026-10-10 18:05:04 +08:00
parent 6de1370ce5
commit d3a805ede0
25 changed files with 3123 additions and 25 deletions

View File

@@ -1710,3 +1710,177 @@ function toast(msg, kind) {
setTimeout(() => d.remove(), 300);
}, 3000);
}
/* ================= 统一设置窗口 + 授权到期预警(2026-10-10 决策 31)=================
两页各挂一行:workbench licMount('.tabright') / admin licMount('.tabs')。
- licDlg() 统一「设置」弹框(本版先放「授权」一节:状态卡/立即刷新/重新激活;
后续新设置项往这里加,不再各自开窗)
- licBannerTick 授权剩余 ≤14 天时顶栏出红字 chip,点击开设置弹框(10 分钟一查)
接口三口在门禁层永放行:GET /api/license/status、POST /api/license/refresh、
POST /api/license/activate —— 未激活状态下弹框也能用(但未激活时整页已是授权页)。
样式作用域全部收在 #licMask / .lic-* 下,不碰页面既有 .dlg 样式。 */
let _licPoll = null;
function licEnsureStyle() {
if (document.getElementById('licStyle')) return;
const st = document.createElement('style');
st.id = 'licStyle';
st.textContent = '' +
'#licMask .dlg.lic{width:540px;max-width:94vw;background:#fff;border-radius:10px;\n' +
' box-shadow:0 10px 40px rgba(16,35,61,.18)}\n' +
'#licMask .dh{display:flex;justify-content:space-between;align-items:center;\n' +
' background:#1f4e79;color:#fff;padding:12px 18px;font-size:15px;font-weight:600;\n' +
' border-radius:10px 10px 0 0;user-select:none}\n' +
'#licMask .dh .close{color:#fff;cursor:pointer;font-size:20px;line-height:1;padding:0 4px}\n' +
'.lic-bd{padding:16px 18px 18px;font-size:13px;color:#1c2733}\n' +
'.lic-st{border:1px solid #e3e8ef;background:#fafbfd;border-radius:8px;\n' +
' padding:10px 14px;line-height:2.05}\n' +
'.lic-st b.ok{color:#1c7c44}.lic-st b.bad{color:#b02a2a}\n' +
'.lic-btns{display:flex;gap:10px;margin-top:12px}\n' +
'.lic-row{margin-top:10px}\n' +
'.lic-row label{display:block;color:#51606f;margin-bottom:4px;font-size:12.5px}\n' +
'.lic-row input{width:100%;padding:8px 10px;border:1px solid #c8d2dd;border-radius:6px;\n' +
' font-size:13px;box-sizing:border-box;font-family:inherit}\n' +
'.lic-tip{margin-top:12px;color:#8a97a5;font-size:12px;line-height:1.7}\n' +
'.btn.lic-warn{color:#b02a2a;border-color:#e4b6b6;background:#fdf3f3;font-weight:600}';
document.head.appendChild(st);
}
function licMount(sel) {
const host = document.querySelector(sel);
if (!host || document.getElementById('licWarn')) return;
licEnsureStyle();
const warn = document.createElement('button');
warn.id = 'licWarn'; warn.className = 'btn lic-warn';
warn.style.display = 'none'; warn.onclick = licDlg; warn.title = '点击查看授权详情';
const btn = document.createElement('button');
btn.className = 'btn'; btn.textContent = '⚙ 设置';
btn.title = '授权状态 / 激活 / 续费提示(后续设置项统一收这里)';
btn.onclick = licDlg;
host.appendChild(warn); host.appendChild(btn);
licBannerTick();
setInterval(licBannerTick, 10 * 60 * 1000);
}
function licBannerTick() {
const warn = document.getElementById('licWarn');
if (!warn) return;
fetch('/api/license/status').then(r => r.json()).then(j => {
const d = j.data || {};
let show = false, txt = '';
if (d.enforce && d.can_run) {
if (d.days_left != null && d.days_left <= 14) {
show = true;
txt = d.days_left <= 0 ? '⚠ 授权即将到期,请立即续费'
: '⚠ 授权剩 ' + d.days_left + ' 天,请及时续费';
} else if (d.lease_state && d.lease_state !== 'active') {
show = true; txt = '⚠ 授权状态异常(' + esc(d.lease_state) + ')';
}
}
warn.textContent = txt;
warn.style.display = show ? '' : 'none';
}).catch(() => {});
}
function licDlg() {
licEnsureStyle();
let mask = document.getElementById('licMask');
if (!mask) {
mask = document.createElement('div');
mask.id = 'licMask'; mask.className = 'mask';
mask.innerHTML =
'<div class="dlg lic"><div class="dh"><span>设置 · 授权</span>' +
'<a class="close" onclick="licClose()">×</a></div>' +
'<div class="db lic-bd">' +
'<div id="licSt" class="lic-st">加载中…</div>' +
'<div class="lic-btns">' +
'<button class="btn" id="licRf" onclick="licRefresh()">立即刷新授权(联网续租)</button>' +
'<button class="btn" id="licFb" onclick="licToggleForm()">重新激活 / 更换凭据</button></div>' +
'<div id="licForm" style="display:none">' +
'<div class="lic-row"><label>实例 ID(instance_id)</label>' +
'<input id="licIid" spellcheck="false" placeholder="形如 11111111-2222-…"></div>' +
'<div class="lic-row"><label>授权码(license_key)</label>' +
'<input id="licLk" spellcheck="false"></div>' +
'<div class="lic-row"><label>激活密钥(secret,一次性交付,本机加密保存)</label>' +
'<input id="licSec" type="password"></div>' +
'<div class="lic-btns"><button class="btn pri" id="licGo" onclick="licActivate()">激活</button></div>' +
'</div>' +
'<div class="lic-tip">续费由软件提供方在授权平台操作,在线状态下一分钟内自动生效,无需重装;' +
'「立即刷新授权」可手动联网续租。激活密钥只保存在本机(Windows 加密存储),不会上传到别处。</div>' +
'</div></div>';
document.body.appendChild(mask);
mask.addEventListener('click', e => { if (e.target === mask) licClose(); });
}
dialogsDraggable(); // 新插入的弹框补挂拖动/钉位(幂等)
mask.classList.add('on');
licLoad();
clearInterval(_licPoll);
_licPoll = setInterval(licLoad, 15000);
}
function licClose() {
const m = document.getElementById('licMask');
if (m) m.classList.remove('on');
clearInterval(_licPoll);
}
function licLoad() {
fetch('/api/license/status').then(r => r.json())
.then(j => licRender(j.data || {})).catch(() => licRender(null));
}
function licRender(d) {
const el = document.getElementById('licSt');
if (!el) return;
if (!d) { el.innerHTML = '<b class="bad">无法读取授权状态(本机服务异常?)</b>'; return; }
let h = '';
if (!d.enforce) h += '模式:开发模式(enforce=false,不强制校验)<br>';
h += '状态:' + (d.can_run ? '<b class="ok">已授权,运行中</b>'
: '<b class="bad">' + esc(d.reason || '不可用') + '</b>') + '<br>';
if (d.instance_id) h += '实例:' + esc(d.instance_id) + '<br>';
if (d.product_id != null) h += '产品 ID:' + esc(d.product_id) + '<br>';
if (d.entitlement_expires_at) {
h += '授权到期:' + esc(d.entitlement_expires_at);
if (d.days_left != null) h += '(剩 ' + d.days_left + ' 天)';
h += '<br>';
}
if (d.lease_expires_at) h += '租约到期:' + esc(d.lease_expires_at) + '<br>';
if (d.last_heartbeat_at) {
h += '最后心跳:' + esc(d.last_heartbeat_at);
if (d.heartbeat_running === false) h += ' <b class="bad">(自动心跳未运行,可点「立即刷新授权」恢复)</b>';
h += '<br>';
}
if (!d.keyring_ready) h += '<b class="bad">⚠ 信任根未配置(pinned_keys 为空),激活/续租都会失败——请联系软件提供方。</b><br>';
if (d.last_error) h += '最近错误:' + esc(d.last_error);
el.innerHTML = h;
}
function licRefresh() {
const b = document.getElementById('licRf');
b.disabled = true;
fetch('/api/license/refresh', { method: 'POST' }).then(r => r.json()).then(j => {
b.disabled = false;
const res = (j.data || {}).result || {};
toast(res.ok ? '授权已刷新' : ('刷新失败:' + (res.message || '未知错误')),
res.ok ? '' : 'err');
licLoad();
}).catch(() => { b.disabled = false; toast('网络错误:无法连接本机服务', 'err'); });
}
function licToggleForm() {
const f = document.getElementById('licForm');
f.style.display = f.style.display === 'none' ? '' : 'none';
}
function licActivate() {
const b = { instance_id: document.getElementById('licIid').value.trim(),
license_key: document.getElementById('licLk').value.trim(),
secret: document.getElementById('licSec').value };
if (!b.instance_id || !b.license_key || !b.secret) { toast('三项都要填写(见交付单)', 'warn'); return; }
const go = document.getElementById('licGo');
go.disabled = true;
fetch('/api/license/activate', { method: 'POST',
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(b) })
.then(r => r.json()).then(j => {
go.disabled = false;
const d = j.data || {}, res = d.result || {};
if (res.ok) {
document.getElementById('licSec').value = '';
document.getElementById('licForm').style.display = 'none';
toast('激活成功', '');
} else {
toast(res.message || '激活失败,请核对凭据', 'err');
}
licLoad();
}).catch(() => { go.disabled = false; toast('网络错误:无法连接本机服务', 'err'); });
}