feat:共富商城订单
This commit is contained in:
1 parent
f8ee97f88a
commit
0fe72f08ae
8 files changed
+156
-91
No files matched your search
@@ -17,7 +17,12 @@
|
||||
</view>
|
||||
<view class="second-text-26">库存 {{ state.selectedSku.stock || 0 }}</view>
|
||||
<view class="second-text-26">
|
||||
已选择 {{ `${state.selectedSku.skuName || "未选择"} / ${state.selectedSku.weightG + "克"},数量 ${state.goodsCount}` }}
|
||||
已选择
|
||||
{{
|
||||
`${state.selectedSku.skuName || "未选择"} / ${
|
||||
state.selectedSku.weightG ? state.selectedSku.weightG + "克" : "未选择"
|
||||
},数量 ${state.goodsCount}`
|
||||
}}
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -34,7 +39,7 @@
|
||||
:key="v.id"
|
||||
@click="onSelectSku(v)"
|
||||
>
|
||||
{{ v.skuName + " / " + v.weightG + "克" }}
|
||||
{{ (v.skuName || "") + " / " + (v.skuWeight || "") + "克" }}
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
<template>
|
||||
<view class="flex-row items-center justify-between" @tap="doContainer">
|
||||
<view class="flex-row-center">
|
||||
<view class="flex-row-center size-80rpx rounded-full bg-#0094ff">
|
||||
<view class="color-white! iconfont icon-biaoqianlanicon_gouwuche-xuanzhong" />
|
||||
</view>
|
||||
<image class="size-100rpx" src="/static/icon/cart.png" />
|
||||
<view class="flex-col m-l-30rpx">
|
||||
<view class="second-text-36 color-#f63a36!">¥ {{ hooks.useMoney(props.totPrice) }}</view>
|
||||
<view class="sub-text-28">共{{ props.totCount }}件</view>
|
||||
@@ -18,14 +16,16 @@ import { hooks } from "@@/index"
|
||||
|
||||
const emit = defineEmits(["submit", "doContainer"])
|
||||
|
||||
export type Props = {
|
||||
totPrice: number
|
||||
totCount: number
|
||||
}
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
totPrice: 0,
|
||||
totCount: 0,
|
||||
})
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
totPrice: number
|
||||
totCount: number
|
||||
}>(),
|
||||
{
|
||||
totPrice: 0,
|
||||
totCount: 0,
|
||||
},
|
||||
)
|
||||
|
||||
const doBuy = () => emit("submit")
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<crud-layout title="购物车" mainIsScroll bg="/static/page/blue-bg.png">
|
||||
<view class="h-20rpx"></view>
|
||||
<view v-if="cartStore.totalCount > 0" class="flex-row card px-15rpx py-25rpx mb-20rpx" v-for="(v, i) in cartStore.cart" :key="i">
|
||||
<view v-if="totPrice > 0" class="flex-row card px-15rpx py-25rpx mb-20rpx" v-for="(v, i) in cartList" :key="i">
|
||||
<image class="size-160rpx rounded-12rpx mr-20rpx bg-#eeeeee" :src="filePath(v.productPic)" mode="aspectFill" />
|
||||
<view class="flex-1 overflow-hidden">
|
||||
<view class="sub-text-30 truncate">{{ v.productName }}</view>
|
||||
@@ -15,12 +15,12 @@
|
||||
<view class="flex-row justify-between items-center">
|
||||
<view
|
||||
class="flex-row-center size-40rpx rounded-full border-1px border-solid border-color-#0094ff"
|
||||
@tap.stop="remove(v.productId)"
|
||||
@tap.stop="removeProductCount(v.productId)"
|
||||
>
|
||||
<view class="iconfont icon-a--" />
|
||||
</view>
|
||||
<view class="sub-text-28 m-x-20rpx">{{ v.productCount }}</view>
|
||||
<view class="flex-row-center size-40rpx rounded-full bg-#0094ff" @tap.stop="add(v.productId)">
|
||||
<view class="flex-row-center size-40rpx rounded-full bg-#0094ff" @tap.stop="addProductCount(v.productId)">
|
||||
<view class="font-size-40rpx color-white iconfont icon-a-" />
|
||||
</view>
|
||||
</view>
|
||||
@@ -29,62 +29,30 @@
|
||||
</view>
|
||||
|
||||
<view v-else class="flex-col justify-center items-center pt-300rpx">
|
||||
<image src="@/static/page/cart_empty.png" class="w-350rpx h-350rpx" />
|
||||
<image src="@/static/page/cart_empty.png" class="size-350rpx" />
|
||||
<text class="sub-text-32 font-bold">购物车空空如也</text>
|
||||
</view>
|
||||
|
||||
<template #main-bottom>
|
||||
<view class="z-200 p-x-30rpx p-y-30rpx bg-white">
|
||||
<product-total :totPrice="cartStore.totPrice" :totCount="cartStore.totalCount" @doContainer="" @submit="doBuy" />
|
||||
<product-total :totPrice="totPrice" :totCount="totalCount" @doContainer="" @submit="doBuy" />
|
||||
</view>
|
||||
</template>
|
||||
</crud-layout>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import ProductTotal from "./components/product-total.vue"
|
||||
import { isEmpty } from "radash"
|
||||
import { useOnlineCartStore } from "@/sheep/stores/online-cart"
|
||||
import { payApi } from "@/sheep/api"
|
||||
import { hooks, router } from "@/sheep"
|
||||
import { onLoad } from "@dcloudio/uni-app"
|
||||
import { hooks } from "@/sheep"
|
||||
import { filePath } from "@/sheep/utils/file"
|
||||
import ProductTotal from "./components/product-total.vue"
|
||||
import useCartBuy from "./use-cart-buy"
|
||||
|
||||
const cartStore = useOnlineCartStore()
|
||||
const { totPrice, totalCount, cartList, getShopCart, addProductCount, removeProductCount, doBuy } = useCartBuy()
|
||||
|
||||
// 点击提交订单
|
||||
const doBuy = async () => {
|
||||
if (isEmpty(cartStore.cart)) 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 payApi.createSupermarketOrder({ carParentId: cartStore.cart[0].parentCarId, hardType: "SUPERMARKET" })
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
const add = async (productId: string) => {
|
||||
await cartStore.addProductCount(productId)
|
||||
}
|
||||
const remove = async (productId: string) => {
|
||||
await cartStore.removeProductCount(productId)
|
||||
}
|
||||
onLoad(async (option: any) => {
|
||||
const { merchantId } = option
|
||||
if (!merchantId) return uni.showToast({ title: "参数错误", icon: "error" })
|
||||
await getShopCart(merchantId)
|
||||
})
|
||||
</script>
|
||||
@@ -52,9 +52,9 @@
|
||||
|
||||
<template #main-bottom>
|
||||
<view class="flex-row justify-between items-center my-4 mx-12">
|
||||
<tm-badge bg-color="red" :label="2">
|
||||
<tm-badge bg-color="red" :label="cartStore.totalCount">
|
||||
<view class="flex-col-center" @tap="goCart">
|
||||
<image class="w-60rpx h-60rpx" src="/static/icon/cart.png" />
|
||||
<image class="size-60rpx" src="/static/icon/cart.png" />
|
||||
<view class="c-#0094ff text-20rpx">购物车</view>
|
||||
</view>
|
||||
</tm-badge>
|
||||
@@ -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<any>({
|
||||
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,
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -78,6 +78,11 @@
|
||||
</scroll-view>
|
||||
<view v-else class="mx-auto mt-10 second-text-28">暂无商品</view>
|
||||
</view>
|
||||
<template #main-bottom>
|
||||
<view class="z-200 p-x-30rpx p-y-30rpx bg-white">
|
||||
<product-total :totPrice="totPrice" :totCount="totalCount" @doContainer="" @submit="doBuy" />
|
||||
</view>
|
||||
</template>
|
||||
</crud-layout>
|
||||
</template>
|
||||
|
||||
@@ -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<any>({
|
||||
id: "",
|
||||
@@ -98,6 +105,7 @@ const state = reactive<any>({
|
||||
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}`,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ interface scrollDetail {
|
||||
scrollWidth: number
|
||||
}
|
||||
|
||||
export function useShopDetailScroll() {
|
||||
export default function useShopDetailScroll() {
|
||||
const scrollDetail = ref<scrollDetail>({} as scrollDetail)
|
||||
|
||||
const onScroll = (e: any) => {
|
||||
|
||||
@@ -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" })
|
||||
},
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user