feat:右上角接入授权中心——常驻版本徽章+公告铃铛+新版本横幅;server 加 /api/hub/*(安装统计/心跳/公告/更新检查,机器码存 hub.json);KEY 经 restart.sh 动态注入;/api/web-site 名称同步米图设计
NodeJS with Webpack / build (push) Canceled after 0s

This commit is contained in:
personal827 committed 2026-10-28 14:12:31 +08:00
1 parent 759f8a6232
commit fdd7c2f639
6 files changed
+385 -3

No files matched your search

+128 -2
View File
@@ -10,6 +10,112 @@ const INDEX = JSON.parse(fs.readFileSync(path.join(DATA, 'index.json'), 'utf8'))
// 封面 AI 助手(AI 生图/修图服务),素材面板从这里取图
const COVER_AI = process.env.COVER_AI || 'http://192.168.68.19:8128';
/* ---------------- 授权中心(CardAuth:安装统计 / 在线数 / 公告 / 版本推送) ----------------
* 失败开放:授权中心挂了 → 版本照常显示、公告保留上次、更新提示消失,功能零影响。
* 机器码只存 DATA/hub.json,不进浏览器;关上报:HUB_DISABLED=1。
* 节流:更新检查 6h(失败 30min 重试)、心跳 30min、公告 60s。 */
const APP_VERSION = process.env.VERSION || '1.0.0';
const HUB_HOST = (process.env.HUB_HOST || 'https://auth.88531.cn').replace(/\/+$/, '');
const HUB_PROJECT_ID = (process.env.HUB_PROJECT_ID || '').trim();
const HUB_API_KEY = (process.env.HUB_API_KEY || '').trim();
const HUB_DISABLED = process.env.HUB_DISABLED === '1';
const HUB_STATE = path.join(DATA, 'hub.json');
function hubState() {
try { return JSON.parse(fs.readFileSync(HUB_STATE, 'utf8')) || {}; } catch { return {}; }
}
function saveHubState(patch) {
const s = Object.assign(hubState(), patch);
try { fs.writeFileSync(HUB_STATE, JSON.stringify(s)); } catch { /* 只读数据卷时静默 */ }
return s;
}
function machineId() {
const s = hubState();
if (!/^\d{10,16}$/.test(String(s.machine_id || ''))) {
const id = String(Math.floor(1e12 + Math.random() * 8e12)).slice(0, 13);
saveHubState({ machine_id: id });
return id;
}
return String(s.machine_id);
}
function hubReady() { return !HUB_DISABLED && !!HUB_PROJECT_ID && !!HUB_API_KEY; }
function hubReq(url, { method = 'GET', headers = {}, body } = {}) {
return new Promise((resolve) => {
const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), 8000);
fetch(url, { method, headers, body, signal: ctrl.signal })
.then(async (r) => {
clearTimeout(t);
let d = null;
try { d = await r.json(); } catch { /* 非 JSON */ }
resolve({ status: r.status, data: d });
})
.catch((e) => { clearTimeout(t); resolve({ status: 0, error: String(e.message || e) }); });
});
}
async function hubCheckUpdate(force = false) {
if (!hubReady()) return null;
const s = hubState();
const now = Date.now();
const gap = s.check_failed ? 30 * 60e3 : 6 * 3600e3;
if (!force && s.checked_at && now - s.checked_at < gap) return s.last_update || null;
const r = await hubReq(`${HUB_HOST}/api/public/installations/check-update`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Api-Key': HUB_API_KEY, 'X-Project-Id': HUB_PROJECT_ID },
body: JSON.stringify({ machine_id: machineId(), client_version: APP_VERSION, device_info: 'kuaitu-editor' }),
});
const d = r.data;
if (r.status !== 200 || !d || d.code !== 200 || !d.data) {
saveHubState({ checked_at: now, check_failed: true });
return s.last_update || null;
}
const u = {
install_id: d.data.install_id ?? null,
latest_version: String(d.data.latest_version || ''),
update_available: !!d.data.update_available,
update_required: !!d.data.update_required || !!d.data.is_force,
changelog: String(d.data.changelog || ''),
download_url: String(d.data.download_url || ''),
checked_at: now,
};
saveHubState({ checked_at: now, check_failed: false, last_update: u });
return u;
}
async function hubHeartbeat(force = false) {
if (!hubReady()) return { ok: false, skipped: true };
const s = hubState();
const now = Date.now();
if (!force && s.beat_at && now - s.beat_at < 30 * 60e3) return { ok: true, throttled: true };
const r = await hubReq(`${HUB_HOST}/api/public/installations/heartbeat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Api-Key': HUB_API_KEY, 'X-Project-Id': HUB_PROJECT_ID },
body: JSON.stringify({ machine_id: machineId(), client_version: APP_VERSION, device_info: 'kuaitu-editor' }),
});
const ok = r.status === 200 && !!r.data && r.data.code === 200;
if (ok) saveHubState({ beat_at: now, online_at: now });
return { ok };
}
async function hubAnnouncements(force = false) {
if (HUB_DISABLED || !HUB_PROJECT_ID) return { list: [], popup: [] };
const s = hubState();
const now = Date.now();
if (!force && s.ann_at && now - s.ann_at < 60e3) return s.ann || { list: [], popup: [] };
const r = await hubReq(`${HUB_HOST}/api/public/announcements?target=client&project_id=${encodeURIComponent(HUB_PROJECT_ID)}&limit=20`);
if (r.status !== 200 || !r.data || r.data.code !== 200) return s.ann || { list: [], popup: [] };
const d = r.data.data || r.data;
const ann = {
list: Array.isArray(d.list) ? d.list : [],
popup: Array.isArray(d.popup) ? d.popup : [],
at: now,
};
saveHubState({ ann_at: now, ann });
return ann;
}
const TYPES = [
{ id: 20, name: '公众号封面' },
{ id: 21, name: '小红书' },
@@ -88,16 +194,36 @@ function loadIndex() {
return indexHtml;
}
const server = http.createServer((req, res) => {
const server = http.createServer(async (req, res) => {
const u = new URL(req.url, 'http://localhost');
const p = u.pathname;
const q = u.searchParams;
// ---------- 授权中心(版本 / 更新提示 / 公告,前端角标与铃铛读这里) ----------
if (p === '/api/hub/status') {
const [up, ann] = await Promise.all([hubCheckUpdate(), hubAnnouncements()]);
return send(res, 200, {
ok: true,
version: APP_VERSION,
hub: HUB_DISABLED ? 'off' : (hubReady() ? 'on' : 'unconfigured'),
update: up || { update_available: false, latest_version: '', changelog: '', download_url: '' },
announcements: (ann && ann.list) || [],
popup: (ann && ann.popup) || [],
});
}
if (p === '/api/hub/heartbeat' && req.method === 'POST') {
return send(res, 200, await hubHeartbeat());
}
if (p === '/api/hub/refresh' && req.method === 'POST') {
const [up, ann, beat] = await Promise.all([hubCheckUpdate(true), hubAnnouncements(true), hubHeartbeat(true)]);
return send(res, 200, { ok: true, version: APP_VERSION, update: up, announcements: (ann && ann.list) || [], heartbeat: beat });
}
// ---------- API ----------
if (p === '/api/templ-types') return send(res, 200, { data: TYPES.map(t => ({ id: t.id, attributes: { name: t.name } })) });
if (p === '/api/web-site') {
return send(res, 200, { data: { id: 1, attributes: {
name: '快图设计 · 封面工坊', url: '/', logo: { data: { attributes: { url: '/logo.svg' } } },
name: '米图设计 · 封面工坊', url: '/', logo: { data: { attributes: { url: '/logo.svg' } } },
} } });
}