整合作业(前端)
This commit is contained in:
@@ -0,0 +1,474 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>商品列表</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body {
|
||||
font-family: "Microsoft YaHei", Arial, sans-serif;
|
||||
background: #f5f6fa;
|
||||
min-height: 100vh;
|
||||
padding: 30px 20px;
|
||||
}
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
|
||||
padding: 30px;
|
||||
}
|
||||
h1 { text-align: center; color: #2c3e50; margin-bottom: 20px; font-size: 28px; }
|
||||
h2 { color: #2c3e50; font-size: 20px; margin: 30px 0 12px; }
|
||||
.toolbar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.toolbar .status { font-size: 14px; color: #7f8c8d; }
|
||||
.btn {
|
||||
padding: 8px 20px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
.btn-primary { background: #3498db; color: #fff; }
|
||||
.btn-primary:hover { background: #2980b9; }
|
||||
.btn-success { background: #2ecc71; color: #fff; }
|
||||
.btn-success:hover { background: #27ae60; }
|
||||
.btn-danger { background: #e74c3c; color: #fff; }
|
||||
.btn-danger:hover { background: #c0392b; }
|
||||
.btn-warning { background: #f39c12; color: #fff; }
|
||||
.btn-warning:hover { background: #e67e22; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 10px; }
|
||||
th, td {
|
||||
padding: 12px 15px;
|
||||
text-align: center;
|
||||
border-bottom: 1px solid #ecf0f1;
|
||||
}
|
||||
th { background: #f8f9fa; color: #2c3e50; font-weight: 600; }
|
||||
tr:hover { background: #fafafa; }
|
||||
.product-image {
|
||||
width: 80px; height: 80px;
|
||||
object-fit: cover;
|
||||
border-radius: 8px;
|
||||
background: #f0f0f0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #999;
|
||||
font-size: 12px;
|
||||
margin: 0 auto;
|
||||
overflow: hidden;
|
||||
}
|
||||
.product-image img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; }
|
||||
.empty { text-align: center; padding: 40px; color: #95a5a6; font-size: 16px; }
|
||||
.error {
|
||||
background: #fdedec; color: #e74c3c;
|
||||
padding: 12px 20px; border-radius: 8px;
|
||||
margin-bottom: 20px; display: none;
|
||||
}
|
||||
.modal-overlay {
|
||||
display: none;
|
||||
position: fixed;
|
||||
top: 0; left: 0; width: 100%; height: 100%;
|
||||
background: rgba(0,0,0,0.5);
|
||||
z-index: 999;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
.modal {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 30px;
|
||||
width: 360px;
|
||||
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
||||
}
|
||||
.modal h2 { margin-bottom: 20px; color: #2c3e50; }
|
||||
.modal input {
|
||||
width: 100%; padding: 10px 12px;
|
||||
margin-bottom: 15px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 10px; }
|
||||
.login-status {
|
||||
font-size: 14px;
|
||||
padding: 6px 14px;
|
||||
border-radius: 20px;
|
||||
background: #ecf0f1;
|
||||
}
|
||||
.login-status.logged-in { background: #d5f5e3; color: #27ae60; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>🛒 商品列表</h1>
|
||||
|
||||
<div class="toolbar">
|
||||
<div class="status" id="statusText">加载商品中...</div>
|
||||
<div>
|
||||
<span id="loginStatus" class="login-status">未登录</span>
|
||||
<button id="loginBtn" class="btn btn-primary" style="margin-left:10px;">登录</button>
|
||||
<button id="logoutBtn" class="btn btn-danger" style="margin-left:5px; display:none;">退出</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="error" id="errorBox"></div>
|
||||
|
||||
<table id="productTable" style="display:none;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>图片</th>
|
||||
<th>名称</th>
|
||||
<th>描述</th>
|
||||
<th>价格</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="productBody"></tbody>
|
||||
</table>
|
||||
|
||||
<div id="emptyBox" class="empty" style="display:none;">暂无商品</div>
|
||||
|
||||
<h2>删除日志</h2>
|
||||
<table id="logTable" style="display:none;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>操作人</th>
|
||||
<th>商品ID</th>
|
||||
<th>删除时间</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="logBody"></tbody>
|
||||
</table>
|
||||
<div id="logEmpty" class="empty" style="display:none;">暂无日志</div>
|
||||
</div>
|
||||
|
||||
<div class="modal-overlay" id="modalOverlay">
|
||||
<div class="modal">
|
||||
<h2>用户登录</h2>
|
||||
<input type="text" id="usernameInput" placeholder="用户名">
|
||||
<input type="password" id="passwordInput" placeholder="密码">
|
||||
<div class="modal-actions">
|
||||
<button class="btn" id="cancelLogin">取消</button>
|
||||
<button class="btn btn-primary" id="confirmLogin">登录</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const API = 'http://localhost:8080/api';
|
||||
const FILE_BASE = 'http://localhost:8080'; // 静态资源根地址
|
||||
const $ = id => document.getElementById(id);
|
||||
|
||||
let token = localStorage.getItem('token');
|
||||
let username = localStorage.getItem('username');
|
||||
let products = [];
|
||||
|
||||
const isLoggedIn = () => !!token;
|
||||
|
||||
// ========== 登录 UI ==========
|
||||
function updateLoginUI() {
|
||||
$('loginStatus').textContent = isLoggedIn() ? `已登录:${username}` : '未登录';
|
||||
$('loginStatus').className = 'login-status' + (isLoggedIn() ? ' logged-in' : '');
|
||||
$('loginBtn').style.display = isLoggedIn() ? 'none' : 'inline-block';
|
||||
$('logoutBtn').style.display = isLoggedIn() ? 'inline-block' : 'none';
|
||||
}
|
||||
|
||||
// ========== 商品加载 ==========
|
||||
async function loadProducts() {
|
||||
$('statusText').textContent = '加载商品中...';
|
||||
$('errorBox').style.display = 'none';
|
||||
try {
|
||||
const res = await fetch(`${API}/prodect`);
|
||||
const result = await res.json();
|
||||
if (result.code !== 200) throw new Error(result.message || '加载失败');
|
||||
products = result.data || [];
|
||||
renderProducts();
|
||||
$('statusText').textContent = `共 ${products.length} 件商品`;
|
||||
} catch (e) {
|
||||
$('errorBox').textContent = '加载失败:' + e.message;
|
||||
$('errorBox').style.display = 'block';
|
||||
$('statusText').textContent = '加载失败';
|
||||
$('productTable').style.display = 'none';
|
||||
$('emptyBox').style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 图片 URL 拼接 ==========
|
||||
function resolveImageUrl(image) {
|
||||
if (!image) return null;
|
||||
if (image.startsWith('http://') || image.startsWith('https://')) return image;
|
||||
return FILE_BASE + (image.startsWith('/') ? image : '/' + image);
|
||||
}
|
||||
|
||||
// ========== 商品渲染 ==========
|
||||
function renderProducts() {
|
||||
const tbody = $('productBody');
|
||||
tbody.textContent = '';
|
||||
|
||||
if (products.length === 0) {
|
||||
$('productTable').style.display = 'none';
|
||||
$('emptyBox').style.display = 'block';
|
||||
return;
|
||||
}
|
||||
$('productTable').style.display = 'table';
|
||||
$('emptyBox').style.display = 'none';
|
||||
|
||||
products.forEach(p => {
|
||||
const price = Number(p.price) || 0;
|
||||
const needLogin = price > 5000 && !isLoggedIn();
|
||||
|
||||
const tr = document.createElement('tr');
|
||||
|
||||
const tdId = document.createElement('td');
|
||||
tdId.textContent = p.id;
|
||||
tr.appendChild(tdId);
|
||||
|
||||
const tdImg = document.createElement('td');
|
||||
const imgDiv = document.createElement('div');
|
||||
imgDiv.className = 'product-image';
|
||||
const url = resolveImageUrl(p.image);
|
||||
if (url) {
|
||||
const img = document.createElement('img');
|
||||
img.src = url;
|
||||
img.alt = p.name || '';
|
||||
img.onerror = () => { imgDiv.textContent = '无图'; };
|
||||
imgDiv.appendChild(img);
|
||||
} else {
|
||||
imgDiv.textContent = '无图';
|
||||
}
|
||||
tdImg.appendChild(imgDiv);
|
||||
tr.appendChild(tdImg);
|
||||
|
||||
const tdName = document.createElement('td');
|
||||
tdName.textContent = p.name || '-';
|
||||
tr.appendChild(tdName);
|
||||
|
||||
const tdDesc = document.createElement('td');
|
||||
tdDesc.textContent = p.p_desc || '-';
|
||||
tr.appendChild(tdDesc);
|
||||
|
||||
const tdPrice = document.createElement('td');
|
||||
const priceSpan = document.createElement('span');
|
||||
priceSpan.style.color = '#e74c3c';
|
||||
priceSpan.style.fontWeight = 'bold';
|
||||
priceSpan.textContent = `¥${price.toFixed(2)}`;
|
||||
tdPrice.appendChild(priceSpan);
|
||||
tr.appendChild(tdPrice);
|
||||
|
||||
const tdAction = document.createElement('td');
|
||||
|
||||
const downloadBtn = document.createElement('button');
|
||||
downloadBtn.className = 'btn ' + (needLogin ? 'btn-warning' : 'btn-success');
|
||||
downloadBtn.textContent = needLogin ? '登录后下载' : '下载';
|
||||
downloadBtn.onclick = () => handleDownload(p, price);
|
||||
tdAction.appendChild(downloadBtn);
|
||||
|
||||
const delBtn = document.createElement('button');
|
||||
delBtn.className = 'btn btn-danger';
|
||||
delBtn.textContent = '删除';
|
||||
delBtn.style.marginLeft = '6px';
|
||||
delBtn.onclick = () => deleteProduct(p.id);
|
||||
tdAction.appendChild(delBtn);
|
||||
|
||||
tr.appendChild(tdAction);
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
}
|
||||
|
||||
// ========== 真实下载 ==========
|
||||
async function handleDownload(p, price) {
|
||||
if (price > 5000 && !isLoggedIn()) {
|
||||
alert('该商品价格超过 5000 元,请先登录');
|
||||
showLogin();
|
||||
return;
|
||||
}
|
||||
if (!isLoggedIn()) {
|
||||
alert('请先登录');
|
||||
showLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API}/product/${p.id}/download`, {
|
||||
headers: { 'Authorization': 'Bearer ' + token }
|
||||
});
|
||||
if (!res.ok) {
|
||||
if (res.status === 401) throw new Error('登录已失效,请重新登录');
|
||||
throw new Error('HTTP ' + res.status);
|
||||
}
|
||||
|
||||
// 从 Content-Disposition 尝试解析文件名
|
||||
let filename = `product-${p.id}.png`;
|
||||
const cd = res.headers.get('Content-Disposition');
|
||||
if (cd) {
|
||||
const m = /filename\*?=(?:UTF-8'')?["']?([^"';]+)/i.exec(cd);
|
||||
if (m) filename = decodeURIComponent(m[1]);
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
const objUrl = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = objUrl;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
URL.revokeObjectURL(objUrl);
|
||||
} catch (e) {
|
||||
alert('下载失败:' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 删除商品 ==========
|
||||
async function deleteProduct(id) {
|
||||
if (!isLoggedIn()) {
|
||||
alert('请先登录');
|
||||
showLogin();
|
||||
return;
|
||||
}
|
||||
if (!confirm('确定删除该商品?')) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API}/product/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Authorization': 'Bearer ' + token }
|
||||
});
|
||||
const result = await res.json();
|
||||
if (result.code !== 200) throw new Error(result.message || '删除失败');
|
||||
alert('删除成功');
|
||||
await loadProducts();
|
||||
await loadLogs();
|
||||
} catch (e) {
|
||||
alert('删除失败:' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 日志加载 ==========
|
||||
async function loadLogs() {
|
||||
try {
|
||||
const res = await fetch(`${API}/logs`);
|
||||
const result = await res.json();
|
||||
if (result.code !== 200) throw new Error(result.message || '日志加载失败');
|
||||
renderLogs(result.data || []);
|
||||
} catch (e) {
|
||||
$('logTable').style.display = 'none';
|
||||
$('logEmpty').style.display = 'block';
|
||||
$('logEmpty').textContent = '日志加载失败:' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 日志渲染 ==========
|
||||
function renderLogs(logs) {
|
||||
const tbody = $('logBody');
|
||||
tbody.textContent = '';
|
||||
|
||||
if (logs.length === 0) {
|
||||
$('logTable').style.display = 'none';
|
||||
$('logEmpty').style.display = 'block';
|
||||
$('logEmpty').textContent = '暂无日志';
|
||||
return;
|
||||
}
|
||||
$('logTable').style.display = 'table';
|
||||
$('logEmpty').style.display = 'none';
|
||||
|
||||
logs.forEach(log => {
|
||||
const tr = document.createElement('tr');
|
||||
|
||||
const tdId = document.createElement('td');
|
||||
tdId.textContent = log.id;
|
||||
tr.appendChild(tdId);
|
||||
|
||||
const tdUser = document.createElement('td');
|
||||
tdUser.textContent = log.username || '-';
|
||||
tr.appendChild(tdUser);
|
||||
|
||||
const tdPid = document.createElement('td');
|
||||
tdPid.textContent = log.pid || '-';
|
||||
tr.appendChild(tdPid);
|
||||
|
||||
const tdTime = document.createElement('td');
|
||||
const t = log.delete_date || log.deleteDate;
|
||||
tdTime.textContent = t ? String(t).replace('T', ' ') : '-';
|
||||
tr.appendChild(tdTime);
|
||||
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
}
|
||||
|
||||
// ========== 登录弹窗 ==========
|
||||
function showLogin() {
|
||||
$('modalOverlay').style.display = 'flex';
|
||||
$('usernameInput').value = '';
|
||||
$('passwordInput').value = '';
|
||||
$('usernameInput').focus();
|
||||
}
|
||||
function hideLogin() {
|
||||
$('modalOverlay').style.display = 'none';
|
||||
}
|
||||
|
||||
async function doLogin() {
|
||||
const u = $('usernameInput').value.trim();
|
||||
const p = $('passwordInput').value.trim();
|
||||
if (!u || !p) return alert('请输入用户名和密码');
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API}/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username: u, password: p })
|
||||
});
|
||||
const result = await res.json();
|
||||
if (result.code !== 200) throw new Error(result.message || '登录失败');
|
||||
|
||||
token = result.data.token;
|
||||
username = result.data.user.username;
|
||||
localStorage.setItem('token', token);
|
||||
localStorage.setItem('username', username);
|
||||
|
||||
updateLoginUI();
|
||||
renderProducts();
|
||||
hideLogin();
|
||||
await loadLogs();
|
||||
} catch (e) {
|
||||
alert('登录失败:' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
function doLogout() {
|
||||
token = null;
|
||||
username = null;
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('username');
|
||||
updateLoginUI();
|
||||
renderProducts();
|
||||
loadLogs();
|
||||
alert('已退出登录');
|
||||
}
|
||||
|
||||
// ========== 事件绑定 ==========
|
||||
$('loginBtn').onclick = showLogin;
|
||||
$('logoutBtn').onclick = doLogout;
|
||||
$('cancelLogin').onclick = hideLogin;
|
||||
$('confirmLogin').onclick = doLogin;
|
||||
$('modalOverlay').onclick = e => {
|
||||
if (e.target === $('modalOverlay')) hideLogin();
|
||||
};
|
||||
|
||||
// ========== 初始化 ==========
|
||||
updateLoginUI();
|
||||
loadProducts();
|
||||
loadLogs();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user