2025-11-11 21:39:12 +08:00
|
|
|
|
{% extends "base.html" %}
|
|
|
|
|
|
|
|
|
|
|
|
{% block title %}产品管理 - 软件授权管理系统{% endblock %}
|
|
|
|
|
|
|
|
|
|
|
|
{% block page_title %}产品管理{% endblock %}
|
|
|
|
|
|
|
|
|
|
|
|
{% block page_actions %}
|
|
|
|
|
|
<a href="{{ url_for('web.create_product') }}" class="btn btn-primary">
|
|
|
|
|
|
<i class="fas fa-plus me-2"></i>
|
|
|
|
|
|
创建产品
|
|
|
|
|
|
</a>
|
|
|
|
|
|
{% endblock %}
|
|
|
|
|
|
|
|
|
|
|
|
{% block content %}
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<!-- 搜索表单 -->
|
2025-11-11 21:39:12 +08:00
|
|
|
|
<div class="card shadow mb-4">
|
|
|
|
|
|
<div class="card-body">
|
|
|
|
|
|
<form id="search-form" class="row g-3">
|
|
|
|
|
|
<div class="col-md-4">
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<label for="search-keyword" class="form-label">关键词搜索</label>
|
|
|
|
|
|
<input type="text" class="form-control" id="search-keyword" placeholder="产品名称或ID">
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="col-md-8 d-flex align-items-end">
|
|
|
|
|
|
<div class="btn-group" role="group">
|
|
|
|
|
|
<button type="submit" class="btn btn-primary">
|
|
|
|
|
|
<i class="fas fa-search me-2"></i>搜索
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button type="button" class="btn btn-outline-secondary" id="reset-search">
|
|
|
|
|
|
<i class="fas fa-undo me-2"></i>重置
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2025-11-11 21:39:12 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<!-- 批量操作栏 -->
|
|
|
|
|
|
<div class="card shadow mb-4">
|
|
|
|
|
|
<div class="card-body">
|
|
|
|
|
|
<div class="d-flex justify-content-between align-items-center">
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<button type="button" class="btn btn-danger btn-sm" id="batch-delete-btn" style="display: none;">
|
|
|
|
|
|
<i class="fas fa-trash me-1"></i>批量删除
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<div class="btn-group btn-group-sm" id="batch-status-btn" style="display: none;">
|
|
|
|
|
|
<button type="button" class="btn btn-outline-success" id="batch-enable-btn">
|
|
|
|
|
|
<i class="fas fa-check-circle me-1"></i>批量启用
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button type="button" class="btn btn-outline-secondary" id="batch-disable-btn">
|
|
|
|
|
|
<i class="fas fa-ban me-1"></i>批量禁用
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="text-muted small">
|
|
|
|
|
|
<span id="selected-count">已选择 0 项</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-11-11 21:39:12 +08:00
|
|
|
|
<!-- 产品列表 -->
|
|
|
|
|
|
<div class="card shadow">
|
|
|
|
|
|
<div class="card-body">
|
|
|
|
|
|
<div class="table-responsive">
|
|
|
|
|
|
<table class="table table-hover">
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<thead class="table-light">
|
2025-11-11 21:39:12 +08:00
|
|
|
|
<tr>
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<th width="50">
|
|
|
|
|
|
<input type="checkbox" id="select-all-checkbox" class="form-check-input">
|
|
|
|
|
|
</th>
|
2025-11-11 21:39:12 +08:00
|
|
|
|
<th>产品ID</th>
|
|
|
|
|
|
<th>产品名称</th>
|
|
|
|
|
|
<th>描述</th>
|
|
|
|
|
|
<th>状态</th>
|
|
|
|
|
|
<th>卡密统计</th>
|
|
|
|
|
|
<th>设备统计</th>
|
|
|
|
|
|
<th>创建时间</th>
|
|
|
|
|
|
<th>操作</th>
|
|
|
|
|
|
</tr>
|
|
|
|
|
|
</thead>
|
|
|
|
|
|
<tbody id="product-list">
|
|
|
|
|
|
<tr>
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<td colspan="9" class="text-center text-muted">加载中...</td>
|
2025-11-11 21:39:12 +08:00
|
|
|
|
</tr>
|
|
|
|
|
|
</tbody>
|
|
|
|
|
|
</table>
|
|
|
|
|
|
</div>
|
2025-11-12 15:11:05 +08:00
|
|
|
|
|
2025-11-11 21:39:12 +08:00
|
|
|
|
<!-- 分页 -->
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<nav aria-label="分页导航">
|
|
|
|
|
|
<ul class="pagination justify-content-center mb-0" id="pagination">
|
2025-11-11 21:39:12 +08:00
|
|
|
|
</ul>
|
|
|
|
|
|
</nav>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- 删除确认模态框 -->
|
|
|
|
|
|
<div class="modal fade" id="deleteModal" tabindex="-1">
|
|
|
|
|
|
<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"></button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="modal-body">
|
2025-11-12 15:11:05 +08:00
|
|
|
|
确定要删除产品 "<strong id="delete-product-name"></strong>" 吗?此操作不可恢复。
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="modal-footer">
|
|
|
|
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
|
|
|
|
|
|
<button type="button" class="btn btn-danger" id="confirm-delete">确定删除</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- 批量删除确认模态框 -->
|
|
|
|
|
|
<div class="modal fade" id="batchDeleteModal" tabindex="-1">
|
|
|
|
|
|
<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"></button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="modal-body">
|
|
|
|
|
|
确定要删除选中的 <strong id="batch-delete-count"></strong> 个产品吗?此操作不可恢复。
|
2025-11-11 21:39:12 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="modal-footer">
|
|
|
|
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<button type="button" class="btn btn-danger" id="confirm-batch-delete">确定删除</button>
|
2025-11-11 21:39:12 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{% endblock %}
|
|
|
|
|
|
|
2025-11-15 23:57:05 +08:00
|
|
|
|
{% block extra_js %}
|
2025-11-11 21:39:12 +08:00
|
|
|
|
<script>
|
|
|
|
|
|
let currentPage = 1;
|
|
|
|
|
|
let currentKeyword = '';
|
|
|
|
|
|
|
|
|
|
|
|
// 页面加载完成后初始化
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// 使用立即执行函数确保在DOM和所有脚本加载完成后执行
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
function init() {
|
|
|
|
|
|
console.log('产品列表页面已加载,开始初始化...');
|
|
|
|
|
|
console.log('apiRequest函数是否存在:', typeof apiRequest);
|
|
|
|
|
|
if (typeof apiRequest === 'function') {
|
|
|
|
|
|
loadProducts();
|
|
|
|
|
|
initEventListeners();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error('apiRequest函数未定义,等待脚本加载...');
|
|
|
|
|
|
// 如果apiRequest未定义,等待一段时间后重试
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
|
if (typeof apiRequest === 'function') {
|
|
|
|
|
|
loadProducts();
|
|
|
|
|
|
initEventListeners();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.error('apiRequest函数仍未定义,请检查脚本加载顺序');
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (document.readyState === 'loading') {
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// DOM已经加载完成,直接执行
|
|
|
|
|
|
init();
|
|
|
|
|
|
}
|
|
|
|
|
|
})();
|
2025-11-11 21:39:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化事件监听器
|
|
|
|
|
|
function initEventListeners() {
|
|
|
|
|
|
// 搜索表单
|
|
|
|
|
|
document.getElementById('search-form').addEventListener('submit', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
currentKeyword = document.getElementById('search-keyword').value.trim();
|
|
|
|
|
|
currentPage = 1;
|
|
|
|
|
|
loadProducts();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 重置搜索
|
|
|
|
|
|
document.getElementById('reset-search').addEventListener('click', function() {
|
|
|
|
|
|
document.getElementById('search-keyword').value = '';
|
|
|
|
|
|
currentKeyword = '';
|
|
|
|
|
|
currentPage = 1;
|
|
|
|
|
|
loadProducts();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 删除确认
|
|
|
|
|
|
document.getElementById('confirm-delete').addEventListener('click', function() {
|
|
|
|
|
|
const productId = document.getElementById('confirm-delete').dataset.productId;
|
|
|
|
|
|
deleteProduct(productId);
|
|
|
|
|
|
});
|
2025-11-12 15:11:05 +08:00
|
|
|
|
|
|
|
|
|
|
// 批量删除确认
|
|
|
|
|
|
document.getElementById('confirm-batch-delete').addEventListener('click', function() {
|
|
|
|
|
|
batchDeleteProducts();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 全选/取消全选
|
|
|
|
|
|
document.getElementById('select-all-checkbox').addEventListener('change', function() {
|
|
|
|
|
|
const checkboxes = document.querySelectorAll('.product-checkbox');
|
|
|
|
|
|
checkboxes.forEach(checkbox => {
|
|
|
|
|
|
checkbox.checked = this.checked;
|
|
|
|
|
|
});
|
|
|
|
|
|
updateBatchButtons();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 批量启用
|
|
|
|
|
|
document.getElementById('batch-enable-btn').addEventListener('click', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
batchUpdateProductStatus(1);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 批量禁用
|
|
|
|
|
|
document.getElementById('batch-disable-btn').addEventListener('click', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
batchUpdateProductStatus(0);
|
|
|
|
|
|
});
|
2025-11-11 21:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 加载产品列表
|
|
|
|
|
|
function loadProducts(page = 1) {
|
2025-11-15 23:57:05 +08:00
|
|
|
|
console.log('loadProducts函数被调用,页码:', page);
|
2025-11-11 21:39:12 +08:00
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
|
|
page: page,
|
|
|
|
|
|
per_page: 10
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (currentKeyword) {
|
|
|
|
|
|
params.append('keyword', currentKeyword);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-13 16:51:51 +08:00
|
|
|
|
const apiUrl = `/api/v1/products?${params}`;
|
2025-11-17 12:55:58 +08:00
|
|
|
|
console.log('请求API URL:', apiUrl);
|
2025-11-15 23:57:05 +08:00
|
|
|
|
console.log('准备请求API:', apiUrl);
|
|
|
|
|
|
|
|
|
|
|
|
// 使用apiRequest函数,它已经处理了加载动画和错误处理
|
2025-11-13 16:51:51 +08:00
|
|
|
|
apiRequest(apiUrl)
|
2025-11-11 21:39:12 +08:00
|
|
|
|
.then(data => {
|
2025-11-13 16:51:51 +08:00
|
|
|
|
if (data && data.success && data.data) {
|
2025-11-11 21:39:12 +08:00
|
|
|
|
renderProductList(data.data.products);
|
|
|
|
|
|
renderPagination(data.data.pagination);
|
|
|
|
|
|
} else {
|
2025-11-13 16:51:51 +08:00
|
|
|
|
renderProductList([]);
|
|
|
|
|
|
renderPagination({ pages: 0, page: 1, has_prev: false, has_next: false });
|
|
|
|
|
|
if (data && data.message) {
|
|
|
|
|
|
showNotification(data.message, 'error');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showNotification('加载产品列表失败', 'error');
|
|
|
|
|
|
}
|
2025-11-11 21:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(error => {
|
|
|
|
|
|
console.error('Failed to load products:', error);
|
2025-11-13 16:51:51 +08:00
|
|
|
|
// 确保在任何错误情况下都清除加载状态
|
|
|
|
|
|
renderProductList([]);
|
|
|
|
|
|
renderPagination({ pages: 0, page: 1, has_prev: false, has_next: false });
|
|
|
|
|
|
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// apiRequest已经处理了401和403错误,这里只处理其他错误
|
|
|
|
|
|
if (error.message && !error.message.includes('401') && !error.message.includes('403')) {
|
|
|
|
|
|
showNotification('加载产品列表失败: ' + error.message, 'error');
|
2025-11-13 16:51:51 +08:00
|
|
|
|
}
|
2025-11-11 21:39:12 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 渲染产品列表
|
|
|
|
|
|
function renderProductList(products) {
|
|
|
|
|
|
const tbody = document.getElementById('product-list');
|
2025-11-13 16:51:51 +08:00
|
|
|
|
|
|
|
|
|
|
// 防御式编程:确保tbody存在
|
|
|
|
|
|
if (!tbody) {
|
|
|
|
|
|
console.error('产品列表容器未找到');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查产品数据是否有效
|
|
|
|
|
|
if (!Array.isArray(products) || products.length === 0) {
|
2025-11-12 15:11:05 +08:00
|
|
|
|
tbody.innerHTML = '<tr><td colspan="9" class="text-center text-muted">暂无数据</td></tr>';
|
2025-11-11 21:39:12 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tbody.innerHTML = products.map(product => `
|
|
|
|
|
|
<tr>
|
2025-11-12 15:11:05 +08:00
|
|
|
|
<td>
|
|
|
|
|
|
<input type="checkbox" class="product-checkbox" data-product-id="${product.product_id}">
|
|
|
|
|
|
</td>
|
2025-11-11 21:39:12 +08:00
|
|
|
|
<td><code>${product.product_id}</code></td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<strong>${product.product_name}</strong>
|
|
|
|
|
|
${product.latest_version ? `<br><small class="text-muted">最新版本: ${product.latest_version}</small>` : ''}
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td>${product.description || '-'}</td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<span class="badge ${product.status === 1 ? 'bg-success' : 'bg-secondary'}">
|
|
|
|
|
|
${product.status_name}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<small>
|
|
|
|
|
|
总计: ${product.total_licenses || 0}<br>
|
|
|
|
|
|
活跃: <span class="text-success">${product.active_licenses || 0}</span>
|
|
|
|
|
|
</small>
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<small>
|
|
|
|
|
|
在线: <span class="text-success">${product.total_devices || 0}</span>
|
|
|
|
|
|
</small>
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<small>${formatDate(product.create_time)}</small>
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<div class="btn-group btn-group-sm" role="group">
|
|
|
|
|
|
<a href="/products/${product.product_id}" class="btn btn-outline-primary" title="查看">
|
|
|
|
|
|
<i class="fas fa-eye"></i>
|
|
|
|
|
|
</a>
|
|
|
|
|
|
<a href="/products/${product.product_id}/edit" class="btn btn-outline-secondary" title="编辑">
|
|
|
|
|
|
<i class="fas fa-edit"></i>
|
|
|
|
|
|
</a>
|
|
|
|
|
|
<button type="button" class="btn btn-outline-danger btn-delete"
|
|
|
|
|
|
data-product-id="${product.product_id}"
|
|
|
|
|
|
data-product-name="${product.product_name}"
|
|
|
|
|
|
title="删除">
|
|
|
|
|
|
<i class="fas fa-trash"></i>
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</td>
|
|
|
|
|
|
</tr>
|
|
|
|
|
|
`).join('');
|
|
|
|
|
|
|
|
|
|
|
|
// 绑定删除按钮事件
|
|
|
|
|
|
document.querySelectorAll('.btn-delete').forEach(btn => {
|
|
|
|
|
|
btn.addEventListener('click', function() {
|
|
|
|
|
|
const productId = this.dataset.productId;
|
|
|
|
|
|
const productName = this.dataset.productName;
|
|
|
|
|
|
showDeleteModal(productId, productName);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2025-11-12 15:11:05 +08:00
|
|
|
|
|
|
|
|
|
|
// 绑定复选框事件
|
|
|
|
|
|
document.querySelectorAll('.product-checkbox').forEach(checkbox => {
|
|
|
|
|
|
checkbox.addEventListener('change', updateBatchButtons);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 重置全选复选框
|
|
|
|
|
|
document.getElementById('select-all-checkbox').checked = false;
|
|
|
|
|
|
updateBatchButtons();
|
2025-11-11 21:39:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 渲染分页
|
|
|
|
|
|
function renderPagination(pagination) {
|
|
|
|
|
|
const paginationEl = document.getElementById('pagination');
|
2025-11-13 16:51:51 +08:00
|
|
|
|
|
|
|
|
|
|
// 防御式编程:确保分页容器存在
|
|
|
|
|
|
if (!paginationEl) {
|
|
|
|
|
|
console.error('分页容器未找到');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查分页数据是否有效
|
|
|
|
|
|
if (!pagination || typeof pagination !== 'object' || !Number.isFinite(pagination.pages) || pagination.pages <= 1) {
|
2025-11-11 21:39:12 +08:00
|
|
|
|
paginationEl.innerHTML = '';
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let html = '';
|
|
|
|
|
|
|
|
|
|
|
|
// 上一页
|
|
|
|
|
|
if (pagination.has_prev) {
|
|
|
|
|
|
html += `<li class="page-item">
|
|
|
|
|
|
<a class="page-link" href="#" data-page="${pagination.page - 1}">上一页</a>
|
|
|
|
|
|
</li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 页码
|
|
|
|
|
|
const startPage = Math.max(1, pagination.page - 2);
|
|
|
|
|
|
const endPage = Math.min(pagination.pages, pagination.page + 2);
|
|
|
|
|
|
|
|
|
|
|
|
if (startPage > 1) {
|
|
|
|
|
|
html += `<li class="page-item"><a class="page-link" href="#" data-page="1">1</a></li>`;
|
|
|
|
|
|
if (startPage > 2) {
|
|
|
|
|
|
html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = startPage; i <= endPage; i++) {
|
|
|
|
|
|
html += `<li class="page-item ${i === pagination.page ? 'active' : ''}">
|
|
|
|
|
|
<a class="page-link" href="#" data-page="${i}">${i}</a>
|
|
|
|
|
|
</li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (endPage < pagination.pages) {
|
|
|
|
|
|
if (endPage < pagination.pages - 1) {
|
|
|
|
|
|
html += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
html += `<li class="page-item"><a class="page-link" href="#" data-page="${pagination.pages}">${pagination.pages}</a></li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 下一页
|
|
|
|
|
|
if (pagination.has_next) {
|
|
|
|
|
|
html += `<li class="page-item">
|
|
|
|
|
|
<a class="page-link" href="#" data-page="${pagination.page + 1}">下一页</a>
|
|
|
|
|
|
</li>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
paginationEl.innerHTML = html;
|
|
|
|
|
|
|
|
|
|
|
|
// 绑定分页点击事件
|
|
|
|
|
|
paginationEl.querySelectorAll('.page-link').forEach(link => {
|
|
|
|
|
|
link.addEventListener('click', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
const page = parseInt(this.dataset.page);
|
|
|
|
|
|
if (page && page !== currentPage) {
|
|
|
|
|
|
currentPage = page;
|
|
|
|
|
|
loadProducts(page);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 显示删除确认模态框
|
|
|
|
|
|
function showDeleteModal(productId, productName) {
|
|
|
|
|
|
document.getElementById('delete-product-name').textContent = productName;
|
|
|
|
|
|
document.getElementById('confirm-delete').dataset.productId = productId;
|
|
|
|
|
|
|
|
|
|
|
|
const modal = new bootstrap.Modal(document.getElementById('deleteModal'));
|
|
|
|
|
|
modal.show();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除产品
|
|
|
|
|
|
function deleteProduct(productId) {
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// 显示加载动画
|
|
|
|
|
|
showLoading();
|
|
|
|
|
|
|
|
|
|
|
|
fetch(`/api/v1/products/${productId}`, {
|
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
|
credentials: 'same-origin',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(response => {
|
|
|
|
|
|
// 隐藏加载动画
|
|
|
|
|
|
hideLoading();
|
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 401) {
|
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
|
throw new Error('未授权访问');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response.json();
|
2025-11-11 21:39:12 +08:00
|
|
|
|
})
|
|
|
|
|
|
.then(data => {
|
2025-11-13 16:51:51 +08:00
|
|
|
|
if (data && data.success) {
|
2025-11-11 21:39:12 +08:00
|
|
|
|
showNotification('产品删除成功', 'success');
|
|
|
|
|
|
loadProducts(currentPage);
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭模态框
|
|
|
|
|
|
const modal = bootstrap.Modal.getInstance(document.getElementById('deleteModal'));
|
|
|
|
|
|
modal.hide();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showNotification(data.message || '删除失败', 'error');
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(error => {
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// 隐藏加载动画
|
|
|
|
|
|
hideLoading();
|
|
|
|
|
|
|
2025-11-11 21:39:12 +08:00
|
|
|
|
console.error('Failed to delete product:', error);
|
2025-11-13 16:51:51 +08:00
|
|
|
|
showNotification('删除失败: ' + (error.message || '未知错误'), 'error');
|
2025-11-11 21:39:12 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-11-12 15:11:05 +08:00
|
|
|
|
|
|
|
|
|
|
// 更新批量操作按钮状态
|
|
|
|
|
|
function updateBatchButtons() {
|
|
|
|
|
|
const selectedCount = document.querySelectorAll('.product-checkbox:checked').length;
|
|
|
|
|
|
const batchDeleteBtn = document.getElementById('batch-delete-btn');
|
|
|
|
|
|
const batchStatusBtn = document.getElementById('batch-status-btn');
|
|
|
|
|
|
|
|
|
|
|
|
if (selectedCount > 0) {
|
|
|
|
|
|
batchDeleteBtn.style.display = 'inline-block';
|
|
|
|
|
|
batchStatusBtn.style.display = 'inline-block';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
batchDeleteBtn.style.display = 'none';
|
|
|
|
|
|
batchStatusBtn.style.display = 'none';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新选中数量显示
|
|
|
|
|
|
document.getElementById('selected-count').textContent = `已选择 ${selectedCount} 项`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 显示批量删除确认模态框
|
|
|
|
|
|
function showBatchDeleteModal() {
|
|
|
|
|
|
const selectedCount = document.querySelectorAll('.product-checkbox:checked').length;
|
|
|
|
|
|
document.getElementById('batch-delete-count').textContent = selectedCount;
|
|
|
|
|
|
|
|
|
|
|
|
const modal = new bootstrap.Modal(document.getElementById('batchDeleteModal'));
|
|
|
|
|
|
modal.show();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量删除产品
|
|
|
|
|
|
function batchDeleteProducts() {
|
|
|
|
|
|
const selectedCheckboxes = document.querySelectorAll('.product-checkbox:checked');
|
|
|
|
|
|
const productIds = Array.from(selectedCheckboxes).map(checkbox => checkbox.dataset.productId);
|
|
|
|
|
|
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// 显示加载动画
|
|
|
|
|
|
showLoading();
|
|
|
|
|
|
|
|
|
|
|
|
fetch('/api/v1/products/batch', {
|
2025-11-12 15:11:05 +08:00
|
|
|
|
method: 'DELETE',
|
2025-11-15 23:57:05 +08:00
|
|
|
|
credentials: 'same-origin',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
},
|
2025-11-12 15:11:05 +08:00
|
|
|
|
body: JSON.stringify({ product_ids: productIds })
|
|
|
|
|
|
})
|
2025-11-15 23:57:05 +08:00
|
|
|
|
.then(response => {
|
|
|
|
|
|
// 隐藏加载动画
|
|
|
|
|
|
hideLoading();
|
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 401) {
|
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
|
throw new Error('未授权访问');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
|
|
})
|
2025-11-12 15:11:05 +08:00
|
|
|
|
.then(data => {
|
2025-11-13 16:51:51 +08:00
|
|
|
|
if (data && data.success) {
|
2025-11-12 15:11:05 +08:00
|
|
|
|
showNotification(data.message || '批量删除成功', 'success');
|
|
|
|
|
|
loadProducts(currentPage);
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭模态框
|
|
|
|
|
|
const modal = bootstrap.Modal.getInstance(document.getElementById('batchDeleteModal'));
|
|
|
|
|
|
modal.hide();
|
|
|
|
|
|
|
|
|
|
|
|
// 重置全选复选框
|
|
|
|
|
|
document.getElementById('select-all-checkbox').checked = false;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showNotification(data.message || '批量删除失败', 'error');
|
|
|
|
|
|
|
|
|
|
|
|
// 如果有无法删除的产品,显示详细信息
|
|
|
|
|
|
if (data.undeletable_products) {
|
|
|
|
|
|
const undeletableInfo = data.undeletable_products.map(p =>
|
|
|
|
|
|
`${p.product_name} (${p.license_count}个卡密)`
|
|
|
|
|
|
).join(', ');
|
|
|
|
|
|
showNotification(`以下产品无法删除: ${undeletableInfo}`, 'error');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭模态框
|
|
|
|
|
|
const modal = bootstrap.Modal.getInstance(document.getElementById('batchDeleteModal'));
|
|
|
|
|
|
modal.hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(error => {
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// 隐藏加载动画
|
|
|
|
|
|
hideLoading();
|
|
|
|
|
|
|
2025-11-12 15:11:05 +08:00
|
|
|
|
console.error('Failed to batch delete products:', error);
|
2025-11-13 16:51:51 +08:00
|
|
|
|
showNotification('批量删除失败: ' + (error.message || '未知错误'), 'error');
|
2025-11-12 15:11:05 +08:00
|
|
|
|
|
|
|
|
|
|
// 关闭模态框
|
|
|
|
|
|
const modal = bootstrap.Modal.getInstance(document.getElementById('batchDeleteModal'));
|
|
|
|
|
|
modal.hide();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量更新产品状态
|
|
|
|
|
|
function batchUpdateProductStatus(status) {
|
|
|
|
|
|
const selectedCheckboxes = document.querySelectorAll('.product-checkbox:checked');
|
|
|
|
|
|
const productIds = Array.from(selectedCheckboxes).map(checkbox => checkbox.dataset.productId);
|
|
|
|
|
|
|
|
|
|
|
|
if (productIds.length === 0) {
|
|
|
|
|
|
showNotification('请至少选择一个产品', 'warning');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// 显示加载动画
|
|
|
|
|
|
showLoading();
|
|
|
|
|
|
|
|
|
|
|
|
fetch('/api/v1/products/batch/status', {
|
2025-11-12 15:11:05 +08:00
|
|
|
|
method: 'PUT',
|
2025-11-15 23:57:05 +08:00
|
|
|
|
credentials: 'same-origin',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
},
|
2025-11-12 15:11:05 +08:00
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
|
product_ids: productIds,
|
|
|
|
|
|
status: status
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
2025-11-15 23:57:05 +08:00
|
|
|
|
.then(response => {
|
|
|
|
|
|
// 隐藏加载动画
|
|
|
|
|
|
hideLoading();
|
|
|
|
|
|
|
|
|
|
|
|
if (response.status === 401) {
|
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
|
throw new Error('未授权访问');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response.json();
|
|
|
|
|
|
})
|
2025-11-12 15:11:05 +08:00
|
|
|
|
.then(data => {
|
2025-11-13 16:51:51 +08:00
|
|
|
|
if (data && data.success) {
|
2025-11-12 15:11:05 +08:00
|
|
|
|
showNotification(data.message || '批量更新状态成功', 'success');
|
|
|
|
|
|
loadProducts(currentPage);
|
|
|
|
|
|
|
|
|
|
|
|
// 重置全选复选框
|
|
|
|
|
|
document.getElementById('select-all-checkbox').checked = false;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showNotification(data.message || '批量更新状态失败', 'error');
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(error => {
|
2025-11-15 23:57:05 +08:00
|
|
|
|
// 隐藏加载动画
|
|
|
|
|
|
hideLoading();
|
|
|
|
|
|
|
2025-11-12 15:11:05 +08:00
|
|
|
|
console.error('Failed to batch update product status:', error);
|
2025-11-13 16:51:51 +08:00
|
|
|
|
showNotification('批量更新状态失败: ' + (error.message || '未知错误'), 'error');
|
2025-11-12 15:11:05 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 在页面加载完成后为批量删除按钮绑定事件
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
|
|
const batchDeleteBtn = document.getElementById('batch-delete-btn');
|
|
|
|
|
|
if (batchDeleteBtn) {
|
|
|
|
|
|
batchDeleteBtn.addEventListener('click', showBatchDeleteModal);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-11-11 21:39:12 +08:00
|
|
|
|
</script>
|
2025-11-15 23:57:05 +08:00
|
|
|
|
{% endblock %}
|