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();