index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <template>
  2. <!-- 点餐页 -->
  3. <view class="content">
  4. <view class="top-info">桌号:A08 &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.name }}
  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. const currentDiners = ref(uni.getStorageSync('currentDiners'));
  47. const currentCategoryIndex = ref(0);
  48. const couponModalOpen = ref(false);
  49. const cartModalOpen = ref(false);
  50. const selectCouponModalOpen = ref(false);
  51. const discountAmount = ref(12); // 优惠金额,示例数据
  52. const selectedCouponId = ref(null); // 选中的优惠券ID
  53. // 分类列表
  54. const categories = ref([
  55. { name: '推荐菜品', id: 'recommend' },
  56. { name: '招牌川菜', id: 'signature' },
  57. { name: '凉菜', id: 'cold' },
  58. { name: '热菜', id: 'hot' },
  59. { name: '汤品', id: 'soup' },
  60. { name: '主食', id: 'staple' },
  61. { name: '饮品', id: 'drink' }
  62. ]);
  63. // 菜品列表数据(示例数据)
  64. const foodList = ref([
  65. {
  66. id: 1,
  67. name: '炭烤牛排',
  68. price: 26,
  69. desc: '正宗川味, 麻辣鲜香, 豆腐滑嫩',
  70. image: '/static/demo.png',
  71. tags: [
  72. { text: '招牌', type: 'signature' },
  73. { text: '中辣', type: 'spicy' }
  74. ],
  75. monthlySales: 160,
  76. quantity: 0,
  77. categoryId: 'recommend'
  78. },
  79. {
  80. id: 2,
  81. name: '炭烤牛排',
  82. price: 26,
  83. desc: '正宗川味, 麻辣鲜香, 豆腐滑嫩',
  84. image: '/static/demo.png',
  85. tags: [
  86. { text: '招牌', type: 'signature' },
  87. { text: '中辣', type: 'spicy' }
  88. ],
  89. monthlySales: 160,
  90. quantity: 99,
  91. categoryId: 'recommend'
  92. },
  93. {
  94. id: 3,
  95. name: '炭烤牛排',
  96. price: 26,
  97. desc: '正宗川味, 麻辣鲜香, 豆腐滑嫩',
  98. image: '/static/demo.png',
  99. tags: [
  100. { text: '招牌', type: 'signature' },
  101. { text: '中辣', type: 'spicy' }
  102. ],
  103. monthlySales: 160,
  104. quantity: 23,
  105. categoryId: 'cold'
  106. },
  107. {
  108. id: 4,
  109. name: '炭烤牛排',
  110. price: 26,
  111. desc: '正宗川味, 麻辣鲜香, 豆腐滑嫩',
  112. image: '/static/demo.png',
  113. tags: [
  114. { text: '招牌', type: 'signature' },
  115. { text: '中辣', type: 'spicy' }
  116. ],
  117. monthlySales: 160,
  118. quantity: 0,
  119. categoryId: 'cold'
  120. }
  121. ]);
  122. // 当前分类的菜品列表
  123. const currentFoodList = computed(() => {
  124. const categoryId = categories.value[currentCategoryIndex.value].id;
  125. return foodList.value.filter(food => food.categoryId === categoryId);
  126. });
  127. // 购物车列表(只包含数量大于0的菜品)
  128. const cartList = computed(() => {
  129. return foodList.value.filter(food => food.quantity > 0);
  130. });
  131. // 优惠券列表(示例数据)
  132. const couponList = ref([
  133. {
  134. id: 1,
  135. amount: 38,
  136. minAmount: 158,
  137. name: '优惠券名称',
  138. expireDate: '2024/07/28',
  139. isReceived: false
  140. },
  141. {
  142. id: 2,
  143. amount: 8,
  144. minAmount: 158,
  145. name: '优惠券名称',
  146. expireDate: '2024/07/28',
  147. isReceived: false
  148. },
  149. {
  150. id: 3,
  151. amount: 682,
  152. minAmount: 1580,
  153. name: '优惠券名称',
  154. expireDate: '2024/07/28',
  155. isReceived: true
  156. },
  157. {
  158. id: 4,
  159. amount: 1038,
  160. minAmount: 1580,
  161. name: '优惠券名称',
  162. expireDate: '2024/07/28',
  163. isReceived: true
  164. }
  165. ]);
  166. // 可用的优惠券列表(已领取的优惠券)
  167. const availableCoupons = computed(() => {
  168. return couponList.value.filter(coupon => coupon.isReceived);
  169. });
  170. // 选择分类
  171. const selectCategory = (index) => {
  172. currentCategoryIndex.value = index;
  173. };
  174. // 增加数量
  175. const handleIncrease = (food) => {
  176. if (food) {
  177. food.quantity = (food.quantity || 0) + 1;
  178. }
  179. };
  180. // 减少数量
  181. const handleDecrease = (food) => {
  182. if (food && food.quantity > 0) {
  183. food.quantity -= 1;
  184. }
  185. };
  186. // 优惠券点击(领取优惠券弹窗)
  187. const handleCouponClick = () => {
  188. // 先关闭其他弹窗
  189. if (cartModalOpen.value) {
  190. cartModalOpen.value = false;
  191. }
  192. if (selectCouponModalOpen.value) {
  193. selectCouponModalOpen.value = false;
  194. }
  195. couponModalOpen.value = true;
  196. };
  197. // 选择优惠券点击(从购物车弹窗中点击)
  198. const handleSelectCouponClick = () => {
  199. // 先关闭其他弹窗
  200. if (cartModalOpen.value) {
  201. cartModalOpen.value = false;
  202. }
  203. if (couponModalOpen.value) {
  204. couponModalOpen.value = false;
  205. }
  206. // 打开选择优惠券弹窗
  207. selectCouponModalOpen.value = true;
  208. };
  209. // 处理优惠券选择
  210. const handleCouponSelect = ({ coupon, index, selectedId }) => {
  211. selectedCouponId.value = selectedId;
  212. // 根据选中的优惠券更新优惠金额
  213. if (selectedId) {
  214. discountAmount.value = coupon.amount;
  215. } else {
  216. discountAmount.value = 0;
  217. }
  218. // 选择后关闭选择优惠券弹窗,打开购物车弹窗
  219. selectCouponModalOpen.value = false;
  220. // 延迟打开购物车弹窗,确保选择优惠券弹窗完全关闭
  221. setTimeout(() => {
  222. cartModalOpen.value = true;
  223. }, 100);
  224. };
  225. // 选择优惠券弹窗关闭
  226. const handleSelectCouponClose = () => {
  227. selectCouponModalOpen.value = false;
  228. };
  229. // 优惠券领取
  230. const handleCouponReceive = ({ coupon, index }) => {
  231. console.log('领取优惠券:', coupon);
  232. // 更新优惠券状态
  233. couponList.value[index].isReceived = true;
  234. uni.showToast({
  235. title: '领取成功',
  236. icon: 'success'
  237. });
  238. // TODO: 调用接口领取优惠券
  239. };
  240. // 优惠券弹窗关闭
  241. const handleCouponClose = () => {
  242. couponModalOpen.value = false;
  243. };
  244. // 购物车点击
  245. const handleCartClick = () => {
  246. if (cartList.value.length === 0) {
  247. uni.showToast({
  248. title: '购物车为空',
  249. icon: 'none'
  250. });
  251. return;
  252. }
  253. // 先关闭其他弹窗
  254. if (couponModalOpen.value) {
  255. couponModalOpen.value = false;
  256. }
  257. if (selectCouponModalOpen.value) {
  258. selectCouponModalOpen.value = false;
  259. }
  260. cartModalOpen.value = true;
  261. };
  262. // 购物车弹窗关闭
  263. const handleCartClose = () => {
  264. cartModalOpen.value = false;
  265. };
  266. // 清空购物车
  267. const handleCartClear = () => {
  268. foodList.value.forEach(food => {
  269. food.quantity = 0;
  270. });
  271. cartModalOpen.value = false;
  272. uni.showToast({
  273. title: '已清空购物车',
  274. icon: 'success'
  275. });
  276. };
  277. // 下单点击
  278. const handleOrderClick = (data) => {
  279. go('/pages/placeOrder/index');
  280. };
  281. onLoad((e) => {
  282. uni.setNavigationBarTitle({
  283. title: '店铺名称'
  284. });
  285. });
  286. </script>
  287. <style lang="scss" scoped>
  288. .content {
  289. display: flex;
  290. flex-direction: column;
  291. height: 100vh;
  292. background-color: #f7f9fa;
  293. padding-bottom: 100rpx;
  294. box-sizing: border-box;
  295. }
  296. .top-info {
  297. font-size: 28rpx;
  298. color: #888888;
  299. text-align: center;
  300. width: 100%;
  301. padding: 20rpx 0;
  302. background-color: #fff;
  303. }
  304. .search-input {
  305. width: 90%;
  306. margin-left: 5%;
  307. height: 80rpx;
  308. background-color: #fff;
  309. border-radius: 40rpx;
  310. padding: 0 20rpx;
  311. margin-top: 20rpx;
  312. box-sizing: border-box;
  313. font-size: 28rpx;
  314. text-align: center;
  315. }
  316. .content-box {
  317. display: flex;
  318. flex: 1;
  319. overflow: hidden;
  320. margin-top: 20rpx;
  321. }
  322. // 左侧分类列表
  323. .category-list {
  324. width: 180rpx;
  325. background-color: #fff;
  326. height: 100%;
  327. }
  328. .category-item {
  329. height: 100rpx;
  330. display: flex;
  331. align-items: center;
  332. justify-content: center;
  333. font-size: 28rpx;
  334. color: #999;
  335. background-color: #fff;
  336. transition: all 0.3s;
  337. &.active {
  338. background-color: #fff4e6;
  339. color: #333;
  340. font-weight: 600;
  341. }
  342. }
  343. // 右侧菜品列表
  344. .food-list {
  345. flex: 1;
  346. // background-color: #fff;
  347. // padding: 0 20rpx;
  348. margin: 0 20rpx;
  349. }
  350. </style>