admin-garments.html
9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>衣服库管理</title>
<link rel="stylesheet" href="style.css" />
<style>
.admin-wrap { padding: 18px 24px; max-width: 1400px; }
.admin-toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.admin-toolbar input[type="search"] { flex: 1; min-width: 220px; }
table.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; background: #fff; }
table.admin-table th, table.admin-table td { border-bottom: 1px solid var(--border); padding: 8px 10px; text-align: left; vertical-align: top; }
table.admin-table th { color: var(--muted); font-weight: 600; white-space: nowrap; }
table.admin-table td.name { max-width: 160px; }
table.admin-table td.desc { max-width: 320px; color: var(--muted); }
table.admin-table td input, table.admin-table td textarea { width: 100%; font-size: 12px; }
table.admin-table td textarea { min-height: 50px; }
.row-actions { display: flex; gap: 6px; white-space: nowrap; }
.row-actions button { padding: 3px 8px; font-size: 12px; }
.thumb { width: 56px; height: 80px; object-fit: cover; border-radius: 4px; border: 1px solid var(--border); }
.add-card { margin-bottom: 16px; }
.row-5 { display: grid; grid-template-columns: 1fr 1fr 1fr 1.6fr 100px; gap: 14px; align-items: start; }
.count-hint { color: var(--muted); font-size: 12px; margin-bottom: 10px; }
.inactive-badge { display: inline-block; padding: 2px 6px; font-size: 11px; border-radius: 4px; background: #fbeaea; color: #c0392b; }
</style>
</head>
<body>
<header class="topbar">
<h1>衣服库管理</h1>
<div class="spacer"></div>
<a href="admin.html">视频库</a>
<a href="admin-characters.html">人物库</a>
<a href="admin-dance-refs.html">参考跳舞视频库</a>
<a href="live.html">← 返回直播频道</a>
</header>
<main class="admin-wrap">
<div class="card add-card">
<div class="field"><label>新增衣服</label></div>
<div class="row-5">
<input type="text" id="newName" placeholder="名称" />
<input type="text" id="newCategory" placeholder="分类(如:古风/泳装/校服)" />
<input type="file" id="newImageFile" accept="image/*" />
<input type="text" id="newTags" placeholder="(此列留空,占位对齐)" disabled />
<label style="display:flex;align-items:center;gap:6px;font-size:12px;">
<input type="checkbox" id="newIsActive" checked /> 上架
</label>
</div>
<div class="field" style="margin-top:8px;">
<textarea id="newAppearanceDescriptor" placeholder="服装特征(颜色/款式/材质,如:a fitted red qipao with gold trim and a side slit)" style="min-height:60px;"></textarea>
</div>
<button id="addBtn" style="margin-top:8px;">新增</button>
<div id="addError" class="error-box" hidden></div>
</div>
<div class="admin-toolbar">
<input type="search" id="searchInput" placeholder="搜索名称 / 分类..." />
</div>
<div class="count-hint" id="countHint"></div>
<div style="overflow-x:auto;">
<table class="admin-table">
<thead>
<tr>
<th>图片</th>
<th>名称</th>
<th>分类</th>
<th>外观描述</th>
<th>上架</th>
<th>来源</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
</div>
</main>
<script>
const _fetch = window.fetch;
window.fetch = async (...args) => {
const res = await _fetch(...args);
if (res.status === 401 && String(args[0]).startsWith("/api/admin/") && !String(args[0]).includes("/login")) {
location.href = "/admin-login.html?next=" + encodeURIComponent(location.pathname);
}
return res;
};
let items = [];
let editingId = null;
function escapeHtml(s) {
const div = document.createElement("div");
div.textContent = s || "";
return div.innerHTML;
}
async function uploadImage(file) {
const fd = new FormData();
fd.append("file", file);
const res = await fetch("/api/admin/upload", { method: "POST", body: fd });
if (!res.ok) throw new Error((await res.json()).error || "上传失败");
return (await res.json()).url;
}
async function load() {
const res = await fetch("/api/admin/garments");
items = await res.json();
render();
}
function filtered() {
const q = document.getElementById("searchInput").value.trim().toLowerCase();
if (!q) return items;
return items.filter((g) => g.name.toLowerCase().includes(q) || (g.category || "").toLowerCase().includes(q));
}
function render() {
const rows = filtered();
document.getElementById("countHint").textContent = `共 ${items.length} 条,当前筛选 ${rows.length} 条`;
const tbody = document.getElementById("tbody");
tbody.innerHTML = rows
.map((g) => {
if (g.id === editingId) {
return `
<tr data-id="${g.id}">
<td><img class="thumb" src="${escapeHtml(g.thumbnailUrl || g.imageUrl)}" /></td>
<td class="name"><input data-field="name" value="${escapeHtml(g.name)}" /></td>
<td class="name"><input data-field="category" value="${escapeHtml(g.category || "")}" /></td>
<td class="desc"><textarea data-field="appearanceDescriptor">${escapeHtml(g.appearanceDescriptor || "")}</textarea></td>
<td><input type="checkbox" data-field="isActive" ${g.isActive ? "checked" : ""} /></td>
<td>${g.source}</td>
<td>${new Date(g.createdAt).toLocaleDateString()}</td>
<td class="row-actions">
<button data-save="${g.id}">保存</button>
<button class="secondary" data-cancel="1">取消</button>
</td>
</tr>`;
}
return `
<tr data-id="${g.id}">
<td><img class="thumb" src="${escapeHtml(g.thumbnailUrl || g.imageUrl)}" /></td>
<td class="name">${escapeHtml(g.name)}</td>
<td class="name">${escapeHtml(g.category || "")}</td>
<td class="desc" title="${escapeHtml(g.appearanceDescriptor || "")}">${escapeHtml(g.appearanceDescriptor || "")}</td>
<td>${g.isActive ? "上架" : '<span class="inactive-badge">下架</span>'}</td>
<td>${g.source}</td>
<td>${new Date(g.createdAt).toLocaleDateString()}</td>
<td class="row-actions">
<button class="secondary" data-edit="${g.id}">编辑</button>
<button class="danger" data-del="${g.id}">删除</button>
</td>
</tr>`;
})
.join("");
tbody.querySelectorAll("[data-edit]").forEach((b) => b.addEventListener("click", () => { editingId = b.dataset.edit; render(); }));
tbody.querySelectorAll("[data-cancel]").forEach((b) => b.addEventListener("click", () => { editingId = null; render(); }));
tbody.querySelectorAll("[data-save]").forEach((b) =>
b.addEventListener("click", async () => {
const tr = b.closest("tr");
const body = {};
tr.querySelectorAll("[data-field]").forEach((el) => {
if (el.type === "checkbox") body[el.dataset.field] = el.checked;
else body[el.dataset.field] = el.value;
});
const res = await fetch(`/api/admin/garments/${b.dataset.save}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (res.ok) {
editingId = null;
await load();
}
})
);
tbody.querySelectorAll("[data-del]").forEach((b) =>
b.addEventListener("click", async () => {
if (!confirm("确认删除这件衣服?")) return;
const res = await fetch(`/api/admin/garments/${b.dataset.del}`, { method: "DELETE" });
if (res.ok) await load();
})
);
}
document.getElementById("searchInput").addEventListener("input", render);
document.getElementById("addBtn").addEventListener("click", async () => {
const errorBox = document.getElementById("addError");
errorBox.hidden = true;
const name = document.getElementById("newName").value.trim();
const category = document.getElementById("newCategory").value.trim();
const file = document.getElementById("newImageFile").files[0];
const appearanceDescriptor = document.getElementById("newAppearanceDescriptor").value.trim();
const isActive = document.getElementById("newIsActive").checked;
if (!name || !file || !appearanceDescriptor) {
errorBox.hidden = false;
errorBox.textContent = "名称、图片、外观描述都必填";
return;
}
try {
const imageUrl = await uploadImage(file);
const res = await fetch("/api/admin/garments", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, category, imageUrl, appearanceDescriptor, isActive }),
});
if (res.ok) {
document.getElementById("newName").value = "";
document.getElementById("newCategory").value = "";
document.getElementById("newImageFile").value = "";
document.getElementById("newAppearanceDescriptor").value = "";
document.getElementById("newIsActive").checked = true;
await load();
} else {
errorBox.hidden = false;
errorBox.textContent = (await res.json()).error || "新增失败";
}
} catch (e) {
errorBox.hidden = false;
errorBox.textContent = e.message;
}
});
load();
</script>
</body>
</html>