From a6658740b6cdc1c0102901fc84273ed486cca804 Mon Sep 17 00:00:00 2001 From: weiwei Date: Mon, 14 Apr 2025 12:45:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A3=9F=E5=A0=82=E7=82=B9?= =?UTF-8?q?=E9=A4=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/merchant/components/product-car.vue | 78 ------ .../merchant/components/product-cart.vue | 62 +++++ src/pages/merchant/components/product-row.vue | 58 ++--- .../merchant/components/product-total.vue | 60 ++--- src/pages/merchant/detail.vue | 227 +++++++++--------- 5 files changed, 208 insertions(+), 277 deletions(-) delete mode 100644 src/pages/merchant/components/product-car.vue create mode 100644 src/pages/merchant/components/product-cart.vue diff --git a/src/pages/merchant/components/product-car.vue b/src/pages/merchant/components/product-car.vue deleted file mode 100644 index d8e464e..0000000 --- a/src/pages/merchant/components/product-car.vue +++ /dev/null @@ -1,78 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/pages/merchant/components/product-cart.vue b/src/pages/merchant/components/product-cart.vue new file mode 100644 index 0000000..65800db --- /dev/null +++ b/src/pages/merchant/components/product-cart.vue @@ -0,0 +1,62 @@ + + + diff --git a/src/pages/merchant/components/product-row.vue b/src/pages/merchant/components/product-row.vue index 625d967..7a7f1c5 100644 --- a/src/pages/merchant/components/product-row.vue +++ b/src/pages/merchant/components/product-row.vue @@ -1,28 +1,24 @@ - - diff --git a/src/pages/merchant/components/product-total.vue b/src/pages/merchant/components/product-total.vue index 8a5aef4..e5c2cbb 100644 --- a/src/pages/merchant/components/product-total.vue +++ b/src/pages/merchant/components/product-total.vue @@ -1,59 +1,37 @@ - - - \ No newline at end of file diff --git a/src/pages/merchant/detail.vue b/src/pages/merchant/detail.vue index f51ca1b..b1f6500 100644 --- a/src/pages/merchant/detail.vue +++ b/src/pages/merchant/detail.vue @@ -7,21 +7,23 @@ import { merchantApi, foodApi, payApi } from "@@/api" import { filePath } from "@@/utils/file" import ProductRow from "./components/product-row.vue" -import ProductCar from "./components/product-car.vue" +import ProductCart from "./components/product-cart.vue" import ProductTotal from "./components/product-total.vue" const state = reactive({ detailId: null, canteenDetail: {}, + foodList: [], + + cartList: [], totPrice: 0, totCount: 0, - currentClassifyIndex: 0, - showCarDialog: false, - carList: [], classifyList: [], - shopList: [], + currentClassifyIndex: 0, + + showCartDialog: false, }) onLoad((options: any) => { @@ -31,10 +33,15 @@ onLoad((options: any) => { }) onShow(() => { getDetail() - getShopCar() + getFoodCart() getClassify() }) +const getHeight = () => { + const rpx = 480 + return `calc(100vh - ${uni.upx2px(rpx)}px)` +} + // 获取食堂详情信息 const getDetail = async () => { uni.showLoading({ title: "加载中...", mask: true }) @@ -43,46 +50,46 @@ const getDetail = async () => { uni.hideLoading() } -const getShopCar = async () => { +// 获取购物车信息 +const getFoodCart = async () => { const { data } = await foodApi.getShopCar({ merchantId: state.detailId, hardType: "CANTEEN" }) - state.carList = data ?? [] - if (isEmpty(state.carList)) { - state.shopList.forEach((v: any) => { + state.cartList = data ?? [] + if (isEmpty(state.cartList)) { + state.foodList.forEach((v: any) => { v.productCount = 0 }) } else { - asyncCarCount(state.shopList) + syncCartCount(state.foodList) } - state.totPrice = state.carList.map((v) => Number(v.totalPrice)).reduce((pre, next) => pre + next, 0) - state.totCount = state.carList.map((v) => v.productCount).reduce((pre, next) => pre + next, 0) + state.totPrice = state.cartList.map((v) => Number(v.totalPrice)).reduce((pre, next) => pre + next, 0) + state.totCount = state.cartList.map((v) => v.productCount).reduce((pre, next) => pre + next, 0) } -const getClassify = async () => { - const _pa = { merchantId: state.detailId } - const { data } = await foodApi.classify(_pa) - state.classifyList = data - if (!isEmpty(state.classifyList)) { - getPorduct() - } -} - -const updateShopCar = async () => { - if (isEmpty(state.carList)) return - const list = state.carList - .filter((v) => v.productCount !== undefined && v.productCount >= 0) - .map((v) => { - return { - productCount: v.productCount, - productId: v.productId, +// 同步购物车购买数量 +const syncCartCount = (foodList: any) => { + if (state.cartList && state.cartList.length) { + for (const i of foodList) { + for (const v of state.cartList) { + if (i.id == v.productId) { + i.productCount = v.productCount + } } - }) - if (isEmpty(list)) return - const _pa = { merchantId: state.detailId, list: list, hardType: "CANTEEN" } - const { data, code }: any = await foodApi.updateShopCar(_pa) - if (code == 0) await getShopCar() + } + } + state.foodList = foodList } -const getPorduct = async () => { +// 获取菜品分类 +const getClassify = async () => { + const { data } = await foodApi.classify({ merchantId: state.detailId }) + state.classifyList = data ?? [] + if (!isEmpty(state.classifyList)) { + getFoodList() + } +} + +// 获取食物列表 +const getFoodList = async () => { const target = state.classifyList[state.currentClassifyIndex] if (isEmpty(target)) return const _pa = { @@ -94,51 +101,31 @@ const getPorduct = async () => { i.productCount = 0 i.productId = i.id }) - asyncCarCount(data) + syncCartCount(data) } -// 同步购买数量 -const asyncCarCount = (shopList) => { - if (state.carList && state.carList.length) { - for (const i of shopList) { - for (const v of state.carList) { - if (i.id == v.productId) { - i.productCount = v.productCount - } - } - } - } - state.shopList = shopList +// 切换选中的分类 +const doClassify = (i: any) => { + state.currentClassifyIndex = i + getClassify() } -const doAdd = async (item: any) => { - chentProductCount(item, "add") -} - -const remove = async (item: any) => { - chentProductCount(item, "remove") -} - -const chentProductCount = async (item: any, type: "add" | "remove") => { +// 设置购物车商品的数量 +const chentProductCartCount = async (item: any) => { try { uni.showLoading({ title: "加载中", mask: true }) - let count = item.productCount - if (type === "add") { - count += 1 - const { code }: any = await foodApi.checkSpuStock({ spuId: item.productId, incrCount: count }) - if (code != 0) throw new Error() - } else if (type === "remove") { - count -= 1 - } - const target = state.carList.find((v) => v.productId == item.productId) - const targetIndex = state.carList.findIndex((v) => v.productId == item.productId) - if (target) { - state.carList[targetIndex].productCount = count + // 检查商品库存 + const { code }: any = await foodApi.checkSpuStock({ spuId: item.productId, incrCount: item.productCount }) + if (code != 0) throw new Error() + + const targetIndex = state.cartList.findIndex((v) => v.productId == item.productId) + if (targetIndex !== -1) { + state.cartList[targetIndex].productCount = item.productCount } else { - state.carList.push({ ...item, productId: item.id, productCount: count }) + state.cartList.push({ ...item, productId: item.id, productCount: item.productCount }) } nextTick(async () => { - await updateShopCar() + await updateFoodCart() }) } catch (e) { console.error(e) @@ -147,52 +134,57 @@ const chentProductCount = async (item: any, type: "add" | "remove") => { } } -const doClassify = (i: any) => { - state.currentClassifyIndex = i - getClassify() +// 保存购物车商品 +const updateFoodCart = async () => { + if (isEmpty(state.cartList)) return + const list = state.cartList + .filter((v) => v.productCount !== undefined && v.productCount >= 0) + .map((v) => { + return { + productCount: v.productCount, + productId: v.productId, + } + }) + if (isEmpty(list)) return + const _pa = { merchantId: state.detailId, list: list, hardType: "CANTEEN" } + const { data, code }: any = await foodApi.updateShopCar(_pa) + if (code == 0) await getFoodCart() } -const doVisibleCar = async () => { - state.showCarDialog = true -} +// 打开购物车 +const doVisibleCart = async () => (state.showCartDialog = true) -const submitPay = async () => { - try { - if (isEmpty(state.carList)) { - uni.showToast({ title: "请至少购买一件商品", icon: "none" }) - return - } - uni.showLoading({ title: "支付中...", mask: false }) - const _createtPa = { - carParentId: state.carList[0].parentCarId, - hardType: "CANTEEN", - } - const { data: createData, code: createCode }: any = await payApi.createHardwareOrder(_createtPa) - if (createCode != 0) throw new Error() - uni.hideLoading() - uni.showToast({ title: "支付成功", icon: "none" }) - router.jump("/pages/tabbar/order") - } catch (error) { - console.error(error) - uni.hideLoading() - } -} - -const doPay = async () => { +// 购买 +const doBuy = async () => { uni.showModal({ title: "提示", content: "是否确定购买", confirmColor: "#0094ff", success: async (res) => { if (!res.confirm) return - submitPay() + submitBuy() }, }) } -const getHeight = () => { - const rpx = 480 - return `calc(100vh - ${uni.upx2px(rpx)}px)` +// 确认购买 +const submitBuy = async () => { + try { + if (isEmpty(state.cartList)) return uni.showToast({ title: "请至少购买一件商品", icon: "none" }) + uni.showLoading({ title: "支付中...", mask: true }) + const _createtPa = { + carParentId: state.cartList[0].parentCarId, + hardType: "CANTEEN", + } + const { data: createData, code: createCode }: any = await payApi.createHardwareOrder(_createtPa) + if (createCode != 0) throw new Error() + uni.showToast({ title: "支付成功", icon: "none" }) + router.jump("/pages/tabbar/order") + } catch (error) { + console.error(error) + } finally { + uni.hideLoading() + } } const doPhone = () => { @@ -202,6 +194,7 @@ const doPhone = () => { } hooks.useMakePhoneCall(state.canteenDetail.phone) } + const doFood = () => { router.jump("/pages/merchant/food", { merchantId: state.detailId }) } @@ -307,11 +300,10 @@ const doFood = () => { - - + + { :marketPrice="pV.marketPrice" :salesCount="pV.salesCount" :stock="pV.stock" - @add="doAdd(pV)" - @remove="remove(pV)" + @add="chentProductCartCount(pV)" + @remove="chentProductCartCount(pV)" /> @@ -332,18 +324,19 @@ const doFood = () => { -