index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <!-- 首页 -->
  3. <view class="content">
  4. <!-- 顶部 -->
  5. <image :src="getFileUrl('img/icon/zptj.png')" mode="aspectFill" class="top-img"></image>
  6. <!-- 内容 -->
  7. <view class="content-box">
  8. <view class="shop-info">
  9. <image :src="getFileUrl('img/icon/shop.png')" mode="aspectFill" class="shop-img"></image>
  10. <view class="shop-name">{{ storeInfo.storeName || '店铺名称' }}</view>
  11. </view>
  12. <!-- 登录卡片 -->
  13. <view class='login-card' v-if="!userStore.getToken">
  14. <view class="login-card-left">
  15. <view class="welcome-title">欢迎来到{{ storeInfo.storeName || 'xxx' }}店铺</view>
  16. <view class="welcome-desc">注册登录即可体验更好的服务</view>
  17. <view class="login-btn" hover-class="hover-active" @click="showLoginModal = true">立即登录</view>
  18. </view>
  19. <view class="login-card-right">
  20. <image :src="getFileUrl('img/icon/welcom.png')" mode="widthFix" class="welcome-img"></image>
  21. </view>
  22. </view>
  23. <view class="banner-box">
  24. <image :src="getFileUrl('img/icon/zptj.png')" mode="widthFix" class="banner-img"></image>
  25. </view>
  26. <!-- 列表 -->
  27. <view class="list-box">
  28. <view class="food-card" v-for="(item, index) in foodList" :key="item.id || index" hover-class="hover-active" @click="handleFoodClick(item)">
  29. <!-- 菜品图片 -->
  30. <image :src="getFileUrl(item.image)" mode="aspectFill" class="food-image"></image>
  31. <!-- 菜品信息 -->
  32. <view class="food-info">
  33. <!-- 标题和月售 -->
  34. <view class="food-header">
  35. <view class="food-left">
  36. <view class="food-name">{{ item.name }}</view>
  37. <view class="food-sales">
  38. <image :src="getFileUrl('img/icon/star.png')" mode="aspectFill" class="star-icon"></image>
  39. <text class="sales-text">月售:{{ item.sales }}</text>
  40. </view>
  41. </view>
  42. <view class="food-right">
  43. <view class="food-price">
  44. <text class="price-symbol">¥</text>
  45. <text class="price-main">{{ getPriceMain(item.price) }}</text>
  46. <text class="price-decimal">.{{ getPriceDecimal(item.price) }}</text>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 标签 -->
  51. <view class="food-tags">
  52. <view v-for="(tag, tagIndex) in item.tags" :key="tagIndex" class="food-tag" :class="tag.type">
  53. {{ tag.text }}
  54. </view>
  55. </view>
  56. <!-- 描述:绑定 dishReview -->
  57. <view class="food-desc" v-if="item.dishReview">{{ item.dishReview }}</view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. <!-- 登录弹窗组件 -->
  63. <view class="login-modal-wrapper">
  64. <LoginModal v-model:open="showLoginModal" @success="handleLoginSuccess" @cancel="handleLoginCancel" />
  65. </view>
  66. <!-- 底部导航 -->
  67. <TabBar />
  68. </view>
  69. </template>
  70. <script setup>
  71. import { ref } from "vue";
  72. import { onLoad } from "@dcloudio/uni-app";
  73. import TabBar from "@/components/TabBar.vue";
  74. import LoginModal from "@/pages/components/LoginModal.vue";
  75. import { go } from "@/utils/utils.js";
  76. import { getFileUrl } from '@/utils/file.js';
  77. import { useUserStore } from '@/store/user.js';
  78. import { TOKEN } from '@/settings/enums.js';
  79. import { GetStoreDetail } from '@/api/dining.js';
  80. const userStore = useUserStore();
  81. const showLoginModal = ref(false);
  82. /** 门店信息(接口返回的 storeInfo,绑定 storeName 等) */
  83. const storeInfo = ref({});
  84. // 首页菜品列表(绑定接口 homepageCuisines,兼容多种字段名)
  85. const foodList = ref([]);
  86. function normalizeHomeCuisine(item) {
  87. const tags = item?.tags ?? [];
  88. const tagArr = Array.isArray(tags) ? tags : [];
  89. const images = item?.images;
  90. const imageUrl = Array.isArray(images) ? images[0] : images;
  91. return {
  92. id: item?.id ?? item?.cuisineId ?? '',
  93. image: imageUrl ?? item?.cuisineImage ?? item?.image ?? 'img/icon/shop.png',
  94. name: item?.cuisineName ?? item?.name ?? '',
  95. sales: item?.monthlySales ?? item?.sales ?? 0,
  96. tags: tagArr.map((t) => (typeof t === 'string' ? { text: t, type: 'normal' } : { text: t?.text ?? t?.tagName ?? '', type: t?.type ?? 'normal' })),
  97. description: item?.description ?? item?.desc ?? '',
  98. dishReview: item?.dishReview ?? item?.description ?? item?.desc ?? '',
  99. price: item?.totalPrice ?? item?.unitPrice ?? item?.price ?? ''
  100. };
  101. }
  102. // 格式化为保留两位小数的价格字符串
  103. const formatPriceFixed2 = (price) => {
  104. if (price === '' || price === null || price === undefined) return '0.00';
  105. const num = Number(price);
  106. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  107. };
  108. // 获取价格整数部分
  109. const getPriceMain = (price) => {
  110. const priceStr = formatPriceFixed2(price);
  111. const dotIndex = priceStr.indexOf('.');
  112. return dotIndex > -1 ? priceStr.substring(0, dotIndex) : priceStr;
  113. };
  114. // 获取价格小数部分(两位)
  115. const getPriceDecimal = (price) => {
  116. const priceStr = formatPriceFixed2(price);
  117. const dotIndex = priceStr.indexOf('.');
  118. return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '00';
  119. };
  120. // 登录成功回调:跳转点餐页
  121. const handleLoginSuccess = () => {
  122. const tableid = uni.getStorageSync('currentTableId') || '';
  123. const diners = uni.getStorageSync('currentDiners') || '1';
  124. const q = [];
  125. if (tableid) q.push(`tableid=${encodeURIComponent(tableid)}`);
  126. if (diners) q.push(`diners=${encodeURIComponent(diners)}`);
  127. const url = q.length ? `/pages/orderFood/index?${q.join('&')}` : '/pages/orderFood/index';
  128. go(url);
  129. };
  130. // 取消登录回调
  131. const handleLoginCancel = () => {
  132. console.log('用户取消登录');
  133. };
  134. // 点击菜品:先校验登录,未登录提示请登录;已登录则跳转菜品详情页
  135. const handleFoodClick = (item) => {
  136. const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  137. if (!token) {
  138. uni.showToast({ title: '请登录', icon: 'none' });
  139. return;
  140. }
  141. const cuisineId = item?.id ?? item?.cuisineId ?? '';
  142. if (!cuisineId) return;
  143. const tableId = uni.getStorageSync('currentTableId') ?? '';
  144. const q = [`cuisineId=${encodeURIComponent(String(cuisineId))}`];
  145. if (tableId !== '') q.push(`tableId=${encodeURIComponent(String(tableId))}`);
  146. go(`/pages/foodDetail/index?${q.join('&')}`);
  147. };
  148. onLoad(async (options) => {
  149. const storeId = options?.storeId ?? uni.getStorageSync('currentStoreId') ?? '';
  150. if (storeId) {
  151. try {
  152. const res = await GetStoreDetail(storeId);
  153. const data = res?.data ?? res;
  154. storeInfo.value = data?.storeInfo ?? data ?? {};
  155. const list = data?.homepageCuisines ?? data?.storeInfo?.homepageCuisines ?? [];
  156. foodList.value = (Array.isArray(list) ? list : []).map(normalizeHomeCuisine);
  157. } catch (e) {
  158. console.error('门店详情加载失败:', e);
  159. }
  160. }
  161. });
  162. </script>
  163. <style scoped lang="scss">
  164. .content {
  165. .top-img {
  166. width: 100%;
  167. height: 572rpx;
  168. }
  169. .content-box {
  170. width: 100%;
  171. box-sizing: border-box;
  172. padding: 0 30rpx 140rpx;
  173. .shop-info {
  174. display: flex;
  175. align-items: center;
  176. justify-content: center;
  177. width: 100%;
  178. height: 100rpx;
  179. background-color: #fff;
  180. border-radius: 17rpx;
  181. margin-top: -50rpx;
  182. position: relative;
  183. z-index: 2;
  184. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  185. .shop-img {
  186. width: 45rpx;
  187. height: 38rpx;
  188. margin-right: 20rpx;
  189. }
  190. .shop-name {
  191. font-weight: bold;
  192. font-size: 31rpx;
  193. color: #151515;
  194. }
  195. }
  196. }
  197. .login-card {
  198. width: 100%;
  199. min-height: 240rpx;
  200. background-color: #fff;
  201. border-radius: 17rpx;
  202. margin-top: 20rpx;
  203. box-sizing: border-box;
  204. padding: 30rpx;
  205. display: flex;
  206. align-items: center;
  207. justify-content: space-between;
  208. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  209. .login-card-left {
  210. flex: 1;
  211. display: flex;
  212. flex-direction: column;
  213. justify-content: center;
  214. padding-right: 20rpx;
  215. .welcome-title {
  216. font-size: 36rpx;
  217. font-weight: bold;
  218. color: #151515;
  219. margin-bottom: 16rpx;
  220. line-height: 1.4;
  221. }
  222. .welcome-desc {
  223. font-size: 27rpx;
  224. color: #666666;
  225. margin-bottom: 24rpx;
  226. line-height: 1.4;
  227. }
  228. .login-btn {
  229. width: 160rpx;
  230. height: 53rpx;
  231. background: #2E2E2E;
  232. display: flex;
  233. align-items: center;
  234. justify-content: center;
  235. font-size: 27rpx;
  236. color: #FFFFFF;
  237. font-weight: 500;
  238. cursor: pointer;
  239. transition: opacity 0.3s;
  240. &:active {
  241. opacity: 0.8;
  242. }
  243. }
  244. }
  245. .login-card-right {
  246. flex-shrink: 0;
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. .welcome-img {
  251. width: 200rpx;
  252. height: auto;
  253. }
  254. }
  255. }
  256. // 登录弹窗层级设置(盖住底部 tabbar)
  257. .login-modal-wrapper {
  258. position: relative;
  259. z-index: 99999;
  260. :deep(.uni-popup) {
  261. z-index: 99999 !important;
  262. top: 0 !important;
  263. left: 0 !important;
  264. right: 0 !important;
  265. bottom: 0 !important;
  266. }
  267. :deep(.uni-popup__wrapper) {
  268. z-index: 99999 !important;
  269. }
  270. }
  271. .banner-box {
  272. width: 100%;
  273. height: 100%;
  274. display: flex;
  275. align-items: center;
  276. justify-content: center;
  277. margin-top: 40rpx;
  278. .banner-img {
  279. width: 480rpx;
  280. height: 66rpx;
  281. margin: 0 auto;
  282. }
  283. }
  284. .list-box {
  285. width: 100%;
  286. margin-top: 40rpx;
  287. padding-bottom: 40rpx;
  288. .food-card {
  289. width: 100%;
  290. background-color: #fff;
  291. border-radius: 17rpx;
  292. overflow: hidden;
  293. margin-bottom: 30rpx;
  294. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  295. .food-image {
  296. width: 100%;
  297. height: 400rpx;
  298. display: block;
  299. }
  300. .food-info {
  301. padding: 30rpx;
  302. position: relative;
  303. box-sizing: border-box;
  304. .food-header {
  305. display: flex;
  306. align-items: center;
  307. justify-content: space-between;
  308. margin-bottom: 20rpx;
  309. .food-name {
  310. font-size: 32rpx;
  311. font-weight: bold;
  312. color: #151515;
  313. flex: 1;
  314. max-width: 360rpx;
  315. overflow: hidden;
  316. text-overflow: ellipsis;
  317. white-space: nowrap;
  318. }
  319. .food-left {
  320. display: flex;
  321. align-items: center;
  322. gap: 20rpx;
  323. }
  324. .food-right {
  325. display: flex;
  326. align-items: center;
  327. gap: 20rpx;
  328. flex-shrink: 0;
  329. }
  330. .food-sales {
  331. display: flex;
  332. align-items: center;
  333. font-size: 24rpx;
  334. color: #999999;
  335. .star-icon {
  336. width: 20rpx;
  337. height: 20rpx;
  338. margin-right: 8rpx;
  339. }
  340. .sales-text {
  341. font-size: 24rpx;
  342. color: #999999;
  343. }
  344. }
  345. .food-price {
  346. display: flex;
  347. align-items: baseline;
  348. color: #FF3B30;
  349. line-height: 1;
  350. .price-symbol {
  351. font-size: 24rpx;
  352. font-weight: bold;
  353. }
  354. .price-main {
  355. font-size: 44rpx;
  356. font-weight: bold;
  357. }
  358. .price-decimal {
  359. font-size: 24rpx;
  360. font-weight: bold;
  361. }
  362. }
  363. }
  364. .food-tags {
  365. display: flex;
  366. align-items: center;
  367. gap: 12rpx;
  368. margin-bottom: 20rpx;
  369. .food-tag {
  370. padding: 6rpx 16rpx;
  371. border-radius: 6rpx;
  372. font-size: 22rpx;
  373. color: #FFFFFF;
  374. line-height: 1.2;
  375. &.signature {
  376. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  377. }
  378. &.spicy {
  379. background: #2E2E2E;
  380. }
  381. }
  382. }
  383. .food-desc {
  384. font-size: 26rpx;
  385. color: #151515;
  386. line-height: 1.6;
  387. margin-top: 20rpx;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. </style>