BottomActionBar.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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'>{{ displayTotalQuantity }}</view>
  18. <view class="price">
  19. <text class="price-symbol">¥</text>
  20. <text class="price-number">{{ formatPrice(displayTotalPrice) }}</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. // 购物车数据列表(可为接口返回的 items:cuisineId/cuisineName/quantity/unitPrice/subtotalAmount)
  34. cartList: {
  35. type: Array,
  36. default: () => []
  37. },
  38. // 可选:接口返回的 totalQuantity,传入时优先使用
  39. totalQuantity: {
  40. type: Number,
  41. default: null
  42. },
  43. // 可选:接口返回的 totalAmount,传入时优先使用
  44. totalAmount: {
  45. type: Number,
  46. default: null
  47. }
  48. });
  49. const emit = defineEmits(['coupon-click', 'cart-click', 'order-click']);
  50. // 取单品金额:接口项用 unitPrice,兼容 totalPrice/price 等
  51. const getItemPrice = (item) => {
  52. const p = item?.unitPrice ?? item?.price ?? item?.totalPrice ?? item?.salePrice ?? item?.currentPrice ?? 0;
  53. return Number(p) || 0;
  54. };
  55. // 行小计:接口项用 subtotalAmount,否则 数量×单价
  56. const getItemLinePrice = (item) => {
  57. if (item?.subtotalAmount != null) return Number(item.subtotalAmount);
  58. return (Number(item?.quantity) || 0) * getItemPrice(item);
  59. };
  60. // 展示总数量:优先父组件传入 totalQuantity,否则从 cart-list 计算
  61. const displayTotalQuantity = computed(() => {
  62. if (props.totalQuantity != null && props.totalQuantity !== '') return Number(props.totalQuantity);
  63. return (props.cartList || []).reduce((sum, item) => sum + (Number(item?.quantity) || 0), 0);
  64. });
  65. // 展示总金额:优先父组件传入 totalAmount,否则从 cart-list 按行小计累加
  66. const displayTotalPrice = computed(() => {
  67. if (props.totalAmount != null && props.totalAmount !== '') return Number(props.totalAmount);
  68. return (props.cartList || []).reduce((sum, item) => sum + getItemLinePrice(item), 0);
  69. });
  70. // 格式化总金额
  71. const formatPrice = (price) => {
  72. const num = Number(price);
  73. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  74. };
  75. // 优惠券点击
  76. const handleCouponClick = () => {
  77. emit('coupon-click');
  78. };
  79. // 购物车点击
  80. const handleCartClick = () => {
  81. emit('cart-click');
  82. };
  83. // 下单点击
  84. const handleOrderClick = () => {
  85. if (displayTotalQuantity.value === 0) return;
  86. emit('order-click', {
  87. cartList: props.cartList,
  88. totalPrice: displayTotalPrice.value,
  89. totalQuantity: displayTotalQuantity.value
  90. });
  91. };
  92. </script>
  93. <style lang="scss" scoped>
  94. .bottom-action-bar {
  95. display: flex;
  96. justify-content: space-between;
  97. align-items: center;
  98. border-radius: 0;
  99. padding: 20rpx;
  100. box-sizing: border-box;
  101. position: fixed;
  102. left: 0;
  103. right: 0;
  104. bottom: 0;
  105. z-index: 999;
  106. padding-bottom: env(safe-area-inset-bottom);
  107. background-color: #fff;
  108. box-shadow: 0rpx -11rpx 46rpx 0rpx rgba(0, 0, 0, 0.05);
  109. }
  110. .coupon-btn {
  111. width: 140rpx;
  112. height: 100rpx;
  113. position: relative;
  114. .bg-img {
  115. width: 100%;
  116. height: 100%;
  117. position: absolute;
  118. left: 0;
  119. top: 0;
  120. }
  121. .coupon-content {
  122. position: absolute;
  123. left: 0;
  124. top: 0;
  125. z-index: 1;
  126. width: 100%;
  127. height: 100%;
  128. color: #fff;
  129. text-align: center;
  130. padding-top: 6rpx;
  131. .icon-coupon {
  132. width: 30rpx;
  133. height: 21rpx;
  134. }
  135. .coupon-text {
  136. width: 100%;
  137. font-size: 24rpx;
  138. }
  139. }
  140. }
  141. .cart-btn {
  142. width: 561rpx;
  143. height: 100rpx;
  144. position: relative;
  145. .bg-img {
  146. width: 100%;
  147. height: 100%;
  148. position: absolute;
  149. left: 0;
  150. top: 0;
  151. }
  152. .shop-cart {
  153. display: flex;
  154. align-items: center;
  155. justify-content: space-between;
  156. width: 100%;
  157. height: 100%;
  158. position: absolute;
  159. left: 0;
  160. top: 0;
  161. z-index: 1;
  162. color: #fff;
  163. padding-left: 30rpx;
  164. .shop-cart-content {
  165. display: flex;
  166. align-items: center;
  167. }
  168. .icon-shopping-cart {
  169. width: 76rpx;
  170. height: 76rpx;
  171. }
  172. .price {
  173. display: flex;
  174. align-items: baseline;
  175. color: #FFFFFF;
  176. font-weight: bold;
  177. .price-symbol {
  178. font-size: 24rpx;
  179. margin-right: 2rpx;
  180. }
  181. .price-number {
  182. font-size: 38rpx;
  183. }
  184. }
  185. .number{
  186. display: inline-block;
  187. padding: 2rpx 10rpx;
  188. background-color: #FF4545;
  189. position: absolute;
  190. left: 80rpx;
  191. top: 10rpx;
  192. border-radius: 40rpx;
  193. color: #fff;
  194. font-size: 24rpx;
  195. }
  196. }
  197. .order-btn {
  198. width: 200rpx;
  199. height: 100rpx;
  200. display: flex;
  201. align-items: center;
  202. justify-content: center;
  203. font-weight: bold;
  204. font-size: 31rpx;
  205. color: #FFFFFF;
  206. }
  207. }
  208. </style>