diff --git a/src/pages/prosperity-shop/components/product-sku.vue b/src/pages/prosperity-shop/components/product-sku.vue
index 5a7d6f0..934c159 100644
--- a/src/pages/prosperity-shop/components/product-sku.vue
+++ b/src/pages/prosperity-shop/components/product-sku.vue
@@ -17,7 +17,12 @@
库存 {{ state.selectedSku.stock || 0 }}
- 已选择 {{ `${state.selectedSku.skuName || "未选择"} / ${state.selectedSku.weightG + "克"},数量 ${state.goodsCount}` }}
+ 已选择
+ {{
+ `${state.selectedSku.skuName || "未选择"} / ${
+ state.selectedSku.weightG ? state.selectedSku.weightG + "克" : "未选择"
+ },数量 ${state.goodsCount}`
+ }}
@@ -34,7 +39,7 @@
:key="v.id"
@click="onSelectSku(v)"
>
- {{ v.skuName + " / " + v.weightG + "克" }}
+ {{ (v.skuName || "") + " / " + (v.skuWeight || "") + "克" }}
diff --git a/src/pages/prosperity-shop/components/product-total.vue b/src/pages/prosperity-shop/components/product-total.vue
index bf9e760..30095e1 100644
--- a/src/pages/prosperity-shop/components/product-total.vue
+++ b/src/pages/prosperity-shop/components/product-total.vue
@@ -1,9 +1,7 @@
-
-
-
+
¥ {{ hooks.useMoney(props.totPrice) }}
共{{ props.totCount }}件
@@ -18,14 +16,16 @@ import { hooks } from "@@/index"
const emit = defineEmits(["submit", "doContainer"])
-export type Props = {
- totPrice: number
- totCount: number
-}
-const props = withDefaults(defineProps(), {
- totPrice: 0,
- totCount: 0,
-})
+const props = withDefaults(
+ defineProps<{
+ totPrice: number
+ totCount: number
+ }>(),
+ {
+ totPrice: 0,
+ totCount: 0,
+ },
+)
const doBuy = () => emit("submit")
diff --git a/src/pages/prosperity-shop/goods-cart.vue b/src/pages/prosperity-shop/goods-cart.vue
index af5e36d..4aeb7f9 100644
--- a/src/pages/prosperity-shop/goods-cart.vue
+++ b/src/pages/prosperity-shop/goods-cart.vue
@@ -1,7 +1,7 @@
-
+
{{ v.productName }}
@@ -15,12 +15,12 @@
{{ v.productCount }}
-
+
@@ -29,62 +29,30 @@
-
+
购物车空空如也
-
+
diff --git a/src/pages/prosperity-shop/goods-detail.vue b/src/pages/prosperity-shop/goods-detail.vue
index 7087750..33a9b87 100644
--- a/src/pages/prosperity-shop/goods-detail.vue
+++ b/src/pages/prosperity-shop/goods-detail.vue
@@ -52,9 +52,9 @@
-
+
-
+
购物车
@@ -73,17 +73,25 @@ import { filePath } from "@/sheep/utils/file"
import { displayPrice } from "@/sheep/utils/formatMoney"
import ProductSku from "./components/product-sku.vue"
import { hooks, router } from "@/sheep"
+import { useProsperityCartStore } from "@/sheep/stores/prosperity-cart"
+
+const cartStore = useProsperityCartStore()
const state = reactive({
id: "",
+ merchantId: "",
detail: {},
showSkuDialog: false,
})
+
onLoad(async (option: any) => {
- if (!option.id) return uni.showToast({ title: "参数错误", icon: "error" })
- uni.showLoading({ title: "加载中..." })
+ const { id, merchantId } = option
+ if (!id || !merchantId) return uni.showToast({ title: "参数错误", icon: "error" })
state.id = option.id
- await getGoodsDetail(option.id)
+ state.merchantId = option.merchantId
+ uni.showLoading({ title: "加载中..." })
+ await getGoodsDetail(id)
+ await cartStore.getShopCart(merchantId)
uni.hideLoading()
})
@@ -95,16 +103,17 @@ const getGoodsDetail = async (id: string) => {
// 选择sku确认加入到购物车
const onSkuAddToCart = (sku: any) => {
- console.log(sku)
-
- // cartStore.addGoodsItemToCart({ ...state.detail, ...sku, productId: sku.skuId })
- // cartStore.updateShopCart()
+ cartStore.addGoodsItemToCart({ ...state.detail, ...sku, productId: sku.skuId })
+ cartStore.updateShopCart(state.merchantId)
state.showSkuDialog = false
}
const goCart = async () => {
const isLogin = await hooks.useCheckLogin()
- if (isLogin) router.jump("/pages/prosperity-shop/goods-cart")
+ if (isLogin)
+ router.jump("/pages/prosperity-shop/goods-cart", {
+ merchantId: state.merchantId,
+ })
}
diff --git a/src/pages/prosperity-shop/shop-detail.vue b/src/pages/prosperity-shop/shop-detail.vue
index 0edda20..b4ffb8a 100644
--- a/src/pages/prosperity-shop/shop-detail.vue
+++ b/src/pages/prosperity-shop/shop-detail.vue
@@ -78,6 +78,11 @@
暂无商品
+
+
+
+
+
@@ -88,7 +93,9 @@ import { onLoad } from "@dcloudio/uni-app"
import { filePath } from "@/sheep/utils/file"
import treeToList from "@/sheep/utils/tree-to-list"
import { displayPrice } from "@/sheep/utils/formatMoney"
-import { useShopDetailScroll } from "./use-shop-detail-scroll"
+import useShopDetailScroll from "./use-shop-detail-scroll"
+import useCartBuy from "./use-cart-buy"
+import ProductTotal from "./components/product-total.vue"
const state = reactive({
id: "",
@@ -98,6 +105,7 @@ const state = reactive({
goodsList: [],
})
const { scrollDetail, onScroll, filterStyle, searchStyle } = useShopDetailScroll()
+const { totPrice, totalCount, doBuy } = useCartBuy()
onLoad(async (option: any) => {
if (!option.id) return uni.showToast({ title: "参数错误", icon: "error" })
@@ -142,9 +150,9 @@ const doCategory = async (index: number) => {
}
const doGoodsDetail = (id: string) => {
- if (!id) return uni.showToast({ title: "商品id不存在", icon: "none" })
+ if (!id || !state.id) return uni.showToast({ title: "商品id不存在", icon: "none" })
uni.navigateTo({
- url: `/pages/prosperity-shop/goods-detail?id=${id}`,
+ url: `/pages/prosperity-shop/goods-detail?id=${id}&merchantId=${state.id}`,
})
}
diff --git a/src/pages/prosperity-shop/use-cart-buy.ts b/src/pages/prosperity-shop/use-cart-buy.ts
new file mode 100644
index 0000000..94ab36c
--- /dev/null
+++ b/src/pages/prosperity-shop/use-cart-buy.ts
@@ -0,0 +1,70 @@
+import { router } from "@/sheep"
+import { orderApi, payApi } from "@/sheep/api"
+import { useProsperityCartStore } from "@/sheep/stores/prosperity-cart"
+import { isEmpty } from "radash"
+
+export default function useCartBuy() {
+ const cartStore = useProsperityCartStore()
+
+ // 购物车商品总金额
+ const totPrice = cartStore.totPrice
+ // 购物车商品总数量
+ const totalCount = cartStore.totalCount
+ // 购物车商品列表
+ const cartList = cartStore.cartList
+
+ // 获取购物车列表
+ const getShopCart = async (merchantId: string) => {
+ await cartStore.getShopCart(merchantId)
+ }
+ // 添加一个商品数量
+ const addProductCount = async (productId: string) => {
+ await cartStore.addProductCount(productId)
+ }
+ // 减少一个商品数量
+ const removeProductCount = async (productId: string) => {
+ await cartStore.removeProductCount(productId)
+ }
+
+ // 点击提交订单
+ const doBuy = async () => {
+ if (isEmpty(cartStore.cartList)) return uni.showToast({ title: "请至少购买一件商品", icon: "none" })
+ uni.showModal({
+ title: "提示",
+ content: "是否确定购买",
+ confirmColor: "#0094ff",
+ success: async (res) => {
+ if (!res.confirm) return
+ submitPay()
+ },
+ })
+ }
+
+ // 确认提交订单
+ const submitPay = async () => {
+ try {
+ uni.showLoading({ title: "支付中...", mask: false })
+ const { code, msg }: any = await orderApi.appCreateFarmerReserveOrder({
+ carParentId: cartStore.cartList[0].parentCarId,
+ hardType: "FARMER",
+ })
+ if (code != 0) throw new Error(msg)
+ uni.showToast({ title: "支付成功", icon: "none" })
+ router.jump("/pages/tabbar/order")
+ } catch (error: any) {
+ uni.showToast({ title: error.message, icon: "none" })
+ } finally {
+ uni.hideLoading()
+ }
+ }
+
+ return {
+ totPrice,
+ totalCount,
+ cartList,
+ getShopCart,
+ addProductCount,
+ removeProductCount,
+ doBuy,
+ }
+}
diff --git a/src/pages/prosperity-shop/use-shop-detail-scroll.ts b/src/pages/prosperity-shop/use-shop-detail-scroll.ts
index 63a908f..10f7b44 100644
--- a/src/pages/prosperity-shop/use-shop-detail-scroll.ts
+++ b/src/pages/prosperity-shop/use-shop-detail-scroll.ts
@@ -9,7 +9,7 @@ interface scrollDetail {
scrollWidth: number
}
-export function useShopDetailScroll() {
+export default function useShopDetailScroll() {
const scrollDetail = ref({} as scrollDetail)
const onScroll = (e: any) => {
diff --git a/src/sheep/stores/prosperity-cart.ts b/src/sheep/stores/prosperity-cart.ts
index aa04f80..0a6f2a4 100644
--- a/src/sheep/stores/prosperity-cart.ts
+++ b/src/sheep/stores/prosperity-cart.ts
@@ -1,8 +1,9 @@
import { defineStore } from "pinia"
import { orderApi } from "../api"
+// 共富商城购物车
export const useProsperityCartStore = defineStore("prosperityCartStore", {
- state: () => ({
+ state: (): any => ({
cartList: [],
}),
getters: {
@@ -19,6 +20,7 @@ export const useProsperityCartStore = defineStore("prosperityCartStore", {
},
},
actions: {
+ // 购物车添加商品项
addGoodsItemToCart(goodsItem: any) {
const findResult = this.cartList.find((item: any) => {
return item.productId === goodsItem.productId
@@ -28,17 +30,8 @@ export const useProsperityCartStore = defineStore("prosperityCartStore", {
} else {
findResult.productCount++
}
- uni.setStorageSync("onlineShopCart", JSON.stringify(this.cartList))
- },
- async addProductCount(productId: string) {
- this.cartList.forEach((item: any) => {
- if (item.productId === productId) {
- item.productCount++
- }
- })
- await this.updateShopCart()
- await this.getShopCart()
},
+ // 购物车商品项数量减一
async removeProductCount(productId: string) {
this.cartList.forEach((item: any) => {
if (item.productId === productId) {
@@ -48,11 +41,23 @@ export const useProsperityCartStore = defineStore("prosperityCartStore", {
await this.updateShopCart()
await this.getShopCart()
},
- async getShopCart(merchantId: string) {
- const { data } = await orderApi.getShopCar({ merchantId, hardType: "SUPERMARKET" })
- this.cartList = data
+ // 购物车商品项数量加一
+ async addProductCount(productId: string) {
+ this.cartList.forEach((item: any) => {
+ if (item.productId === productId) {
+ item.productCount++
+ }
+ })
+ await this.updateShopCart()
+ await this.getShopCart()
},
- async updateShopCart() {
+ // 获取购物车商品列表
+ async getShopCart(merchantId: string) {
+ const { data } = await orderApi.getShopCar({ merchantId, hardType: "FARMER" })
+ this.cartList = data ?? []
+ },
+ // 更新购物车商品列表
+ async updateShopCart(merchantId: string) {
const list = this.cartList
.filter((v) => v.productCount && v.productCount > 0)
.map((v) => {
@@ -61,8 +66,8 @@ export const useProsperityCartStore = defineStore("prosperityCartStore", {
productId: v.productId,
}
})
- const { code }: any = await orderApi.updateShopCar({ merchantId: this.shopMerchantId, list, hardType: "SUPERMARKET" })
- if (code == 0) return await this.getShopCart()
+ const { code }: any = await orderApi.updateShopCar({ merchantId, list, hardType: "FARMER" })
+ if (code == 0) return await this.getShopCart(merchantId)
uni.showToast({ title: "更新购物车失败", icon: "none" })
},
},