index.vue 14 KB

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