From 937afcae0a47237c62c94e5af9fe02a9b82c3aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Tue, 6 Jan 2026 13:36:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81?= =?UTF-8?q?=E4=B8=8B=E6=9E=B6=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?label=E5=AD=97=E6=AE=B5=E4=B8=BA1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/goods-update/goods-update.js | 58 +++++++++++++++++++++ pages/goods-update/goods-update.wxml | 4 +- server-example/server-mysql.js | 78 ++++++++++++++++++++++++++++ utils/api.js | 17 ++++++ 4 files changed, 155 insertions(+), 2 deletions(-) diff --git a/pages/goods-update/goods-update.js b/pages/goods-update/goods-update.js index 69338a6..4449690 100644 --- a/pages/goods-update/goods-update.js +++ b/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) { const field = e.currentTarget.dataset.field; diff --git a/pages/goods-update/goods-update.wxml b/pages/goods-update/goods-update.wxml index 6f86dec..fbc66c9 100644 --- a/pages/goods-update/goods-update.wxml +++ b/pages/goods-update/goods-update.wxml @@ -172,9 +172,9 @@ diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 3816dc4..cfae1eb 100644 --- a/server-example/server-mysql.js +++ b/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) => { try { diff --git a/utils/api.js b/utils/api.js index 4ec32f3..cea9fc9 100644 --- a/utils/api.js +++ b/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) { console.log('===== updateProductImages调用开始 =====');