940 lines
24 KiB
HTML
940 lines
24 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>PC装机配置表</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
|
|
background: #f0f2f5;
|
|
color: #333;
|
|
min-height: 100vh;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1100px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
}
|
|
|
|
/* Header */
|
|
.header {
|
|
background: #fff;
|
|
border-radius: 12px;
|
|
padding: 20px 24px;
|
|
margin-bottom: 20px;
|
|
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
flex-wrap: wrap;
|
|
gap: 12px;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 22px;
|
|
font-weight: 600;
|
|
color: #1a1a1a;
|
|
}
|
|
|
|
.header-actions {
|
|
display: flex;
|
|
gap: 8px;
|
|
align-items: center;
|
|
}
|
|
|
|
.btn {
|
|
padding: 8px 16px;
|
|
border: none;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.btn-primary { background: #1677ff; color: #fff; }
|
|
.btn-primary:hover { background: #4096ff; }
|
|
.btn-default { background: #fff; color: #333; border: 1px solid #d9d9d9; }
|
|
.btn-default:hover { border-color: #1677ff; color: #1677ff; }
|
|
.btn-danger { background: #ff4d4f; color: #fff; }
|
|
.btn-danger:hover { background: #ff7875; }
|
|
.btn-sm { padding: 4px 10px; font-size: 12px; }
|
|
|
|
.mode-toggle {
|
|
background: #f0f0f0;
|
|
border-radius: 8px;
|
|
padding: 3px;
|
|
display: flex;
|
|
}
|
|
|
|
.mode-toggle button {
|
|
padding: 6px 14px;
|
|
border: none;
|
|
border-radius: 6px;
|
|
font-size: 13px;
|
|
cursor: pointer;
|
|
background: transparent;
|
|
color: #666;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.mode-toggle button.active {
|
|
background: #fff;
|
|
color: #1677ff;
|
|
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
|
}
|
|
|
|
/* Table */
|
|
.config-table-wrap {
|
|
background: #fff;
|
|
border-radius: 12px;
|
|
box-shadow: 0 2px 8px rgba(0,0,0,0.06);
|
|
overflow: hidden;
|
|
margin-bottom: 80px;
|
|
}
|
|
|
|
.config-table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.config-table thead {
|
|
background: #fafafa;
|
|
}
|
|
|
|
.config-table th {
|
|
padding: 12px 16px;
|
|
text-align: left;
|
|
font-weight: 600;
|
|
color: #666;
|
|
font-size: 13px;
|
|
border-bottom: 1px solid #f0f0f0;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.config-table td {
|
|
padding: 12px 16px;
|
|
border-bottom: 1px solid #f0f0f0;
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.config-table tbody tr:last-child td {
|
|
border-bottom: none;
|
|
}
|
|
|
|
.config-table tbody tr:hover {
|
|
background: #fafafa;
|
|
}
|
|
|
|
/* Image cell */
|
|
.cell-img {
|
|
width: 64px;
|
|
height: 64px;
|
|
border-radius: 8px;
|
|
object-fit: contain;
|
|
background: #f5f5f5;
|
|
border: 1px solid #eee;
|
|
}
|
|
|
|
.cell-img-placeholder {
|
|
width: 64px;
|
|
height: 64px;
|
|
border-radius: 8px;
|
|
background: #f5f5f5;
|
|
border: 1px solid #eee;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: #ccc;
|
|
font-size: 20px;
|
|
}
|
|
|
|
/* Type badge */
|
|
.type-badge {
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
color: #1677ff;
|
|
background: #e6f4ff;
|
|
padding: 4px 10px;
|
|
border-radius: 6px;
|
|
display: inline-block;
|
|
}
|
|
|
|
/* Price cell */
|
|
.price-cell {
|
|
font-weight: 700;
|
|
color: #ff4d4f;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.price-cell .currency {
|
|
font-size: 12px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
/* Link cell */
|
|
.link-cell a {
|
|
color: #1677ff;
|
|
text-decoration: none;
|
|
font-size: 13px;
|
|
}
|
|
.link-cell a:hover { text-decoration: underline; }
|
|
|
|
/* Alt selector */
|
|
.alt-selector {
|
|
margin-top: 4px;
|
|
}
|
|
|
|
.alt-label {
|
|
font-size: 11px;
|
|
color: #999;
|
|
margin-bottom: 4px;
|
|
}
|
|
|
|
.alt-options {
|
|
display: flex;
|
|
gap: 4px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.alt-pill {
|
|
padding: 3px 8px;
|
|
border: 1px solid #e8e8e8;
|
|
border-radius: 4px;
|
|
font-size: 11px;
|
|
cursor: pointer;
|
|
background: #fff;
|
|
color: #666;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.alt-pill:hover { border-color: #1677ff; color: #1677ff; }
|
|
.alt-pill.active { border-color: #1677ff; background: #e6f4ff; color: #1677ff; font-weight: 500; }
|
|
|
|
/* Action cells */
|
|
.action-cell {
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.action-cell button {
|
|
background: none;
|
|
border: 1px solid #e8e8e8;
|
|
border-radius: 6px;
|
|
padding: 4px 8px;
|
|
cursor: pointer;
|
|
font-size: 12px;
|
|
color: #666;
|
|
transition: all 0.2s;
|
|
margin-left: 4px;
|
|
}
|
|
|
|
.action-cell button:first-child { margin-left: 0; }
|
|
.action-cell button:hover { border-color: #1677ff; color: #1677ff; }
|
|
.action-cell button.delete-btn:hover { border-color: #ff4d4f; color: #ff4d4f; }
|
|
|
|
/* Add button row */
|
|
.add-row {
|
|
padding: 16px;
|
|
text-align: center;
|
|
}
|
|
|
|
/* Footer / Total */
|
|
.footer {
|
|
position: fixed;
|
|
bottom: 0;
|
|
left: 0;
|
|
right: 0;
|
|
background: #fff;
|
|
box-shadow: 0 -2px 8px rgba(0,0,0,0.08);
|
|
padding: 16px 24px;
|
|
z-index: 100;
|
|
}
|
|
|
|
.footer-inner {
|
|
max-width: 1100px;
|
|
margin: 0 auto;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
}
|
|
|
|
.footer-label { font-size: 14px; color: #666; }
|
|
|
|
.footer-total {
|
|
font-size: 28px;
|
|
font-weight: 700;
|
|
color: #ff4d4f;
|
|
}
|
|
|
|
.footer-total .currency { font-size: 16px; }
|
|
|
|
/* Modal */
|
|
.modal-overlay {
|
|
display: none;
|
|
position: fixed;
|
|
inset: 0;
|
|
background: rgba(0,0,0,0.45);
|
|
z-index: 1000;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
|
|
.modal-overlay.active { display: flex; }
|
|
|
|
.modal {
|
|
background: #fff;
|
|
border-radius: 12px;
|
|
width: 600px;
|
|
max-width: 90vw;
|
|
max-height: 85vh;
|
|
overflow-y: auto;
|
|
box-shadow: 0 8px 32px rgba(0,0,0,0.2);
|
|
}
|
|
|
|
.modal-header {
|
|
padding: 20px 24px 0;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
}
|
|
|
|
.modal-header h2 { font-size: 18px; font-weight: 600; }
|
|
|
|
.modal-close {
|
|
background: none;
|
|
border: none;
|
|
font-size: 20px;
|
|
cursor: pointer;
|
|
color: #999;
|
|
padding: 4px;
|
|
}
|
|
.modal-close:hover { color: #333; }
|
|
|
|
.modal-body { padding: 20px 24px; }
|
|
|
|
.form-group { margin-bottom: 16px; }
|
|
|
|
.form-group label {
|
|
display: block;
|
|
font-size: 13px;
|
|
font-weight: 500;
|
|
color: #333;
|
|
margin-bottom: 6px;
|
|
}
|
|
|
|
.form-group input,
|
|
.form-group select {
|
|
width: 100%;
|
|
padding: 8px 12px;
|
|
border: 1px solid #d9d9d9;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
transition: border-color 0.2s;
|
|
font-family: inherit;
|
|
}
|
|
|
|
.form-group input:focus,
|
|
.form-group select:focus {
|
|
outline: none;
|
|
border-color: #1677ff;
|
|
box-shadow: 0 0 0 2px rgba(22,119,255,0.1);
|
|
}
|
|
|
|
.form-row {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 12px;
|
|
}
|
|
|
|
/* Alternatives in modal */
|
|
.alt-section {
|
|
margin-top: 20px;
|
|
padding-top: 16px;
|
|
border-top: 1px solid #f0f0f0;
|
|
}
|
|
|
|
.alt-section-header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
margin-bottom: 12px;
|
|
}
|
|
|
|
.alt-section-header h3 { font-size: 14px; font-weight: 600; }
|
|
|
|
.alt-item {
|
|
background: #fafafa;
|
|
border-radius: 8px;
|
|
padding: 12px;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.alt-item-header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.alt-item-label { font-size: 12px; font-weight: 600; color: #1677ff; }
|
|
|
|
.alt-item-remove {
|
|
background: none;
|
|
border: none;
|
|
color: #ff4d4f;
|
|
cursor: pointer;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.modal-footer {
|
|
padding: 16px 24px;
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
gap: 8px;
|
|
border-top: 1px solid #f0f0f0;
|
|
}
|
|
|
|
/* Empty State */
|
|
.empty-state {
|
|
text-align: center;
|
|
padding: 60px 20px;
|
|
color: #999;
|
|
}
|
|
|
|
.empty-state p { margin-bottom: 16px; font-size: 15px; }
|
|
|
|
/* Import Overlay */
|
|
.import-overlay {
|
|
display: none;
|
|
position: fixed;
|
|
inset: 0;
|
|
background: rgba(0,0,0,0.45);
|
|
z-index: 1000;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
|
|
.import-overlay.active { display: flex; }
|
|
|
|
.import-box {
|
|
background: #fff;
|
|
border-radius: 12px;
|
|
padding: 40px;
|
|
text-align: center;
|
|
box-shadow: 0 8px 32px rgba(0,0,0,0.2);
|
|
}
|
|
|
|
.import-box p { margin-bottom: 16px; color: #666; }
|
|
|
|
/* Responsive */
|
|
@media (max-width: 768px) {
|
|
.container { padding: 12px; }
|
|
.header { padding: 16px; }
|
|
.config-table-wrap { overflow-x: auto; }
|
|
.config-table { min-width: 700px; }
|
|
.form-row { grid-template-columns: 1fr; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="container">
|
|
<div class="header">
|
|
<h1 id="configName">PC装机配置表</h1>
|
|
<div class="header-actions">
|
|
<div class="mode-toggle">
|
|
<button id="viewBtn" class="active" onclick="setMode('view')">查看</button>
|
|
<button id="editBtn" onclick="setMode('edit')">编辑</button>
|
|
</div>
|
|
<button class="btn btn-default btn-sm" onclick="showImport()">导入JSON</button>
|
|
<button class="btn btn-primary btn-sm" id="exportBtn" style="display:none" onclick="exportJSON()">导出JSON</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="contentArea"></div>
|
|
</div>
|
|
|
|
<!-- Footer Total -->
|
|
<div class="footer">
|
|
<div class="footer-inner">
|
|
<span class="footer-label">总计</span>
|
|
<span class="footer-total"><span class="currency">¥</span><span id="totalPrice">0</span></span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Modal -->
|
|
<div class="modal-overlay" id="editModal">
|
|
<div class="modal">
|
|
<div class="modal-header">
|
|
<h2 id="modalTitle">添加部件</h2>
|
|
<button class="modal-close" onclick="closeModal()">×</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" id="editComponentIndex" value="-1">
|
|
|
|
<div class="form-group">
|
|
<label>部件类型</label>
|
|
<select id="formType" onchange="onTypeChange()">
|
|
<option value="">请选择或自定义</option>
|
|
<option value="CPU">CPU</option>
|
|
<option value="主板">主板</option>
|
|
<option value="显卡">显卡</option>
|
|
<option value="内存">内存</option>
|
|
<option value="硬盘">硬盘</option>
|
|
<option value="电源">电源</option>
|
|
<option value="机箱">机箱</option>
|
|
<option value="散热器">散热器</option>
|
|
<option value="显示器">显示器</option>
|
|
<option value="__custom__">自定义...</option>
|
|
</select>
|
|
<input type="text" id="formTypeCustom" style="display:none;margin-top:6px" placeholder="输入自定义类型名称">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>图片链接</label>
|
|
<input type="url" id="formImage" placeholder="https://example.com/image.jpg">
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label>品牌</label>
|
|
<input type="text" id="formBrand" placeholder="如:Intel、华硕">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>型号</label>
|
|
<input type="text" id="formModel" placeholder="如:i7-14700K">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>参数</label>
|
|
<input type="text" id="formSpecs" placeholder="如:20核28线程 / 3.4GHz">
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label>价格(元)</label>
|
|
<input type="number" id="formPrice" min="0" step="1" placeholder="0">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>商品链接</label>
|
|
<input type="url" id="formLink" placeholder="https://...">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Alternatives Section -->
|
|
<div class="alt-section" id="altSection" style="display:none">
|
|
<div class="alt-section-header">
|
|
<h3>备选配置</h3>
|
|
<button class="btn btn-default btn-sm" onclick="addAltItem()">+ 添加备选</button>
|
|
</div>
|
|
<div id="altList"></div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button class="btn btn-default" onclick="closeModal()">取消</button>
|
|
<button class="btn btn-primary" onclick="saveComponent()">保存</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Import Modal -->
|
|
<div class="import-overlay" id="importModal">
|
|
<div class="import-box">
|
|
<p>选择JSON配置文件</p>
|
|
<input type="file" id="fileInput" accept=".json" onchange="handleFileImport(event)" style="margin-bottom:12px">
|
|
<br>
|
|
<button class="btn btn-default btn-sm" onclick="closeImport()">取消</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Confirm Delete Modal -->
|
|
<div class="modal-overlay" id="confirmModal">
|
|
<div class="modal" style="width:360px">
|
|
<div class="modal-header">
|
|
<h2>确认删除</h2>
|
|
<button class="modal-close" onclick="closeConfirm()">×</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p id="confirmMessage">确定要删除这个部件吗?</p>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button class="btn btn-default" onclick="closeConfirm()">取消</button>
|
|
<button class="btn btn-danger" id="confirmDeleteBtn">删除</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
// ========== State ==========
|
|
let config = { configName: 'PC装机配置表', components: [] };
|
|
let currentMode = 'view';
|
|
let tempAlts = [];
|
|
|
|
const PRESET_TYPES = ['CPU', '主板', '显卡', '内存', '硬盘', '电源', '机箱', '散热器', '显示器'];
|
|
|
|
// ========== Init ==========
|
|
document.addEventListener('DOMContentLoaded', loadFromURL);
|
|
|
|
function loadFromURL() {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const configURL = params.get('config');
|
|
if (configURL) {
|
|
fetch(configURL)
|
|
.then(r => { if (!r.ok) throw new Error(`HTTP ${r.status}`); return r.json(); })
|
|
.then(data => { config = data; document.getElementById('configName').textContent = config.configName || 'PC装机配置表'; render(); })
|
|
.catch(err => { console.error('加载配置失败:', err); render(); });
|
|
} else {
|
|
render();
|
|
}
|
|
}
|
|
|
|
// ========== Mode ==========
|
|
function setMode(mode) {
|
|
currentMode = mode;
|
|
document.getElementById('viewBtn').classList.toggle('active', mode === 'view');
|
|
document.getElementById('editBtn').classList.toggle('active', mode === 'edit');
|
|
document.getElementById('exportBtn').style.display = mode === 'edit' ? '' : 'none';
|
|
render();
|
|
}
|
|
|
|
// ========== Render ==========
|
|
function render() {
|
|
const area = document.getElementById('contentArea');
|
|
|
|
if (!config.components || config.components.length === 0) {
|
|
area.innerHTML = `
|
|
<div class="empty-state">
|
|
<p>暂无配置数据</p>
|
|
<button class="btn btn-primary" onclick="loadSample()">加载示例配置</button>
|
|
<button class="btn btn-default" style="margin-left:8px" onclick="showImport()">导入JSON</button>
|
|
</div>`;
|
|
updateTotal();
|
|
return;
|
|
}
|
|
|
|
const editHeaders = currentMode === 'edit' ? '<th style="width:100px">操作</th>' : '';
|
|
|
|
let rows = config.components.map((comp, i) => {
|
|
const selectedIdx = comp.selected || 0;
|
|
const opt = comp.options[selectedIdx];
|
|
if (!opt) return '';
|
|
|
|
const imgCell = opt.image
|
|
? `<img class="cell-img" src="${opt.image}" alt="${opt.model}" onerror="this.outerHTML='<div class=\\'cell-img-placeholder\\'> </div>'">`
|
|
: '<div class="cell-img-placeholder"> </div>';
|
|
|
|
const linkCell = opt.link
|
|
? `<a href="${opt.link}" target="_blank" rel="noopener">查看商品</a>`
|
|
: '-';
|
|
|
|
const altCell = comp.options.length > 1 ? `
|
|
<div class="alt-selector">
|
|
<div class="alt-label">备选 (${comp.options.length})</div>
|
|
<div class="alt-options">
|
|
${comp.options.map((o, j) => `
|
|
<button class="alt-pill ${j === selectedIdx ? 'active' : ''}" onclick="selectAlt(${i}, ${j})">
|
|
${o.brand} ${o.model} ¥${o.price}
|
|
</button>
|
|
`).join('')}
|
|
</div>
|
|
</div>` : '';
|
|
|
|
const actionCell = currentMode === 'edit' ? `
|
|
<td class="action-cell">
|
|
<button onclick="editComponent(${i})">编辑</button>
|
|
<button class="delete-btn" onclick="confirmDelete(${i})">删除</button>
|
|
</td>` : '';
|
|
|
|
return `
|
|
<tr>
|
|
<td>${imgCell}</td>
|
|
<td><span class="type-badge">${comp.type}</span></td>
|
|
<td>${opt.brand || '-'}</td>
|
|
<td>${opt.model || '-'}</td>
|
|
<td>${opt.specs || '-'}</td>
|
|
<td class="price-cell"><span class="currency">¥</span>${(opt.price || 0).toLocaleString()}</td>
|
|
<td class="link-cell">${linkCell}</td>
|
|
<td>${altCell}</td>
|
|
${actionCell}
|
|
</tr>`;
|
|
}).join('');
|
|
|
|
const addRow = currentMode === 'edit'
|
|
? `<div class="add-row"><button class="btn btn-primary" onclick="addComponent()">+ 添加部件</button></div>`
|
|
: '';
|
|
|
|
area.innerHTML = `
|
|
<div class="config-table-wrap">
|
|
<table class="config-table">
|
|
<thead>
|
|
<tr>
|
|
<th style="width:80px">图片</th>
|
|
<th style="width:80px">类型</th>
|
|
<th style="width:80px">品牌</th>
|
|
<th style="width:120px">型号</th>
|
|
<th>参数</th>
|
|
<th style="width:100px">价格</th>
|
|
<th style="width:80px">链接</th>
|
|
<th style="width:200px">备选</th>
|
|
${editHeaders}
|
|
</tr>
|
|
</thead>
|
|
<tbody>${rows}</tbody>
|
|
</table>
|
|
${addRow}
|
|
</div>`;
|
|
|
|
updateTotal();
|
|
}
|
|
|
|
// ========== Total Price ==========
|
|
function updateTotal() {
|
|
const total = (config.components || []).reduce((sum, comp) => {
|
|
const opt = comp.options[comp.selected || 0];
|
|
return sum + (opt ? (opt.price || 0) : 0);
|
|
}, 0);
|
|
document.getElementById('totalPrice').textContent = total.toLocaleString();
|
|
}
|
|
|
|
// ========== Select Alternative ==========
|
|
function selectAlt(compIndex, altIndex) {
|
|
config.components[compIndex].selected = altIndex;
|
|
render();
|
|
}
|
|
|
|
// ========== Import ==========
|
|
function showImport() {
|
|
document.getElementById('importModal').classList.add('active');
|
|
document.getElementById('fileInput').value = '';
|
|
}
|
|
|
|
function closeImport() {
|
|
document.getElementById('importModal').classList.remove('active');
|
|
}
|
|
|
|
function handleFileImport(event) {
|
|
const file = event.target.files[0];
|
|
if (!file) return;
|
|
const reader = new FileReader();
|
|
reader.onload = (e) => {
|
|
try {
|
|
config = JSON.parse(e.target.result);
|
|
document.getElementById('configName').textContent = config.configName || 'PC装机配置表';
|
|
closeImport();
|
|
render();
|
|
} catch (err) {
|
|
alert('JSON格式错误: ' + err.message);
|
|
}
|
|
};
|
|
reader.readAsText(file);
|
|
}
|
|
|
|
function loadSample() {
|
|
fetch('sample_config.json')
|
|
.then(r => r.json())
|
|
.then(data => { config = data; document.getElementById('configName').textContent = config.configName || 'PC装机配置表'; render(); })
|
|
.catch(err => alert('加载示例失败: ' + err.message));
|
|
}
|
|
|
|
// ========== Export ==========
|
|
function exportJSON() {
|
|
const json = JSON.stringify(config, null, 2);
|
|
const blob = new Blob([json], { type: 'application/json' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = (config.configName || 'config') + '.json';
|
|
a.click();
|
|
URL.revokeObjectURL(url);
|
|
}
|
|
|
|
// ========== Modal ==========
|
|
function openModal(title) {
|
|
document.getElementById('modalTitle').textContent = title;
|
|
document.getElementById('editModal').classList.add('active');
|
|
}
|
|
|
|
function closeModal() {
|
|
document.getElementById('editModal').classList.remove('active');
|
|
tempAlts = [];
|
|
}
|
|
|
|
function onTypeChange() {
|
|
const sel = document.getElementById('formType');
|
|
document.getElementById('formTypeCustom').style.display = sel.value === '__custom__' ? '' : 'none';
|
|
}
|
|
|
|
function getFormType() {
|
|
const sel = document.getElementById('formType');
|
|
return sel.value === '__custom__' ? document.getElementById('formTypeCustom').value.trim() : sel.value;
|
|
}
|
|
|
|
function setFormType(type) {
|
|
const sel = document.getElementById('formType');
|
|
const custom = document.getElementById('formTypeCustom');
|
|
if (PRESET_TYPES.includes(type)) {
|
|
sel.value = type;
|
|
custom.style.display = 'none';
|
|
} else if (type) {
|
|
sel.value = '__custom__';
|
|
custom.value = type;
|
|
custom.style.display = '';
|
|
}
|
|
}
|
|
|
|
function getFormData() {
|
|
return {
|
|
image: document.getElementById('formImage').value.trim(),
|
|
brand: document.getElementById('formBrand').value.trim(),
|
|
model: document.getElementById('formModel').value.trim(),
|
|
specs: document.getElementById('formSpecs').value.trim(),
|
|
price: parseFloat(document.getElementById('formPrice').value) || 0,
|
|
link: document.getElementById('formLink').value.trim()
|
|
};
|
|
}
|
|
|
|
function setFormData(opt) {
|
|
document.getElementById('formImage').value = opt.image || '';
|
|
document.getElementById('formBrand').value = opt.brand || '';
|
|
document.getElementById('formModel').value = opt.model || '';
|
|
document.getElementById('formSpecs').value = opt.specs || '';
|
|
document.getElementById('formPrice').value = opt.price || '';
|
|
document.getElementById('formLink').value = opt.link || '';
|
|
}
|
|
|
|
function clearForm() {
|
|
document.getElementById('formType').value = '';
|
|
document.getElementById('formTypeCustom').value = '';
|
|
document.getElementById('formTypeCustom').style.display = 'none';
|
|
['formImage', 'formBrand', 'formModel', 'formSpecs', 'formPrice', 'formLink'].forEach(id => {
|
|
document.getElementById(id).value = '';
|
|
});
|
|
tempAlts = [];
|
|
renderAltList();
|
|
}
|
|
|
|
// ========== Add / Edit Component ==========
|
|
function addComponent() {
|
|
clearForm();
|
|
document.getElementById('editComponentIndex').value = -1;
|
|
document.getElementById('altSection').style.display = 'none';
|
|
openModal('添加部件');
|
|
}
|
|
|
|
function editComponent(index) {
|
|
const comp = config.components[index];
|
|
document.getElementById('editComponentIndex').value = index;
|
|
setFormType(comp.type);
|
|
setFormData(comp.options[0] || {});
|
|
tempAlts = comp.options.slice(1).map(o => ({...o}));
|
|
document.getElementById('altSection').style.display = tempAlts.length > 0 || comp.options.length > 0 ? '' : 'none';
|
|
renderAltList();
|
|
openModal('编辑部件');
|
|
}
|
|
|
|
// ========== Alternatives in Modal ==========
|
|
function addAltItem() {
|
|
tempAlts.push({ image: '', brand: '', model: '', specs: '', price: 0, link: '' });
|
|
renderAltList();
|
|
}
|
|
|
|
function removeAltItem(idx) {
|
|
tempAlts.splice(idx, 1);
|
|
renderAltList();
|
|
}
|
|
|
|
function renderAltList() {
|
|
const container = document.getElementById('altList');
|
|
if (tempAlts.length === 0) {
|
|
container.innerHTML = '<p style="font-size:12px;color:#999">暂无备选配置,点击上方按钮添加</p>';
|
|
return;
|
|
}
|
|
container.innerHTML = tempAlts.map((alt, i) => `
|
|
<div class="alt-item">
|
|
<div class="alt-item-header">
|
|
<span class="alt-item-label">备选 ${i + 1}</span>
|
|
<button class="alt-item-remove" onclick="removeAltItem(${i})">删除</button>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>图片链接</label>
|
|
<input type="url" value="${alt.image || ''}" onchange="tempAlts[${i}].image=this.value" placeholder="https://example.com/image.jpg">
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label>品牌</label>
|
|
<input type="text" value="${alt.brand}" onchange="tempAlts[${i}].brand=this.value">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>型号</label>
|
|
<input type="text" value="${alt.model}" onchange="tempAlts[${i}].model=this.value">
|
|
</div>
|
|
</div>
|
|
<div class="form-group">
|
|
<label>参数</label>
|
|
<input type="text" value="${alt.specs}" onchange="tempAlts[${i}].specs=this.value">
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label>价格(元)</label>
|
|
<input type="number" value="${alt.price}" min="0" onchange="tempAlts[${i}].price=parseFloat(this.value)||0">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>商品链接</label>
|
|
<input type="url" value="${alt.link}" onchange="tempAlts[${i}].link=this.value">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
}
|
|
|
|
// ========== Save Component ==========
|
|
function saveComponent() {
|
|
const type = getFormType();
|
|
if (!type) { alert('请选择或输入部件类型'); return; }
|
|
|
|
const mainOpt = getFormData();
|
|
const options = [mainOpt, ...tempAlts];
|
|
const index = parseInt(document.getElementById('editComponentIndex').value);
|
|
|
|
if (index >= 0) {
|
|
config.components[index].type = type;
|
|
config.components[index].options = options;
|
|
if (config.components[index].selected >= options.length) config.components[index].selected = 0;
|
|
} else {
|
|
config.components.push({ type, selected: 0, options });
|
|
}
|
|
|
|
closeModal();
|
|
render();
|
|
}
|
|
|
|
// ========== Delete ==========
|
|
function confirmDelete(index) {
|
|
document.getElementById('confirmMessage').textContent = `确定要删除「${config.components[index].type}」吗?`;
|
|
document.getElementById('confirmDeleteBtn').onclick = () => {
|
|
config.components.splice(index, 1);
|
|
closeConfirm();
|
|
render();
|
|
};
|
|
document.getElementById('confirmModal').classList.add('active');
|
|
}
|
|
|
|
function closeConfirm() {
|
|
document.getElementById('confirmModal').classList.remove('active');
|
|
}
|
|
|
|
// ========== Keyboard ==========
|
|
document.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Escape') { closeModal(); closeImport(); closeConfirm(); }
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|