FoodCard.vue 8.7 KB

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