购物车逻辑

This commit is contained in:
2024-03-20 22:20:24 +08:00
parent 8d7f82b07c
commit 792aa4268a
9 changed files with 118 additions and 63 deletions

View File

@@ -1,20 +1,43 @@
import { defineStore } from 'pinia';
const useShoppingCartStore = defineStore('shoppingCart', {
state: () => ({
list: [],
total: 0,
count: 0
state: (): { shoppingCartList: any[] } => ({
shoppingCartList: []
}),
persist: {
// 修改存储中使用的键名称,默认为当前 Store的 id
key: 'shoppingCartState',
storage: {
setItem(key, value) {
uni.setStorageSync(key, value); // [!code warning]
},
getItem(key) {
return uni.getStorageSync(key); // [!code warning]
}
},
// 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined持久化整个状态)
paths: undefined
},
getters: {
getList() {
return this.list;
getShoppingCartList(): any[] {
return this.shoppingCartList;
},
getTotal() {
return this.total;
getTotalCount(): number {
return this.shoppingCartList.length;
}
},
actions: {
save(partial: Partial<any>) {
this.shoppingCartList.push(partial);
},
getCount() {
return this.count;
clear() {
this.shoppingCartList = [];
}
}
});
export default useShoppingCartStore;