index.vue 11 KB

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