FoodCard.vue 9.2 KB

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