node-blog/views/admin/users/list.ejs
2025-06-24 11:42:12 +08:00

103 lines
3.5 KiB
Plaintext

<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">用户列表</h5>
<div>
<input type="text" class="form-control" placeholder="搜索用户..." id="searchUser">
</div>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>用户名</th>
<th>邮箱</th>
<th>注册时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<% users.forEach(user => { %>
<tr>
<td><%= user._id %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= new Date(user.createdAt).toLocaleString() %></td>
<td class="action-btns">
<button class="btn btn-sm btn-info reset-pwd-btn" data-id="<%= user._id %>">重置密码</button>
</td>
</tr>
<% }); %>
</tbody>
</table>
</div>
<!-- 分页组件 -->
<%- include('../../components/pagination', {
currentPage: pagination.currentPage,
totalPages: pagination.totalPages,
baseUrl: baseUrl,
query: query
}) %>
</div>
</div>
<script>
// 冻结/解冻用户
document.querySelectorAll('.freeze-btn, .activate-btn').forEach(btn => {
btn.addEventListener('click', async function() {
const userId = this.getAttribute('data-id');
const isFreeze = this.classList.contains('freeze-btn');
try {
const response = await fetch(`/admin/users/${userId}/status`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ isActive: !isFreeze })
});
if (response.ok) {
location.reload();
} else {
alert('操作失败');
}
} catch (error) {
console.error(error);
alert('操作失败');
}
});
});
// 重置密码
document.querySelectorAll('.reset-pwd-btn').forEach(btn => {
btn.addEventListener('click', function() {
const userId = this.getAttribute('data-id');
const newPassword = prompt('请输入新密码:');
if (newPassword) {
fetch(`/admin/users/${userId}/password`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ newPassword })
})
.then(response => response.json())
.then(result => {
if (result.success) {
alert('密码重置成功');
} else {
alert('密码重置失败');
}
})
.catch(error => {
console.error(error);
alert('密码重置失败');
});
}
});
});
</script>