CartModal.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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="getItemImageSrc(item)" 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. // 菜品图片地址:兼容接口字段 image/imageUrl/pic/cover,相对路径经 getFileUrl 补全 CDN
  107. const getItemImageSrc = (item) => {
  108. const url = item?.image ?? item?.imageUrl ?? item?.pic ?? item?.cover ?? '';
  109. return url ? getFileUrl(url) : '';
  110. };
  111. // 处理关闭
  112. const handleClose = () => {
  113. getOpen.value = false;
  114. emit('close');
  115. };
  116. // 增加数量
  117. const handleIncrease = (item) => {
  118. if (item.quantity >= 99) return;
  119. emit('increase', item);
  120. };
  121. // 减少数量
  122. const handleDecrease = (item) => {
  123. if (item && item.quantity > 0) {
  124. emit('decrease', item);
  125. }
  126. };
  127. // 清空购物车
  128. const handleClear = () => {
  129. uni.showModal({
  130. title: '提示',
  131. content: '确定要清空购物车吗?',
  132. success: (res) => {
  133. if (res.confirm) {
  134. emit('clear');
  135. }
  136. }
  137. });
  138. };
  139. // 优惠券点击
  140. const handleCouponClick = () => {
  141. emit('coupon-click');
  142. };
  143. </script>
  144. <style scoped lang="scss">
  145. .cart-modal {
  146. width: 100%;
  147. background: #FFFFFF;
  148. border-radius: 24rpx 24rpx 0 0;
  149. padding: 0;
  150. box-sizing: border-box;
  151. max-height: 80vh;
  152. display: flex;
  153. flex-direction: column;
  154. overflow: hidden;
  155. &__header {
  156. position: relative;
  157. display: flex;
  158. align-items: center;
  159. justify-content: space-between;
  160. height: 88rpx;
  161. padding: 0 30rpx;
  162. background: #FFFFFF;
  163. .header-title {
  164. font-weight: bold;
  165. font-size: 34rpx;
  166. color: #151515;
  167. }
  168. .header-clear {
  169. display: flex;
  170. align-items: center;
  171. gap: 8rpx;
  172. color: #AAAAAA;
  173. font-size: 28rpx;
  174. padding: 8rpx 16rpx;
  175. border-radius: 8rpx;
  176. transition: all 0.3s;
  177. &:active {
  178. background-color: #F5F5F5;
  179. }
  180. .clear-text {
  181. font-size: 28rpx;
  182. color: #AAAAAA;
  183. }
  184. }
  185. }
  186. &__list {
  187. flex: 1;
  188. padding: 24rpx 30rpx;
  189. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  190. box-sizing: border-box;
  191. overflow-y: auto;
  192. min-height: 0;
  193. }
  194. }
  195. .cart-item {
  196. display: flex;
  197. align-items: center;
  198. padding: 20rpx 0;
  199. border-bottom: 1rpx solid #F0F0F0;
  200. &:last-child {
  201. border-bottom: none;
  202. }
  203. &__image {
  204. width: 140rpx;
  205. height: 140rpx;
  206. border-radius: 8rpx;
  207. flex-shrink: 0;
  208. background-color: #f5f5f5;
  209. }
  210. &__info {
  211. flex: 1;
  212. margin-left: 20rpx;
  213. display: flex;
  214. flex-direction: column;
  215. justify-content: space-between;
  216. min-height: 140rpx;
  217. }
  218. &__name {
  219. font-size: 28rpx;
  220. font-weight: 500;
  221. color: #151515;
  222. margin-bottom: 8rpx;
  223. }
  224. &__tags {
  225. display: flex;
  226. gap: 10rpx;
  227. margin-bottom: 12rpx;
  228. }
  229. &__tag {
  230. padding: 4rpx 12rpx;
  231. border-radius: 4rpx;
  232. font-size: 20rpx;
  233. &.signature {
  234. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  235. color: #fff;
  236. }
  237. &.spicy {
  238. background: #2E2E2E;
  239. color: #fff;
  240. }
  241. }
  242. &__price {
  243. display: flex;
  244. align-items: baseline;
  245. color: #151515;
  246. font-weight: 500;
  247. .price-symbol {
  248. font-size: 24rpx;
  249. margin-right: 2rpx;
  250. }
  251. .price-number {
  252. font-size: 32rpx;
  253. }
  254. }
  255. &__actions {
  256. display: flex;
  257. align-items: center;
  258. justify-content: space-between;
  259. width: 180rpx;
  260. height: 58rpx;
  261. background: #F8F8F8;
  262. border-radius: 56rpx;
  263. box-sizing: border-box;
  264. padding: 0 3rpx;
  265. margin-left: 20rpx;
  266. }
  267. }
  268. .action-btn {
  269. width: 52rpx;
  270. height: 52rpx;
  271. border-radius: 50%;
  272. display: flex;
  273. align-items: center;
  274. justify-content: center;
  275. transition: all 0.3s;
  276. background-color: #fff;
  277. .action-icon {
  278. width: 24rpx;
  279. height: 24rpx;
  280. }
  281. &.disabled {
  282. opacity: 0.5;
  283. }
  284. }
  285. .quantity {
  286. font-size: 28rpx;
  287. color: #151515;
  288. min-width: 40rpx;
  289. text-align: center;
  290. font-weight: bold;
  291. }
  292. .coupon-section {
  293. display: flex;
  294. align-items: center;
  295. justify-content: space-between;
  296. padding: 24rpx 0;
  297. margin-top: 20rpx;
  298. &__label {
  299. font-size: 28rpx;
  300. color: #151515;
  301. }
  302. &__value {
  303. display: flex;
  304. align-items: center;
  305. gap: 8rpx;
  306. .discount-amount {
  307. font-size: 28rpx;
  308. color: #E61F19;
  309. font-weight: 500;
  310. }
  311. .arrow-text {
  312. font-size: 32rpx;
  313. color: #999999;
  314. line-height: 1;
  315. }
  316. }
  317. }
  318. .cart-modal__footer {
  319. display: flex;
  320. align-items: center;
  321. justify-content: space-between;
  322. padding: 20rpx 30rpx;
  323. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  324. background: #2E2E2E;
  325. border-top: 1rpx solid #3E3E3E;
  326. .footer-coupon-btn {
  327. display: flex;
  328. flex-direction: column;
  329. align-items: center;
  330. justify-content: center;
  331. width: 140rpx;
  332. height: 100rpx;
  333. background: rgba(255, 255, 255, 0.1);
  334. border-radius: 8rpx;
  335. gap: 6rpx;
  336. .footer-coupon-icon {
  337. width: 30rpx;
  338. height: 21rpx;
  339. }
  340. .footer-coupon-text {
  341. font-size: 24rpx;
  342. color: #FFFFFF;
  343. }
  344. }
  345. .footer-cart-info {
  346. flex: 1;
  347. display: flex;
  348. align-items: center;
  349. justify-content: space-between;
  350. margin-left: 20rpx;
  351. }
  352. .footer-cart-content {
  353. display: flex;
  354. align-items: center;
  355. position: relative;
  356. .footer-cart-icon {
  357. width: 76rpx;
  358. height: 76rpx;
  359. }
  360. .footer-cart-number {
  361. display: inline-block;
  362. padding: 2rpx 10rpx;
  363. background-color: #FF4545;
  364. position: absolute;
  365. left: 60rpx;
  366. top: -10rpx;
  367. border-radius: 40rpx;
  368. color: #fff;
  369. font-size: 24rpx;
  370. }
  371. .footer-cart-price {
  372. display: flex;
  373. align-items: baseline;
  374. color: #FFFFFF;
  375. font-weight: bold;
  376. margin-left: 20rpx;
  377. .price-symbol {
  378. font-size: 24rpx;
  379. margin-right: 2rpx;
  380. }
  381. .price-number {
  382. font-size: 38rpx;
  383. }
  384. }
  385. }
  386. .footer-order-btn {
  387. width: 200rpx;
  388. height: 100rpx;
  389. display: flex;
  390. align-items: center;
  391. justify-content: center;
  392. background: #FF6B35;
  393. border-radius: 50rpx;
  394. font-weight: bold;
  395. font-size: 31rpx;
  396. color: #FFFFFF;
  397. margin-left: 20rpx;
  398. }
  399. }
  400. </style>