FoodCard.vue 9.0 KB

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