CartModal.vue 12 KB

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