CartModal.vue 12 KB

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