index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. <!-- 标签:招牌橙底白字,其他黑底白字(与 FoodCard/foodDetail 一致) -->
  51. <view class="food-tags" v-if="item.tags && item.tags.length">
  52. <view v-for="(tag, tagIndex) in item.tags" :key="tagIndex" class="food-tag"
  53. :class="{ 'food-tag--signature': (tag.text || '').includes('招牌') }">{{ 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, SCAN_QR_CACHE } from '@/settings/enums.js';
  79. import { GetStoreDetail } from '@/api/dining.js';
  80. import { isScanEntryAllowed } from '@/utils/qrScene.js';
  81. import { syncM2GenericPricingStorage } from '@/utils/m2GenericApiPath.js';
  82. import {
  83. getScanEntryIdsFromOptions,
  84. runTableDiningStatusAndRedirect,
  85. trim
  86. } from '@/utils/tableDiningLaunch.js';
  87. const userStore = useUserStore();
  88. const showLoginModal = ref(false);
  89. /** 门店信息(接口返回的 storeInfo,绑定 storeName 等) */
  90. const storeInfo = ref({});
  91. // 首页菜品列表(绑定接口 homepageCuisines,兼容多种字段名)
  92. const foodList = ref([]);
  93. function normalizeHomeCuisine(item) {
  94. const raw = item?.tags ?? item?.tagList ?? item?.tagNames ?? item?.labels ?? item?.tag ?? [];
  95. let tagArr = [];
  96. if (Array.isArray(raw)) tagArr = raw;
  97. else if (raw && typeof raw === 'object') tagArr = Array.isArray(raw.list) ? raw.list : Array.isArray(raw.items) ? raw.items : [];
  98. else if (typeof raw === 'string') {
  99. const trimmed = raw.trim();
  100. if (trimmed.startsWith('[')) {
  101. try {
  102. const parsed = JSON.parse(trimmed);
  103. tagArr = Array.isArray(parsed) ? parsed : trimmed ? [trimmed] : [];
  104. } catch {
  105. tagArr = trimmed ? trimmed.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  106. }
  107. } else {
  108. tagArr = trimmed ? trimmed.split(/[,,、\s]+/).map(s => s.trim()).filter(Boolean) : [];
  109. }
  110. }
  111. const tagArrNormalized = tagArr.map((t) => {
  112. if (t == null) return { text: '', type: 'normal' };
  113. if (typeof t === 'string') return { text: t, type: 'normal' };
  114. return { text: t?.text ?? t?.tagName ?? t?.name ?? t?.label ?? '', type: t?.type ?? 'normal' };
  115. }).filter((t) => t.text !== '' && t.text != null);
  116. // 商品图片:取第一张,若为逗号分隔字符串则截取逗号前的第一张
  117. const firstImage = (val) => {
  118. if (val == null || val === '') return '';
  119. if (Array.isArray(val)) return (val[0] != null && val[0] !== '') ? String(val[0]).split(',')[0].trim() : '';
  120. const str = String(val).trim();
  121. return str ? str.split(/[,,]/)[0].trim() : '';
  122. };
  123. const images = item?.images ?? item?.cuisineImage ?? item?.image;
  124. const imageUrl = firstImage(images) || firstImage(item?.cuisineImage) || firstImage(item?.image) || '';
  125. return {
  126. id: item?.id ?? item?.cuisineId ?? '',
  127. image: imageUrl || 'img/icon/shop.png',
  128. name: item?.cuisineName ?? item?.name ?? '',
  129. sales: item?.monthlySales ?? item?.sales ?? 0,
  130. tags: tagArrNormalized,
  131. description: item?.description ?? item?.desc ?? '',
  132. dishReview: item?.dishReview ?? item?.description ?? item?.desc ?? '',
  133. price: item?.totalPrice ?? item?.unitPrice ?? item?.price ?? ''
  134. };
  135. }
  136. // 格式化为保留两位小数的价格字符串
  137. const formatPriceFixed2 = (price) => {
  138. if (price === '' || price === null || price === undefined) return '0.00';
  139. const num = Number(price);
  140. return Number.isNaN(num) ? '0.00' : num.toFixed(2);
  141. };
  142. // 获取价格整数部分
  143. const getPriceMain = (price) => {
  144. const priceStr = formatPriceFixed2(price);
  145. const dotIndex = priceStr.indexOf('.');
  146. return dotIndex > -1 ? priceStr.substring(0, dotIndex) : priceStr;
  147. };
  148. // 获取价格小数部分(两位)
  149. const getPriceDecimal = (price) => {
  150. const priceStr = formatPriceFixed2(price);
  151. const dotIndex = priceStr.indexOf('.');
  152. return dotIndex > -1 ? priceStr.substring(dotIndex + 1) : '00';
  153. };
  154. // 登录成功回调:跳转点餐页
  155. const handleLoginSuccess = () => {
  156. const tableid = uni.getStorageSync('currentTableId') || '';
  157. const diners = uni.getStorageSync('currentDiners') || '1';
  158. const q = [];
  159. if (tableid) q.push(`tableid=${encodeURIComponent(tableid)}`);
  160. if (diners) q.push(`diners=${encodeURIComponent(diners)}`);
  161. const url = q.length ? `/pages/orderFood/index?${q.join('&')}` : '/pages/orderFood/index';
  162. go(url);
  163. };
  164. // 取消登录回调
  165. const handleLoginCancel = () => {
  166. console.log('用户取消登录');
  167. };
  168. // 点击菜品:先校验登录,未登录提示请登录;已登录则跳转菜品详情页
  169. const handleFoodClick = (item) => {
  170. const token = userStore.getToken || uni.getStorageSync(TOKEN) || '';
  171. if (!token) {
  172. uni.showToast({ title: '请登录', icon: 'none' });
  173. return;
  174. }
  175. const cuisineId = item?.id ?? item?.cuisineId ?? '';
  176. if (!cuisineId) return;
  177. const tableId = uni.getStorageSync('currentTableId') ?? '';
  178. const q = [`cuisineId=${encodeURIComponent(String(cuisineId))}`];
  179. if (tableId !== '') q.push(`tableId=${encodeURIComponent(String(tableId))}`);
  180. go(`/pages/foodDetail/index?${q.join('&')}`);
  181. };
  182. onLoad(async (options) => {
  183. /** 太阳码若直达首页(非 launch),须同样走 table-dining-status */
  184. let { tableid, storeId: scanStoreId, m } = getScanEntryIdsFromOptions(options);
  185. if (trim(m) === '') {
  186. try {
  187. const c = uni.getStorageSync(SCAN_QR_CACHE);
  188. if (c) m = trim(JSON.parse(c).m ?? '');
  189. } catch (_) {}
  190. }
  191. syncM2GenericPricingStorage(m);
  192. const fromScan = trim(tableid);
  193. if (fromScan && isScanEntryAllowed(m)) {
  194. uni.setStorageSync('currentTableId', fromScan);
  195. if (trim(scanStoreId)) uni.setStorageSync('currentStoreId', trim(scanStoreId));
  196. await runTableDiningStatusAndRedirect(fromScan);
  197. return;
  198. }
  199. const storeId = options?.storeId ?? uni.getStorageSync('currentStoreId') ?? '';
  200. if (storeId) {
  201. try {
  202. const res = await GetStoreDetail(storeId);
  203. const data = res?.data ?? res;
  204. storeInfo.value = data?.storeInfo ?? data ?? {};
  205. const list = data?.homepageCuisines ?? data?.storeInfo?.homepageCuisines ?? [];
  206. foodList.value = (Array.isArray(list) ? list : []).map(normalizeHomeCuisine);
  207. } catch (e) {
  208. console.error('门店详情加载失败:', e);
  209. }
  210. }
  211. });
  212. </script>
  213. <style scoped lang="scss">
  214. .content {
  215. .top-img {
  216. width: 100%;
  217. height: 572rpx;
  218. }
  219. .content-box {
  220. width: 100%;
  221. box-sizing: border-box;
  222. padding: 0 30rpx 140rpx;
  223. .shop-info {
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. width: 100%;
  228. height: 100rpx;
  229. background-color: #fff;
  230. border-radius: 17rpx;
  231. margin-top: -50rpx;
  232. position: relative;
  233. z-index: 2;
  234. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  235. .shop-img {
  236. width: 45rpx;
  237. height: 38rpx;
  238. margin-right: 20rpx;
  239. }
  240. .shop-name {
  241. font-weight: bold;
  242. font-size: 31rpx;
  243. color: #151515;
  244. }
  245. }
  246. }
  247. .login-card {
  248. width: 100%;
  249. min-height: 240rpx;
  250. background-color: #fff;
  251. border-radius: 17rpx;
  252. margin-top: 20rpx;
  253. box-sizing: border-box;
  254. padding: 30rpx;
  255. display: flex;
  256. align-items: center;
  257. justify-content: space-between;
  258. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  259. .login-card-left {
  260. flex: 1;
  261. display: flex;
  262. flex-direction: column;
  263. justify-content: center;
  264. padding-right: 20rpx;
  265. .welcome-title {
  266. font-size: 36rpx;
  267. font-weight: bold;
  268. color: #151515;
  269. margin-bottom: 16rpx;
  270. line-height: 1.4;
  271. }
  272. .welcome-desc {
  273. font-size: 27rpx;
  274. color: #666666;
  275. margin-bottom: 24rpx;
  276. line-height: 1.4;
  277. }
  278. .login-btn {
  279. width: 160rpx;
  280. height: 53rpx;
  281. background: #2E2E2E;
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. font-size: 27rpx;
  286. color: #FFFFFF;
  287. font-weight: 500;
  288. cursor: pointer;
  289. transition: opacity 0.3s;
  290. &:active {
  291. opacity: 0.8;
  292. }
  293. }
  294. }
  295. .login-card-right {
  296. flex-shrink: 0;
  297. display: flex;
  298. align-items: center;
  299. justify-content: center;
  300. .welcome-img {
  301. width: 200rpx;
  302. height: auto;
  303. }
  304. }
  305. }
  306. // 登录弹窗层级设置(盖住底部 tabbar)
  307. .login-modal-wrapper {
  308. position: relative;
  309. z-index: 99999;
  310. :deep(.uni-popup) {
  311. z-index: 99999 !important;
  312. top: 0 !important;
  313. left: 0 !important;
  314. right: 0 !important;
  315. bottom: 0 !important;
  316. }
  317. :deep(.uni-popup__wrapper) {
  318. z-index: 99999 !important;
  319. }
  320. }
  321. .banner-box {
  322. width: 100%;
  323. height: 100%;
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. margin-top: 40rpx;
  328. .banner-img {
  329. width: 480rpx;
  330. height: 66rpx;
  331. margin: 0 auto;
  332. }
  333. }
  334. .list-box {
  335. width: 100%;
  336. margin-top: 40rpx;
  337. padding-bottom: 40rpx;
  338. .food-card {
  339. width: 100%;
  340. background-color: #fff;
  341. border-radius: 17rpx;
  342. overflow: hidden;
  343. margin-bottom: 30rpx;
  344. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  345. .food-image {
  346. width: 100%;
  347. height: 400rpx;
  348. display: block;
  349. }
  350. .food-info {
  351. padding: 30rpx;
  352. position: relative;
  353. box-sizing: border-box;
  354. .food-header {
  355. display: flex;
  356. align-items: center;
  357. justify-content: space-between;
  358. margin-bottom: 20rpx;
  359. .food-name {
  360. font-size: 32rpx;
  361. font-weight: bold;
  362. color: #151515;
  363. flex: 1;
  364. max-width: 360rpx;
  365. overflow: hidden;
  366. text-overflow: ellipsis;
  367. white-space: nowrap;
  368. }
  369. .food-left {
  370. display: flex;
  371. align-items: center;
  372. gap: 20rpx;
  373. }
  374. .food-right {
  375. display: flex;
  376. align-items: center;
  377. gap: 20rpx;
  378. flex-shrink: 0;
  379. }
  380. .food-sales {
  381. display: flex;
  382. align-items: center;
  383. font-size: 24rpx;
  384. color: #999999;
  385. .star-icon {
  386. width: 20rpx;
  387. height: 20rpx;
  388. margin-right: 8rpx;
  389. }
  390. .sales-text {
  391. font-size: 24rpx;
  392. color: #999999;
  393. }
  394. }
  395. .food-price {
  396. display: flex;
  397. align-items: baseline;
  398. color: #FF3B30;
  399. line-height: 1;
  400. .price-symbol {
  401. font-size: 24rpx;
  402. font-weight: bold;
  403. }
  404. .price-main {
  405. font-size: 44rpx;
  406. font-weight: bold;
  407. }
  408. .price-decimal {
  409. font-size: 24rpx;
  410. font-weight: bold;
  411. }
  412. }
  413. }
  414. .food-tags {
  415. display: flex;
  416. align-items: center;
  417. gap: 12rpx;
  418. margin-bottom: 20rpx;
  419. .food-tag {
  420. padding: 6rpx 16rpx;
  421. border-radius: 6rpx;
  422. font-size: 22rpx;
  423. color: #FFFFFF;
  424. line-height: 1.2;
  425. background: #2E2E2E;
  426. &--signature {
  427. background: linear-gradient(90deg, #FCB13F 0%, #FC793D 100%);
  428. }
  429. }
  430. }
  431. .food-desc {
  432. font-size: 26rpx;
  433. color: #151515;
  434. line-height: 1.6;
  435. margin-top: 20rpx;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. </style>