index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <Navbar class="mb-3" />
  3. <v-card class="default-width card-box mb-5 pa-5 resume-box">
  4. <div class="resume-header">
  5. <div class="resume-title">我的购物车</div>
  6. <v-btn :disabled="!cartList?.length" prepend-icon="mdi-refresh" color="primary" variant="text" @click="getCartList">刷新购物车</v-btn>
  7. </div>
  8. <div v-if="cartList.length" class="mt-3">
  9. <v-data-table
  10. v-model="selectedData"
  11. :items="cartList"
  12. :headers="headers"
  13. :loading="false"
  14. :elevation="0"
  15. :show-select="true"
  16. select-strategy="all"
  17. >
  18. <template v-slot:[`header.data-table-select`]>
  19. <v-checkbox v-model="selectAll" hide-details color="primary" @update:modelValue="handleSelectAll"></v-checkbox>
  20. </template>
  21. <template v-slot:[`item.data-table-select`]="{ item }">
  22. <v-checkbox v-model="item.selected" hide-details color="primary" @update:modelValue="val => handleSelect(val, item.id)"></v-checkbox>
  23. </template>
  24. <template v-slot:[`item.spuName`]="{ item }">
  25. <GoodsItem :item="{ ...item.sku, ...item.spu, spuName: item.spu.name }" :showLine="false" :showPriceCount="false" class="mb-1" />
  26. </template>
  27. <template v-slot:[`item.count`]="{ item }">
  28. <v-number-input
  29. v-model="item.count"
  30. color="primary"
  31. :min="1" :max="item.sku.stock"
  32. hide-details
  33. control-variant="split"
  34. inset density="compact"
  35. style="width: 170px;"
  36. @update:modelValue="val => handleChangeCount(val, item.id)"
  37. />
  38. </template>
  39. <template v-slot:[`item.actions`]="{ item }">
  40. <v-btn color="error" @click.stop="handleDelete(false, item.id)" variant="text">删除</v-btn>
  41. </template>
  42. <template #bottom></template>
  43. </v-data-table>
  44. <v-divider></v-divider>
  45. <div class="d-flex align-center justify-space-between py-4">
  46. <v-btn :disabled="!selectedData.length" color="error" variant="outlined" @click.stop="handleDelete(true)">删除选中的商品</v-btn>
  47. <div class="d-flex align-center">
  48. <div class="color-666 mr-8">共{{ totalCount }}件商品,合计:¥{{ fen2yuan(totalPrice) }}</div>
  49. <v-btn :disabled="!totalCount" color="primary" @click.stop="handleSettlement">结算<span v-if="totalCount > 0">({{ totalCount }})</span></v-btn>
  50. </div>
  51. </div>
  52. </div>
  53. <div v-else class="text-center">
  54. <Empty :elevation="false" message="购物车空空如也,去首页逛逛吧" />
  55. <v-btn elevation="5" color="primary" @click.stop="router.push('/mall')" size="x-large" width="200">去逛逛</v-btn>
  56. </div>
  57. </v-card>
  58. <!-- 结算 -->
  59. <CtDialog :visible="showPay" titleClass="text-h6" :widthType="3" title="订单信息" @submit="handleSubmit" @close="handleClose">
  60. <confirm ref="confirmRef" :data="skuInfo" @paySuccess="paySuccess"></confirm>
  61. </CtDialog>
  62. </template>
  63. <script setup>
  64. defineOptions({ name: 'mall-cart'})
  65. import { ref, computed, onMounted } from 'vue'
  66. import Navbar from '../components/navbar.vue'
  67. import { useRouter } from 'vue-router'
  68. import { getMallUserCartList } from '@/api/mall/cart'
  69. import { fen2yuan } from '@/hooks/web/useGoods'
  70. import GoodsItem from '../components/GoodsItem'
  71. import { updateCartSelected, updateCartCount, deleteCartGoods } from '@/api/mall/cart'
  72. import Snackbar from '@/plugins/snackbar'
  73. import confirm from '@/views/mall/components/details/order/confirm.vue'
  74. import Confirm from '@/plugins/confirm'
  75. import { useI18n } from '@/hooks/web/useI18n'; const { t } = useI18n()
  76. const router = useRouter()
  77. const cartList = ref([])
  78. const selectAll = ref(false)
  79. const headers = [
  80. { title: '商品信息', key: 'spuName', sortable: false },
  81. { title: '单价', key: 'sku.price', sortable: false, value: item => '¥' + fen2yuan(item.sku.price) },
  82. { title: '数量', key: 'count', sortable: false },
  83. { title: '合计', key: 'totalCount', sortable: false, value: item => '¥' + fen2yuan(item.sku.price * item.count) },
  84. { title: '操作', key: 'actions', align: 'center', sortable: false }
  85. ]
  86. // 获取购物车列表
  87. const getCartList = async () => {
  88. const data = await getMallUserCartList()
  89. cartList.value = data.validList || []
  90. selectAll.value = cartList.value.every(e => e.selected)
  91. }
  92. onMounted(async () => {
  93. getCartList()
  94. })
  95. // 全选
  96. const handleSelectAll = async (selected) => {
  97. const ids = cartList.value.map(item => item.id)
  98. await updateCartSelected({ ids, selected })
  99. getCartList()
  100. }
  101. // 单选
  102. const handleSelect = async (selected, id) => {
  103. await updateCartSelected({ ids: [id], selected })
  104. getCartList()
  105. }
  106. // 选中的商品列表
  107. const selectedData = computed(() => {
  108. return cartList.value.map(item => {
  109. if (item.selected) return item.id
  110. }).filter(Boolean)
  111. })
  112. // 总结算金额
  113. const totalPrice = computed(() => {
  114. return cartList.value.reduce((total, item) => {
  115. if (item.selected) {
  116. return total + item.sku.price * item.count
  117. }
  118. return total
  119. }, 0)
  120. })
  121. // 总商品件数
  122. const totalCount = computed(() => {
  123. return cartList.value.reduce((total, item) => {
  124. if (item.selected) {
  125. return total + item.count
  126. }
  127. return total
  128. }, 0)
  129. })
  130. // 删除购物车中的商品
  131. const handleDelete = async (isAll, id) => {
  132. Confirm(t('common.confirmTitle'), `是否确定删除${isAll? '选中的' : ''}商品?`).then(async () => {
  133. const ids = isAll ? selectedData.value.join(',') : id
  134. await deleteCartGoods(ids)
  135. await getCartList()
  136. })
  137. }
  138. // 商品数量更新
  139. const handleChangeCount = async (count, id) => {
  140. await updateCartCount({ count, id })
  141. await getCartList()
  142. }
  143. const confirmRef = ref()
  144. const handleSubmit = () => {
  145. if (confirmRef.value) confirmRef.value.onConfirm()
  146. }
  147. const handleClose = () => {
  148. showPay.value = false
  149. }
  150. const paySuccess = () => {
  151. // 更新购物车列表
  152. showPay.value = false
  153. getCartList()
  154. }
  155. // 结算
  156. const showPay = ref(false)
  157. const selectedList = ref([])
  158. const skuInfo = ref(null) // 购买商品规格信息
  159. const handleSettlement = () => {
  160. let items = []
  161. let goods_list = [];
  162. selectedList.value = cartList.value.filter((item) => selectedData.value.includes(item.id));
  163. selectedList.value.map((item) => {
  164. // 此处前端做出修改
  165. items.push({
  166. skuId: item.sku.id,
  167. count: item.count,
  168. cartId: item.id,
  169. categoryId: item.spu.categoryId
  170. })
  171. goods_list.push({
  172. goods_id: item.spu.id,
  173. goods_num: item.count,
  174. });
  175. });
  176. // return;
  177. if (goods_list.length === 0) {
  178. Snackbar.warning('请选择商品')
  179. return
  180. }
  181. skuInfo.value = JSON.stringify({
  182. items
  183. })
  184. showPay.value = true
  185. }
  186. </script>
  187. <style scoped lang="scss">
  188. :deep(.v-table.v-table--fixed-header > .v-table__wrapper > table > thead > tr > th) {
  189. text-wrap: nowrap !important;
  190. background-color: #f7f8fa !important;
  191. }
  192. :deep(.v-selection-control__input) {
  193. color: var(--v-primary-base) !important;
  194. }
  195. :deep(.v-table.v-table--hover > .v-table__wrapper > table > tbody > tr > td) {
  196. white-space: nowrap !important;
  197. }
  198. </style>