From 280cbb82b31f352c8762a4559c184b17a6fcf597 Mon Sep 17 00:00:00 2001 From: dzq Date: Fri, 25 Apr 2025 11:42:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=95=86=E5=93=81=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=8D=95=E4=B8=AA=E5=95=86?= =?UTF-8?q?=E5=93=81=E4=BF=A1=E6=81=AF=E7=9A=84API=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=BA=93=E5=AD=98=E9=85=8D=E7=BD=AE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增`getGoodsInfo` API用于获取单个商品的详细信息,并在库存配置时增加剩余库存的校验和提示,确保分配的库存不超过剩余库存。 --- src/api/shop/goods.ts | 9 +++++++++ src/views/shop/cabinet-goods/index.vue | 24 +++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/api/shop/goods.ts b/src/api/shop/goods.ts index 6d07df0..6e5ea7a 100644 --- a/src/api/shop/goods.ts +++ b/src/api/shop/goods.ts @@ -70,6 +70,15 @@ export const getGoodsListApi = (params?: GoodsQuery) => { }); }; +/** 获取单个商品信息 */ +export const getGoodsInfo = (goodsId: number) => { + return http.request>("get", "/shop/goods/getGoodsInfo", { + params: { + goodsId + } + }); +}; + /** 新增商品 */ export const addGoodsApi = (data: GoodsRequest) => { return http.request>("post", "/shop/goods", { diff --git a/src/views/shop/cabinet-goods/index.vue b/src/views/shop/cabinet-goods/index.vue index 6dbd93f..78ff1e6 100644 --- a/src/views/shop/cabinet-goods/index.vue +++ b/src/views/shop/cabinet-goods/index.vue @@ -13,6 +13,7 @@ import Refresh from "@iconify-icons/ep/refresh"; import { ElMessage, ElMessageBox } from "element-plus"; import { on } from "events"; import CabinetGoodsConfigModal from "./cabinet-goods-config-modal.vue"; +import { getGoodsInfo } from "@/api/shop/goods"; defineOptions({ name: "CabinetGoods" @@ -156,16 +157,33 @@ const switchCellType = (cellType: number) => { const handleStockConfig = async (row: CabinetCellDTO) => { try { + // 获取商品详细信息 + const { data } = await getGoodsInfo(row.goodsId!); + const remainingStock = data.stock - data.totalStock + row.stock; + const { value } = await ElMessageBox.prompt( - `请输入${row.goodsName || '未配置商品'}的库存数量`, + `请输入${row.goodsName || '未配置商品'}的库存数量(本次最多可分配:${remainingStock})。 + 若可分配数量不足,请先调整商品列表中的库存。`, '配置库存', { - inputPattern: /^0$|^[1-9]\d*/, + inputPattern: /^0$|^[1-9]\d*$/, inputValue: row.stock?.toString(), - inputErrorMessage: '库存必须大于等于0' + inputErrorMessage: '请输入有效的非负整数', + inputValidator: (inputValue: string) => { + const num = Number(inputValue); + if (num > remainingStock) { + return '分配数量不能超过剩余库存'; + } + return true; + } } ); + if (Number(value) > remainingStock) { + ElMessage.warning('分配数量不能超过剩余库存'); + return; + } + await changeGoodsCellsStock(row.cellId!, Number(value)); ElMessage.success('库存更新成功'); getList();