index.vue 13 KB

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