vending-machine/mobile/pages/coupons/coupons.vue
18631081161 d43406380c
Some checks reported errors
continuous-integration/drone/push Build encountered an error
逻辑优化
2026-04-13 13:43:06 +08:00

292 lines
6.4 KiB
Vue

<template>
<view class="coupons-page">
<!-- 自定义导航栏 -->
<view class="custom-nav" :style="{ paddingTop: statusBarHeight + 'px' }">
<view class="nav-inner">
<view class="nav-back" @click="goBack">
<image class="back-icon" src="/static/ic_back2.png" mode="aspectFit" />
</view>
<text class="nav-title">{{ t('coupons.title') }}</text>
<view class="nav-placeholder" />
</view>
</view>
<!-- 页面内容 -->
<view :style="{ paddingTop: (statusBarHeight + 44) + 'px' }">
<!-- Tab切换 -->
<view class="tab-bar">
<view
v-for="tab in tabs"
:key="tab.value"
class="tab-item"
:class="{ active: currentTab === tab.value }"
@click="switchTab(tab.value)"
>
<text class="tab-text">{{ t(tab.label) }}</text>
<view v-if="currentTab === tab.value" class="tab-line" />
</view>
</view>
<!-- 优惠券列表 -->
<view class="coupon-list">
<view
v-for="coupon in coupons"
:key="coupon.couponId"
class="coupon-card"
>
<image
class="coupon-bg"
:src="currentTab === 'available' ? '/static/ic_coupon_bg.png' : '/static/ic_coupon_used_bg.png'"
mode="aspectFill"
/>
<view class="coupon-inner">
<view class="coupon-info">
<text class="coupon-name">{{ coupon.name }}</text>
<view class="coupon-detail-row">
<text class="coupon-dot">●</text>
<text class="coupon-detail-label">{{ t('couponCard.expireLabel') }}</text>
<text class="coupon-detail-value highlight">{{ formatExpire(coupon.expireAt) }}</text>
</view>
<view class="coupon-detail-row">
<text class="coupon-dot">●</text>
<text class="coupon-detail-label">{{ t('couponCard.conditionLabel') }}</text>
<text class="coupon-detail-value highlight">{{ coupon.pointsCost ?? 0 }}{{ t('couponCard.pointsUnit') }}</text>
</view>
</view>
<view class="coupon-action" v-if="currentTab === 'available'">
<view class="redeem-btn" @click="onUse(coupon)">
<text class="redeem-btn-text">{{ t('couponCard.redeemBtn') }}</text>
</view>
</view>
</view>
</view>
<!-- 空状态 -->
<EmptyState v-if="!loading && coupons.length === 0" text="暂无优惠券" />
</view>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { getMyCoupons } from '../../api/coupon.js'
import EmptyState from '../../components/EmptyState.vue'
const { t } = useI18n()
// 状态栏高度
const statusBarHeight = ref(0)
try {
const sysInfo = uni.getSystemInfoSync()
statusBarHeight.value = sysInfo.statusBarHeight || 0
} catch (e) {}
// Tab配置
const tabs = [
{ label: 'coupons.availableTab', value: 'available' },
{ label: 'coupons.usedTab', value: 'used' },
{ label: 'coupons.expiredTab', value: 'expired' }
]
const currentTab = ref('available')
const coupons = ref([])
const loading = ref(false)
// 返回上一页
function goBack() {
uni.navigateBack()
}
// 加载优惠券列表
async function loadCoupons() {
loading.value = true
try {
const res = await getMyCoupons(currentTab.value)
coupons.value = res.data || []
} catch (e) {
// 错误已统一处理
} finally {
loading.value = false
}
}
// 切换Tab
function switchTab(tab) {
if (currentTab.value === tab) return
currentTab.value = tab
coupons.value = []
loadCoupons()
}
// 格式化到期时间
function formatExpire(dateStr) {
if (!dateStr) return t('couponCard.noLimit')
const d = new Date(dateStr)
return `${d.getFullYear()}${d.getMonth() + 1}${d.getDate()}`
}
// 使用优惠券
function onUse(coupon) {
uni.showToast({ title: '请在购买时使用', icon: 'none' })
}
// 初始加载
loadCoupons()
</script>
<style scoped>
.coupons-page {
min-height: 100vh;
background-color: #f5f0e8;
}
/* 自定义导航栏 */
.custom-nav {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
background-color: #DBDBDB;
}
.nav-inner {
height: 44px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24rpx;
}
.nav-back {
width: 60rpx;
height: 44px;
display: flex;
align-items: center;
}
.back-icon {
width: 40rpx;
height: 40rpx;
}
.nav-title {
font-size: 34rpx;
font-weight: 600;
color: #333;
}
.nav-placeholder {
width: 60rpx;
}
/* Tab栏 */
.tab-bar {
display: flex;
background-color: #ffffff;
}
.tab-item {
flex: 1;
height: 88rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: relative;
}
.tab-text {
font-size: 28rpx;
color: #999;
}
.tab-item.active .tab-text {
color: #333;
font-weight: 500;
}
.tab-line {
position: absolute;
bottom: 0;
width: 48rpx;
height: 6rpx;
background-color: #333;
border-radius: 3rpx;
}
/* 优惠券列表 */
.coupon-list {
padding: 24rpx 24rpx;
}
.coupon-card {
position: relative;
margin-bottom: 20rpx;
border-radius: 20rpx;
overflow: hidden;
}
.coupon-bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.coupon-inner {
position: relative;
z-index: 1;
padding: 32rpx;
display: flex;
flex-direction: column;
min-height: 240rpx;
}
.coupon-info {
flex: 1;
}
.coupon-name {
font-size: 34rpx;
color: #333;
font-weight: 600;
display: block;
margin-left: 28rpx;
margin-bottom: 16rpx;
}
.coupon-detail-row {
display: flex;
align-items: center;
margin-left: 28rpx;
margin-bottom: 8rpx;
}
.coupon-dot {
font-size: 14rpx;
color: #D9DED7;
margin-right: 10rpx;
}
.coupon-detail-label {
font-size: 24rpx;
color: #666;
}
.coupon-detail-value {
font-size: 24rpx;
color: #666;
}
.coupon-detail-value.highlight {
color: #c9a96e;
font-weight: 500;
}
/* 兑换按钮 */
.coupon-action {
display: flex;
justify-content: flex-end;
margin-top: 16rpx;
}
.redeem-btn {
background: linear-gradient(135deg, #d4a855, #c9a96e);
border-radius: 28rpx;
width: 172rpx;
height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
}
.redeem-btn-text {
font-size: 28rpx;
color: #ffffff;
white-space: nowrap;
}
</style>