index.vue 12 KB

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