Browse Source

修复下架功能的确认对话框代码结构

pull/1/head
徐飞洋 3 months ago
parent
commit
58a1c0d772
  1. 162
      pages/seller/index.js

162
pages/seller/index.js

@ -3854,90 +3854,104 @@ Page({
} }
const supplyId = e.currentTarget.dataset.id; const supplyId = e.currentTarget.dataset.id;
console.log('下架商品 - ID:', supplyId);
console.log('当前商品列表长度:', this.data.supplies.length); // 添加确认对话框,防止误触
console.log('当前已上架商品列表长度:', this.data.publishedSupplies.length); wx.showModal({
title: '确认下架',
// 查找对应商品 content: '确定要下架该商品吗?',
let supply = this.data.supplies.find(s => s.id === supplyId); confirmText: '确定',
cancelText: '取消',
// 如果在主列表中找不到,尝试在已上架商品列表中查找 success: (res) => {
if (!supply) { if (res.confirm) {
supply = this.data.publishedSupplies.find(s => s.id === supplyId); console.log('下架商品 - ID:', supplyId);
console.log('在已上架列表中查找结果:', supply ? '找到' : '未找到'); console.log('当前商品列表长度:', this.data.supplies.length);
} console.log('当前已上架商品列表长度:', this.data.publishedSupplies.length);
// 如果仍然找不到商品,尝试直接使用传入的ID下架(容错处理) // 查找对应商品
if (!supply) { let supply = this.data.supplies.find(s => s.id === supplyId);
console.warn('未在本地列表中找到商品,但尝试直接下架:', supplyId);
// 禁用页面滚动 // 如果在主列表中找不到,尝试在已上架商品列表中查找
this.disablePageScroll(); if (!supply) {
wx.showLoading({ title: '下架中...', mask: true }); supply = this.data.publishedSupplies.find(s => s.id === supplyId);
console.log('在已上架列表中查找结果:', supply ? '找到' : '未找到');
}
// 直接使用传入的ID尝试下架 // 如果仍然找不到商品,尝试直接使用传入的ID下架(容错处理)
API.hideProduct(supplyId) if (!supply) {
.then(res => { console.warn('未在本地列表中找到商品,但尝试直接下架:', supplyId);
console.log('直接下架成功:', res);
wx.hideLoading();
this.enablePageScroll();
wx.showToast({ title: '已下架', icon: 'success', duration: 2000 });
// 清理购物车并重新加载列表 // 禁用页面滚动
this.cleanUnpublishedFromAllCarts(supplyId); this.disablePageScroll();
setTimeout(() => { wx.showLoading({ title: '下架中...', mask: true });
this.loadSupplies();
}, 100); // 直接使用传入的ID尝试下架
}) API.hideProduct(supplyId)
.catch(err => { .then(res => {
console.error('直接下架失败:', err); console.log('直接下架成功:', res);
wx.hideLoading(); wx.hideLoading();
this.enablePageScroll(); this.enablePageScroll();
wx.showToast({ title: '下架失败,请重试', icon: 'none', duration: 2000 }); wx.showToast({ title: '已下架', icon: 'success', duration: 2000 });
});
return; // 清理购物车并重新加载列表
} this.cleanUnpublishedFromAllCarts(supplyId);
setTimeout(() => {
this.loadSupplies();
}, 100);
})
.catch(err => {
console.error('直接下架失败:', err);
wx.hideLoading();
this.enablePageScroll();
wx.showToast({ title: '下架失败,请重试', icon: 'none', duration: 2000 });
});
return;
}
// 检查是否有serverProductId,只有上传到服务器的商品才能下架 // 检查是否有serverProductId,只有上传到服务器的商品才能下架
if (!supply.serverProductId) { if (!supply.serverProductId) {
// 没有serverProductId,提示用户 // 没有serverProductId,提示用户
wx.showToast({ title: '无法下架,商品未上传到服务器', icon: 'none', duration: 2000 }); wx.showToast({ title: '无法下架,商品未上传到服务器', icon: 'none', duration: 2000 });
return; return;
} }
// 禁用页面滚动 // 禁用页面滚动
this.disablePageScroll(); this.disablePageScroll();
wx.showLoading({ title: '下架中...', mask: true }); wx.showLoading({ title: '下架中...', mask: true });
// 调用API下架商品 // 调用API下架商品
API.hideProduct(supply.serverProductId) API.hideProduct(supply.serverProductId)
.then(res => { .then(res => {
console.log('服务器下架成功:', res); console.log('服务器下架成功:', res);
wx.hideLoading(); wx.hideLoading();
// 恢复页面滚动 // 恢复页面滚动
this.enablePageScroll(); this.enablePageScroll();
wx.showToast({ title: '已下架', icon: 'success', duration: 2000 }); wx.showToast({ title: '已下架', icon: 'success', duration: 2000 });
// 清理所有用户购物车中已下架的商品 // 清理所有用户购物车中已下架的商品
this.cleanUnpublishedFromAllCarts(supply.serverProductId); this.cleanUnpublishedFromAllCarts(supply.serverProductId);
// 只需要调用一次loadSupplies // 只需要调用一次loadSupplies
setTimeout(() => { setTimeout(() => {
this.loadSupplies(); this.loadSupplies();
}, 100); }, 100);
}) })
.catch(err => { .catch(err => {
console.error('服务器下架失败:', err); console.error('服务器下架失败:', err);
wx.hideLoading(); wx.hideLoading();
// 恢复页面滚动 // 恢复页面滚动
this.enablePageScroll(); this.enablePageScroll();
wx.showToast({ title: '服务器同步失败,请重试', icon: 'none', duration: 3000 }); wx.showToast({ title: '服务器同步失败,请重试', icon: 'none', duration: 3000 });
// 只需要调用一次loadSupplies // 只需要调用一次loadSupplies
setTimeout(() => { setTimeout(() => {
this.loadSupplies(); this.loadSupplies();
}, 100); }, 100);
}); });
} else if (res.cancel) {
console.log('用户取消下架操作');
}
}
});
}, },
// 清理所有用户购物车中已下架的商品 - 移除本地存储操作 // 清理所有用户购物车中已下架的商品 - 移除本地存储操作

Loading…
Cancel
Save