CartModal.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <template>
  2. <BasicModal type="bottom" v-model:open="getOpen" :isMack="true">
  3. <view class="cart-modal">
  4. <!-- 顶部标题栏 -->
  5. <view class="cart-modal__header">
  6. <text class="header-title">已选菜品</text>
  7. <view class="header-clear" @click="handleClear" v-if="cartList.length > 0">
  8. <text class="clear-text">清空</text>
  9. </view>
  10. </view>
  11. <!-- 购物车列表 -->
  12. <scroll-view class="cart-modal__list" scroll-y>
  13. <view v-for="(item, index) in cartList" :key="item.id || index" class="cart-item">
  14. <!-- 菜品图片 -->
  15. <image :src="item.image" mode="aspectFill" class="cart-item__image"></image>
  16. <!-- 菜品信息 -->
  17. <view class="cart-item__info">
  18. <view class="cart-item__name">{{ item.name }}</view>
  19. <view class="cart-item__tags" v-if="item.tags && item.tags.length > 0">
  20. <view v-for="(tag, tagIndex) in item.tags" :key="tagIndex" class="cart-item__tag"
  21. :class="tag.type">
  22. {{ tag.text }}
  23. </view>
  24. </view>
  25. <view class="cart-item__price">
  26. <text class="price-symbol">¥</text>
  27. <text class="price-number">{{ formatPrice(item.price) }}</text>
  28. </view>
  29. </view>
  30. <!-- 数量选择器 -->
  31. <view class="cart-item__actions">
  32. <view class="action-btn minus" :class="{ disabled: item.quantity === 0 }"
  33. @click="handleDecrease(item)" hover-class="hover-active">
  34. <image :src="getFileUrl('img/icon/reduce1.png')" mode="aspectFit" class="action-icon"
  35. v-show="item.quantity == 0"></image>
  36. <image :src="getFileUrl('img/icon/reduce2.png')" mode="aspectFit" class="action-icon"
  37. v-show="item.quantity != 0"></image>
  38. </view>
  39. <view class="quantity">{{ item.quantity || 0 }}</view>
  40. <view class="action-btn plus" @click="handleIncrease(item)" hover-class="hover-active">
  41. <image :src="getFileUrl('img/icon/add2.png')" mode="widthFix" class="action-icon"
  42. v-show="item.quantity < 99"></image>
  43. <image :src="getFileUrl('img/icon/add1.png')" mode="widthFix" class="action-icon"
  44. v-show="item.quantity >= 99"></image>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- 优惠券部分 -->
  49. <view class="coupon-section" v-if="showCouponSection" @click="handleCouponClick">
  50. <view class="coupon-section__label">优惠券</view>
  51. <view class="coupon-section__value" >
  52. <text class="discount-amount" v-if="discountAmount > 0">-¥{{ formatPrice(discountAmount)
  53. }}</text>
  54. <text class="arrow-text">›</text>
  55. </view>
  56. </view>
  57. </scroll-view>
  58. </view>
  59. </BasicModal>
  60. </template>
  61. <script setup>
  62. import { computed } from 'vue';
  63. import BasicModal from '@/components/Modal/BasicModal.vue';
  64. import { getFileUrl } from '@/utils/file.js';
  65. const props = defineProps({
  66. open: {
  67. type: Boolean,
  68. default: false
  69. },
  70. cartList: {
  71. type: Array,
  72. default: () => []
  73. },
  74. discountAmount: {
  75. type: Number,
  76. default: 0
  77. },
  78. showCouponSection: {
  79. type: Boolean,
  80. default: true
  81. }
  82. });
  83. const emit = defineEmits(['update:open', 'increase', 'decrease', 'clear', 'coupon-click', 'order-click', 'close']);
  84. const getOpen = computed({
  85. get: () => props.open,
  86. set: (val) => emit('update:open', val)
  87. });
  88. // 计算总数量
  89. const totalQuantity = computed(() => {
  90. return props.cartList.reduce((sum, item) => {
  91. return sum + (item.quantity || 0);
  92. }, 0);
  93. });
  94. // 计算总价格
  95. const totalPrice = computed(() => {
  96. return props.cartList.reduce((sum, item) => {
  97. const quantity = item.quantity || 0;
  98. const price = item.price || 0;
  99. return sum + (quantity * price);
  100. }, 0);
  101. });
  102. // 格式化价格(显示为整数)
  103. const formatPrice = (price) => {
  104. return Math.round(price).toFixed(0);
  105. };
  106. // 处理关闭
  107. const handleClose = () => {
  108. getOpen.value = false;
  109. emit('close');
  110. };
  111. // 增加数量
  112. const handleIncrease = (item) => {
  113. if (item.quantity >= 99) return;
  114. emit('increase', item);
  115. };
  116. // 减少数量
  117. const handleDecrease = (item) => {
  118. if (item && item.quantity > 0) {
  119. emit('decrease', item);
  120. }
  121. };
  122. // 清空购物车
  123. const handleClear = () => {
  124. uni.showModal({
  125. title: '提示',
  126. content: '确定要清空购物车吗?',
  127. success: (res) => {
  128. if (res.confirm) {
  129. emit('clear');
  130. }
  131. }
  132. });
  133. };
  134. // 优惠券点击
  135. const handleCouponClick = () => {
  136. emit('coupon-click');
  137. };
  138. </script>
  139. <style scoped lang="scss">
  140. .cart-modal {
  141. width: 100%;
  142. background: #FFFFFF;
  143. border-radius: 24rpx 24rpx 0 0;
  144. padding: 0;
  145. box-sizing: border-box;
  146. max-height: 80vh;
  147. display: flex;
  148. flex-direction: column;
  149. overflow: hidden;
  150. &__header {
  151. position: relative;
  152. display: flex;
  153. align-items: center;
  154. justify-content: space-between;
  155. height: 88rpx;
  156. padding: 0 30rpx;
  157. background: #FFFFFF;
  158. .header-title {
  159. font-weight: bold;
  160. font-size: 34rpx;
  161. color: #151515;
  162. }
  163. .header-clear {
  164. display: flex;
  165. align-items: center;
  166. gap: 8rpx;
  167. color: #AAAAAA;
  168. font-size: 28rpx;
  169. padding: 8rpx 16rpx;
  170. border-radius: 8rpx;
  171. transition: all 0.3s;
  172. &:active {
  173. background-color: #F5F5F5;
  174. }
  175. .clear-text {
  176. font-size: 28rpx;
  177. color: #AAAAAA;
  178. }
  179. }
  180. }
  181. &__list {
  182. flex: 1;
  183. padding: 24rpx 30rpx;
  184. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  185. box-sizing: border-box;
  186. overflow-y: auto;
  187. min-height: 0;
  188. }
  189. }
  190. .cart-item {
  191. display: flex;
  192. align-items: center;
  193. padding: 20rpx 0;
  194. border-bottom: 1rpx solid #F0F0F0;
  195. &:last-child {
  196. border-bottom: none;
  197. }
  198. &__image {
  199. width: 140rpx;
  200. height: 140rpx;
  201. border-radius: 8rpx;
  202. flex-shrink: 0;
  203. background-color: #f5f5f5;
  204. }
  205. &__info {
  206. flex: 1;
  207. margin-left: 20rpx;
  208. display: flex;
  209. flex-direction: column;
  210. justify-content: space-between;
  211. min-height: 140rpx;
  212. }
  213. &__name {
  214. font-size: 28rpx;
  215. font-weight: 500;
  216. color: #151515;
  217. margin-bottom: 8rpx;
  218. }
  219. &__tags {
  220. display: flex;
  221. gap: 10rpx;
  222. margin-bottom: 12rpx;
  223. }
  224. &__tag {
  225. padding: 4rpx 12rpx;
  226. border-radius: 4rpx;
  227. font-size: 20rpx;
  228. &.signature {
  229. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  230. color: #fff;
  231. }
  232. &.spicy {
  233. background: #2E2E2E;
  234. color: #fff;
  235. }
  236. }
  237. &__price {
  238. display: flex;
  239. align-items: baseline;
  240. color: #151515;
  241. font-weight: 500;
  242. .price-symbol {
  243. font-size: 24rpx;
  244. margin-right: 2rpx;
  245. }
  246. .price-number {
  247. font-size: 32rpx;
  248. }
  249. }
  250. &__actions {
  251. display: flex;
  252. align-items: center;
  253. justify-content: space-between;
  254. width: 180rpx;
  255. height: 58rpx;
  256. background: #F8F8F8;
  257. border-radius: 56rpx;
  258. box-sizing: border-box;
  259. padding: 0 3rpx;
  260. margin-left: 20rpx;
  261. }
  262. }
  263. .action-btn {
  264. width: 52rpx;
  265. height: 52rpx;
  266. border-radius: 50%;
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. transition: all 0.3s;
  271. background-color: #fff;
  272. .action-icon {
  273. width: 24rpx;
  274. height: 24rpx;
  275. }
  276. &.disabled {
  277. opacity: 0.5;
  278. }
  279. }
  280. .quantity {
  281. font-size: 28rpx;
  282. color: #151515;
  283. min-width: 40rpx;
  284. text-align: center;
  285. font-weight: bold;
  286. }
  287. .coupon-section {
  288. display: flex;
  289. align-items: center;
  290. justify-content: space-between;
  291. padding: 24rpx 0;
  292. margin-top: 20rpx;
  293. &__label {
  294. font-size: 28rpx;
  295. color: #151515;
  296. }
  297. &__value {
  298. display: flex;
  299. align-items: center;
  300. gap: 8rpx;
  301. .discount-amount {
  302. font-size: 28rpx;
  303. color: #E61F19;
  304. font-weight: 500;
  305. }
  306. .arrow-text {
  307. font-size: 32rpx;
  308. color: #999999;
  309. line-height: 1;
  310. }
  311. }
  312. }
  313. .cart-modal__footer {
  314. display: flex;
  315. align-items: center;
  316. justify-content: space-between;
  317. padding: 20rpx 30rpx;
  318. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  319. background: #2E2E2E;
  320. border-top: 1rpx solid #3E3E3E;
  321. .footer-coupon-btn {
  322. display: flex;
  323. flex-direction: column;
  324. align-items: center;
  325. justify-content: center;
  326. width: 140rpx;
  327. height: 100rpx;
  328. background: rgba(255, 255, 255, 0.1);
  329. border-radius: 8rpx;
  330. gap: 6rpx;
  331. .footer-coupon-icon {
  332. width: 30rpx;
  333. height: 21rpx;
  334. }
  335. .footer-coupon-text {
  336. font-size: 24rpx;
  337. color: #FFFFFF;
  338. }
  339. }
  340. .footer-cart-info {
  341. flex: 1;
  342. display: flex;
  343. align-items: center;
  344. justify-content: space-between;
  345. margin-left: 20rpx;
  346. }
  347. .footer-cart-content {
  348. display: flex;
  349. align-items: center;
  350. position: relative;
  351. .footer-cart-icon {
  352. width: 76rpx;
  353. height: 76rpx;
  354. }
  355. .footer-cart-number {
  356. display: inline-block;
  357. padding: 2rpx 10rpx;
  358. background-color: #FF4545;
  359. position: absolute;
  360. left: 60rpx;
  361. top: -10rpx;
  362. border-radius: 40rpx;
  363. color: #fff;
  364. font-size: 24rpx;
  365. }
  366. .footer-cart-price {
  367. display: flex;
  368. align-items: baseline;
  369. color: #FFFFFF;
  370. font-weight: bold;
  371. margin-left: 20rpx;
  372. .price-symbol {
  373. font-size: 24rpx;
  374. margin-right: 2rpx;
  375. }
  376. .price-number {
  377. font-size: 38rpx;
  378. }
  379. }
  380. }
  381. .footer-order-btn {
  382. width: 200rpx;
  383. height: 100rpx;
  384. display: flex;
  385. align-items: center;
  386. justify-content: center;
  387. background: #FF6B35;
  388. border-radius: 50rpx;
  389. font-weight: bold;
  390. font-size: 31rpx;
  391. color: #FFFFFF;
  392. margin-left: 20rpx;
  393. }
  394. }
  395. </style>