feat:共富商城购物车

This commit is contained in:
weiwei committed 2025-10-28 14:20:14 +08:00
1 parent a2ecdbc187
commit 467dcc637d
20 files changed
+429 -122

No files matched your search

-12
View File
@@ -23,18 +23,6 @@ export default {
method: "GET",
params,
}),
getShopCar: (params: any) =>
request({
url: `/hard/hard-car/app/mch-detail`,
method: "GET",
params,
}),
updateShopCar: (data: any) =>
request({
url: `/hard/hard-car/app/save-or-upload`,
method: "POST",
data,
}),
checkSpuStock: (params) => {
return request({
url: "/canteen/canteenOnlineSpu/app/checkSpuStock",
+12
View File
@@ -25,6 +25,18 @@ export default {
method: "GET",
params,
}),
getShopCar: (params: any) =>
request({
url: `/hard/hard-car/app/mch-detail`,
method: "GET",
params,
}),
updateShopCar: (data: any) =>
request({
url: `/hard/hard-car/app/save-or-upload`,
method: "POST",
data,
}),
/**
* 预约时间列表
*/
@@ -1,14 +1,14 @@
import { defineStore } from "pinia"
import { foodApi } from "../api"
import { orderApi } from "../api"
export const useCartStore = defineStore("cartStore", {
export const useOnlineCartStore = defineStore("onlineCartStore", {
state: () => ({
cart: JSON.parse(uni.getStorageSync("onlineShopCart") || "[]"),
shopMerchantId: uni.getStorageSync("onlineShopMerchantId") || "",
}),
getters: {
totalCount(state) {
return state.cart.reduce((prev: string, cur: any) => {
return state.cart.reduce((prev: number, cur: { productCount: number }) => {
return prev + cur.productCount
}, 0)
},
@@ -58,7 +58,7 @@ export const useCartStore = defineStore("cartStore", {
await this.getShopCart()
},
async getShopCart() {
const { data } = await foodApi.getShopCar({ merchantId: this.shopMerchantId, hardType: "SUPERMARKET" })
const { data } = await orderApi.getShopCar({ merchantId: this.shopMerchantId, hardType: "SUPERMARKET" })
this.saveShopCart(data ?? [])
},
async updateShopCart() {
@@ -70,7 +70,7 @@ export const useCartStore = defineStore("cartStore", {
productId: v.productId,
}
})
const { code }: any = await foodApi.updateShopCar({ merchantId: this.shopMerchantId, list, hardType: "SUPERMARKET" })
const { code }: any = await orderApi.updateShopCar({ merchantId: this.shopMerchantId, list, hardType: "SUPERMARKET" })
if (code == 0) return await this.getShopCart()
uni.showToast({ title: "更新购物车失败", icon: "none" })
},
+69
View File
@@ -0,0 +1,69 @@
import { defineStore } from "pinia"
import { orderApi } from "../api"
export const useProsperityCartStore = defineStore("prosperityCartStore", {
state: () => ({
cartList: [],
}),
getters: {
totalCount(state) {
return state.cartList.reduce((prev: number, cur: { productCount: number }) => {
return prev + cur.productCount
}, 0)
},
totPrice(state) {
return state.cartList
.map((item: any) => Number(item.totalPrice))
.reduce((prev: number, cur: number) => prev + cur, 0)
.toFixed(2)
},
},
actions: {
addGoodsItemToCart(goodsItem: any) {
const findResult = this.cartList.find((item: any) => {
return item.productId === goodsItem.productId
})
if (!findResult) {
this.cartList.unshift(goodsItem)
} 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) {
item.productCount--
}
})
await this.updateShopCart()
await this.getShopCart()
},
async getShopCart(merchantId: string) {
const { data } = await orderApi.getShopCar({ merchantId, hardType: "SUPERMARKET" })
this.cartList = data
},
async updateShopCart() {
const list = this.cartList
.filter((v) => v.productCount && v.productCount > 0)
.map((v) => {
return {
productCount: v.productCount,
productId: v.productId,
}
})
const { code }: any = await orderApi.updateShopCar({ merchantId: this.shopMerchantId, list, hardType: "SUPERMARKET" })
if (code == 0) return await this.getShopCart()
uni.showToast({ title: "更新购物车失败", icon: "none" })
},
},
})