优化购物车数据逻辑
This commit is contained in:
parent
d1050658ac
commit
5043db03b5
|
@ -19,7 +19,7 @@
|
|||
<script>
|
||||
import WidgetCountModify from "@/components/widgets/count-modify";
|
||||
import ServicePreviewItem from "@/components/service/preview-item";
|
||||
import { mapState } from "vuex";
|
||||
import { mapGetters } from "vuex";
|
||||
export default {
|
||||
name: "component-cate-template-list",
|
||||
data() {
|
||||
|
@ -36,8 +36,8 @@ export default {
|
|||
},
|
||||
},
|
||||
computed: {
|
||||
...mapState({
|
||||
briefCart: (state) => state.cart.briefCart,
|
||||
...mapGetters({
|
||||
briefCart: "cart/briefCart",
|
||||
}),
|
||||
},
|
||||
components: {
|
||||
|
|
|
@ -58,7 +58,7 @@ import WidgetTips from "@/components/widgets/tips";
|
|||
import WidgetCheckBox from "@/components/widgets/checkbox";
|
||||
import WidgetCountModify from "@/components/widgets/count-modify";
|
||||
import ServicePreviewItem from "@/components/service/preview-item";
|
||||
import { mapGetters } from "vuex";
|
||||
import { mapGetters, mapState } from "vuex";
|
||||
export default {
|
||||
name: "order-cart",
|
||||
data() {
|
||||
|
@ -75,37 +75,39 @@ export default {
|
|||
ServicePreviewItem,
|
||||
},
|
||||
computed: {
|
||||
...mapState({
|
||||
cart: (state) => state.cart.cart,
|
||||
}),
|
||||
...mapGetters({
|
||||
isLogin: "user/isLogin",
|
||||
}),
|
||||
},
|
||||
onLoad() {
|
||||
async onLoad() {
|
||||
if (!this.isLogin) {
|
||||
this.$store.commit("user/showLoginModal", true);
|
||||
return;
|
||||
}
|
||||
await this.$store.dispatch("cart/update");
|
||||
this.list = [];
|
||||
this.$models.cart.list().then((list) => {
|
||||
list.forEach((item) => {
|
||||
let goods = [];
|
||||
item.good.forEach((v) => {
|
||||
goods.push({
|
||||
id: v.gid,
|
||||
name: v.title,
|
||||
times: v.post_hits,
|
||||
cover: v.thumbnail,
|
||||
price: v.money,
|
||||
number: v.number,
|
||||
checked: false,
|
||||
});
|
||||
});
|
||||
this.list.push({
|
||||
name: item.cate,
|
||||
total: 0,
|
||||
list: goods,
|
||||
this.cart.list.forEach((item) => {
|
||||
let goods = [];
|
||||
item.good.forEach((v) => {
|
||||
goods.push({
|
||||
id: v.gid,
|
||||
name: v.title,
|
||||
times: v.post_hits,
|
||||
cover: v.thumbnail,
|
||||
price: v.money,
|
||||
number: v.number,
|
||||
checked: false,
|
||||
});
|
||||
});
|
||||
this.list.push({
|
||||
name: item.cate,
|
||||
total: 0,
|
||||
list: goods,
|
||||
checked: false,
|
||||
});
|
||||
});
|
||||
},
|
||||
onShow() {},
|
||||
|
@ -166,7 +168,9 @@ export default {
|
|||
this.updateTotal(idx.parentIndex);
|
||||
});
|
||||
} else {
|
||||
this.$utils.toast("亲,不能再" + (e.type == "sub" ? "减少" : "增加") + "了~");
|
||||
if (e.type == "sub") {
|
||||
this.deleteCart(idx.parentIndex, idx.itemIndex);
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
<text class="iconfont icon-gouwuche"></text>
|
||||
</view>
|
||||
<view class="cart-number">
|
||||
<text class="text">{{ cartCount }}</text>
|
||||
<text class="text">{{ briefCart.count }}</text>
|
||||
</view>
|
||||
</movable-view>
|
||||
</movable-area>
|
||||
|
@ -58,10 +58,10 @@ export default {
|
|||
computed: {
|
||||
...mapState({
|
||||
currentCateId: (state) => state.system.currentCateId,
|
||||
cartCount: (state) => state.cart.count,
|
||||
}),
|
||||
...mapGetters({
|
||||
isLogin: "user/isLogin",
|
||||
briefCart: "cart/briefCart",
|
||||
}),
|
||||
},
|
||||
async onLoad() {
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
<text class="iconfont icon-gouwuche"></text>
|
||||
</view>
|
||||
<view class="cart-number">
|
||||
<text class="text">{{ cartCount }}</text>
|
||||
<text class="text">{{ briefCart.count }}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="order-btn" :class="{ active: canUse }" @click="toCart">
|
||||
|
@ -29,7 +29,7 @@
|
|||
<script>
|
||||
import AppLayout from "@/components/layout/layout";
|
||||
import AppCate from "@/components/cate/cate";
|
||||
import { mapGetters, mapState } from "vuex";
|
||||
import { mapGetters } from "vuex";
|
||||
export default {
|
||||
name: "service-list",
|
||||
data() {
|
||||
|
@ -46,11 +46,9 @@ export default {
|
|||
AppCate,
|
||||
},
|
||||
computed: {
|
||||
...mapState({
|
||||
cartCount: (state) => state.cart.count,
|
||||
}),
|
||||
...mapGetters({
|
||||
isLogin: "user/isLogin",
|
||||
briefCart: "cart/briefCart",
|
||||
}),
|
||||
},
|
||||
onLoad(e) {
|
||||
|
@ -63,8 +61,8 @@ export default {
|
|||
onShareTimeline() {},
|
||||
onShareAppMessage() {},
|
||||
watch: {
|
||||
cartCount(count) {
|
||||
this.canUse = count > 0;
|
||||
briefCart(cart) {
|
||||
this.canUse = cart.count > 0;
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
|
@ -76,7 +74,7 @@ export default {
|
|||
if (this.isLogin) {
|
||||
this.$store.dispatch("cart/update");
|
||||
}
|
||||
this.canUse = this.cartCount > 0;
|
||||
this.canUse = this.briefCart.count > 0;
|
||||
if (!e.id) {
|
||||
return this.$utils.toast("参数错误");
|
||||
}
|
||||
|
|
|
@ -3,42 +3,42 @@ import cart from '@/core/models/cart';
|
|||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
count: 0,
|
||||
briefCart: {
|
||||
cart: {
|
||||
loaded: false,
|
||||
ids: [],
|
||||
list: {},
|
||||
list: [],
|
||||
},
|
||||
},
|
||||
getters: {},
|
||||
getters: {
|
||||
briefCart(state) {
|
||||
let data = {
|
||||
count: 0,
|
||||
ids: [],
|
||||
list: {},
|
||||
};
|
||||
state.cart.list.forEach((item) => {
|
||||
item.good.forEach((k) => {
|
||||
data.count += k.number;
|
||||
data.list[k.gid] = k.number;
|
||||
});
|
||||
});
|
||||
data.ids = Object.keys(data.list);
|
||||
|
||||
return data;
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
count(state, data) {
|
||||
state.count = data;
|
||||
},
|
||||
briefCart(state, data) {
|
||||
state.briefCart = data;
|
||||
cart(state, data) {
|
||||
state.cart = data;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
update(context, refresh) {
|
||||
if (!context.state.briefCart.loaded || refresh) {
|
||||
cart.list().then((list) => {
|
||||
let count = 0, data = {
|
||||
ids: [],
|
||||
list: {},
|
||||
};
|
||||
list.forEach((item) => {
|
||||
item.good.forEach((k) => {
|
||||
count += k.number;
|
||||
data.list[k.gid] = k.number;
|
||||
});
|
||||
});
|
||||
data.ids = Object.keys(data.list);
|
||||
context.commit('briefCart', {
|
||||
...data,
|
||||
async update(context, refresh) {
|
||||
if (!context.state.cart.loaded || refresh) {
|
||||
await cart.list().then((list) => {
|
||||
context.commit('cart', {
|
||||
list: list,
|
||||
loaded: true,
|
||||
});
|
||||
context.commit('count', count);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue