index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <Navbar v-if="props.showNavbar" />
  3. <v-card class="card-box mb-5 pa-5 resume-box mt-3" :class="props.defaultWidth ? 'default-width' : '100%'" :elevation="props.elevation">
  4. <div class="resume-header">
  5. <div class="resume-title" style="cursor: pointer;" @click="getCartList">我的购物车</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="/recruit/personal/personalCenter/tradeOrder?key=1"><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. if (!data?.validList?.length) return
  120. cartList.value = data.validList || []
  121. selectAll.value = cartList.value.every(e => e.selected)
  122. }
  123. onMounted(async () => {
  124. getCartList()
  125. })
  126. // 全选
  127. const handleSelectAll = async (selected) => {
  128. const ids = cartList.value.map(item => item.id)
  129. await updateCartSelected({ ids, selected })
  130. getCartList()
  131. }
  132. // 单选
  133. const handleSelect = async (selected, id) => {
  134. await updateCartSelected({ ids: [id], selected })
  135. getCartList()
  136. }
  137. // 选中的商品列表
  138. const selectedData = computed(() => {
  139. return cartList.value.map(item => {
  140. if (item.selected) return item.id
  141. }).filter(Boolean)
  142. })
  143. // 总结算金额
  144. const totalPrice = computed(() => {
  145. return cartList.value.reduce((total, item) => {
  146. if (item.selected) {
  147. return total + item.sku.price * item.count
  148. }
  149. return total
  150. }, 0)
  151. })
  152. // 总商品件数
  153. const totalCount = computed(() => {
  154. return cartList.value.reduce((total, item) => {
  155. if (item.selected) {
  156. return total + item.count
  157. }
  158. return total
  159. }, 0)
  160. })
  161. // 删除购物车中的商品
  162. const handleDelete = async (isAll, id) => {
  163. Confirm(t('common.confirmTitle'), `是否确定删除${isAll? '选中的' : ''}商品?`).then(async () => {
  164. const ids = isAll ? selectedData.value.join(',') : id
  165. await deleteCartGoods(ids)
  166. await getCartList()
  167. })
  168. }
  169. // 商品数量更新
  170. const handleChangeCount = async (count, id) => {
  171. await updateCartCount({ count, id })
  172. await getCartList()
  173. }
  174. // 结算
  175. // const showSettlement = ref(false)
  176. const selectedList = ref([])
  177. // const skuInfo = ref(null) // 购买商品规格信息
  178. const handleSettlement = () => {
  179. let items = []
  180. let goods_list = [];
  181. selectedList.value = cartList.value.filter((item) => selectedData.value.includes(item.id));
  182. selectedList.value.map((item) => {
  183. // 此处前端做出修改
  184. items.push({
  185. skuId: item.sku.id,
  186. count: item.count,
  187. cartId: item.id,
  188. categoryId: item.spu.categoryId
  189. })
  190. goods_list.push({
  191. goods_id: item.spu.id,
  192. goods_num: item.count,
  193. });
  194. });
  195. // return;
  196. if (goods_list.length === 0) {
  197. Snackbar.warning('请选择商品')
  198. return
  199. }
  200. // skuInfo.value = JSON.stringify({
  201. // items
  202. // })
  203. // showSettlement.value = true
  204. localStorage.setItem('confirm_order_data', JSON.stringify({ items }))
  205. nextTick(() => {
  206. router.push('/mall/confirm_order')
  207. })
  208. }
  209. // const confirmRef = ref()
  210. // const handleSubmit = () => {
  211. // if (confirmRef.value) confirmRef.value.onConfirm()
  212. // }
  213. // const handleClose = () => {
  214. // showSettlement.value = false
  215. // }
  216. // 创建订单完成
  217. // const payRef = ref()
  218. // const showPay = ref(false)
  219. // const payOrderId = ref('')
  220. // const orderCreated = (id) => {
  221. // showSettlement.value = false
  222. // payOrderId.value = id
  223. // showPay.value = true
  224. // // 更新购物车列表
  225. // getCartList()
  226. // }
  227. // const payCancel = () => {
  228. // Snackbar.warning('您已取消支付!')
  229. // showPay.value = false
  230. // // window.open('/recruit/personal/personalCenter/tradeOrder?key=1')
  231. // setTimeout(() => { router.push('/recruit/personal/personalCenter/tradeOrder?key=1') }, 500);
  232. // }
  233. // const paySuccess = (e) => {
  234. // // Snackbar.success('支付成功!')
  235. // // showPay.value = false
  236. // // setTimeout(() => { router.push('/recruit/personal/personalCenter/tradeOrder?key=1') }, 500);
  237. // router.push({ path: '/mall/payOver', query: { price: e.price }})
  238. // }
  239. </script>
  240. <style scoped lang="scss">
  241. :deep(.v-table.v-table--fixed-header > .v-table__wrapper > table > thead > tr > th) {
  242. text-wrap: nowrap !important;
  243. background-color: #f7f8fa !important;
  244. }
  245. :deep(.v-selection-control__input) {
  246. color: var(--v-primary-base) !important;
  247. }
  248. :deep(.v-table.v-table--hover > .v-table__wrapper > table > tbody > tr > td) {
  249. white-space: nowrap !important;
  250. }
  251. </style>