CartModal.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. <!-- 数量选择器(餐具费 0 元时不可修改) -->
  32. <view class="cart-item__actions" :class="{ 'cart-item__actions--disabled': !canEditItemQuantity(item) }">
  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);餐具费为 0 元时不允许修改数量
  107. const isTablewareItem = (item) => {
  108. if (!item) return false;
  109. const id = item.cuisineId ?? item.id;
  110. return Number(id) === -1;
  111. };
  112. // 该项是否允许修改数量:非餐具允许;餐具仅当单价>0 时允许
  113. const canEditItemQuantity = (item) => {
  114. if (!item) return false;
  115. if (!isTablewareItem(item)) return true;
  116. const price = getItemPrice(item);
  117. return Number(price) > 0;
  118. };
  119. // 处理关闭
  120. const handleClose = () => {
  121. getOpen.value = false;
  122. emit('close');
  123. };
  124. // 增加数量(餐具费 0 元时不触发)
  125. const handleIncrease = (item) => {
  126. if (!canEditItemQuantity(item)) return;
  127. if (item.quantity >= 99) return;
  128. emit('increase', item);
  129. };
  130. // 减少数量(餐具费 0 元时不触发)
  131. const handleDecrease = (item) => {
  132. if (!canEditItemQuantity(item)) return;
  133. if (item && item.quantity > 0) {
  134. emit('decrease', item);
  135. }
  136. };
  137. // 清空购物车
  138. const handleClear = () => {
  139. uni.showModal({
  140. title: '提示',
  141. content: '确定要清空购物车吗?',
  142. success: (res) => {
  143. if (res.confirm) {
  144. emit('clear');
  145. }
  146. }
  147. });
  148. };
  149. </script>
  150. <style scoped lang="scss">
  151. .cart-modal {
  152. width: 100%;
  153. height: 70vh;
  154. max-height: 70vh;
  155. background: #FFFFFF;
  156. border-radius: 24rpx 24rpx 0 0;
  157. padding: 0;
  158. box-sizing: border-box;
  159. display: flex;
  160. flex-direction: column;
  161. overflow: hidden;
  162. &__header {
  163. flex-shrink: 0;
  164. position: relative;
  165. display: flex;
  166. align-items: center;
  167. justify-content: space-between;
  168. height: 88rpx;
  169. padding: 0 30rpx;
  170. background: #FFFFFF;
  171. .header-title {
  172. font-weight: bold;
  173. font-size: 34rpx;
  174. color: #151515;
  175. }
  176. .header-clear {
  177. display: flex;
  178. align-items: center;
  179. gap: 8rpx;
  180. color: #AAAAAA;
  181. font-size: 28rpx;
  182. padding: 8rpx 16rpx;
  183. border-radius: 8rpx;
  184. transition: all 0.3s;
  185. &:active {
  186. background-color: #F5F5F5;
  187. }
  188. .clear-text {
  189. font-size: 28rpx;
  190. color: #AAAAAA;
  191. }
  192. }
  193. }
  194. &__list {
  195. flex: 1;
  196. min-height: 0;
  197. /* iOS:不设 height:0,避免 flex 子项在 iOS WebView 中被算成 0 导致列表不显示;用 min-height 兜底 */
  198. min-height: 300rpx;
  199. padding: 24rpx 30rpx;
  200. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  201. box-sizing: border-box;
  202. -webkit-overflow-scrolling: touch;
  203. }
  204. }
  205. .cart-item {
  206. display: flex;
  207. align-items: center;
  208. padding: 20rpx 0;
  209. border-bottom: 1rpx solid #F0F0F0;
  210. &:last-child {
  211. border-bottom: none;
  212. }
  213. &__image {
  214. width: 140rpx;
  215. height: 140rpx;
  216. border-radius: 8rpx;
  217. flex-shrink: 0;
  218. background-color: #f5f5f5;
  219. }
  220. &__info {
  221. flex: 1;
  222. margin-left: 20rpx;
  223. display: flex;
  224. flex-direction: column;
  225. justify-content: space-between;
  226. min-height: 140rpx;
  227. }
  228. &__name {
  229. font-size: 28rpx;
  230. font-weight: 500;
  231. color: #151515;
  232. margin-bottom: 8rpx;
  233. }
  234. &__remark {
  235. font-size: 24rpx;
  236. color: #999;
  237. margin-bottom: 8rpx;
  238. }
  239. &__tags {
  240. display: flex;
  241. gap: 20rpx;
  242. margin-bottom: 12rpx;
  243. }
  244. &__tag {
  245. padding: 6rpx 16rpx;
  246. border-radius: 4rpx;
  247. font-size: 20rpx;
  248. background: #000;
  249. color: #fff;
  250. &--signature {
  251. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  252. color: #fff;
  253. border-radius: 4rpx;
  254. }
  255. }
  256. &__price {
  257. display: flex;
  258. align-items: baseline;
  259. color: #151515;
  260. font-weight: 500;
  261. .price-symbol {
  262. font-size: 24rpx;
  263. margin-right: 2rpx;
  264. }
  265. .price-number {
  266. font-size: 32rpx;
  267. }
  268. }
  269. &__actions {
  270. display: flex;
  271. align-items: center;
  272. justify-content: space-between;
  273. width: 180rpx;
  274. height: 58rpx;
  275. background: #F8F8F8;
  276. border-radius: 56rpx;
  277. box-sizing: border-box;
  278. padding: 0 3rpx;
  279. margin-left: 20rpx;
  280. &--disabled {
  281. opacity: 0.6;
  282. pointer-events: none;
  283. }
  284. }
  285. }
  286. .action-btn {
  287. width: 52rpx;
  288. height: 52rpx;
  289. border-radius: 50%;
  290. display: flex;
  291. align-items: center;
  292. justify-content: center;
  293. transition: all 0.3s;
  294. background-color: #fff;
  295. .action-icon {
  296. width: 24rpx;
  297. height: 24rpx;
  298. }
  299. &.disabled {
  300. opacity: 0.5;
  301. }
  302. }
  303. .quantity {
  304. font-size: 28rpx;
  305. color: #151515;
  306. min-width: 40rpx;
  307. text-align: center;
  308. font-weight: bold;
  309. }
  310. .cart-modal__footer {
  311. display: flex;
  312. align-items: center;
  313. justify-content: space-between;
  314. padding: 20rpx 30rpx;
  315. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  316. background: #2E2E2E;
  317. border-top: 1rpx solid #3E3E3E;
  318. .footer-coupon-btn {
  319. display: flex;
  320. flex-direction: column;
  321. align-items: center;
  322. justify-content: center;
  323. width: 140rpx;
  324. height: 100rpx;
  325. background: rgba(255, 255, 255, 0.1);
  326. border-radius: 8rpx;
  327. gap: 6rpx;
  328. .footer-coupon-icon {
  329. width: 30rpx;
  330. height: 21rpx;
  331. }
  332. .footer-coupon-text {
  333. font-size: 24rpx;
  334. color: #FFFFFF;
  335. }
  336. }
  337. .footer-cart-info {
  338. flex: 1;
  339. display: flex;
  340. align-items: center;
  341. justify-content: space-between;
  342. margin-left: 20rpx;
  343. }
  344. .footer-cart-content {
  345. display: flex;
  346. align-items: center;
  347. position: relative;
  348. .footer-cart-icon {
  349. width: 76rpx;
  350. height: 76rpx;
  351. }
  352. .footer-cart-number {
  353. display: inline-block;
  354. padding: 2rpx 10rpx;
  355. background-color: #FF4545;
  356. position: absolute;
  357. left: 60rpx;
  358. top: -10rpx;
  359. border-radius: 40rpx;
  360. color: #fff;
  361. font-size: 24rpx;
  362. }
  363. .footer-cart-price {
  364. display: flex;
  365. align-items: baseline;
  366. color: #FFFFFF;
  367. font-weight: bold;
  368. margin-left: 20rpx;
  369. .price-symbol {
  370. font-size: 24rpx;
  371. margin-right: 2rpx;
  372. }
  373. .price-number {
  374. font-size: 38rpx;
  375. }
  376. }
  377. }
  378. .footer-order-btn {
  379. width: 200rpx;
  380. height: 100rpx;
  381. display: flex;
  382. align-items: center;
  383. justify-content: center;
  384. background: #FF6B35;
  385. border-radius: 50rpx;
  386. font-weight: bold;
  387. font-size: 31rpx;
  388. color: #FFFFFF;
  389. margin-left: 20rpx;
  390. }
  391. }
  392. </style>