index.vue 8.8 KB

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