FoodCard.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <view class="food-card">
  3. <view class="food-item">
  4. <image class="food-image" :src="food.images" 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.totalPrice) }}</text>
  11. <text class="price-decimal" v-if="getPriceDecimal(food.totalPrice)">.{{ getPriceDecimal(food.totalPrice) }}</text>
  12. </view>
  13. </view>
  14. <view class="food-desc">{{ food.description }}</view>
  15. <view class="food-tags" v-if="normalizedTags.length">
  16. <view v-for="(tag, tagIndex) in normalizedTags" :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 { computed } from "vue";
  43. import { getFileUrl } from "@/utils/file.js";
  44. import { go } from "@/utils/utils.js";
  45. const props = defineProps({
  46. food: {
  47. type: Object,
  48. required: true,
  49. default: () => ({
  50. id: null,
  51. name: '',
  52. price: 0,
  53. desc: '',
  54. image: '',
  55. tags: [],
  56. monthlySales: 0,
  57. quantity: 0
  58. })
  59. }
  60. });
  61. const emit = defineEmits(['increase', 'decrease']);
  62. // 后端返回的 tags 统一为 [{ text, type }] 便于绑定(兼容多种字段名与格式)
  63. const normalizedTags = computed(() => {
  64. const food = props.food;
  65. let raw = food?.tags ?? food?.tagList ?? food?.tagNames ?? food?.labels ?? food?.tag;
  66. if (raw == null) return [];
  67. // 第一步:先把各种类型统一转为数组
  68. let arr = [];
  69. if (Array.isArray(raw)) {
  70. arr = raw;
  71. } else if (typeof raw === 'string') {
  72. const trimmed = raw.trim();
  73. if (trimmed.startsWith('[')) {
  74. try {
  75. arr = JSON.parse(trimmed);
  76. if (!Array.isArray(arr)) arr = [];
  77. } catch {
  78. arr = trimmed ? [trimmed] : [];
  79. }
  80. } else {
  81. arr = trimmed ? trimmed.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  82. }
  83. } else if (raw && typeof raw === 'object') {
  84. arr = Array.isArray(raw.list) ? raw.list : Array.isArray(raw.items) ? raw.items : [];
  85. }
  86. // 第二步:将数组每一项转为 { text, type }
  87. return arr.map(item => {
  88. if (item == null) return { text: '', type: '' };
  89. if (typeof item === 'string') return { text: item, type: '' };
  90. if (typeof item === 'number') return { text: String(item), type: '' };
  91. return {
  92. text: item.text ?? item.tagName ?? item.name ?? item.label ?? item.title ?? '',
  93. type: item.type ?? item.tagType ?? ''
  94. };
  95. }).filter(t => t.text !== '' && t.text != null);
  96. });
  97. const handleFoodClick = () => {
  98. go('/pages/foodDetail/index');
  99. };
  100. const handleIncrease = () => {
  101. if (props.food.quantity >= 99) return;
  102. emit('increase', props.food);
  103. };
  104. const handleDecrease = () => {
  105. if (props.food.quantity > 0) {
  106. emit('decrease', props.food);
  107. }
  108. };
  109. // 获取价格整数部分
  110. const getPriceMain = (price) => {
  111. if (!price) return '0';
  112. const priceStr = String(price);
  113. const dotIndex = priceStr.indexOf('.');
  114. return dotIndex > -1 ? priceStr.substring(0, dotIndex) : priceStr;
  115. };
  116. // 获取价格小数部分
  117. const getPriceDecimal = (price) => {
  118. if (!price) return '';
  119. const priceStr = String(price);
  120. const dotIndex = priceStr.indexOf('.');
  121. return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '';
  122. };
  123. </script>
  124. <style lang="scss" scoped>
  125. .food-card {
  126. padding: 20rpx;
  127. background-color: #fff;
  128. margin-bottom: 20rpx;
  129. border-radius: 8rpx;
  130. box-sizing: border-box;
  131. }
  132. .food-item {
  133. display: flex;
  134. }
  135. .food-image {
  136. width: 180rpx;
  137. height: 180rpx;
  138. border-radius: 8rpx;
  139. flex-shrink: 0;
  140. background-color: #f5f5f5;
  141. }
  142. .food-info {
  143. flex: 1;
  144. margin-left: 20rpx;
  145. display: flex;
  146. flex-direction: column;
  147. position: relative;
  148. }
  149. .food-header {
  150. display: flex;
  151. align-items: center;
  152. justify-content: space-between;
  153. margin-bottom: 10rpx;
  154. }
  155. .food-title {
  156. font-weight: bold;
  157. font-size: 32rpx;
  158. color: #151515;
  159. flex: 1;
  160. margin-right: 20rpx;
  161. }
  162. .food-price {
  163. display: flex;
  164. align-items: baseline;
  165. color: #E61F19;
  166. line-height: 1;
  167. flex-shrink: 0;
  168. .price-symbol {
  169. font-size: 20rpx;
  170. font-weight: bold;
  171. }
  172. .price-main {
  173. font-size: 32rpx;
  174. font-weight: bold;
  175. }
  176. .price-decimal {
  177. font-size: 24rpx;
  178. font-weight: bold;
  179. }
  180. }
  181. .food-desc {
  182. font-size: 24rpx;
  183. color: #797979;
  184. margin-bottom: 10rpx;
  185. line-height: 1.5;
  186. }
  187. .food-tags {
  188. display: flex;
  189. gap: 10rpx;
  190. margin-bottom: 10rpx;
  191. }
  192. .food-tag {
  193. padding: 4rpx 12rpx;
  194. border-radius: 4rpx;
  195. font-size: 20rpx;
  196. color: #333;
  197. background-color: #f0f0f0;
  198. &.signature {
  199. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  200. color: #fff;
  201. }
  202. &.spicy {
  203. background: #2E2E2E;
  204. color: #fff;
  205. }
  206. }
  207. .food-footer {
  208. display: flex;
  209. align-items: center;
  210. justify-content: space-between;
  211. margin-top: 10rpx;
  212. }
  213. .food-sales {
  214. font-size: 22rpx;
  215. color: #999;
  216. display: flex;
  217. align-items: center;
  218. gap: 4rpx;
  219. .star-icon {
  220. width: 26rpx;
  221. height: 26rpx;
  222. }
  223. .sales-text {
  224. font-size: 22rpx;
  225. color: #999;
  226. }
  227. }
  228. .food-actions {
  229. display: flex;
  230. align-items: center;
  231. justify-content: space-between;
  232. width: 214rpx;
  233. height: 58rpx;
  234. background: #F8F8F8;
  235. border-radius: 56rpx;
  236. box-sizing: border-box;
  237. padding: 0 3rpx;
  238. }
  239. .action-btn {
  240. width: 52rpx;
  241. height: 52rpx;
  242. border-radius: 50%;
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. font-size: 32rpx;
  247. font-weight: 600;
  248. transition: all 0.3s;
  249. background-color: #fff;
  250. .action-icon {
  251. width: 24rpx;
  252. height: 24rpx;
  253. }
  254. }
  255. .quantity {
  256. font-size: 28rpx;
  257. color: #333;
  258. min-width: 40rpx;
  259. text-align: center;
  260. }
  261. </style>