FoodCard.vue 7.5 KB

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