FoodCard.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <view class="food-card">
  3. <view class="food-item">
  4. <image class="food-image" :src="food.image" mode="aspectFill" @click="handleFoodClick" />
  5. <view class="food-info">
  6. <view class="food-header">
  7. <view class="food-title">{{ food.name }}</view>
  8. <view class="food-price">
  9. <text class="price-symbol">¥</text>
  10. <text class="price-main">{{ getPriceMain(food.price) }}</text>
  11. <text class="price-decimal" v-if="getPriceDecimal(food.price)">.{{ getPriceDecimal(food.price) }}</text>
  12. </view>
  13. </view>
  14. <view class="food-desc">{{ food.desc }}</view>
  15. <view class="food-tags">
  16. <view v-for="(tag, tagIndex) in food.tags" :key="tagIndex" class="food-tag" :class="tag.type">
  17. {{ tag.text }}
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. <view class="food-footer">
  23. <view class="food-sales">
  24. <image :src="getFileUrl('img/icon/star.png')" mode="aspectFill" class="star-icon"></image>
  25. <text class="sales-text">月售:{{ food.monthlySales || 0 }}</text>
  26. </view>
  27. <view class="food-actions">
  28. <view class="action-btn minus" :class="{ disabled: food.quantity === 0 }" @click="handleDecrease" hover-class="hover-active">
  29. <image :src="getFileUrl('img/icon/reduce1.png')" mode="aspectFit" class="action-icon" v-show="food.quantity == 0"></image>
  30. <image :src="getFileUrl('img/icon/reduce2.png')" mode="aspectFit" class="action-icon" v-show="food.quantity != 0"></image>
  31. </view>
  32. <view class="quantity">{{ food.quantity || 0 }}</view>
  33. <view class="action-btn plus" @click="handleIncrease" hover-class="hover-active">
  34. <image :src="getFileUrl('img/icon/add2.png')" mode="widthFix" class="action-icon" v-show="food.quantity < 99"></image>
  35. <image :src="getFileUrl('img/icon/add1.png')" mode="widthFix" class="action-icon" v-show="food.quantity >= 99"></image>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script setup>
  42. import { getFileUrl } from "@/utils/file.js";
  43. import { go } from "@/utils/utils.js";
  44. const props = defineProps({
  45. food: {
  46. type: Object,
  47. required: true,
  48. default: () => ({
  49. id: null,
  50. name: '',
  51. price: 0,
  52. desc: '',
  53. image: '',
  54. tags: [],
  55. monthlySales: 0,
  56. quantity: 0
  57. })
  58. }
  59. });
  60. const emit = defineEmits(['increase', 'decrease']);
  61. const handleFoodClick = () => {
  62. go('/pages/foodDetail/index');
  63. };
  64. const handleIncrease = () => {
  65. if (props.food.quantity >= 99) return;
  66. emit('increase', props.food);
  67. };
  68. const handleDecrease = () => {
  69. if (props.food.quantity > 0) {
  70. emit('decrease', props.food);
  71. }
  72. };
  73. // 获取价格整数部分
  74. const getPriceMain = (price) => {
  75. if (!price) return '0';
  76. const priceStr = String(price);
  77. const dotIndex = priceStr.indexOf('.');
  78. return dotIndex > -1 ? priceStr.substring(0, dotIndex) : priceStr;
  79. };
  80. // 获取价格小数部分
  81. const getPriceDecimal = (price) => {
  82. if (!price) return '';
  83. const priceStr = String(price);
  84. const dotIndex = priceStr.indexOf('.');
  85. return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '';
  86. };
  87. </script>
  88. <style lang="scss" scoped>
  89. .food-card {
  90. padding: 20rpx;
  91. background-color: #fff;
  92. margin-bottom: 20rpx;
  93. border-radius: 8rpx;
  94. box-sizing: border-box;
  95. }
  96. .food-item {
  97. display: flex;
  98. }
  99. .food-image {
  100. width: 180rpx;
  101. height: 180rpx;
  102. border-radius: 8rpx;
  103. flex-shrink: 0;
  104. background-color: #f5f5f5;
  105. }
  106. .food-info {
  107. flex: 1;
  108. margin-left: 20rpx;
  109. display: flex;
  110. flex-direction: column;
  111. position: relative;
  112. }
  113. .food-header {
  114. display: flex;
  115. align-items: center;
  116. justify-content: space-between;
  117. margin-bottom: 10rpx;
  118. }
  119. .food-title {
  120. font-weight: bold;
  121. font-size: 32rpx;
  122. color: #151515;
  123. flex: 1;
  124. margin-right: 20rpx;
  125. }
  126. .food-price {
  127. display: flex;
  128. align-items: baseline;
  129. color: #E61F19;
  130. line-height: 1;
  131. flex-shrink: 0;
  132. .price-symbol {
  133. font-size: 20rpx;
  134. font-weight: bold;
  135. }
  136. .price-main {
  137. font-size: 32rpx;
  138. font-weight: bold;
  139. }
  140. .price-decimal {
  141. font-size: 24rpx;
  142. font-weight: bold;
  143. }
  144. }
  145. .food-desc {
  146. font-size: 24rpx;
  147. color: #797979;
  148. margin-bottom: 10rpx;
  149. line-height: 1.5;
  150. }
  151. .food-tags {
  152. display: flex;
  153. gap: 10rpx;
  154. margin-bottom: 10rpx;
  155. }
  156. .food-tag {
  157. padding: 4rpx 12rpx;
  158. border-radius: 4rpx;
  159. font-size: 20rpx;
  160. &.signature {
  161. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  162. color: #fff;
  163. }
  164. &.spicy {
  165. background: #2E2E2E;
  166. color: #fff;
  167. }
  168. }
  169. .food-footer {
  170. display: flex;
  171. align-items: center;
  172. justify-content: space-between;
  173. margin-top: 10rpx;
  174. }
  175. .food-sales {
  176. font-size: 22rpx;
  177. color: #999;
  178. display: flex;
  179. align-items: center;
  180. gap: 4rpx;
  181. .star-icon {
  182. width: 26rpx;
  183. height: 26rpx;
  184. }
  185. .sales-text {
  186. font-size: 22rpx;
  187. color: #999;
  188. }
  189. }
  190. .food-actions {
  191. display: flex;
  192. align-items: center;
  193. justify-content: space-between;
  194. width: 214rpx;
  195. height: 58rpx;
  196. background: #F8F8F8;
  197. border-radius: 56rpx;
  198. box-sizing: border-box;
  199. padding: 0 3rpx;
  200. }
  201. .action-btn {
  202. width: 52rpx;
  203. height: 52rpx;
  204. border-radius: 50%;
  205. display: flex;
  206. align-items: center;
  207. justify-content: center;
  208. font-size: 32rpx;
  209. font-weight: 600;
  210. transition: all 0.3s;
  211. background-color: #fff;
  212. .action-icon {
  213. width: 24rpx;
  214. height: 24rpx;
  215. }
  216. }
  217. .quantity {
  218. font-size: 28rpx;
  219. color: #333;
  220. min-width: 40rpx;
  221. text-align: center;
  222. }
  223. </style>