|
@@ -0,0 +1,124 @@
|
|
|
+<template>
|
|
|
+ <Navbar class="mb-3" />
|
|
|
+ <v-card class="default-width card-box mb-5 pa-5">
|
|
|
+ <div v-if="cartList.length">
|
|
|
+ <v-data-table
|
|
|
+ v-model="selectedData"
|
|
|
+ :items="cartList"
|
|
|
+ :headers="headers"
|
|
|
+ :loading="false"
|
|
|
+ :elevation="0"
|
|
|
+ :show-select="true"
|
|
|
+ select-strategy="all"
|
|
|
+ >
|
|
|
+ <template v-slot:[`header.data-table-select`]>
|
|
|
+ <v-checkbox v-model="selectAll" hide-details color="primary" indeterminate @update:modelValue="handleSelectAll"></v-checkbox>
|
|
|
+ </template>
|
|
|
+ <template v-slot:[`item.data-table-select`]="{ item }">
|
|
|
+ <v-checkbox v-model="item.selected" hide-details color="primary" @update:modelValue="val => handleSelect(val, item.id)"></v-checkbox>
|
|
|
+ </template>
|
|
|
+ <template v-slot:[`item.spuName`]="{ item }">
|
|
|
+ <GoodsItem :item="{ ...item.sku, ...item.spu, spuName: item.spu.name }" :showLine="false" :showPriceCount="false" class="mb-1" />
|
|
|
+ </template>
|
|
|
+ <template v-slot:[`item.count`]="{ item }">
|
|
|
+ <v-number-input
|
|
|
+ v-model="item.count"
|
|
|
+ color="primary"
|
|
|
+ :min="1" :max="item.sku.stock"
|
|
|
+ hide-details
|
|
|
+ control-variant="split"
|
|
|
+ inset density="compact"
|
|
|
+ style="width: 170px;"
|
|
|
+ @update:modelValue="val => handleChangeCount(val, item.id)"
|
|
|
+ />
|
|
|
+ </template>
|
|
|
+ <template v-slot:[`item.actions`]="{ item }">
|
|
|
+ <v-btn color="error" @click.stop="handleDelete(item.id)" variant="text">删除</v-btn>
|
|
|
+ </template>
|
|
|
+ <template #bottom></template>
|
|
|
+ </v-data-table>
|
|
|
+ </div>
|
|
|
+ <div v-else class="text-center">
|
|
|
+ <Empty :elevation="false" message="购物车空空如也,去首页逛逛吧" />
|
|
|
+ <v-btn elevation="5" color="primary" @click.stop="router.push('/mall')" size="x-large" width="200">去逛逛</v-btn>
|
|
|
+ </div>
|
|
|
+ </v-card>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup>
|
|
|
+defineOptions({ name: 'mall-cart'})
|
|
|
+import { ref, computed, onMounted } from 'vue'
|
|
|
+import Navbar from '../components/navbar.vue'
|
|
|
+import { useRouter } from 'vue-router'
|
|
|
+import { getMallUserCartList } from '@/api/mall/cart'
|
|
|
+import { fen2yuan } from '@/hooks/web/useGoods'
|
|
|
+import GoodsItem from '../components/GoodsItem'
|
|
|
+import { updateCartSelected, updateCartCount, deleteCartGoods } from '@/api/mall/cart'
|
|
|
+
|
|
|
+const router = useRouter()
|
|
|
+const cartList = ref([])
|
|
|
+const selectAll = ref(false)
|
|
|
+const headers = [
|
|
|
+ { title: '商品信息', key: 'spuName', sortable: false, align: 'center' },
|
|
|
+ { title: '单价', key: 'sku.price', sortable: false, value: item => '¥' + fen2yuan(item.sku.price) },
|
|
|
+ { title: '数量', key: 'count', sortable: false },
|
|
|
+ { title: '操作', key: 'actions', align: 'center', sortable: false }
|
|
|
+]
|
|
|
+
|
|
|
+// 获取购物车列表
|
|
|
+const getCartList = async () => {
|
|
|
+ const data = await getMallUserCartList()
|
|
|
+ cartList.value = data.validList || []
|
|
|
+ selectAll.value = cartList.value.every(e => e.selected)
|
|
|
+ console.log(selectAll.value, 'selectAll.value')
|
|
|
+}
|
|
|
+onMounted(async () => {
|
|
|
+ getCartList()
|
|
|
+})
|
|
|
+
|
|
|
+// 全选
|
|
|
+const handleSelectAll = async (selected) => {
|
|
|
+ console.log(selected, 'selected')
|
|
|
+ // const ids = cartList.value.map(item => item.id)
|
|
|
+ // await updateCartSelected({ ids, selected })
|
|
|
+ // getCartList()
|
|
|
+}
|
|
|
+
|
|
|
+// 单选
|
|
|
+const handleSelect = async (selected, id) => {
|
|
|
+ await updateCartSelected({ ids: [id], selected })
|
|
|
+ getCartList()
|
|
|
+}
|
|
|
+
|
|
|
+// 选中的商品列表
|
|
|
+const selectedData = computed(() => {
|
|
|
+ return cartList.value.map(item => {
|
|
|
+ if (item.selected) return item.id
|
|
|
+ }).filter(Boolean)
|
|
|
+})
|
|
|
+
|
|
|
+// 删除购物车中的商品
|
|
|
+const handleDelete = async (id) => {
|
|
|
+ await deleteCartGoods(id)
|
|
|
+ await getCartList()
|
|
|
+}
|
|
|
+
|
|
|
+// 商品数量更新
|
|
|
+const handleChangeCount = async (count, id) => {
|
|
|
+ await updateCartCount({ count, id })
|
|
|
+ await getCartList()
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped lang="scss">
|
|
|
+ :deep(.v-table.v-table--fixed-header > .v-table__wrapper > table > thead > tr > th) {
|
|
|
+ text-wrap: nowrap !important;
|
|
|
+ background-color: #f7f8fa !important;
|
|
|
+ }
|
|
|
+ :deep(.v-selection-control__input) {
|
|
|
+ color: var(--v-primary-base) !important;
|
|
|
+ }
|
|
|
+ :deep(.v-table.v-table--hover > .v-table__wrapper > table > tbody > tr > td) {
|
|
|
+ white-space: nowrap !important;
|
|
|
+ }
|
|
|
+</style>
|