index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <script lang="ts" setup>
  2. import couponBg from '@img/index/coupon-bg.png'
  3. import indexBg from '@img/index/index-bg.png'
  4. import { useRequest } from 'alova/client'
  5. import { storeToRefs } from 'pinia'
  6. import { ref, watch } from 'vue'
  7. import { getAccountCount, getCouponSituation } from '@/api/home'
  8. import DiscountCoupon from '@/components/discountCoupon.vue'
  9. import SpendAndSaveCoupon from '@/components/spendAndSaveCoupon.vue'
  10. import { useCouponStore } from '@/store/coupon'
  11. import { useTokenStore } from '@/store/token'
  12. import { toLoginPage } from '@/utils/toLoginPage'
  13. defineOptions({
  14. name: 'Home',
  15. })
  16. definePage({
  17. // 使用 type: "home" 属性设置首页,其他页面不需要设置,默认为page
  18. type: 'home',
  19. style: {
  20. // 'custom' 表示开启自定义导航栏,默认 'default'
  21. navigationStyle: 'custom',
  22. navigationBarTitleText: '首页',
  23. },
  24. })
  25. const tokenStore = useTokenStore()
  26. const { hasLogin } = storeToRefs(tokenStore)
  27. // 获取优惠券
  28. const couponStore = useCouponStore()
  29. const { couponList, discountVoucherList } = storeToRefs(couponStore)
  30. // 获取首页收益
  31. const { send: getAccountCountRequest, data: accountCountData } = useRequest(getAccountCount, {
  32. immediate: false,
  33. })
  34. // 获取首页领券情况数据
  35. const { send: getCouponSituationRequest, data: couponSituationData } = useRequest(getCouponSituation, {
  36. immediate: false,
  37. })
  38. onLoad(async () => {
  39. // 获取优惠券
  40. couponStore.getCouponListByType()
  41. })
  42. onShow(() => {
  43. // 登录后查询收益数据
  44. if (hasLogin) {
  45. getAccountCountRequest()
  46. getCouponSituationRequest()
  47. }
  48. })
  49. watch(() => couponSituationData, (newVal) => {
  50. console.log('首页领券情况数据:', newVal)
  51. })
  52. // #ifdef MP-WEIXIN
  53. async function login() {
  54. const currentPage = getCurrentPages()[0]
  55. const redirectUrl = `/${currentPage.route}`
  56. toLoginPage({ queryString: `?redirect=${encodeURIComponent(redirectUrl)}` })
  57. }
  58. // #endif
  59. // 顶部导航栏高度,设置banner位置
  60. const navigationBarHeight = ref(0)
  61. // #ifdef MP-WEIXIN
  62. function getNavigationBarHeight() {
  63. uni.getSystemInfo({
  64. success: (res) => {
  65. const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  66. console.log('顶部导航栏高度:', res.statusBarHeight, menuButtonInfo)
  67. // 顶部导航栏高度 = 状态栏高度 + 胶囊的高度
  68. navigationBarHeight.value = res.statusBarHeight + menuButtonInfo.height + 12
  69. },
  70. })
  71. }
  72. getNavigationBarHeight()
  73. // #endif
  74. </script>
  75. <template>
  76. <view class="home-container">
  77. <!-- 顶部区域 -->
  78. <view class="home-header">
  79. <image class="home-header-bg" :src="indexBg" mode="aspectFill" />
  80. <view class="absolute left-5 z-3 text-xl c-white" :style="{ top: `${navigationBarHeight - 39}px` }">
  81. 券中心
  82. </view>
  83. <view class="home-header-avatar-info" :style="{ paddingTop: `${navigationBarHeight + 10}px` }">
  84. <view class="home-header-balance">
  85. 我的收益(元)
  86. </view>
  87. <view class="home-header-balance-num">
  88. <view class="home-header-balance-num-amount">
  89. {{ accountCountData?.balance || 0 }}
  90. </view>
  91. </view>
  92. </view>
  93. <view class="home-header-tips">
  94. <view class="home-header-tips-item">
  95. <view class="home-header-tips-item-num">
  96. {{ couponSituationData?.usedQuantity || 0 }}张
  97. </view>
  98. <view class="home-header-tips-item-des">
  99. 已核销
  100. </view>
  101. </view>
  102. <view class="home-header-tips-item">
  103. <view class="home-header-tips-item-num">
  104. {{ couponSituationData?.quantityToBeUsed || 0 }}张
  105. </view>
  106. <view class="home-header-tips-item-des">
  107. 未核销
  108. </view>
  109. </view>
  110. <view class="home-header-tips-item">
  111. <view class="home-header-tips-item-num">
  112. {{ couponSituationData?.quantityForComplimentary || 0 }}张
  113. </view>
  114. <view class="home-header-tips-item-des">
  115. 已发放
  116. </view>
  117. </view>
  118. </view>
  119. <view v-if="!hasLogin" class="home-hidden" @click="login">
  120. <image class="home-hidden-img" src="@img/index/lock.png" mode="scaleToFill" />
  121. <view class="home-hidden-text">
  122. 请登录,查看更多内容~
  123. </view>
  124. </view>
  125. </view>
  126. <!-- 满减券 -->
  127. <view class="home-header-coupon">
  128. <image class="home-header-coupon-bg" :src="couponBg" mode="aspectFill" />
  129. <view class="home-header-coupon-title">
  130. <image class="home-header-coupon-title-icon" src="@img/index/icon1.png" mode="scaleToFill" />
  131. <view class="home-header-coupon-title-text">
  132. 满减券
  133. </view>
  134. <view class="home-header-coupon-title-des">
  135. 平台满减&nbsp;&nbsp;乐享不停
  136. </view>
  137. </view>
  138. <view class="home-header-coupon-content">
  139. <template v-for="item in discountVoucherList" :key="item.id">
  140. <spend-and-save-coupon :coupon="item" />
  141. </template>
  142. </view>
  143. <view class="home-header-coupon-btn">
  144. <up-button class="home-header-coupon-btn-text" text="查看更多优惠券"
  145. color="linear-gradient(0deg, #FFE8CE 0%, #FBB8A0 100%)" />
  146. </view>
  147. </view>
  148. <!-- 折扣券 -->
  149. <view class="home-coupon">
  150. <view class="home-coupon-title">
  151. <image class="home-coupon-title-icon" src="@img/index/icon2.png" mode="scaleToFill" />
  152. <view class="home-coupon-title-text">
  153. 折扣券
  154. </view>
  155. <view class="home-coupon-title-des">
  156. 分享折扣&nbsp;&nbsp;立享优惠
  157. </view>
  158. <view class="home-coupon-title-more">
  159. <view class="home-coupon-title-more-text">
  160. 更多
  161. </view>
  162. <up-icon size="14" name="arrow-right" />
  163. </view>
  164. </view>
  165. <view class="home-coupon-content">
  166. <template v-for="item in couponList" :key="item.id">
  167. <discount-coupon :coupon="item" />
  168. </template>
  169. </view>
  170. </view>
  171. </view>
  172. </template>
  173. <style lang="scss" scoped>
  174. .home-container {
  175. background-color: #f5f5f5;
  176. line-height: 1;
  177. position: relative;
  178. .home-header {
  179. height: 550rpx;
  180. position: relative;
  181. overflow: hidden;
  182. .home-header-bg {
  183. position: absolute;
  184. top: 0;
  185. left: 0;
  186. width: 125%;
  187. height: 200%;
  188. object-fit: cover;
  189. transform: translate(-80px, 0);
  190. z-index: 0;
  191. }
  192. .home-header-avatar-info {
  193. display: flex;
  194. flex-direction: column;
  195. justify-content: center;
  196. text-align: center;
  197. gap: 30rpx;
  198. padding: 0 24rpx;
  199. position: relative;
  200. z-index: 1;
  201. .home-header-balance {
  202. font-weight: 400;
  203. font-size: 26rpx;
  204. color: #ffffff;
  205. }
  206. .home-header-balance-num {
  207. display: flex;
  208. justify-content: space-between;
  209. align-items: center;
  210. .home-header-balance-num-amount {
  211. width: 100%;
  212. font-weight: 500;
  213. font-size: 65rpx;
  214. color: #ffffff;
  215. }
  216. .home-header-balance-num-btns {
  217. display: flex;
  218. gap: 15rpx;
  219. .home-header-balance-num-btn {
  220. padding: 20rpx 40rpx;
  221. border-radius: 33rpx;
  222. font-weight: 400;
  223. font-size: 26rpx;
  224. &.js {
  225. background: #da4c47;
  226. color: #ffffff;
  227. }
  228. &.tx {
  229. background: #bfbfbf;
  230. color: #747474;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. .home-header-tips {
  237. height: 124rpx;
  238. display: flex;
  239. border-radius: 10rpx;
  240. margin: 47rpx 24rpx 0 24rpx;
  241. position: relative;
  242. z-index: 1;
  243. .home-header-tips-item {
  244. flex: 1;
  245. display: flex;
  246. flex-direction: column;
  247. justify-content: center;
  248. align-items: center;
  249. gap: 15rpx;
  250. font-weight: 500;
  251. font-size: 34rpx;
  252. color: #ffffff;
  253. line-height: 1;
  254. position: relative;
  255. &:not(:last-child):after {
  256. content: '';
  257. position: absolute;
  258. top: 50%;
  259. right: 0;
  260. transform: translateY(-50%);
  261. width: 2px;
  262. height: 40rpx;
  263. background: #ffffff;
  264. }
  265. .home-header-tips-item-num {
  266. font-weight: bold;
  267. }
  268. .home-header-tips-item-des {
  269. font-weight: 400;
  270. font-size: 24rpx;
  271. color: #ffffff;
  272. }
  273. }
  274. }
  275. .home-hidden {
  276. height: 550rpx;
  277. width: 100%;
  278. position: absolute;
  279. top: 0;
  280. right: 0;
  281. display: flex;
  282. flex-direction: column;
  283. justify-content: center;
  284. align-items: center;
  285. background-color: rgba(255, 255, 255, 0.1);
  286. backdrop-filter: blur(25rpx);
  287. -webkit-backdrop-filter: blur(25rpx);
  288. z-index: 2;
  289. .home-hidden-img {
  290. width: 209rpx;
  291. height: 209rpx;
  292. object-fit: cover;
  293. }
  294. .home-hidden-text {
  295. font-weight: 400;
  296. font-size: 26rpx;
  297. color: #ffffff;
  298. margin-top: -25rpx;
  299. }
  300. }
  301. }
  302. // 优惠券
  303. .home-header-coupon {
  304. position: relative;
  305. height: 419rpx;
  306. margin: -58rpx 20rpx 20rpx 20rpx;
  307. gap: 20rpx;
  308. padding: 28rpx 20rpx;
  309. overflow: hidden;
  310. z-index: 3;
  311. .home-header-coupon-bg {
  312. position: absolute;
  313. top: 0;
  314. right: 0;
  315. width: 100%;
  316. object-fit: cover;
  317. z-index: 0;
  318. }
  319. .home-header-coupon-title {
  320. display: flex;
  321. flex-direction: row;
  322. position: relative;
  323. z-index: 1;
  324. .home-header-coupon-title-icon {
  325. width: 38rpx;
  326. height: 38rpx;
  327. object-fit: cover;
  328. margin-right: 13rpx;
  329. }
  330. .home-header-coupon-title-text {
  331. font-weight: 500;
  332. font-size: 30rpx;
  333. color: #333333;
  334. margin-right: 21rpx;
  335. }
  336. .home-header-coupon-title-des {
  337. display: inline-flex;
  338. align-items: center;
  339. font-size: 24rpx;
  340. font-weight: 400;
  341. color: #888888;
  342. }
  343. }
  344. .home-header-coupon-content {
  345. height: 238rpx;
  346. display: flex;
  347. flex-direction: row;
  348. align-items: flex-end;
  349. gap: 20rpx;
  350. position: relative;
  351. z-index: 1;
  352. }
  353. .home-header-coupon-btn {
  354. padding: 21rpx 70rpx 22px 69rpx;
  355. position: relative;
  356. z-index: 1;
  357. .home-header-coupon-btn-text {
  358. box-shadow: 0rpx 4rpx 7rpx 0rpx rgba(230, 77, 13, 0.17);
  359. border-radius: 37rpx;
  360. color: #651e03 !important;
  361. font-size: 26rpx;
  362. font-weight: 400;
  363. }
  364. }
  365. }
  366. // 折扣券
  367. .home-coupon {
  368. margin: -20rpx 20rpx 20rpx 20rpx;
  369. padding: 28rpx 20rpx;
  370. background-color: #ffffff;
  371. border-radius: 15rpx;
  372. .home-coupon-title {
  373. display: flex;
  374. flex-direction: row;
  375. .home-coupon-title-icon {
  376. width: 38rpx;
  377. height: 38rpx;
  378. object-fit: cover;
  379. margin-right: 13rpx;
  380. }
  381. .home-coupon-title-text {
  382. font-weight: 500;
  383. font-size: 30rpx;
  384. color: #333333;
  385. margin-right: 21rpx;
  386. }
  387. .home-coupon-title-des {
  388. display: inline-flex;
  389. align-items: center;
  390. font-size: 24rpx;
  391. font-weight: 400;
  392. color: #888888;
  393. }
  394. .home-coupon-title-more {
  395. margin-left: auto;
  396. display: flex;
  397. flex-direction: row;
  398. align-items: center;
  399. .home-coupon-title-more-text {
  400. font-weight: 400;
  401. font-size: 24rpx;
  402. color: #666666;
  403. height: 23rpx;
  404. line-height: 23rpx;
  405. }
  406. }
  407. }
  408. .home-coupon-content {
  409. padding-top: 28rpx;
  410. display: flex;
  411. flex-direction: column;
  412. gap: 20rpx;
  413. }
  414. }
  415. }
  416. </style>