taibai_shopping/app/static/js/favorites.js
2025-07-09 05:22:28 +08:00

230 lines
6.6 KiB
JavaScript

// 收藏页面JavaScript
let selectedItems = [];
let isSelectAll = false;
document.addEventListener('DOMContentLoaded', function() {
// 初始化事件监听
initEventListeners();
});
function initEventListeners() {
// 复选框变化事件
document.querySelectorAll('.favorite-checkbox').forEach(checkbox => {
checkbox.addEventListener('change', function() {
updateSelectedItems();
});
});
}
function updateSelectedItems() {
selectedItems = [];
document.querySelectorAll('.favorite-checkbox:checked').forEach(checkbox => {
selectedItems.push(parseInt(checkbox.value));
});
// 更新按钮状态
const batchBtn = document.querySelector('[onclick="batchRemove()"]');
if (batchBtn) {
batchBtn.disabled = selectedItems.length === 0;
}
}
function toggleSelectAll() {
isSelectAll = !isSelectAll;
const checkboxes = document.querySelectorAll('.favorite-checkbox');
checkboxes.forEach(checkbox => {
checkbox.checked = isSelectAll;
});
updateSelectedItems();
// 更新按钮文本
const selectAllBtn = document.querySelector('[onclick="toggleSelectAll()"]');
if (selectAllBtn) {
selectAllBtn.innerHTML = isSelectAll ?
'<i class="bi bi-square"></i> 取消全选' :
'<i class="bi bi-check-square"></i> 全选';
}
}
function removeFavorite(productId) {
if (confirm('确定要取消收藏这个商品吗?')) {
fetch('/favorite/remove', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
product_id: productId
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
showSuccessMessage(data.message);
// 移除商品卡片
const itemElement = document.querySelector(`[data-product-id="${productId}"]`);
if (itemElement) {
itemElement.remove();
}
// 更新收藏数量
updateFavoriteCount();
// 检查是否为空
checkEmptyState();
} else {
showErrorMessage(data.message);
}
})
.catch(error => {
console.error('Error:', error);
showErrorMessage('操作失败,请稍后再试');
});
}
}
function batchRemove() {
if (selectedItems.length === 0) {
showErrorMessage('请选择要删除的商品');
return;
}
const modal = new bootstrap.Modal(document.getElementById('confirmModal'));
document.getElementById('confirmMessage').textContent =
`确定要取消收藏这 ${selectedItems.length} 个商品吗?`;
document.getElementById('confirmBtn').onclick = function() {
performBatchRemove();
modal.hide();
};
modal.show();
}
function performBatchRemove() {
fetch('/favorite/batch-remove', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
product_ids: selectedItems
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
showSuccessMessage(data.message);
// 移除选中的商品卡片
selectedItems.forEach(productId => {
const itemElement = document.querySelector(`[data-product-id="${productId}"]`);
if (itemElement) {
itemElement.remove();
}
});
// 重置选择状态
selectedItems = [];
isSelectAll = false;
updateSelectedItems();
// 更新收藏数量
updateFavoriteCount();
// 检查是否为空
checkEmptyState();
} else {
showErrorMessage(data.message);
}
})
.catch(error => {
console.error('Error:', error);
showErrorMessage('批量删除失败,请稍后再试');
});
}
function addToCart(productId) {
// 调用购物车添加功能
fetch('/cart/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
product_id: productId,
quantity: 1
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
showSuccessMessage(data.message);
// 更新购物车数量
if (typeof updateCartBadge === 'function') {
updateCartBadge(data.cart_count);
}
} else {
showErrorMessage(data.message);
}
})
.catch(error => {
console.error('Error:', error);
showErrorMessage('加入购物车失败,请稍后再试');
});
}
function updateFavoriteCount() {
fetch('/favorite/count')
.then(response => response.json())
.then(data => {
if (data.success) {
// 更新收藏数量显示
const badge = document.querySelector('.badge.bg-secondary');
if (badge) {
badge.textContent = `${data.favorite_count} 件商品`;
}
}
})
.catch(error => {
console.error('Error:', error);
});
}
function checkEmptyState() {
const itemsContainer = document.querySelector('.row');
const items = itemsContainer.querySelectorAll('.favorite-item');
if (items.length === 0) {
// 显示空状态
itemsContainer.innerHTML = `
<div class="col-12">
<div class="empty-state">
<div class="text-center py-5">
<i class="bi bi-heart display-1 text-muted"></i>
<h4 class="mt-3 text-muted">还没有收藏任何商品</h4>
<p class="text-muted">去逛逛,收藏心仪的商品吧~</p>
<a href="/" class="btn btn-primary">
<i class="bi bi-house"></i> 去首页逛逛
</a>
</div>
</div>
</div>
`;
}
}
function showSuccessMessage(message) {
// 使用现有的消息提示函数
if (typeof showMessage === 'function') {
showMessage(message, 'success');
} else {
alert(message);
}
}
function showErrorMessage(message) {
// 使用现有的消息提示函数
if (typeof showMessage === 'function') {
showMessage(message, 'error');
} else {
alert(message);
}
}