CartModal.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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="{ 'cart-item__tag--signature': (tag.text || '').includes('招牌') }">
  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. <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. </scroll-view>
  49. </view>
  50. </BasicModal>
  51. </template>
  52. <script setup>
  53. import { computed } from 'vue';
  54. import BasicModal from '@/components/Modal/BasicModal.vue';
  55. import { getFileUrl } from '@/utils/file.js';
  56. const props = defineProps({
  57. open: {
  58. type: Boolean,
  59. default: false
  60. },
  61. cartList: {
  62. type: Array,
  63. default: () => []
  64. }
  65. });
  66. const emit = defineEmits(['update:open', 'increase', 'decrease', 'clear', 'order-click', 'close']);
  67. const getOpen = computed({
  68. get: () => props.open,
  69. set: (val) => emit('update:open', val)
  70. });
  71. // 计算总数量
  72. const totalQuantity = computed(() => {
  73. return props.cartList.reduce((sum, item) => {
  74. return sum + (item.quantity || 0);
  75. }, 0);
  76. });
  77. // 计算总价格:接口项用 subtotalAmount,否则 数量×单价
  78. const totalPrice = computed(() => {
  79. return props.cartList.reduce((sum, item) => sum + getItemLinePrice(item), 0);
  80. });
  81. // 格式化价格(保留两位小数,避免 NaN)
  82. const formatPrice = (price) => {
  83. const num = Number(price);
  84. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  85. };
  86. const getItemImageSrc = (item) => {
  87. const raw = item?.cuisineImage ?? item?.image ?? item?.images ?? item?.imageUrl ?? item?.pic ?? item?.cover ?? '';
  88. const url = typeof raw === 'string' ? raw.split(',')[0].trim() : raw;
  89. return url ? getFileUrl(url) : '';
  90. };
  91. // 单品单价:接口返回 unitPrice,兼容 price/salePrice 等
  92. const getItemPrice = (item) => {
  93. const p = item?.unitPrice ?? item?.price ?? item?.salePrice ?? item?.currentPrice ?? item?.totalPrice ?? 0;
  94. return Number(p) || 0;
  95. };
  96. // 行小计:接口返回 subtotalAmount,否则 数量×单价
  97. const getItemLinePrice = (item) => {
  98. if (item?.subtotalAmount != null) return Number(item.subtotalAmount);
  99. const qty = Number(item?.quantity) || 0;
  100. const unitPrice = getItemPrice(item);
  101. return qty * unitPrice;
  102. };
  103. // 处理关闭
  104. const handleClose = () => {
  105. getOpen.value = false;
  106. emit('close');
  107. };
  108. const handleIncrease = (item) => {
  109. if (item.quantity >= 99) return;
  110. emit('increase', item);
  111. };
  112. const handleDecrease = (item) => {
  113. if (item && item.quantity > 0) {
  114. emit('decrease', item);
  115. }
  116. };
  117. // 清空购物车
  118. const handleClear = () => {
  119. uni.showModal({
  120. title: '提示',
  121. content: '确定要清空购物车吗?',
  122. success: (res) => {
  123. if (res.confirm) {
  124. emit('clear');
  125. }
  126. }
  127. });
  128. };
  129. </script>
  130. <style scoped lang="scss">
  131. .cart-modal {
  132. width: 100%;
  133. height: 70vh;
  134. max-height: 70vh;
  135. background: #FFFFFF;
  136. border-radius: 24rpx 24rpx 0 0;
  137. padding: 0;
  138. box-sizing: border-box;
  139. display: flex;
  140. flex-direction: column;
  141. overflow: hidden;
  142. &__header {
  143. flex-shrink: 0;
  144. position: relative;
  145. display: flex;
  146. align-items: center;
  147. justify-content: space-between;
  148. height: 88rpx;
  149. padding: 0 30rpx;
  150. background: #FFFFFF;
  151. .header-title {
  152. font-weight: bold;
  153. font-size: 34rpx;
  154. color: #151515;
  155. }
  156. .header-clear {
  157. display: flex;
  158. align-items: center;
  159. gap: 8rpx;
  160. color: #AAAAAA;
  161. font-size: 28rpx;
  162. padding: 8rpx 16rpx;
  163. border-radius: 8rpx;
  164. transition: all 0.3s;
  165. &:active {
  166. background-color: #F5F5F5;
  167. }
  168. .clear-text {
  169. font-size: 28rpx;
  170. color: #AAAAAA;
  171. }
  172. }
  173. }
  174. &__list {
  175. flex: 1;
  176. min-height: 0;
  177. /* iOS:不设 height:0,避免 flex 子项在 iOS WebView 中被算成 0 导致列表不显示;用 min-height 兜底 */
  178. min-height: 300rpx;
  179. padding: 24rpx 30rpx;
  180. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  181. box-sizing: border-box;
  182. -webkit-overflow-scrolling: touch;
  183. }
  184. }
  185. .cart-item {
  186. display: flex;
  187. align-items: center;
  188. padding: 20rpx 0;
  189. border-bottom: 1rpx solid #F0F0F0;
  190. &:last-child {
  191. border-bottom: none;
  192. }
  193. &__image {
  194. width: 140rpx;
  195. height: 140rpx;
  196. border-radius: 8rpx;
  197. flex-shrink: 0;
  198. background-color: #f5f5f5;
  199. }
  200. &__info {
  201. flex: 1;
  202. margin-left: 20rpx;
  203. display: flex;
  204. flex-direction: column;
  205. justify-content: space-between;
  206. min-height: 140rpx;
  207. }
  208. &__name {
  209. font-size: 28rpx;
  210. font-weight: 500;
  211. color: #151515;
  212. margin-bottom: 8rpx;
  213. }
  214. &__remark {
  215. font-size: 24rpx;
  216. color: #999;
  217. margin-bottom: 8rpx;
  218. }
  219. &__tags {
  220. display: flex;
  221. gap: 20rpx;
  222. margin-bottom: 12rpx;
  223. }
  224. &__tag {
  225. padding: 6rpx 16rpx;
  226. border-radius: 4rpx;
  227. font-size: 20rpx;
  228. background: #000;
  229. color: #fff;
  230. &--signature {
  231. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  232. color: #fff;
  233. border-radius: 4rpx;
  234. }
  235. }
  236. &__price {
  237. display: flex;
  238. align-items: baseline;
  239. color: #151515;
  240. font-weight: 500;
  241. .price-symbol {
  242. font-size: 24rpx;
  243. margin-right: 2rpx;
  244. }
  245. .price-number {
  246. font-size: 32rpx;
  247. }
  248. }
  249. &__actions {
  250. display: flex;
  251. align-items: center;
  252. justify-content: space-between;
  253. width: 180rpx;
  254. height: 58rpx;
  255. background: #F8F8F8;
  256. border-radius: 56rpx;
  257. box-sizing: border-box;
  258. padding: 0 3rpx;
  259. margin-left: 20rpx;
  260. &--disabled {
  261. opacity: 0.6;
  262. pointer-events: none;
  263. }
  264. }
  265. }
  266. .action-btn {
  267. width: 52rpx;
  268. height: 52rpx;
  269. border-radius: 50%;
  270. display: flex;
  271. align-items: center;
  272. justify-content: center;
  273. transition: all 0.3s;
  274. background-color: #fff;
  275. .action-icon {
  276. width: 24rpx;
  277. height: 24rpx;
  278. }
  279. &.disabled {
  280. opacity: 0.5;
  281. }
  282. }
  283. .quantity {
  284. font-size: 28rpx;
  285. color: #151515;
  286. min-width: 40rpx;
  287. text-align: center;
  288. font-weight: bold;
  289. }
  290. .cart-modal__footer {
  291. display: flex;
  292. align-items: center;
  293. justify-content: space-between;
  294. padding: 20rpx 30rpx;
  295. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  296. background: #2E2E2E;
  297. border-top: 1rpx solid #3E3E3E;
  298. .footer-coupon-btn {
  299. display: flex;
  300. flex-direction: column;
  301. align-items: center;
  302. justify-content: center;
  303. width: 140rpx;
  304. height: 100rpx;
  305. background: rgba(255, 255, 255, 0.1);
  306. border-radius: 8rpx;
  307. gap: 6rpx;
  308. .footer-coupon-icon {
  309. width: 30rpx;
  310. height: 21rpx;
  311. }
  312. .footer-coupon-text {
  313. font-size: 24rpx;
  314. color: #FFFFFF;
  315. }
  316. }
  317. .footer-cart-info {
  318. flex: 1;
  319. display: flex;
  320. align-items: center;
  321. justify-content: space-between;
  322. margin-left: 20rpx;
  323. }
  324. .footer-cart-content {
  325. display: flex;
  326. align-items: center;
  327. position: relative;
  328. .footer-cart-icon {
  329. width: 76rpx;
  330. height: 76rpx;
  331. }
  332. .footer-cart-number {
  333. display: inline-block;
  334. padding: 2rpx 10rpx;
  335. background-color: #FF4545;
  336. position: absolute;
  337. left: 60rpx;
  338. top: -10rpx;
  339. border-radius: 40rpx;
  340. color: #fff;
  341. font-size: 24rpx;
  342. }
  343. .footer-cart-price {
  344. display: flex;
  345. align-items: baseline;
  346. color: #FFFFFF;
  347. font-weight: bold;
  348. margin-left: 20rpx;
  349. .price-symbol {
  350. font-size: 24rpx;
  351. margin-right: 2rpx;
  352. }
  353. .price-number {
  354. font-size: 38rpx;
  355. }
  356. }
  357. }
  358. .footer-order-btn {
  359. width: 200rpx;
  360. height: 100rpx;
  361. display: flex;
  362. align-items: center;
  363. justify-content: center;
  364. background: #FF6B35;
  365. border-radius: 50rpx;
  366. font-weight: bold;
  367. font-size: 31rpx;
  368. color: #FFFFFF;
  369. margin-left: 20rpx;
  370. }
  371. }
  372. </style>