Browse Source

feat: 添加商品下架功能,修改label字段为1

pull/8/head
徐飞洋 2 months ago
parent
commit
937afcae0a
  1. 58
      pages/goods-update/goods-update.js
  2. 4
      pages/goods-update/goods-update.wxml
  3. 78
      server-example/server-mysql.js
  4. 17
      utils/api.js

58
pages/goods-update/goods-update.js

@ -718,6 +718,64 @@ Page({
}); });
}, },
// 准备下架
prepareUnpublishSupply: function() {
console.log('准备下架商品');
const goodsDetail = this.data.goodsDetail;
const productId = goodsDetail.id || goodsDetail.productId;
wx.showModal({
title: '确认下架',
content: '确定要下架此商品吗?',
success: (res) => {
if (res.confirm) {
this.unpublishSupply(productId);
}
}
});
},
// 下架商品
unpublishSupply: function(productId) {
console.log('下架商品,productId:', productId);
wx.showLoading({
title: '下架中...',
mask: true
});
API.unpublishProduct({ productId: productId })
.then(res => {
wx.hideLoading();
console.log('下架商品成功:', res);
if (res && res.code === 200) {
wx.showToast({
title: '下架成功',
icon: 'success',
duration: 2000
});
setTimeout(() => {
wx.navigateBack();
}, 2000);
} else {
wx.showToast({
title: '下架失败',
icon: 'none',
duration: 2000
});
}
})
.catch(err => {
wx.hideLoading();
console.error('下架商品失败:', err);
wx.showToast({
title: '下架失败',
icon: 'none',
duration: 2000
});
});
},
// 编辑输入处理 // 编辑输入处理
onEditInput: function(e) { onEditInput: function(e) {
const field = e.currentTarget.dataset.field; const field = e.currentTarget.dataset.field;

4
pages/goods-update/goods-update.wxml

@ -172,9 +172,9 @@
</button> </button>
<button <button
class="publish-button bottom-button" class="publish-button bottom-button"
bindtap="preparePublishSupply" bindtap="unpublishSupply"
> >
</button> </button>
</view> </view>

78
server-example/server-mysql.js

@ -4145,6 +4145,84 @@ app.post('/api/products/delete', async (req, res) => {
} }
}); });
// 下架商品 - 修改label字段为1
app.post('/api/products/unpublish', async (req, res) => {
console.log('收到下架商品请求:', req.body);
try {
const { productId } = req.body;
if (!productId) {
console.error('下架商品失败: 缺少productId参数');
return res.status(400).json({
success: false,
code: 400,
message: '缺少productId参数'
});
}
// 查找商品
const product = await Product.findOne({
where: { productId }
});
if (!product) {
console.error('下架商品失败: 商品不存在');
return res.status(404).json({
success: false,
code: 404,
message: '商品不存在'
});
}
console.log('准备下架商品,productId:', productId, ',当前label:', product.label);
// 直接使用商品实例更新label字段
product.label = 1;
product.updated_at = getBeijingTime();
try {
await product.save();
console.log('下架商品成功(使用save方法):', { productId: product.productId, newLabel: product.label });
} catch (saveError) {
console.error('使用save方法更新失败,尝试使用update方法:', saveError);
try {
const updateResult = await Product.update(
{ label: 1, updated_at: getBeijingTime() },
{ where: { productId } }
);
console.log('下架商品成功(使用update方法):', { productId, updateResult });
} catch (updateError) {
console.error('使用update方法也失败:', updateError);
throw new Error('下架商品失败: ' + updateError.message);
}
}
// 重新查询商品以确保返回最新状态
const updatedProduct = await Product.findOne({
where: { productId }
});
res.json({
success: true,
code: 200,
message: '下架商品成功',
product: {
productId: updatedProduct.productId,
label: updatedProduct.label
}
});
} catch (error) {
console.error('下架商品失败:', error);
res.status(500).json({
success: false,
code: 500,
message: '下架商品失败',
error: error.message
});
}
});
// 测试路由:用于调试购物车数据格式 - 增强版,包含完整的数量处理测试 // 测试路由:用于调试购物车数据格式 - 增强版,包含完整的数量处理测试
app.post('/api/cart/test-format', async (req, res) => { app.post('/api/cart/test-format', async (req, res) => {
try { try {

17
utils/api.js

@ -917,6 +917,23 @@ module.exports = {
} }
}, },
// 下架商品 - 修改label字段为1
unpublishProduct: function(data) {
console.log('===== unpublishProduct调用开始 =====')
console.log('请求参数:', data)
return request('/api/products/unpublish', 'POST', data)
.then(res => {
console.log('===== 下架商品成功 =====')
console.log('响应数据:', res)
return res
})
.catch(error => {
console.error('===== 下架商品失败 =====')
console.error('错误详情:', error)
throw error
})
},
// 更新商品图片 - 专门用于为已存在的商品上传图片 // 更新商品图片 - 专门用于为已存在的商品上传图片
updateProductImages: function (productId, imageUrls, uploadData) { updateProductImages: function (productId, imageUrls, uploadData) {
console.log('===== updateProductImages调用开始 ====='); console.log('===== updateProductImages调用开始 =====');

Loading…
Cancel
Save