| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <template>
- <!-- 优惠券页面 -->
- <view class="page">
- <!-- 标签页 -->
- <view class="tabs">
- <view v-for="(tab, index) in tabs" :key="index"
- :class="['tab-item', { 'tab-item--active': currentTab === index }]" @click="handleTabChange(index)">
- {{ tab }}
- </view>
- </view>
- <!-- 优惠券列表 -->
- <scroll-view class="content" scroll-y>
- <view class="coupon-list" v-if="filteredCoupons.length > 0">
- <view v-for="(coupon, index) in filteredCoupons" :key="coupon.id || index"
- :class="['coupon-card', getCouponCardClass(coupon)]">
- <image :src="getFileUrl('img/personal/coupon.png')" mode="widthFix" class="coupon-card-bg"></image>
- <image :src="getFileUrl('img/personal/couponLeft.png')" mode="heightFix" class="coupon-card-bgLeft"></image>
- <view class="coupon-card-content">
- <!-- 左侧金额区域 -->
- <view class="coupon-card__left">
- <view class="amount-wrapper">
- <text class="amount-number">{{ coupon.amount }}</text>
- <text class="amount-unit">{{ coupon.amountUnit || '元' }}</text>
- </view>
- <text class="condition-text">{{ coupon.conditionText || (coupon.minAmount > 0 ? '满' + coupon.minAmount + '可用' : '无门槛') }}</text>
- </view>
- <!-- 右侧信息区域 -->
- <view class="coupon-card__right">
- <view class="coupon-info">
- <text class="coupon-name">{{ coupon.name }}</text>
- <view class="coupon-rules" @click="handleShowRules(coupon)">
- 使用规则
- <text class="arrow">›</text>
- </view>
- <text class="coupon-expire">{{ coupon.expireDate ? coupon.expireDate + '到期' : '' }}</text>
- </view>
- </view>
- </view>
- </view>
- </view>
- <!-- 空状态 -->
- <view class="empty-state" v-else>
- <image :src="getFileUrl('img/icon/noCoupon.png')" mode="widthFix" class="empty-icon"></image>
- <text class="empty-text">暂无优惠券</text>
- </view>
- </scroll-view>
- <!-- 使用规则弹窗 -->
- <RulesModal v-model:open="showRulesModal" :couponData="selectedCoupon" />
- </view>
- </template>
- <script setup>
- import { onShow } from "@dcloudio/uni-app";
- import { ref, computed } from "vue";
- import { go } from "@/utils/utils.js";
- import { getFileUrl } from "@/utils/file.js";
- import RulesModal from "./components/RulesModal.vue";
- import * as diningApi from "@/api/dining.js";
- // 标签页:0未使用 1即将过期 2已使用 3已过期
- const tabs = ['未使用', '即将过期', '已使用', '已过期'];
- const currentTab = ref(0);
- // 弹窗控制
- const showRulesModal = ref(false);
- const selectedCoupon = ref({
- amount: 0,
- minAmount: 0,
- name: '',
- expireDate: '',
- specifiedDay: '',
- supplementaryInstruction: ''
- });
- // 优惠券数据(接口返回)
- const couponList = ref([]);
- const loading = ref(false);
- // 规范化接口返回的优惠券项
- function normalizeCouponItem(raw) {
- if (!raw || typeof raw !== 'object') return null;
- const couponType = Number(raw.couponType) ?? 1;
- const nominalValue = Number(raw.nominalValue ?? raw.amount ?? 0) || 0;
- const discountRate = ((Number(raw.discountRate ?? 0) || 0) / 10) || 0;
- const minAmount = Number(raw.minimumSpendingAmount ?? raw.minAmount ?? 0) || 0;
- let amount = nominalValue;
- let amountUnit = '元';
- let conditionText = minAmount > 0 ? `满${minAmount}可用` : '无门槛';
- if (couponType === 2 && discountRate > 0) {
- amount = discountRate;
- amountUnit = '折';
- conditionText = minAmount > 0 ? `满${minAmount}可用` : '无门槛';
- }
- return {
- id: raw.id ?? raw.userCouponId ?? raw.couponId ?? '',
- amount,
- amountUnit,
- minAmount,
- name: raw.name ?? raw.title ?? raw.couponName ?? '',
- expireDate: raw.expirationTime ?? raw.endGetDate ?? raw.expireDate ?? '',
- status: currentTab.value,
- couponType,
- nominalValue,
- discountRate,
- conditionText,
- specifiedDay: raw.specifiedDay ?? raw.validDays ?? '',
- supplementaryInstruction: raw.supplementaryInstruction ?? raw.description ?? ''
- };
- }
- // 优惠券列表
- const filteredCoupons = computed(() => couponList.value);
- // 切换标签页
- const handleTabChange = (index) => {
- currentTab.value = index;
- fetchCouponList();
- };
- // 拉取优惠券列表(coupon/getUserCouponList)
- const fetchCouponList = async () => {
- loading.value = true;
- try {
- const storeId = uni.getStorageSync('currentStoreId') || '';
- const res = await diningApi.GetUserCouponList({ storeId, tabType: currentTab.value, page: 1, size: 20 });
- const list = Array.isArray(res) ? res : (res?.data ?? res?.records ?? res?.list ?? []);
- const arr = Array.isArray(list) ? list : [];
- couponList.value = arr.map(normalizeCouponItem).filter(Boolean);
- } catch (err) {
- console.error('获取优惠券列表失败:', err);
- uni.showToast({ title: '加载失败', icon: 'none' });
- couponList.value = [];
- } finally {
- loading.value = false;
- }
- };
- // 获取优惠券卡片样式类
- const getCouponCardClass = (coupon) => {
- if (coupon?.status === 2) return 'coupon-card--used';
- if (coupon?.status === 3) return 'coupon-card--expired';
- return '';
- };
- // 查看使用规则
- const handleShowRules = (coupon) => {
- selectedCoupon.value = { ...coupon };
- showRulesModal.value = true;
- };
- // 使用 onShow 拉取数据(onLoad 在 uni-app Vue3 组合式 API 下可能不触发,onShow 更可靠)
- onShow(() => {
- fetchCouponList();
- });
- </script>
- <style lang="scss" scoped>
- .page {
- height: 100vh;
- min-height: 100vh;
- background: #F5F5F5;
- display: flex;
- flex-direction: column;
- overflow: hidden;
- }
- .header {
- background: #FFFFFF;
- padding: 20rpx 30rpx;
- padding-top: calc(20rpx + env(safe-area-inset-top));
- .header-title {
- font-size: 36rpx;
- font-weight: bold;
- color: #151515;
- text-align: center;
- }
- }
- .tabs {
- flex-shrink: 0;
- background: #FFFFFF;
- display: flex;
- align-items: center;
- padding: 0 30rpx;
- .tab-item {
- flex: 1;
- text-align: center;
- font-size: 28rpx;
- color: #666666;
- padding: 28rpx 0;
- position: relative;
- transition: all 0.3s;
- &--active {
- color: #151515;
- font-weight: bold;
- &::after {
- content: '';
- position: absolute;
- bottom: 20rpx;
- left: 50%;
- transform: translateX(-50%);
- width: 40rpx;
- height: 6rpx;
- background: linear-gradient(90deg, #FF8A57 0%, #F47D1F 100%);
- border-radius: 3rpx;
- }
- }
- }
- }
- .content {
- flex: 1;
- min-height: 0;
- padding: 24rpx 30rpx;
- padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
- box-sizing: border-box;
- }
- .coupon-list {
- position: relative;
- .coupon-card-bg {
- position: absolute;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- z-index: 1;
- }
- .coupon-card-bgLeft {
- position: absolute;
- top: 0;
- left: 0;
- width: auto;
- height: 190rpx;
- z-index: 1;
- }
- .coupon-card-content {
- position: relative;
- z-index: 3;
- display: flex;
- align-items: center;
- }
- .coupon-card {
- display: flex;
- align-items: center;
- margin-bottom: 24rpx;
- overflow: hidden;
- position: relative;
- box-sizing: border-box;
- position: relative;
- z-index: 3;
- &:last-child {
- margin-bottom: 0;
- }
- &__left {
- width: 200rpx;
- padding: 32rpx 0;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- .amount-wrapper {
- display: flex;
- align-items: baseline;
- margin-bottom: 8rpx;
- .amount-number {
- font-size: 64rpx;
- font-weight: bold;
- color: #F47D1F;
- line-height: 1;
- }
- .amount-unit {
- font-size: 28rpx;
- color: #F47D1F;
- margin-left: 4rpx;
- }
- }
- .condition-text {
- font-size: 22rpx;
- color: #F47D1F;
- }
- }
- &__divider {
- width: 2rpx;
- height: 100%;
- position: relative;
- .dash-line {
- width: 2rpx;
- height: 100%;
- // background-image: linear-gradient(to bottom, #FFD9C2 0%, #FFD9C2 50%, transparent 50%, transparent 100%);
- background-size: 2rpx 12rpx;
- background-repeat: repeat-y;
- }
- }
- &__right {
- flex: 1;
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 32rpx 24rpx;
- .coupon-info {
- flex: 1;
- display: flex;
- flex-direction: column;
- .coupon-name {
- font-size: 28rpx;
- font-weight: bold;
- color: #151515;
- margin-bottom: 12rpx;
- }
- .coupon-rules {
- font-size: 22rpx;
- color: #999999;
- margin-bottom: 12rpx;
- display: flex;
- align-items: center;
- .arrow {
- font-size: 28rpx;
- margin-left: 4rpx;
- }
- }
- .coupon-expire {
- font-size: 22rpx;
- color: #999999;
- }
- }
- }
- // 已使用状态
- &--used {
- background: #F8F8F8;
- .coupon-card__left {
- .amount-number,
- .amount-unit,
- .condition-text {
- color: #CCCCCC;
- }
- }
- .coupon-card__right {
- .coupon-info {
- .coupon-name,
- .coupon-rules,
- .coupon-expire {
- color: #CCCCCC;
- }
- }
- }
- }
- // 已过期状态
- &--expired {
- background: #F8F8F8;
- .coupon-card__left {
- .amount-number,
- .amount-unit,
- .condition-text {
- color: #CCCCCC;
- }
- }
- .coupon-card__right {
- .coupon-info {
- .coupon-name,
- .coupon-rules,
- .coupon-expire {
- color: #CCCCCC;
- }
- }
- }
- }
- }
- }
- .hover-active {
- opacity: 0.8;
- transform: scale(0.98);
- }
- .empty-state {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding-top: 200rpx;
- .empty-icon {
- width: 300rpx;
- height: 280rpx;
- margin-bottom: 40rpx;
- }
- .empty-text {
- font-size: 28rpx;
- color: #AAAAAA;
- }
- }
- </style>
|