index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. <!-- 描述 -->
  57. <view class="food-desc">{{ item.description }}</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. price: item?.totalPrice ?? item?.unitPrice ?? item?.price ?? ''
  99. };
  100. }
  101. // 格式化为保留两位小数的价格字符串
  102. const formatPriceFixed2 = (price) => {
  103. if (price === '' || price === null || price === undefined) return '0.00';
  104. const num = Number(price);
  105. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  106. };
  107. // 获取价格整数部分
  108. const getPriceMain = (price) => {
  109. const priceStr = formatPriceFixed2(price);
  110. const dotIndex = priceStr.indexOf('.');
  111. return dotIndex > -1 ? priceStr.substring(0, dotIndex) : priceStr;
  112. };
  113. // 获取价格小数部分(两位)
  114. const getPriceDecimal = (price) => {
  115. const priceStr = formatPriceFixed2(price);
  116. const dotIndex = priceStr.indexOf('.');
  117. return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '00';
  118. };
  119. // 登录成功回调:跳转点餐页
  120. const handleLoginSuccess = () => {
  121. const tableid = uni.getStorageSync('currentTableId') || '';
  122. const diners = uni.getStorageSync('currentDiners') || '1';
  123. const q = [];
  124. if (tableid) q.push(`tableid=${encodeURIComponent(tableid)}`);
  125. if (diners) q.push(`diners=${encodeURIComponent(diners)}`);
  126. const url = q.length ? `/pages/orderFood/index?${q.join('&')}` : '/pages/orderFood/index';
  127. go(url);
  128. };
  129. // 取消登录回调
  130. const handleLoginCancel = () => {
  131. console.log('用户取消登录');
  132. };
  133. // 点击菜品:先校验登录,未登录提示请登录;已登录则跳转菜品详情页
  134. const handleFoodClick = (item) => {
  135. const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  136. if (!token) {
  137. uni.showToast({ title: '请登录', icon: 'none' });
  138. return;
  139. }
  140. const cuisineId = item?.id ?? item?.cuisineId ?? '';
  141. if (!cuisineId) return;
  142. const tableId = uni.getStorageSync('currentTableId') ?? '';
  143. const q = [`cuisineId=${encodeURIComponent(String(cuisineId))}`];
  144. if (tableId !== '') q.push(`tableId=${encodeURIComponent(String(tableId))}`);
  145. go(`/pages/foodDetail/index?${q.join('&')}`);
  146. };
  147. onLoad(async (options) => {
  148. const storeId = options?.storeId ?? uni.getStorageSync('currentStoreId') ?? '';
  149. if (storeId) {
  150. try {
  151. const res = await GetStoreDetail(storeId);
  152. const data = res?.data ?? res;
  153. storeInfo.value = data?.storeInfo ?? data ?? {};
  154. const list = data?.homepageCuisines ?? data?.storeInfo?.homepageCuisines ?? [];
  155. foodList.value = (Array.isArray(list) ? list : []).map(normalizeHomeCuisine);
  156. } catch (e) {
  157. console.error('门店详情加载失败:', e);
  158. }
  159. }
  160. });
  161. </script>
  162. <style scoped lang="scss">
  163. .content {
  164. .top-img {
  165. width: 100%;
  166. height: 572rpx;
  167. }
  168. .content-box {
  169. width: 100%;
  170. box-sizing: border-box;
  171. padding: 0 30rpx 140rpx;
  172. .shop-info {
  173. display: flex;
  174. align-items: center;
  175. justify-content: center;
  176. width: 100%;
  177. height: 100rpx;
  178. background-color: #fff;
  179. border-radius: 17rpx;
  180. margin-top: -50rpx;
  181. position: relative;
  182. z-index: 2;
  183. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  184. .shop-img {
  185. width: 45rpx;
  186. height: 38rpx;
  187. margin-right: 20rpx;
  188. }
  189. .shop-name {
  190. font-weight: bold;
  191. font-size: 31rpx;
  192. color: #151515;
  193. }
  194. }
  195. }
  196. .login-card {
  197. width: 100%;
  198. min-height: 240rpx;
  199. background-color: #fff;
  200. border-radius: 17rpx;
  201. margin-top: 20rpx;
  202. box-sizing: border-box;
  203. padding: 30rpx;
  204. display: flex;
  205. align-items: center;
  206. justify-content: space-between;
  207. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  208. .login-card-left {
  209. flex: 1;
  210. display: flex;
  211. flex-direction: column;
  212. justify-content: center;
  213. padding-right: 20rpx;
  214. .welcome-title {
  215. font-size: 36rpx;
  216. font-weight: bold;
  217. color: #151515;
  218. margin-bottom: 16rpx;
  219. line-height: 1.4;
  220. }
  221. .welcome-desc {
  222. font-size: 27rpx;
  223. color: #666666;
  224. margin-bottom: 24rpx;
  225. line-height: 1.4;
  226. }
  227. .login-btn {
  228. width: 160rpx;
  229. height: 53rpx;
  230. background: #2E2E2E;
  231. display: flex;
  232. align-items: center;
  233. justify-content: center;
  234. font-size: 27rpx;
  235. color: #FFFFFF;
  236. font-weight: 500;
  237. cursor: pointer;
  238. transition: opacity 0.3s;
  239. &:active {
  240. opacity: 0.8;
  241. }
  242. }
  243. }
  244. .login-card-right {
  245. flex-shrink: 0;
  246. display: flex;
  247. align-items: center;
  248. justify-content: center;
  249. .welcome-img {
  250. width: 200rpx;
  251. height: auto;
  252. }
  253. }
  254. }
  255. // 登录弹窗层级设置(盖住底部 tabbar)
  256. .login-modal-wrapper {
  257. position: relative;
  258. z-index: 99999;
  259. :deep(.uni-popup) {
  260. z-index: 99999 !important;
  261. top: 0 !important;
  262. left: 0 !important;
  263. right: 0 !important;
  264. bottom: 0 !important;
  265. }
  266. :deep(.uni-popup__wrapper) {
  267. z-index: 99999 !important;
  268. }
  269. }
  270. .banner-box {
  271. width: 100%;
  272. height: 100%;
  273. display: flex;
  274. align-items: center;
  275. justify-content: center;
  276. margin-top: 40rpx;
  277. .banner-img {
  278. width: 480rpx;
  279. height: 66rpx;
  280. margin: 0 auto;
  281. }
  282. }
  283. .list-box {
  284. width: 100%;
  285. margin-top: 40rpx;
  286. padding-bottom: 40rpx;
  287. .food-card {
  288. width: 100%;
  289. background-color: #fff;
  290. border-radius: 17rpx;
  291. overflow: hidden;
  292. margin-bottom: 30rpx;
  293. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  294. .food-image {
  295. width: 100%;
  296. height: 400rpx;
  297. display: block;
  298. }
  299. .food-info {
  300. padding: 30rpx;
  301. position: relative;
  302. box-sizing: border-box;
  303. .food-header {
  304. display: flex;
  305. align-items: center;
  306. justify-content: space-between;
  307. margin-bottom: 20rpx;
  308. .food-name {
  309. font-size: 32rpx;
  310. font-weight: bold;
  311. color: #151515;
  312. flex: 1;
  313. max-width: 360rpx;
  314. overflow: hidden;
  315. text-overflow: ellipsis;
  316. white-space: nowrap;
  317. }
  318. .food-left {
  319. display: flex;
  320. align-items: center;
  321. gap: 20rpx;
  322. }
  323. .food-right {
  324. display: flex;
  325. align-items: center;
  326. gap: 20rpx;
  327. flex-shrink: 0;
  328. }
  329. .food-sales {
  330. display: flex;
  331. align-items: center;
  332. font-size: 24rpx;
  333. color: #999999;
  334. .star-icon {
  335. width: 20rpx;
  336. height: 20rpx;
  337. margin-right: 8rpx;
  338. }
  339. .sales-text {
  340. font-size: 24rpx;
  341. color: #999999;
  342. }
  343. }
  344. .food-price {
  345. display: flex;
  346. align-items: baseline;
  347. color: #FF3B30;
  348. line-height: 1;
  349. .price-symbol {
  350. font-size: 24rpx;
  351. font-weight: bold;
  352. }
  353. .price-main {
  354. font-size: 44rpx;
  355. font-weight: bold;
  356. }
  357. .price-decimal {
  358. font-size: 24rpx;
  359. font-weight: bold;
  360. }
  361. }
  362. }
  363. .food-tags {
  364. display: flex;
  365. align-items: center;
  366. gap: 12rpx;
  367. margin-bottom: 20rpx;
  368. .food-tag {
  369. padding: 6rpx 16rpx;
  370. border-radius: 6rpx;
  371. font-size: 22rpx;
  372. color: #FFFFFF;
  373. line-height: 1.2;
  374. &.signature {
  375. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  376. }
  377. &.spicy {
  378. background: #2E2E2E;
  379. }
  380. }
  381. }
  382. .food-desc {
  383. font-size: 26rpx;
  384. color: #151515;
  385. line-height: 1.6;
  386. margin-top: 20rpx;
  387. }
  388. }
  389. }
  390. }
  391. }
  392. </style>