BottomActionBar.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view class="bottom-action-bar">
  3. <!-- 左侧:优惠券按钮 -->
  4. <view class="coupon-btn" @click="handleCouponClick">
  5. <image :src="getFileUrl('img/icon/shopBtn1.png')" mode="aspectFit" class="bg-img"></image>
  6. <view class="coupon-content">
  7. <image :src="getFileUrl('img/icon/coupon.png')" mode="widthFix" class="icon-coupon"></image>
  8. <view class="coupon-text">优惠券</view>
  9. </view>
  10. </view>
  11. <!-- 右侧:购物车按钮 -->
  12. <view class="cart-btn">
  13. <image :src="getFileUrl('img/icon/shopBtn2.png')" mode="aspectFit" class="bg-img"></image>
  14. <view class="shop-cart">
  15. <view class="shop-cart-content" @click="handleCartClick">
  16. <image :src="getFileUrl('img/icon/shoppingCart.png')" mode="widthFix" class="icon-shopping-cart"></image>
  17. <view class='number'>{{ totalQuantity }}</view>
  18. <view class="price">
  19. <text class="price-symbol">¥</text>
  20. <text class="price-number">{{ formatPrice(totalPrice) }}</text>
  21. </view>
  22. </view>
  23. <!-- 下单 -->
  24. <view class='order-btn' @click="handleOrderClick">去下单</view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script setup>
  30. import { computed } from 'vue';
  31. import { getFileUrl } from '@/utils/file.js';
  32. const props = defineProps({
  33. // 购物车数据列表
  34. cartList: {
  35. type: Array,
  36. default: () => []
  37. }
  38. });
  39. const emit = defineEmits(['coupon-click', 'cart-click', 'order-click']);
  40. // 取单品金额(与 FoodCard 第 10 行显示的菜品金额一致:优先 totalPrice,再兼容 price 等)
  41. const getItemPrice = (item) => {
  42. const p = item?.totalPrice ?? item?.price ?? item?.salePrice ?? item?.currentPrice ?? item?.unitPrice ?? 0;
  43. return Number(p) || 0;
  44. };
  45. // 计算总数量(与 FoodCard 第 32 行当前菜品数量一致)
  46. const totalQuantity = computed(() => {
  47. return props.cartList.reduce((sum, item) => sum + (Number(item?.quantity) || 0), 0);
  48. });
  49. // 总金额 = 菜品金额×当前菜品数量 + 其他选中项(菜品金额×当前数量)… 即 Σ(菜品金额 × 当前菜品数量)
  50. const totalPrice = computed(() => {
  51. return props.cartList.reduce((sum, item) => {
  52. const quantity = Number(item?.quantity) || 0;
  53. const price = getItemPrice(item);
  54. return sum + quantity * price;
  55. }, 0);
  56. });
  57. // 格式化总金额
  58. const formatPrice = (price) => {
  59. const num = Number(price);
  60. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  61. };
  62. // 优惠券点击
  63. const handleCouponClick = () => {
  64. emit('coupon-click');
  65. };
  66. // 购物车点击
  67. const handleCartClick = () => {
  68. emit('cart-click');
  69. };
  70. // 下单点击
  71. const handleOrderClick = () => {
  72. if (totalQuantity.value === 0) return;
  73. emit('order-click', {
  74. cartList: props.cartList,
  75. totalPrice: totalPrice.value,
  76. totalQuantity: totalQuantity.value
  77. });
  78. };
  79. </script>
  80. <style lang="scss" scoped>
  81. .bottom-action-bar {
  82. display: flex;
  83. justify-content: space-between;
  84. align-items: center;
  85. border-radius: 0;
  86. padding: 20rpx;
  87. box-sizing: border-box;
  88. position: fixed;
  89. left: 0;
  90. right: 0;
  91. bottom: 0;
  92. z-index: 999;
  93. padding-bottom: env(safe-area-inset-bottom);
  94. background-color: #fff;
  95. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(0, 0, 0, 0.05);
  96. }
  97. .coupon-btn {
  98. width: 140rpx;
  99. height: 100rpx;
  100. position: relative;
  101. .bg-img {
  102. width: 100%;
  103. height: 100%;
  104. position: absolute;
  105. left: 0;
  106. top: 0;
  107. }
  108. .coupon-content {
  109. position: absolute;
  110. left: 0;
  111. top: 0;
  112. z-index: 1;
  113. width: 100%;
  114. height: 100%;
  115. color: #fff;
  116. text-align: center;
  117. padding-top: 6rpx;
  118. .icon-coupon {
  119. width: 30rpx;
  120. height: 21rpx;
  121. }
  122. .coupon-text {
  123. width: 100%;
  124. font-size: 24rpx;
  125. }
  126. }
  127. }
  128. .cart-btn {
  129. width: 561rpx;
  130. height: 100rpx;
  131. position: relative;
  132. .bg-img {
  133. width: 100%;
  134. height: 100%;
  135. position: absolute;
  136. left: 0;
  137. top: 0;
  138. }
  139. .shop-cart {
  140. display: flex;
  141. align-items: center;
  142. justify-content: space-between;
  143. width: 100%;
  144. height: 100%;
  145. position: absolute;
  146. left: 0;
  147. top: 0;
  148. z-index: 1;
  149. color: #fff;
  150. padding-left: 30rpx;
  151. .shop-cart-content {
  152. display: flex;
  153. align-items: center;
  154. }
  155. .icon-shopping-cart {
  156. width: 76rpx;
  157. height: 76rpx;
  158. }
  159. .price {
  160. display: flex;
  161. align-items: baseline;
  162. color: #FFFFFF;
  163. font-weight: bold;
  164. .price-symbol {
  165. font-size: 24rpx;
  166. margin-right: 2rpx;
  167. }
  168. .price-number {
  169. font-size: 38rpx;
  170. }
  171. }
  172. .number{
  173. display: inline-block;
  174. padding: 2rpx 10rpx;
  175. background-color: #FF4545;
  176. position: absolute;
  177. left: 80rpx;
  178. top: 10rpx;
  179. border-radius: 40rpx;
  180. color: #fff;
  181. font-size: 24rpx;
  182. }
  183. }
  184. .order-btn {
  185. width: 200rpx;
  186. height: 100rpx;
  187. display: flex;
  188. align-items: center;
  189. justify-content: center;
  190. font-weight: bold;
  191. font-size: 31rpx;
  192. color: #FFFFFF;
  193. }
  194. }
  195. </style>