144 lines
6.0 KiB
Plaintext
144 lines
6.0 KiB
Plaintext
<div class="card">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<h5 class="mb-0">分类管理</h5>
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCategoryModal">
|
|
<i class="bi bi-plus"></i> 添加分类
|
|
</button>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>排序</th>
|
|
<th>分类名称</th>
|
|
<th>别名</th>
|
|
<th>文章数</th>
|
|
<th>状态</th>
|
|
<th>操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<% categories.forEach(category => { %>
|
|
<tr data-id="<%= category._id %>">
|
|
<td><%= category.order %></td>
|
|
<td>
|
|
<% if (category.isTop) { %>
|
|
<span class="badge bg-danger me-1">置顶</span>
|
|
<% } %>
|
|
<%= category.name %>
|
|
</td>
|
|
<td><%= category.slug %></td>
|
|
<td><%= category.postCount || 0 %></td>
|
|
<td>
|
|
<% if (category.isActive) { %>
|
|
<span class="badge bg-success">启用</span>
|
|
<% } else { %>
|
|
<span class="badge bg-secondary">禁用</span>
|
|
<% } %>
|
|
</td>
|
|
<td class="action-btns">
|
|
<button class="btn btn-sm btn-primary edit-btn" data-id="<%= category._id %>">编辑</button>
|
|
<button class="btn btn-sm btn-danger delete-btn" data-id="<%= category._id %>">删除</button>
|
|
<% if (category.isTop) { %>
|
|
<button class="btn btn-sm btn-secondary cancel-top-btn" data-id="<%= category._id %>">取消置顶</button>
|
|
<% } else { %>
|
|
<button class="btn btn-sm btn-warning set-top-btn" data-id="<%= category._id %>">设为置顶</button>
|
|
<% } %>
|
|
</td>
|
|
</tr>
|
|
<% }); %>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 添加分类模态框 -->
|
|
<div class="modal fade" id="addCategoryModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">添加分类</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<form id="addCategoryForm" action="/admin/categories" method="POST">
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">分类名称</label>
|
|
<input type="text" class="form-control" name="name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">别名</label>
|
|
<input type="text" class="form-control" name="slug" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">排序值</label>
|
|
<input type="number" class="form-control" name="order" value="0">
|
|
</div>
|
|
<div class="form-check mb-3">
|
|
<input class="form-check-input" type="checkbox" name="isTop" id="isTop">
|
|
<label class="form-check-label" for="isTop">置顶分类</label>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
|
|
<button type="submit" class="btn btn-primary">保存</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
// 分类置顶/取消置顶
|
|
document.querySelectorAll('.set-top-btn, .cancel-top-btn').forEach(btn => {
|
|
btn.addEventListener('click', async function() {
|
|
const categoryId = this.getAttribute('data-id');
|
|
const isTop = this.classList.contains('set-top-btn');
|
|
|
|
try {
|
|
const response = await fetch(`/admin/categories/${categoryId}/top`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ isTop })
|
|
});
|
|
|
|
if (response.ok) {
|
|
location.reload();
|
|
} else {
|
|
alert('操作失败');
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
alert('操作失败');
|
|
}
|
|
});
|
|
});
|
|
|
|
// 删除分类
|
|
document.querySelectorAll('.delete-btn').forEach(btn => {
|
|
btn.addEventListener('click', function() {
|
|
const categoryId = this.getAttribute('data-id');
|
|
if (confirm('确定要删除这个分类吗?')) {
|
|
fetch(`/admin/categories/${categoryId}`, {
|
|
method: 'DELETE'
|
|
})
|
|
.then(response => {
|
|
if (response.ok) {
|
|
location.reload();
|
|
} else {
|
|
alert('删除失败');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error(error);
|
|
alert('删除失败');
|
|
});
|
|
}
|
|
});
|
|
});
|
|
</script>
|