FoodCard.vue 7.7 KB

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