FoodCard.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.description }}</view>
  15. <view class="food-tags" v-if="normalizedTags.length">
  16. <view v-for="(tag, tagIndex) in normalizedTags" :key="tagIndex" class="food-tag" :class="tag.type">
  17. {{ 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 || isTableware }" @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" :class="{ disabled: isTableware }" @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 (isTableware.value) {
  121. uni.showToast({ title: '不允许更改餐具数量', icon: 'none' });
  122. return;
  123. }
  124. if (props.food.quantity >= 99) return;
  125. emit('increase', props.food);
  126. };
  127. const handleDecrease = () => {
  128. if (isTableware.value) {
  129. uni.showToast({ title: '不允许更改餐具数量', icon: 'none' });
  130. return;
  131. }
  132. if (props.food.quantity > 0) {
  133. emit('decrease', props.food);
  134. }
  135. };
  136. // 获取价格整数部分
  137. const getPriceMain = (price) => {
  138. if (!price) return '0';
  139. const priceStr = String(price);
  140. const dotIndex = priceStr.indexOf('.');
  141. return dotIndex > -1 ? priceStr.substring(0, dotIndex) : priceStr;
  142. };
  143. // 获取价格小数部分
  144. const getPriceDecimal = (price) => {
  145. if (!price) return '';
  146. const priceStr = String(price);
  147. const dotIndex = priceStr.indexOf('.');
  148. return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '';
  149. };
  150. </script>
  151. <style lang="scss" scoped>
  152. .food-card {
  153. padding: 20rpx;
  154. background-color: #fff;
  155. margin-bottom: 20rpx;
  156. border-radius: 8rpx;
  157. box-sizing: border-box;
  158. }
  159. .food-item {
  160. display: flex;
  161. }
  162. .food-image {
  163. width: 180rpx;
  164. height: 180rpx;
  165. border-radius: 8rpx;
  166. flex-shrink: 0;
  167. background-color: #f5f5f5;
  168. }
  169. .food-info {
  170. flex: 1;
  171. margin-left: 20rpx;
  172. display: flex;
  173. flex-direction: column;
  174. position: relative;
  175. }
  176. .food-header {
  177. display: flex;
  178. align-items: center;
  179. justify-content: space-between;
  180. margin-bottom: 10rpx;
  181. }
  182. .food-title {
  183. font-weight: bold;
  184. font-size: 32rpx;
  185. color: #151515;
  186. flex: 1;
  187. margin-right: 20rpx;
  188. }
  189. .food-price {
  190. display: flex;
  191. align-items: baseline;
  192. color: #E61F19;
  193. line-height: 1;
  194. flex-shrink: 0;
  195. .price-symbol {
  196. font-size: 20rpx;
  197. font-weight: bold;
  198. }
  199. .price-main {
  200. font-size: 32rpx;
  201. font-weight: bold;
  202. }
  203. .price-decimal {
  204. font-size: 24rpx;
  205. font-weight: bold;
  206. }
  207. }
  208. .food-desc {
  209. font-size: 24rpx;
  210. color: #797979;
  211. margin-bottom: 10rpx;
  212. line-height: 1.5;
  213. }
  214. .food-tags {
  215. display: flex;
  216. gap: 10rpx;
  217. margin-bottom: 10rpx;
  218. }
  219. .food-tag {
  220. padding: 4rpx 12rpx;
  221. border-radius: 4rpx;
  222. font-size: 20rpx;
  223. color: #333;
  224. background-color: #f0f0f0;
  225. &.signature {
  226. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  227. color: #fff;
  228. }
  229. &.spicy {
  230. background: #2E2E2E;
  231. color: #fff;
  232. }
  233. }
  234. .food-footer {
  235. display: flex;
  236. align-items: center;
  237. justify-content: space-between;
  238. margin-top: 10rpx;
  239. }
  240. .food-sales {
  241. font-size: 22rpx;
  242. color: #999;
  243. display: flex;
  244. align-items: center;
  245. gap: 4rpx;
  246. .star-icon {
  247. width: 26rpx;
  248. height: 26rpx;
  249. }
  250. .sales-text {
  251. font-size: 22rpx;
  252. color: #999;
  253. }
  254. }
  255. .food-actions {
  256. display: flex;
  257. align-items: center;
  258. justify-content: space-between;
  259. width: 214rpx;
  260. height: 58rpx;
  261. background: #F8F8F8;
  262. border-radius: 56rpx;
  263. box-sizing: border-box;
  264. padding: 0 3rpx;
  265. }
  266. .action-btn {
  267. width: 52rpx;
  268. height: 52rpx;
  269. border-radius: 50%;
  270. display: flex;
  271. align-items: center;
  272. justify-content: center;
  273. font-size: 32rpx;
  274. font-weight: 600;
  275. transition: all 0.3s;
  276. background-color: #fff;
  277. .action-icon {
  278. width: 24rpx;
  279. height: 24rpx;
  280. }
  281. }
  282. .quantity {
  283. font-size: 28rpx;
  284. color: #333;
  285. min-width: 40rpx;
  286. text-align: center;
  287. }
  288. </style>