FoodCard.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <template>
  2. <view class="food-card">
  3. <view class="food-item">
  4. <image class="food-image" :src="foodImageUrl" 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(displayUnitPrice) }}</text>
  11. <text class="price-decimal" v-if="getPriceDecimal(displayUnitPrice)">.{{ getPriceDecimal(displayUnitPrice) }}</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. // 商品图片:与首页 index 一致,取第一张(逗号分隔则截取逗号前第一张),支持对象 { url/path/src },再通过 getFileUrl 转成完整地址
  65. function firstImage(val) {
  66. if (val == null || val === '') return '';
  67. if (Array.isArray(val)) {
  68. const first = val[0];
  69. if (first != null && first !== '') {
  70. if (typeof first === 'object' && first !== null) return first.url ?? first.path ?? first.src ?? first.link ?? '';
  71. return String(first).split(/[,,]/)[0].trim();
  72. }
  73. return '';
  74. }
  75. if (typeof val === 'object') return val.url ?? val.path ?? val.src ?? val.link ?? '';
  76. const str = String(val).trim();
  77. return str ? str.split(/[,,]/)[0].trim() : '';
  78. }
  79. const foodImageUrl = computed(() => {
  80. const f = props.food;
  81. if (!f) return getFileUrl('img/icon/shop.png');
  82. const imageUrl =
  83. firstImage(f.images) ||
  84. firstImage(f.imageContent) ||
  85. firstImage(f.cuisineImage) ||
  86. firstImage(f.image) ||
  87. firstImage(f.imageUrl) ||
  88. firstImage(f.pic) ||
  89. firstImage(f.cover) ||
  90. '';
  91. return getFileUrl(imageUrl || 'img/icon/shop.png');
  92. });
  93. /** 单价展示:兼容美食 totalPrice 与通用价目 currentPrice */
  94. const displayUnitPrice = computed(() => {
  95. const f = props.food;
  96. if (!f) return 0;
  97. const v = f.totalPrice ?? f.currentPrice ?? f.price ?? f.salePrice ?? f.unitPrice ?? f.originalPrice ?? 0;
  98. const n = Number(v);
  99. return Number.isFinite(n) && !Number.isNaN(n) ? n : 0;
  100. });
  101. // 后端返回的 tags 统一为 [{ text, type }] 便于绑定(兼容多种字段名与格式)
  102. const normalizedTags = computed(() => {
  103. const food = props.food;
  104. let raw = food?.tags ?? food?.tagList ?? food?.tagNames ?? food?.labels ?? food?.tag;
  105. if (raw == null) return [];
  106. // 第一步:先把各种类型统一转为数组(保证最后每个 tag 一个 view)
  107. let arr = [];
  108. if (Array.isArray(raw)) {
  109. arr = raw;
  110. } else if (typeof raw === 'string') {
  111. const trimmed = raw.trim();
  112. if (trimmed.startsWith('[')) {
  113. try {
  114. arr = JSON.parse(trimmed);
  115. if (!Array.isArray(arr)) arr = [];
  116. } catch {
  117. arr = trimmed ? [trimmed] : [];
  118. }
  119. } else {
  120. arr = trimmed ? trimmed.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  121. }
  122. } else if (raw && typeof raw === 'object') {
  123. if (Array.isArray(raw.list)) arr = raw.list;
  124. else if (Array.isArray(raw.items)) arr = raw.items;
  125. else if (raw.text != null || raw.tagName != null || raw.name != null || raw.label != null || raw.title != null) arr = [raw];
  126. else arr = [];
  127. }
  128. // 第二步:将数组每一项转为 { text, type }
  129. const withText = arr.map(item => {
  130. if (item == null) return { text: '', type: '' };
  131. if (typeof item === 'string') return { text: item, type: '' };
  132. if (typeof item === 'number') return { text: String(item), type: '' };
  133. return {
  134. text: item.text ?? item.tagName ?? item.name ?? item.label ?? item.title ?? '',
  135. type: item.type ?? item.tagType ?? ''
  136. };
  137. }).filter(t => t.text !== '' && t.text != null);
  138. // 第三步:text 内若含逗号/顿号/空格则拆成多条,几个 tag 就几个 view
  139. return withText.flatMap((t) => {
  140. const parts = (t.text || '').split(/[,,、\s]+/).map((s) => s.trim()).filter(Boolean);
  141. return parts.map((p) => ({ text: p, type: t.type }));
  142. });
  143. });
  144. const handleFoodClick = () => {
  145. const id = props.food?.id ?? props.food?.cuisineId ?? '';
  146. if (!id) {
  147. uni.showToast({ title: '暂无菜品信息', icon: 'none' });
  148. return;
  149. }
  150. const tableId = props.tableId != null && props.tableId !== '' ? String(props.tableId) : '';
  151. const qty = props.food?.quantity ?? 0;
  152. const q = [`cuisineId=${encodeURIComponent(id)}`];
  153. if (tableId) q.push(`tableId=${encodeURIComponent(tableId)}`);
  154. q.push(`quantity=${encodeURIComponent(String(qty))}`);
  155. go(`/pages/foodDetail/index?${q.join('&')}`);
  156. };
  157. const handleIncrease = () => {
  158. if (props.food.quantity >= 99) return;
  159. emit('increase', props.food);
  160. };
  161. const handleDecrease = () => {
  162. if (props.food.quantity > 0) {
  163. emit('decrease', props.food);
  164. }
  165. };
  166. // 获取价格整数部分
  167. const getPriceMain = (price) => {
  168. if (!price) return '0';
  169. const priceStr = String(price);
  170. const dotIndex = priceStr.indexOf('.');
  171. return dotIndex > -1 ? priceStr.substring(0, dotIndex) : priceStr;
  172. };
  173. // 获取价格小数部分
  174. const getPriceDecimal = (price) => {
  175. if (!price) return '';
  176. const priceStr = String(price);
  177. const dotIndex = priceStr.indexOf('.');
  178. return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '';
  179. };
  180. </script>
  181. <style lang="scss" scoped>
  182. .food-card {
  183. padding: 20rpx;
  184. background-color: #fff;
  185. margin-bottom: 20rpx;
  186. border-radius: 8rpx;
  187. box-sizing: border-box;
  188. }
  189. .food-item {
  190. display: flex;
  191. }
  192. .food-image {
  193. width: 180rpx;
  194. height: 180rpx;
  195. border-radius: 8rpx;
  196. flex-shrink: 0;
  197. background-color: #f5f5f5;
  198. }
  199. .food-info {
  200. flex: 1;
  201. margin-left: 20rpx;
  202. display: flex;
  203. flex-direction: column;
  204. position: relative;
  205. }
  206. .food-header {
  207. display: flex;
  208. align-items: center;
  209. justify-content: space-between;
  210. margin-bottom: 10rpx;
  211. }
  212. .food-title {
  213. font-weight: bold;
  214. font-size: 32rpx;
  215. color: #151515;
  216. flex: 1;
  217. margin-right: 20rpx;
  218. }
  219. .food-price {
  220. display: flex;
  221. align-items: baseline;
  222. color: #E61F19;
  223. line-height: 1;
  224. flex-shrink: 0;
  225. .price-symbol {
  226. font-size: 20rpx;
  227. font-weight: bold;
  228. }
  229. .price-main {
  230. font-size: 32rpx;
  231. font-weight: bold;
  232. }
  233. .price-decimal {
  234. font-size: 24rpx;
  235. font-weight: bold;
  236. }
  237. }
  238. .food-desc {
  239. font-size: 24rpx;
  240. color: #797979;
  241. margin-bottom: 10rpx;
  242. line-height: 1.5;
  243. }
  244. .food-tags {
  245. display: flex;
  246. gap: 20rpx;
  247. margin-bottom: 10rpx;
  248. }
  249. .food-tag {
  250. padding: 6rpx 16rpx;
  251. border-radius: 4rpx;
  252. font-size: 20rpx;
  253. background: #000;
  254. color: #fff;
  255. &--signature {
  256. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  257. color: #fff;
  258. border-radius: 4rpx;
  259. }
  260. }
  261. .food-footer {
  262. display: flex;
  263. align-items: center;
  264. justify-content: space-between;
  265. margin-top: 10rpx;
  266. }
  267. .food-sales {
  268. font-size: 22rpx;
  269. color: #999;
  270. display: flex;
  271. align-items: center;
  272. gap: 4rpx;
  273. .star-icon {
  274. width: 26rpx;
  275. height: 26rpx;
  276. }
  277. .sales-text {
  278. font-size: 22rpx;
  279. color: #999;
  280. }
  281. }
  282. .food-actions {
  283. display: flex;
  284. align-items: center;
  285. justify-content: space-between;
  286. width: 214rpx;
  287. height: 58rpx;
  288. background: #F8F8F8;
  289. border-radius: 56rpx;
  290. box-sizing: border-box;
  291. padding: 0 3rpx;
  292. }
  293. .action-btn {
  294. width: 52rpx;
  295. height: 52rpx;
  296. border-radius: 50%;
  297. display: flex;
  298. align-items: center;
  299. justify-content: center;
  300. font-size: 32rpx;
  301. font-weight: 600;
  302. transition: all 0.3s;
  303. background-color: #fff;
  304. .action-icon {
  305. width: 24rpx;
  306. height: 24rpx;
  307. }
  308. }
  309. .quantity {
  310. font-size: 28rpx;
  311. color: #333;
  312. min-width: 40rpx;
  313. text-align: center;
  314. }
  315. </style>