| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <script lang="ts" setup>
- import { useRequest } from 'alova/client'
- import { storeToRefs } from 'pinia'
- import { ref, watch } from 'vue'
- import { getAccountCount, getCouponDetail, getCouponSituation, getShareInfo } from '@/api/home'
- import DiscountCoupon from '@/components/DiscountCoupon.vue'
- import SpendAndSaveCoupon from '@/components/SpendAndSaveCoupon.vue'
- import { useShare } from '@/hooks/useShare'
- import { useCouponStore } from '@/store/coupon'
- import { useTokenStore } from '@/store/token'
- import { getImageUrl } from '@/utils/imageUtil'
- import { toLoginPage } from '@/utils/toLoginPage'
- defineOptions({
- name: 'Home',
- })
- definePage({
- // 使用 type: "home" 属性设置首页,其他页面不需要设置,默认为page
- type: 'home',
- style: {
- // 'custom' 表示开启自定义导航栏,默认 'default'
- navigationStyle: 'custom',
- navigationBarTitleText: '首页',
- },
- })
- const tokenStore = useTokenStore()
- const { hasLogin } = storeToRefs(tokenStore)
- const refreshing = ref(false)
- // 分享配置
- const shareConfig = ref(null)
- const shareButtonRef = ref<HTMLElement | null>(null)
- const currentCouponId = ref<string>('')
- // 获取优惠券
- const couponStore = useCouponStore()
- const { couponList, discountVoucherList, loading } = storeToRefs(couponStore)
- // 获取首页收益
- const { send: getAccountCountRequest, data: accountCountData } = useRequest(getAccountCount, {
- immediate: false,
- dependencies: [],
- })
- // 获取首页领券情况数据
- const { send: getCouponSituationRequest, data: couponSituationData } = useRequest(getCouponSituation, {
- immediate: false,
- dependencies: [],
- })
- // onLoad(async () => {
- // // 获取优惠券
- // couponStore.getCouponListByType()
- // })
- onShow((options) => {
- couponStore.getCouponListByType()
- // 登录后查询收益数据
- if (hasLogin) {
- Promise.allSettled([getAccountCountRequest(), getCouponSituationRequest()])
- // getAccountCountRequest()
- // getCouponSituationRequest()
- }
- })
- // #ifdef MP-WEIXIN
- async function login() {
- const currentPage = getCurrentPages()[0]
- const redirectUrl = `/${currentPage.route}`
- toLoginPage({ queryString: `?redirect=${encodeURIComponent(redirectUrl)}` })
- }
- // #endif
- // 顶部导航栏高度,设置banner位置
- const navigationBarHeight = ref(0)
- // #ifdef MP-WEIXIN
- function getNavigationBarHeight() {
- uni.getSystemInfo({
- success: (res) => {
- const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
- console.log('顶部导航栏高度:', res.statusBarHeight, menuButtonInfo)
- // 顶部导航栏高度 = 状态栏高度 + 胶囊的高度
- navigationBarHeight.value = res.statusBarHeight + menuButtonInfo.height + 12
- },
- })
- }
- getNavigationBarHeight()
- // #endif
- function toDiscountCouponList() {
- uni.navigateTo({
- url: '/pages-A/discountcouponList/index?type=2',
- })
- }
- function toSpendAndSaveCouponList() {
- uni.navigateTo({
- url: '/pages-A/spendAndSaveCouponList/index?type=3',
- })
- }
- function toCouponRedemptionList(state) {
- uni.navigateTo({
- url: `/pages-A/couponRedemptionList/index?state=${state}`,
- })
- }
- // 创建分享hook实例
- const { getShareConfig } = useShare()
- const { getShareConfig: getShareCouponConfig } = useShare({
- shareType: 'COUPON',
- imageSource: 'REMOTE',
- path: '/pages/receiveCoupon/index',
- pathParamKey: 'couponShareRecordId',
- })
- // #ifdef MP-WEIXIN
- // 分享功能实现
- // 分享生命周期函数
- onShareAppMessage(async (options) => {
- console.log(options)
- if (options.from === 'button' && options.target.dataset.shareType === 'coupon') {
- const couponId = options.target.dataset.couponId
- const couponinfo = await getCouponDetail({ templateId: couponId })
- return await getShareCouponConfig({
- imageUrl: couponinfo?.imageUrl,
- }, {
- shareContentId: couponId,
- })
- }
- else {
- return await getShareConfig()
- }
- })
- // #endif
- async function onRefresh() {
- refreshing.value = true
- couponStore.getCouponListByType()
- setTimeout(() => {
- refreshing.value = false
- }, 1000)
- }
- </script>
- <template>
- <view class="home-container">
- <up-pull-refresh :refreshing="refreshing" @refresh="onRefresh">
- <!-- 顶部区域 -->
- <view class="home-header">
- <image class="home-header-bg" :src="getImageUrl('@img/index/index-bg.png')" mode="aspectFill" />
- <view class="absolute left-5 z-3 text-xl c-white" :style="{ top: `${navigationBarHeight - 39}px` }">
- 券中心
- </view>
- <view class="home-header-avatar-info" :style="{ paddingTop: `${navigationBarHeight + 10}px` }">
- <view class="home-header-balance">
- 我的收益(元)
- </view>
- <view class="home-header-balance-num">
- <view class="home-header-balance-num-amount">
- {{ accountCountData?.balance || 0 }}
- </view>
- </view>
- </view>
- <view class="home-header-tips">
- <view class="home-header-tips-item" @click="toCouponRedemptionList('2')">
- <view class="home-header-tips-item-num">
- {{ couponSituationData?.usedQuantity || 0 }}张
- </view>
- <view class="home-header-tips-item-des">
- 已核销
- </view>
- </view>
- <view class="home-header-tips-item" @click="toCouponRedemptionList('1')">
- <view class="home-header-tips-item-num">
- {{ couponSituationData?.quantityToBeUsed || 0 }}张
- </view>
- <view class="home-header-tips-item-des">
- 未核销
- </view>
- </view>
- <view class="home-header-tips-item">
- <view class="home-header-tips-item-num">
- {{ couponSituationData?.quantityForComplimentary || 0 }}张
- </view>
- <view class="home-header-tips-item-des">
- 已发放
- </view>
- </view>
- </view>
- <view v-if="!hasLogin" class="home-hidden" @click="login">
- <image class="home-hidden-img" :src="getImageUrl('@img/index/lock.png')" mode="scaleToFill" />
- <view class="home-hidden-text">
- 请登录,查看更多内容~
- </view>
- </view>
- </view>
- <!-- 满减券 -->
- <view class="home-header-coupon">
- <image class="home-header-coupon-bg" :src="getImageUrl('@img/index/coupon-bg.png')" mode="aspectFill" />
- <view class="home-header-coupon-title">
- <image class="home-header-coupon-title-icon" :src="getImageUrl('@img/index/icon1.png')"
- mode="scaleToFill" />
- <view class="home-header-coupon-title-text">
- 满减券
- </view>
- <view class="home-header-coupon-title-des">
- 平台满减 乐享不停
- </view>
- </view>
- <view class="home-header-coupon-content">
- <template v-for="item in couponList" :key="item.id">
- <spend-and-save-coupon :coupon="item" />
- </template>
- <up-loading-icon v-if="loading" class="coupon-content-loading" text="加载中" text-size="18" />
- </view>
- <view class="home-header-coupon-btn">
- <up-button class="home-header-coupon-btn-text" text="查看更多优惠券"
- color="linear-gradient(0deg, #FFE8CE 0%, #FBB8A0 100%)" @click="toSpendAndSaveCouponList" />
- </view>
- </view>
- <!-- 折扣券 -->
- <view class="home-coupon">
- <view class="home-coupon-title">
- <image class="home-coupon-title-icon" :src="getImageUrl('@img/index/icon2.png')"
- mode="scaleToFill" />
- <view class="home-coupon-title-text">
- 折扣券
- </view>
- <view class="home-coupon-title-des">
- 分享折扣 立享优惠
- </view>
- <view class="home-coupon-title-more" @click="toDiscountCouponList">
- <view class="home-coupon-title-more-text">
- 更多
- </view>
- <up-icon size="14" name="arrow-right" />
- </view>
- </view>
- <view class="home-coupon-content">
- <template v-for="item in discountVoucherList" :key="item.id">
- <discount-coupon :coupon="item" />
- </template>
- <up-loading-icon v-if="loading" text="加载中" text-size="18" />
- </view>
- </view>
- </up-pull-refresh>
- </view>
- </template>
- <style lang="scss" scoped>
- .home-container {
- background-color: #f5f5f5;
- line-height: 1;
- position: relative;
- .home-header {
- height: 550rpx;
- position: relative;
- overflow: hidden;
- .home-header-bg {
- position: absolute;
- top: 0;
- left: 0;
- width: 125%;
- height: 200%;
- object-fit: cover;
- transform: translate(-80px, 0);
- z-index: 0;
- }
- .home-header-avatar-info {
- display: flex;
- flex-direction: column;
- justify-content: center;
- text-align: center;
- gap: 30rpx;
- padding: 0 24rpx;
- position: relative;
- z-index: 1;
- .home-header-balance {
- font-weight: 400;
- font-size: 26rpx;
- color: #ffffff;
- }
- .home-header-balance-num {
- display: flex;
- justify-content: space-between;
- align-items: center;
- .home-header-balance-num-amount {
- width: 100%;
- font-weight: 500;
- font-size: 65rpx;
- color: #ffffff;
- }
- .home-header-balance-num-btns {
- display: flex;
- gap: 15rpx;
- .home-header-balance-num-btn {
- padding: 20rpx 40rpx;
- border-radius: 33rpx;
- font-weight: 400;
- font-size: 26rpx;
- &.js {
- background: #da4c47;
- color: #ffffff;
- }
- &.tx {
- background: #bfbfbf;
- color: #747474;
- }
- }
- }
- }
- }
- .home-header-tips {
- height: 124rpx;
- display: flex;
- border-radius: 10rpx;
- margin: 47rpx 24rpx 0 24rpx;
- position: relative;
- z-index: 1;
- .home-header-tips-item {
- flex: 1;
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- gap: 15rpx;
- font-weight: 500;
- font-size: 34rpx;
- color: #ffffff;
- line-height: 1;
- position: relative;
- &:not(:last-child):after {
- content: '';
- position: absolute;
- top: 50%;
- right: 0;
- transform: translateY(-50%);
- width: 2px;
- height: 40rpx;
- background: #ffffff;
- }
- .home-header-tips-item-num {
- font-weight: bold;
- }
- .home-header-tips-item-des {
- font-weight: 400;
- font-size: 24rpx;
- color: #ffffff;
- }
- }
- }
- .home-hidden {
- height: 550rpx;
- width: 100%;
- position: absolute;
- top: 0;
- right: 0;
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- background-color: rgba(255, 255, 255, 0.1);
- backdrop-filter: blur(25rpx);
- -webkit-backdrop-filter: blur(25rpx);
- z-index: 2;
- .home-hidden-img {
- width: 209rpx;
- height: 209rpx;
- object-fit: cover;
- }
- .home-hidden-text {
- font-weight: 400;
- font-size: 26rpx;
- color: #ffffff;
- margin-top: -25rpx;
- }
- }
- }
- // 优惠券
- .home-header-coupon {
- position: relative;
- height: 419rpx;
- margin: -58rpx 20rpx 20rpx 20rpx;
- gap: 20rpx;
- padding: 28rpx 20rpx;
- overflow: hidden;
- z-index: 3;
- .home-header-coupon-bg {
- position: absolute;
- top: 0;
- right: 0;
- width: 100%;
- object-fit: cover;
- z-index: 0;
- }
- .home-header-coupon-title {
- display: flex;
- flex-direction: row;
- position: relative;
- z-index: 1;
- .home-header-coupon-title-icon {
- width: 38rpx;
- height: 38rpx;
- object-fit: cover;
- margin-right: 13rpx;
- }
- .home-header-coupon-title-text {
- font-weight: 500;
- font-size: 30rpx;
- color: #333333;
- margin-right: 21rpx;
- }
- .home-header-coupon-title-des {
- display: inline-flex;
- align-items: center;
- font-size: 24rpx;
- font-weight: 400;
- color: #888888;
- }
- }
- .home-header-coupon-content {
- height: 238rpx;
- display: flex;
- flex-direction: row;
- align-items: flex-end;
- gap: 20rpx;
- position: relative;
- z-index: 1;
- .hidden-share-btn {
- /* 隐藏按钮但保持可点击 */
- position: absolute;
- left: -9999px;
- top: -9999px;
- width: 0;
- height: 0;
- opacity: 0;
- pointer-events: none;
- }
- .coupon-content-loading {
- align-self: center;
- }
- }
- .home-header-coupon-btn {
- padding: 21rpx 70rpx 22px 69rpx;
- position: relative;
- .home-header-coupon-btn-text {
- box-shadow: 0rpx 4rpx 7rpx 0rpx rgba(230, 77, 13, 0.17);
- border-radius: 37rpx;
- color: #651e03 !important;
- font-size: 26rpx;
- font-weight: 400;
- }
- }
- }
- // 折扣券
- .home-coupon {
- margin: -20rpx 20rpx 20rpx 20rpx;
- padding: 28rpx 20rpx;
- background-color: #ffffff;
- border-radius: 15rpx;
- .home-coupon-title {
- display: flex;
- flex-direction: row;
- .home-coupon-title-icon {
- width: 38rpx;
- height: 38rpx;
- object-fit: cover;
- margin-right: 13rpx;
- }
- .home-coupon-title-text {
- font-weight: 500;
- font-size: 30rpx;
- color: #333333;
- margin-right: 21rpx;
- }
- .home-coupon-title-des {
- display: inline-flex;
- align-items: center;
- font-size: 24rpx;
- font-weight: 400;
- color: #888888;
- }
- .home-coupon-title-more {
- margin-left: auto;
- display: flex;
- flex-direction: row;
- align-items: center;
- .home-coupon-title-more-text {
- font-weight: 400;
- font-size: 24rpx;
- color: #666666;
- height: 23rpx;
- line-height: 23rpx;
- }
- }
- }
- .home-coupon-content {
- padding-top: 28rpx;
- display: flex;
- flex-direction: column;
- gap: 20rpx;
- }
- }
- }
- </style>
|