index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <template>
  2. <!-- 点餐页 -->
  3. <view class="content">
  4. <view class="top-info">桌号:{{ tableId || '—' }} &nbsp;&nbsp;就餐人数:{{ currentDiners }}人</view>
  5. <!-- 搜索 -->
  6. <input type="text" placeholder="请输入菜品名称" class="search-input" />
  7. <!-- 内容 -->
  8. <view class="content-box">
  9. <!-- 左侧分类列表 -->
  10. <scroll-view class="category-list" scroll-y>
  11. <view v-for="(category, index) in categories" :key="index" class="category-item"
  12. :class="{ active: currentCategoryIndex === index }" @click="selectCategory(index)">
  13. {{ category.categoryName }}
  14. </view>
  15. </scroll-view>
  16. <!-- 右侧菜品列表 -->
  17. <scroll-view class="food-list" scroll-y>
  18. <FoodCard v-for="(food, index) in currentFoodList" :key="food.id || index" :food="food"
  19. @increase="handleIncrease" @decrease="handleDecrease"/>
  20. </scroll-view>
  21. </view>
  22. <!-- 底部下单 -->
  23. <BottomActionBar :cart-list="cartList" @coupon-click="handleCouponClick" @cart-click="handleCartClick"
  24. @order-click="handleOrderClick" />
  25. <!-- 领取优惠券弹窗 -->
  26. <CouponModal v-model:open="couponModalOpen" :coupon-list="couponList" @receive="handleCouponReceive"
  27. @close="handleCouponClose" />
  28. <!-- 购物车弹窗 -->
  29. <CartModal v-model:open="cartModalOpen" :cart-list="cartList" :discount-amount="discountAmount"
  30. @increase="handleIncrease" @decrease="handleDecrease" @clear="handleCartClear"
  31. @coupon-click="handleSelectCouponClick" @order-click="handleOrderClick" @close="handleCartClose" />
  32. <!-- 选择优惠券弹窗 -->
  33. <SelectCouponModal v-model:open="selectCouponModalOpen" :coupon-list="availableCoupons"
  34. :selected-coupon-id="selectedCouponId" @select="handleCouponSelect" @close="handleSelectCouponClose" />
  35. </view>
  36. </template>
  37. <script setup>
  38. import { onLoad } from "@dcloudio/uni-app";
  39. import { ref, computed } from "vue";
  40. import FoodCard from "./components/FoodCard.vue";
  41. import BottomActionBar from "./components/BottomActionBar.vue";
  42. import CouponModal from "./components/CouponModal.vue";
  43. import CartModal from "./components/CartModal.vue";
  44. import SelectCouponModal from "./components/SelectCouponModal.vue";
  45. import { go } from "@/utils/utils.js";
  46. import { DiningOrderFood, GetStoreCategories, GetStoreCuisines } from "@/api/dining.js";
  47. const tableId = ref(''); // 桌号,来自上一页 url 参数 tableid
  48. const currentDiners = ref(uni.getStorageSync('currentDiners') || '');
  49. const currentCategoryIndex = ref(0);
  50. const couponModalOpen = ref(false);
  51. const cartModalOpen = ref(false);
  52. const selectCouponModalOpen = ref(false);
  53. const discountAmount = ref(12); // 优惠金额,示例数据
  54. const selectedCouponId = ref(null); // 选中的优惠券ID
  55. // 分类列表(由接口 /store/info/categories 返回后赋值)
  56. const categories = ref([]);
  57. // 菜品列表(由接口 /store/info/cuisines 按分类拉取并合并,每项含 quantity、categoryId)
  58. const foodList = ref([]);
  59. // 当前分类的菜品列表(按当前选中的分类 id 过滤)
  60. const currentFoodList = computed(() => {
  61. const cat = categories.value[currentCategoryIndex.value];
  62. if (!cat) return [];
  63. const categoryId = cat.id ?? cat.categoryId;
  64. return foodList.value.filter((food) => String(food?.categoryId ?? '') === String(categoryId ?? ''));
  65. });
  66. // 购物车列表:只包含数量>0 的菜品,并按菜品 id 去重(同一菜品只算一条,避免金额叠加)
  67. const cartList = computed(() => {
  68. const withQty = foodList.value.filter((food) => (food.quantity || 0) > 0);
  69. const seen = new Set();
  70. return withQty.filter((f) => {
  71. const id = String(f?.id ?? '');
  72. if (seen.has(id)) return false;
  73. seen.add(id);
  74. return true;
  75. });
  76. });
  77. // 优惠券列表(示例数据)
  78. const couponList = ref([
  79. {
  80. id: 1,
  81. amount: 38,
  82. minAmount: 158,
  83. name: '优惠券名称',
  84. expireDate: '2024/07/28',
  85. isReceived: false
  86. },
  87. {
  88. id: 2,
  89. amount: 8,
  90. minAmount: 158,
  91. name: '优惠券名称',
  92. expireDate: '2024/07/28',
  93. isReceived: false
  94. },
  95. {
  96. id: 3,
  97. amount: 682,
  98. minAmount: 1580,
  99. name: '优惠券名称',
  100. expireDate: '2024/07/28',
  101. isReceived: true
  102. },
  103. {
  104. id: 4,
  105. amount: 1038,
  106. minAmount: 1580,
  107. name: '优惠券名称',
  108. expireDate: '2024/07/28',
  109. isReceived: true
  110. }
  111. ]);
  112. // 可用的优惠券列表(已领取的优惠券)
  113. const availableCoupons = computed(() => {
  114. return couponList.value.filter(coupon => coupon.isReceived);
  115. });
  116. // 分类 id 统一转字符串,避免 number/string 比较导致误判
  117. const sameCategory = (a, b) => String(a ?? '') === String(b ?? '');
  118. // 根据分类 id 拉取菜品并合并到 foodList,切换分类时保留其他分类已选菜品及本分类已选数量
  119. const fetchCuisinesByCategoryId = async (categoryId) => {
  120. if (!categoryId) return;
  121. try {
  122. const res = await GetStoreCuisines({ categoryId });
  123. const list = res?.list ?? res?.data ?? (Array.isArray(res) ? res : []);
  124. const normalized = (Array.isArray(list) ? list : []).map(item => ({
  125. ...item,
  126. quantity: item.quantity ?? 0,
  127. categoryId: item.categoryId ?? categoryId
  128. }));
  129. // 其他分类的菜品原样保留(含已选数量)
  130. const rest = foodList.value.filter(f => !sameCategory(f.categoryId, categoryId));
  131. // 本分类:按菜品 id 从整份列表里取已选数量,id 一致则自动带上数量
  132. const merged = normalized.map((newItem) => {
  133. const existing = foodList.value.find(
  134. (e) => String(e?.id ?? '') === String(newItem?.id ?? '')
  135. );
  136. const quantity = existing ? existing.quantity : (newItem.quantity ?? 0);
  137. return { ...newItem, quantity };
  138. });
  139. foodList.value = [...rest, ...merged];
  140. } catch (err) {
  141. console.error('获取菜品失败:', err);
  142. }
  143. };
  144. // 选择分类:切换高亮并拉取该分类菜品
  145. const selectCategory = async (index) => {
  146. currentCategoryIndex.value = index;
  147. const cat = categories.value[index];
  148. if (!cat) return;
  149. const categoryId = cat.id ?? cat.categoryId;
  150. await fetchCuisinesByCategoryId(categoryId);
  151. };
  152. // 更新菜品数量:菜品 id 一致则全部同步为同一数量,触发响应式使底部金额重新计算
  153. const updateFoodQuantity = (food, delta) => {
  154. if (!food) return;
  155. const id = food.id;
  156. const nextQty = Math.max(0, (food.quantity || 0) + delta);
  157. const sameId = (item) =>
  158. String(item?.id ?? '') === String(id ?? '');
  159. foodList.value = foodList.value.map((item) =>
  160. sameId(item) ? { ...item, quantity: nextQty } : item
  161. );
  162. };
  163. // 增加数量
  164. const handleIncrease = (food) => {
  165. updateFoodQuantity(food, 1);
  166. };
  167. // 减少数量
  168. const handleDecrease = (food) => {
  169. updateFoodQuantity(food, -1);
  170. };
  171. // 优惠券点击(领取优惠券弹窗)
  172. const handleCouponClick = () => {
  173. // 先关闭其他弹窗
  174. if (cartModalOpen.value) {
  175. cartModalOpen.value = false;
  176. }
  177. if (selectCouponModalOpen.value) {
  178. selectCouponModalOpen.value = false;
  179. }
  180. couponModalOpen.value = true;
  181. };
  182. // 选择优惠券点击(从购物车弹窗中点击)
  183. const handleSelectCouponClick = () => {
  184. // 先关闭其他弹窗
  185. if (cartModalOpen.value) {
  186. cartModalOpen.value = false;
  187. }
  188. if (couponModalOpen.value) {
  189. couponModalOpen.value = false;
  190. }
  191. // 打开选择优惠券弹窗
  192. selectCouponModalOpen.value = true;
  193. };
  194. // 处理优惠券选择
  195. const handleCouponSelect = ({ coupon, index, selectedId }) => {
  196. selectedCouponId.value = selectedId;
  197. // 根据选中的优惠券更新优惠金额
  198. if (selectedId) {
  199. discountAmount.value = coupon.amount;
  200. } else {
  201. discountAmount.value = 0;
  202. }
  203. // 选择后关闭选择优惠券弹窗,打开购物车弹窗
  204. selectCouponModalOpen.value = false;
  205. // 延迟打开购物车弹窗,确保选择优惠券弹窗完全关闭
  206. setTimeout(() => {
  207. cartModalOpen.value = true;
  208. }, 100);
  209. };
  210. // 选择优惠券弹窗关闭
  211. const handleSelectCouponClose = () => {
  212. selectCouponModalOpen.value = false;
  213. };
  214. // 优惠券领取
  215. const handleCouponReceive = ({ coupon, index }) => {
  216. console.log('领取优惠券:', coupon);
  217. // 更新优惠券状态
  218. couponList.value[index].isReceived = true;
  219. uni.showToast({
  220. title: '领取成功',
  221. icon: 'success'
  222. });
  223. // TODO: 调用接口领取优惠券
  224. };
  225. // 优惠券弹窗关闭
  226. const handleCouponClose = () => {
  227. couponModalOpen.value = false;
  228. };
  229. // 购物车点击
  230. const handleCartClick = () => {
  231. if (cartList.value.length === 0) {
  232. uni.showToast({
  233. title: '购物车为空',
  234. icon: 'none'
  235. });
  236. return;
  237. }
  238. // 先关闭其他弹窗
  239. if (couponModalOpen.value) {
  240. couponModalOpen.value = false;
  241. }
  242. if (selectCouponModalOpen.value) {
  243. selectCouponModalOpen.value = false;
  244. }
  245. cartModalOpen.value = true;
  246. };
  247. // 购物车弹窗关闭
  248. const handleCartClose = () => {
  249. cartModalOpen.value = false;
  250. };
  251. // 清空购物车
  252. const handleCartClear = () => {
  253. foodList.value.forEach(food => {
  254. food.quantity = 0;
  255. });
  256. cartModalOpen.value = false;
  257. uni.showToast({
  258. title: '已清空购物车',
  259. icon: 'success'
  260. });
  261. };
  262. // 下单点击
  263. const handleOrderClick = (data) => {
  264. go('/pages/placeOrder/index');
  265. };
  266. onLoad(async (options) => {
  267. // 获取上一页(选择就餐人数页)传来的桌号和就餐人数
  268. const tableid = options.tableid || '';
  269. const diners = options.diners || '';
  270. tableId.value = tableid;
  271. currentDiners.value = diners;
  272. console.log('点餐页接收参数 - 桌号(tableid):', tableid, '就餐人数(diners):', diners);
  273. // 调用点餐页接口,入参 dinerCount、tableId
  274. if (tableid || diners) {
  275. try {
  276. const res = await DiningOrderFood({
  277. tableId: tableid,
  278. dinerCount: diners
  279. });
  280. console.log('点餐页接口返回:', res);
  281. // 成功后调接口获取菜品种类(storeId 取自点餐页接口返回,若无则需从别处获取)
  282. const storeId = res?.storeId ?? res?.data?.storeId ?? '';
  283. if (storeId) {
  284. try {
  285. const categoriesRes = await GetStoreCategories({ storeId });
  286. const list = categoriesRes?.list ?? categoriesRes?.data ?? categoriesRes;
  287. if (Array.isArray(list) && list.length) {
  288. categories.value = list;
  289. // 默认用第一项的分类 id 拉取菜品
  290. const firstCat = list[0];
  291. const firstCategoryId = firstCat.id ?? firstCat.categoryId;
  292. if (firstCategoryId) {
  293. const cuisinesRes = await GetStoreCuisines({ categoryId: firstCategoryId });
  294. const cuisinesList = cuisinesRes?.list ?? cuisinesRes?.data ?? (Array.isArray(cuisinesRes) ? cuisinesRes : []);
  295. foodList.value = (Array.isArray(cuisinesList) ? cuisinesList : []).map(item => ({
  296. ...item,
  297. quantity: item.quantity ?? 0,
  298. categoryId: item.categoryId ?? firstCategoryId
  299. }));
  300. console.log('默认分类菜品:', cuisinesRes);
  301. }
  302. }
  303. console.log('菜品种类:', categoriesRes);
  304. } catch (err) {
  305. console.error('获取菜品种类失败:', err);
  306. }
  307. }
  308. } catch (e) {
  309. console.error('点餐页接口失败:', e);
  310. }
  311. }
  312. });
  313. </script>
  314. <style lang="scss" scoped>
  315. .content {
  316. display: flex;
  317. flex-direction: column;
  318. height: 100vh;
  319. background-color: #f7f9fa;
  320. padding-bottom: 100rpx;
  321. box-sizing: border-box;
  322. }
  323. .top-info {
  324. font-size: 28rpx;
  325. color: #888888;
  326. text-align: center;
  327. width: 100%;
  328. padding: 20rpx 0;
  329. background-color: #fff;
  330. }
  331. .search-input {
  332. width: 90%;
  333. margin-left: 5%;
  334. height: 80rpx;
  335. background-color: #fff;
  336. border-radius: 40rpx;
  337. padding: 0 20rpx;
  338. margin-top: 20rpx;
  339. box-sizing: border-box;
  340. font-size: 28rpx;
  341. text-align: center;
  342. }
  343. .content-box {
  344. display: flex;
  345. flex: 1;
  346. overflow: hidden;
  347. margin-top: 20rpx;
  348. }
  349. // 左侧分类列表
  350. .category-list {
  351. width: 180rpx;
  352. background-color: #fff;
  353. height: 100%;
  354. }
  355. .category-item {
  356. height: 100rpx;
  357. display: flex;
  358. align-items: center;
  359. justify-content: center;
  360. font-size: 28rpx;
  361. color: #999;
  362. background-color: #fff;
  363. transition: all 0.3s;
  364. &.active {
  365. background-color: #fff4e6;
  366. color: #333;
  367. font-weight: 600;
  368. }
  369. }
  370. // 右侧菜品列表
  371. .food-list {
  372. flex: 1;
  373. // background-color: #fff;
  374. // padding: 0 20rpx;
  375. margin: 0 20rpx;
  376. }
  377. </style>