CartModal.vue 13 KB

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