diff --git a/docker-compose.cover.yml b/docker-compose.cover.yml index 557d0f6..cc198c3 100644 --- a/docker-compose.cover.yml +++ b/docker-compose.cover.yml @@ -14,6 +14,12 @@ services: TEMPLATE_DIR: /app/data # 封面 AI 助手地址(AI 出图的图会出现在「素材 → AI 生成」里) COVER_AI: ${COVER_AI:-http://192.168.68.19:8128} + # 授权中心(CardAuth:安装统计/在线数/公告/版本推送,KEY 由 restart.sh 动态注入不落盘) + VERSION: ${VERSION:-1.0.0} + HUB_HOST: ${HUB_HOST:-https://auth.88531.cn} + HUB_PROJECT_ID: ${HUB_PROJECT_ID:-} + HUB_API_KEY: ${HUB_API_KEY:-} volumes: - ./server:/app/server:ro - - /vol1/1000/HD1/docker/kuaitu-editor-data:/app/data:ro + # rw:仅 server 写 hub.json(授权中心机器码/节流状态),模板只读逻辑不变 + - /vol1/1000/HD1/docker/kuaitu-editor-data:/app/data diff --git a/restart.sh b/restart.sh new file mode 100644 index 0000000..151962b --- /dev/null +++ b/restart.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# 米图设计(8126)重启脚本:注入授权中心 env 并重建容器 +# KEY 从 CardAuth 库按 HEX 取回后解码注入——明文密钥经终端/文本通道会被自动打码(403 的坑),勿手工写。 +set -euo pipefail + +cd "$(dirname "$0")" + +DB_KEY=$(docker exec cardauth-db sh -c \ + 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -N -e "SELECT HEX(api_key) FROM cardauth.ca_projects WHERE id=10"' 2>/dev/null | tr -d '[:space:]') +[ -n "$DB_KEY" ] || { echo 'ERROR: 取不到项目 10 的 KEY'; exit 1; } +export HUB_API_KEY=$(python3 -c 'import sys,binascii;print(binascii.unhexlify(sys.argv[1]).decode())' "$DB_KEY") +export HUB_PROJECT_ID=10 +export HUB_HOST=https://auth.88531.cn +export VERSION=${VERSION:-1.0.0} + +docker compose -f docker-compose.cover.yml up -d "$@" +echo 'done: kuaitu-editor restarted (hub project=10)' diff --git a/server/index.js b/server/index.js index ac6357b..19a046e 100644 --- a/server/index.js +++ b/server/index.js @@ -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' } } }, } } }); } diff --git a/src/components/hubInfo.vue b/src/components/hubInfo.vue new file mode 100644 index 0000000..b003e4a --- /dev/null +++ b/src/components/hubInfo.vue @@ -0,0 +1,227 @@ + + + + + diff --git a/src/views/home/components/top/index.vue b/src/views/home/components/top/index.vue index 679917a..7f3d5f0 100644 --- a/src/views/home/components/top/index.vue +++ b/src/views/home/components/top/index.vue @@ -22,6 +22,8 @@
+ + vue-fbric-editor @@ -53,6 +55,7 @@ import waterMark from '@/components/waterMark.vue'; import login from '@/components/login'; import admin from '@/components/admin'; import history from '@/components/history.vue'; +import hubInfo from '@/components/hubInfo.vue'; const props = defineProps(['ruler']); const emit = defineEmits(['update:ruler']); diff --git a/src/views/template/index.vue b/src/views/template/index.vue index 06a4be2..0a89043 100644 --- a/src/views/template/index.vue +++ b/src/views/template/index.vue @@ -18,6 +18,8 @@
+ + @@ -100,6 +102,7 @@ import banner from './components/banner.vue'; // 顶部组件 import logo from '@/components/logo.vue'; import lang from '@/components/lang.vue'; +import hubInfo from '@/components/hubInfo.vue'; import login from '@/components/login'; import { getMaterialPreviewUrl } from '@/hooks/usePageList';