Changes before error encountered
Co-authored-by: yhlhoh <205733785+yhlhoh@users.noreply.github.com>
This commit is contained in:
1 parent
3563676eed
commit
a845707585
13 files changed
+3588
-416
No files matched your search
@@ -7,15 +7,25 @@ const app = express();
|
||||
const server = http.createServer(app);
|
||||
const wss = new WebSocket.Server({ server });
|
||||
|
||||
// In-memory preset storage:包含一个示例预置晚会节目样式
|
||||
// Global settings
|
||||
let settings = {
|
||||
autoHideDuration: 0 // 0 = disabled, >0 = ms to auto-hide
|
||||
};
|
||||
|
||||
// Appearance presets: define the visual template with {{placeholder}} support
|
||||
let presets = [
|
||||
{
|
||||
id: 'preset-gala-iframe-1',
|
||||
name: '晚会右侧信息区 · 自定义 HTML 示例',
|
||||
name: '晚会右侧信息区 · 模板示例',
|
||||
kind: 'iframe',
|
||||
align: 'right',
|
||||
themeColor: '',
|
||||
extraCss: '',
|
||||
fieldDefs: [
|
||||
{ key: 'tag', label: '标签', defaultValue: 'LIVE · GALA' },
|
||||
{ key: 'mainTitle', label: '主标题', defaultValue: '2026 新春特别晚会' },
|
||||
{ key: 'subtitle', label: '副标题', defaultValue: '示例:你可以在这里完全自定义 HTML / CSS / JS' }
|
||||
],
|
||||
html: `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
@@ -62,33 +72,75 @@ let presets = [
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<div class="tag">LIVE · GALA</div>
|
||||
<div class="title">2026 新春特别晚会</div>
|
||||
<div class="subtitle">示例:你可以在这里完全自定义 HTML / CSS / JS</div>
|
||||
<div class="tag">{{tag}}</div>
|
||||
<div class="title">{{mainTitle}}</div>
|
||||
<div class="subtitle">{{subtitle}}</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>`
|
||||
}
|
||||
];
|
||||
|
||||
// Content presets: define field values to fill into appearance preset templates
|
||||
let contentPresets = [
|
||||
{
|
||||
id: 'content-preset-1',
|
||||
name: '春晚主持人介绍',
|
||||
appearancePresetId: 'preset-gala-iframe-1',
|
||||
fields: {
|
||||
tag: 'LIVE · GALA',
|
||||
mainTitle: '2026 新春特别晚会',
|
||||
subtitle: '主持人:某某某 · 某某某'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'content-preset-2',
|
||||
name: '节目单第一项',
|
||||
appearancePresetId: 'preset-gala-iframe-1',
|
||||
fields: {
|
||||
tag: '节目单 01',
|
||||
mainTitle: '《欢乐中国年》',
|
||||
subtitle: '表演:XX歌舞团'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
let activePresetId = null;
|
||||
let activeContentPresetId = null;
|
||||
let activeFieldOverrides = {};
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
// REST API for presets
|
||||
// ── Settings API ─────────────────────────────────────────────────────────────
|
||||
|
||||
app.get('/api/settings', (req, res) => {
|
||||
res.json(settings);
|
||||
});
|
||||
|
||||
app.put('/api/settings', (req, res) => {
|
||||
const { autoHideDuration } = req.body || {};
|
||||
if (typeof autoHideDuration === 'number') {
|
||||
settings.autoHideDuration = Math.max(0, autoHideDuration);
|
||||
}
|
||||
res.json(settings);
|
||||
});
|
||||
|
||||
// ── Appearance Presets API ────────────────────────────────────────────────────
|
||||
|
||||
app.get('/api/presets', (req, res) => {
|
||||
res.json({ presets, activePresetId });
|
||||
res.json({ presets, contentPresets, activePresetId, activeContentPresetId, settings });
|
||||
});
|
||||
|
||||
app.post('/api/presets', (req, res) => {
|
||||
// 外观 preset:允许自定义 kind/html 等
|
||||
const {
|
||||
name = '未命名外观',
|
||||
kind = 'iframe',
|
||||
html = '',
|
||||
themeColor = '',
|
||||
align = 'right',
|
||||
extraCss = ''
|
||||
extraCss = '',
|
||||
fieldDefs = []
|
||||
} = req.body || {};
|
||||
const preset = {
|
||||
id: Date.now().toString(),
|
||||
@@ -97,7 +149,8 @@ app.post('/api/presets', (req, res) => {
|
||||
html,
|
||||
themeColor,
|
||||
align,
|
||||
extraCss
|
||||
extraCss,
|
||||
fieldDefs
|
||||
};
|
||||
presets.push(preset);
|
||||
res.json(preset);
|
||||
@@ -113,9 +166,10 @@ app.put('/api/presets/:id', (req, res) => {
|
||||
html = presets[idx].html,
|
||||
themeColor = presets[idx].themeColor,
|
||||
align = presets[idx].align,
|
||||
extraCss = presets[idx].extraCss
|
||||
extraCss = presets[idx].extraCss,
|
||||
fieldDefs = presets[idx].fieldDefs || []
|
||||
} = req.body || {};
|
||||
presets[idx] = { ...presets[idx], name, kind, html, themeColor, align, extraCss };
|
||||
presets[idx] = { ...presets[idx], name, kind, html, themeColor, align, extraCss, fieldDefs };
|
||||
res.json(presets[idx]);
|
||||
});
|
||||
|
||||
@@ -129,30 +183,132 @@ app.delete('/api/presets/:id', (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Activate preset
|
||||
// ── Content Presets API ───────────────────────────────────────────────────────
|
||||
|
||||
app.get('/api/content-presets', (req, res) => {
|
||||
res.json(contentPresets);
|
||||
});
|
||||
|
||||
app.post('/api/content-presets', (req, res) => {
|
||||
const {
|
||||
name = '未命名内容',
|
||||
appearancePresetId = '',
|
||||
fields = {}
|
||||
} = req.body || {};
|
||||
const cp = {
|
||||
id: Date.now().toString(),
|
||||
name,
|
||||
appearancePresetId,
|
||||
fields
|
||||
};
|
||||
contentPresets.push(cp);
|
||||
res.json(cp);
|
||||
});
|
||||
|
||||
app.put('/api/content-presets/:id', (req, res) => {
|
||||
const { id } = req.params;
|
||||
const idx = contentPresets.findIndex(c => c.id === id);
|
||||
if (idx === -1) return res.status(404).json({ error: 'Not found' });
|
||||
const {
|
||||
name = contentPresets[idx].name,
|
||||
appearancePresetId = contentPresets[idx].appearancePresetId,
|
||||
fields = contentPresets[idx].fields
|
||||
} = req.body || {};
|
||||
contentPresets[idx] = { ...contentPresets[idx], name, appearancePresetId, fields };
|
||||
res.json(contentPresets[idx]);
|
||||
});
|
||||
|
||||
app.delete('/api/content-presets/:id', (req, res) => {
|
||||
const { id } = req.params;
|
||||
contentPresets = contentPresets.filter(c => c.id !== id);
|
||||
if (activeContentPresetId === id) {
|
||||
activeContentPresetId = null;
|
||||
broadcastActive();
|
||||
}
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// ── Activate ──────────────────────────────────────────────────────────────────
|
||||
|
||||
// Activate by content preset id
|
||||
app.post('/api/content-presets/:id/activate', (req, res) => {
|
||||
const { id } = req.params;
|
||||
const cp = contentPresets.find(c => c.id === id);
|
||||
if (!cp) return res.status(404).json({ error: 'Not found' });
|
||||
|
||||
activeContentPresetId = id;
|
||||
activePresetId = cp.appearancePresetId;
|
||||
activeFieldOverrides = req.body?.fieldOverrides || {};
|
||||
|
||||
const bodyDuration = req.body && typeof req.body.autoHideDuration === 'number'
|
||||
? req.body.autoHideDuration
|
||||
: null;
|
||||
broadcastActive(bodyDuration !== null ? bodyDuration : settings.autoHideDuration);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Activate by appearance preset id (legacy + direct)
|
||||
app.post('/api/presets/:id/activate', (req, res) => {
|
||||
const { id } = req.params;
|
||||
const preset = presets.find(p => p.id === id);
|
||||
if (!preset) return res.status(404).json({ error: 'Not found' });
|
||||
|
||||
activePresetId = id;
|
||||
broadcastActive();
|
||||
activeContentPresetId = null;
|
||||
activeFieldOverrides = req.body?.fieldOverrides || {};
|
||||
|
||||
const bodyDuration = req.body && typeof req.body.autoHideDuration === 'number'
|
||||
? req.body.autoHideDuration
|
||||
: null;
|
||||
broadcastActive(bodyDuration !== null ? bodyDuration : settings.autoHideDuration);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// Clear active
|
||||
app.post('/api/clear', (req, res) => {
|
||||
activePresetId = null;
|
||||
broadcastActive();
|
||||
activeContentPresetId = null;
|
||||
activeFieldOverrides = {};
|
||||
broadcastActive(0);
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
function getActivePreset() {
|
||||
if (!activePresetId) return null;
|
||||
return presets.find(p => p.id === activePresetId) || null;
|
||||
}
|
||||
|
||||
function broadcastActive() {
|
||||
const msg = JSON.stringify({ type: 'activePreset', preset: getActivePreset() });
|
||||
function getContentData() {
|
||||
let fields = {};
|
||||
// Start with appearance preset defaults
|
||||
const ap = getActivePreset();
|
||||
if (ap && ap.fieldDefs) {
|
||||
for (const fd of ap.fieldDefs) {
|
||||
fields[fd.key] = fd.defaultValue || '';
|
||||
}
|
||||
}
|
||||
// Override with active content preset fields
|
||||
if (activeContentPresetId) {
|
||||
const cp = contentPresets.find(c => c.id === activeContentPresetId);
|
||||
if (cp) Object.assign(fields, cp.fields);
|
||||
}
|
||||
// Override with per-activation overrides
|
||||
Object.assign(fields, activeFieldOverrides);
|
||||
return fields;
|
||||
}
|
||||
|
||||
function broadcastActive(autoHideDuration) {
|
||||
const preset = getActivePreset();
|
||||
const contentData = getContentData();
|
||||
const hideDuration = typeof autoHideDuration === 'number' ? autoHideDuration : settings.autoHideDuration;
|
||||
const msg = JSON.stringify({
|
||||
type: 'activePreset',
|
||||
preset,
|
||||
contentData,
|
||||
autoHideDuration: hideDuration
|
||||
});
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(msg);
|
||||
@@ -162,11 +318,16 @@ function broadcastActive() {
|
||||
|
||||
wss.on('connection', ws => {
|
||||
// send current active on connect
|
||||
ws.send(JSON.stringify({ type: 'activePreset', preset: getActivePreset() }));
|
||||
ws.send(JSON.stringify({
|
||||
type: 'activePreset',
|
||||
preset: getActivePreset(),
|
||||
contentData: getContentData(),
|
||||
autoHideDuration: settings.autoHideDuration
|
||||
}));
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 30001;
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Server listening on http://localhost:${PORT}`);
|
||||
console.log('Admin UI: /admin.html, Widget: /widget.html');
|
||||
console.log('Admin UI: /admin.html, Management: /manage.html, Widget: /widget.html');
|
||||
});
|
||||
Reference in new issue
Block a user