CartModal.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. <!-- 数量选择器 -->
  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. max-height: 70vh;
  145. background: #FFFFFF;
  146. border-radius: 24rpx 24rpx 0 0;
  147. padding: 0;
  148. box-sizing: border-box;
  149. display: flex;
  150. flex-direction: column;
  151. overflow: hidden;
  152. &__header {
  153. flex-shrink: 0;
  154. position: relative;
  155. display: flex;
  156. align-items: center;
  157. justify-content: space-between;
  158. height: 88rpx;
  159. padding: 0 30rpx;
  160. background: #FFFFFF;
  161. .header-title {
  162. font-weight: bold;
  163. font-size: 34rpx;
  164. color: #151515;
  165. }
  166. .header-clear {
  167. display: flex;
  168. align-items: center;
  169. gap: 8rpx;
  170. color: #AAAAAA;
  171. font-size: 28rpx;
  172. padding: 8rpx 16rpx;
  173. border-radius: 8rpx;
  174. transition: all 0.3s;
  175. &:active {
  176. background-color: #F5F5F5;
  177. }
  178. .clear-text {
  179. font-size: 28rpx;
  180. color: #AAAAAA;
  181. }
  182. }
  183. }
  184. &__list {
  185. flex: 1;
  186. min-height: 0;
  187. height: 0;
  188. padding: 24rpx 30rpx;
  189. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  190. box-sizing: border-box;
  191. }
  192. }
  193. .cart-item {
  194. display: flex;
  195. align-items: center;
  196. padding: 20rpx 0;
  197. border-bottom: 1rpx solid #F0F0F0;
  198. &:last-child {
  199. border-bottom: none;
  200. }
  201. &__image {
  202. width: 140rpx;
  203. height: 140rpx;
  204. border-radius: 8rpx;
  205. flex-shrink: 0;
  206. background-color: #f5f5f5;
  207. }
  208. &__info {
  209. flex: 1;
  210. margin-left: 20rpx;
  211. display: flex;
  212. flex-direction: column;
  213. justify-content: space-between;
  214. min-height: 140rpx;
  215. }
  216. &__name {
  217. font-size: 28rpx;
  218. font-weight: 500;
  219. color: #151515;
  220. margin-bottom: 8rpx;
  221. }
  222. &__remark {
  223. font-size: 24rpx;
  224. color: #999;
  225. margin-bottom: 8rpx;
  226. }
  227. &__tags {
  228. display: flex;
  229. gap: 10rpx;
  230. margin-bottom: 12rpx;
  231. }
  232. &__tag {
  233. padding: 4rpx 12rpx;
  234. border-radius: 4rpx;
  235. font-size: 20rpx;
  236. &.signature {
  237. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  238. color: #fff;
  239. }
  240. &.spicy {
  241. background: #2E2E2E;
  242. color: #fff;
  243. }
  244. }
  245. &__price {
  246. display: flex;
  247. align-items: baseline;
  248. color: #151515;
  249. font-weight: 500;
  250. .price-symbol {
  251. font-size: 24rpx;
  252. margin-right: 2rpx;
  253. }
  254. .price-number {
  255. font-size: 32rpx;
  256. }
  257. }
  258. &__actions {
  259. display: flex;
  260. align-items: center;
  261. justify-content: space-between;
  262. width: 180rpx;
  263. height: 58rpx;
  264. background: #F8F8F8;
  265. border-radius: 56rpx;
  266. box-sizing: border-box;
  267. padding: 0 3rpx;
  268. margin-left: 20rpx;
  269. }
  270. }
  271. .action-btn {
  272. width: 52rpx;
  273. height: 52rpx;
  274. border-radius: 50%;
  275. display: flex;
  276. align-items: center;
  277. justify-content: center;
  278. transition: all 0.3s;
  279. background-color: #fff;
  280. .action-icon {
  281. width: 24rpx;
  282. height: 24rpx;
  283. }
  284. &.disabled {
  285. opacity: 0.5;
  286. }
  287. }
  288. .quantity {
  289. font-size: 28rpx;
  290. color: #151515;
  291. min-width: 40rpx;
  292. text-align: center;
  293. font-weight: bold;
  294. }
  295. .cart-modal__footer {
  296. display: flex;
  297. align-items: center;
  298. justify-content: space-between;
  299. padding: 20rpx 30rpx;
  300. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  301. background: #2E2E2E;
  302. border-top: 1rpx solid #3E3E3E;
  303. .footer-coupon-btn {
  304. display: flex;
  305. flex-direction: column;
  306. align-items: center;
  307. justify-content: center;
  308. width: 140rpx;
  309. height: 100rpx;
  310. background: rgba(255, 255, 255, 0.1);
  311. border-radius: 8rpx;
  312. gap: 6rpx;
  313. .footer-coupon-icon {
  314. width: 30rpx;
  315. height: 21rpx;
  316. }
  317. .footer-coupon-text {
  318. font-size: 24rpx;
  319. color: #FFFFFF;
  320. }
  321. }
  322. .footer-cart-info {
  323. flex: 1;
  324. display: flex;
  325. align-items: center;
  326. justify-content: space-between;
  327. margin-left: 20rpx;
  328. }
  329. .footer-cart-content {
  330. display: flex;
  331. align-items: center;
  332. position: relative;
  333. .footer-cart-icon {
  334. width: 76rpx;
  335. height: 76rpx;
  336. }
  337. .footer-cart-number {
  338. display: inline-block;
  339. padding: 2rpx 10rpx;
  340. background-color: #FF4545;
  341. position: absolute;
  342. left: 60rpx;
  343. top: -10rpx;
  344. border-radius: 40rpx;
  345. color: #fff;
  346. font-size: 24rpx;
  347. }
  348. .footer-cart-price {
  349. display: flex;
  350. align-items: baseline;
  351. color: #FFFFFF;
  352. font-weight: bold;
  353. margin-left: 20rpx;
  354. .price-symbol {
  355. font-size: 24rpx;
  356. margin-right: 2rpx;
  357. }
  358. .price-number {
  359. font-size: 38rpx;
  360. }
  361. }
  362. }
  363. .footer-order-btn {
  364. width: 200rpx;
  365. height: 100rpx;
  366. display: flex;
  367. align-items: center;
  368. justify-content: center;
  369. background: #FF6B35;
  370. border-radius: 50rpx;
  371. font-weight: bold;
  372. font-size: 31rpx;
  373. color: #FFFFFF;
  374. margin-left: 20rpx;
  375. }
  376. }
  377. </style>