feat: 初次提交我的项目代码
This commit is contained in:
590
pages/mall/consumer/coupons.uvue
Normal file
590
pages/mall/consumer/coupons.uvue
Normal file
@@ -0,0 +1,590 @@
|
||||
<!-- 优惠券页面 -->
|
||||
<template>
|
||||
<view class="coupons-page">
|
||||
<!-- 顶部栏 -->
|
||||
<view class="coupons-header">
|
||||
<view class="header-tabs">
|
||||
<view :class="['header-tab', { active: activeTab === 'available' }]" @click="changeTab('available')">
|
||||
<text class="tab-text">可用券</text>
|
||||
</view>
|
||||
<view :class="['header-tab', { active: activeTab === 'unavailable' }]" @click="changeTab('unavailable')">
|
||||
<text class="tab-text">已失效</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 优惠券列表 -->
|
||||
<scroll-view class="coupons-list" scroll-y>
|
||||
<!-- 为空提示 -->
|
||||
<view v-if="coupons.length === 0" class="empty-coupons">
|
||||
<text class="empty-icon">🎫</text>
|
||||
<text class="empty-text">{{ getEmptyText() }}</text>
|
||||
<text class="empty-subtext">{{ getEmptySubtext() }}</text>
|
||||
<button v-if="activeTab === 'available'" class="get-coupons-btn" @click="goToCouponCenter">
|
||||
去领券中心
|
||||
</button>
|
||||
</view>
|
||||
|
||||
<!-- 优惠券项 -->
|
||||
<view v-for="coupon in coupons" :key="coupon.id" class="coupon-item">
|
||||
<view class="coupon-left">
|
||||
<text class="coupon-value">{{ formatCouponValue(coupon) }}</text>
|
||||
<text class="coupon-condition">{{ formatCondition(coupon) }}</text>
|
||||
</view>
|
||||
<view class="coupon-right">
|
||||
<view class="coupon-info">
|
||||
<text class="coupon-name">{{ coupon.template?.name || coupon.name }}</text>
|
||||
<text class="coupon-desc">{{ coupon.template?.description || '' }}</text>
|
||||
<text class="coupon-time">{{ formatTimeRange(coupon) }}</text>
|
||||
</view>
|
||||
<view class="coupon-actions">
|
||||
<button v-if="coupon.status === 1 && coupon.is_valid"
|
||||
class="use-btn"
|
||||
@click="useCoupon(coupon)">
|
||||
立即使用
|
||||
</button>
|
||||
<button v-else-if="coupon.template_id && activeTab === 'available'"
|
||||
class="receive-btn"
|
||||
@click="receiveCoupon(coupon)">
|
||||
领取
|
||||
</button>
|
||||
<text v-else class="status-text">{{ getStatusText(coupon) }}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</scroll-view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import { ref, onMounted, watch } from 'vue'
|
||||
import supa from '@/components/supadb/aksupainstance.uts'
|
||||
|
||||
type CouponTemplateType = {
|
||||
id: string
|
||||
name: string
|
||||
description: string | null
|
||||
coupon_type: number
|
||||
discount_value: number
|
||||
min_order_amount: number
|
||||
start_time: string
|
||||
end_time: string
|
||||
per_user_limit: number
|
||||
total_quantity: number
|
||||
used_quantity: number
|
||||
status: number
|
||||
}
|
||||
|
||||
type UserCouponType = {
|
||||
id: string
|
||||
user_id: string
|
||||
template_id: string
|
||||
coupon_code: string
|
||||
status: number
|
||||
is_valid: boolean
|
||||
used_at: string | null
|
||||
expire_at: string
|
||||
created_at: string
|
||||
template: CouponTemplateType | null
|
||||
}
|
||||
|
||||
const activeTab = ref<string>('available')
|
||||
const coupons = ref<Array<any>>([])
|
||||
const isLoading = ref<boolean>(false)
|
||||
|
||||
// 监听标签页变化
|
||||
watch(activeTab, () => {
|
||||
loadCoupons()
|
||||
})
|
||||
|
||||
// 生命周期
|
||||
onMounted(() => {
|
||||
loadCoupons()
|
||||
})
|
||||
|
||||
// 加载优惠券
|
||||
const loadCoupons = async () => {
|
||||
const userId = getCurrentUserId()
|
||||
if (!userId) {
|
||||
uni.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none'
|
||||
})
|
||||
uni.navigateTo({
|
||||
url: '/pages/user/login'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
isLoading.value = true
|
||||
|
||||
try {
|
||||
if (activeTab.value === 'available') {
|
||||
await loadAvailableCoupons(userId)
|
||||
} else {
|
||||
await loadUnavailableCoupons(userId)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('加载优惠券异常:', err)
|
||||
} finally {
|
||||
isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 加载可用优惠券
|
||||
const loadAvailableCoupons = async (userId: string) => {
|
||||
try {
|
||||
const now = new Date().toISOString()
|
||||
|
||||
// 加载用户已领取的优惠券
|
||||
const { data: userCoupons, error: userError } = await supa
|
||||
.from('user_coupons')
|
||||
.select(`
|
||||
*,
|
||||
template:coupon_templates(*)
|
||||
`)
|
||||
.eq('user_id', userId)
|
||||
.eq('status', 1)
|
||||
.gte('expire_at', now)
|
||||
.order('expire_at', { ascending: true })
|
||||
|
||||
if (userError !== null) {
|
||||
console.error('加载用户优惠券失败:', userError)
|
||||
return
|
||||
}
|
||||
|
||||
// 加载可领取的优惠券
|
||||
const { data: availableCoupons, error: availableError } = await supa
|
||||
.from('coupon_templates')
|
||||
.select('*')
|
||||
.eq('status', 1)
|
||||
.gte('end_time', now)
|
||||
.lte('start_time', now)
|
||||
.order('created_at', { ascending: false })
|
||||
|
||||
if (availableError !== null) {
|
||||
console.error('加载可领取优惠券失败:', availableError)
|
||||
return
|
||||
}
|
||||
|
||||
// 合并结果
|
||||
const allCoupons = [...(userCoupons || []), ...(availableCoupons || [])]
|
||||
coupons.value = allCoupons
|
||||
} catch (err) {
|
||||
console.error('加载可用优惠券异常:', err)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载不可用优惠券
|
||||
const loadUnavailableCoupons = async (userId: string) => {
|
||||
try {
|
||||
const now = new Date().toISOString()
|
||||
|
||||
const { data, error } = await supa
|
||||
.from('user_coupons')
|
||||
.select(`
|
||||
*,
|
||||
template:coupon_templates(*)
|
||||
`)
|
||||
.eq('user_id', userId)
|
||||
.or('status.eq.2,expire_at.lt.' + now)
|
||||
.order('used_at', { ascending: false })
|
||||
.order('expire_at', { ascending: false })
|
||||
|
||||
if (error !== null) {
|
||||
console.error('加载失效优惠券失败:', error)
|
||||
return
|
||||
}
|
||||
|
||||
coupons.value = data ?? []
|
||||
} catch (err) {
|
||||
console.error('加载失效优惠券异常:', err)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前用户ID
|
||||
const getCurrentUserId = (): string | null => {
|
||||
const userStore = uni.getStorageSync('userInfo')
|
||||
return userStore?.id || null
|
||||
}
|
||||
|
||||
// 获取空状态文本
|
||||
const getEmptyText = (): string => {
|
||||
return activeTab.value === 'available' ? '暂无可用优惠券' : '暂无失效优惠券'
|
||||
}
|
||||
|
||||
// 获取空状态副文本
|
||||
const getEmptySubtext = (): string => {
|
||||
return activeTab.value === 'available' ? '去领券中心看看' : '努力使用优惠券吧'
|
||||
}
|
||||
|
||||
// 格式化优惠券价值
|
||||
const formatCouponValue = (coupon: any): string => {
|
||||
if (coupon.template_id) {
|
||||
// 用户优惠券
|
||||
const template = coupon.template
|
||||
if (!template) return '未知'
|
||||
|
||||
if (template.coupon_type === 1) {
|
||||
return `¥${template.discount_value}`
|
||||
} else if (template.coupon_type === 2) {
|
||||
return `${template.discount_value}折`
|
||||
} else {
|
||||
return '未知'
|
||||
}
|
||||
} else {
|
||||
// 优惠券模板
|
||||
if (coupon.coupon_type === 1) {
|
||||
return `¥${coupon.discount_value}`
|
||||
} else if (coupon.coupon_type === 2) {
|
||||
return `${coupon.discount_value}折`
|
||||
} else {
|
||||
return '未知'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化使用条件
|
||||
const formatCondition = (coupon: any): string => {
|
||||
const minAmount = coupon.template?.min_order_amount || coupon.min_order_amount
|
||||
if (minAmount > 0) {
|
||||
return `满${minAmount}元可用`
|
||||
}
|
||||
return '无门槛'
|
||||
}
|
||||
|
||||
// 格式化时间范围
|
||||
const formatTimeRange = (coupon: any): string => {
|
||||
const startTime = coupon.template?.start_time || coupon.start_time
|
||||
const endTime = coupon.template?.end_time || coupon.expire_at
|
||||
|
||||
if (startTime && endTime) {
|
||||
const start = new Date(startTime)
|
||||
const end = new Date(endTime)
|
||||
|
||||
const startStr = `${start.getMonth() + 1}月${start.getDate()}日`
|
||||
const endStr = `${end.getMonth() + 1}月${end.getDate()}日`
|
||||
|
||||
return `${startStr}-${endStr}`
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
// 获取状态文本
|
||||
const getStatusText = (coupon: any): string => {
|
||||
if (coupon.status === 2) {
|
||||
return '已使用'
|
||||
} else if (!coupon.is_valid) {
|
||||
return '已失效'
|
||||
} else if (new Date(coupon.expire_at) < new Date()) {
|
||||
return '已过期'
|
||||
}
|
||||
return '未知'
|
||||
}
|
||||
|
||||
// 切换标签页
|
||||
const changeTab = (tab: string) => {
|
||||
activeTab.value = tab
|
||||
}
|
||||
|
||||
// 使用优惠券
|
||||
const useCoupon = (coupon: any) => {
|
||||
// 如果是从订单页面跳转过来的,返回选择的优惠券
|
||||
const pages = getCurrentPages()
|
||||
const prevPage = pages[pages.length - 2]
|
||||
|
||||
if (prevPage && prevPage.route === 'pages/mall/consumer/checkout') {
|
||||
uni.$emit('couponSelected', coupon)
|
||||
uni.navigateBack()
|
||||
return
|
||||
}
|
||||
|
||||
// 否则跳转到商品列表页
|
||||
uni.navigateTo({
|
||||
url: '/pages/mall/consumer/index'
|
||||
})
|
||||
}
|
||||
|
||||
// 领取优惠券
|
||||
const receiveCoupon = async (coupon: any) => {
|
||||
const userId = getCurrentUserId()
|
||||
if (!userId) return
|
||||
|
||||
// 检查是否已领取
|
||||
const { data: existingCoupons, error: checkError } = await supa
|
||||
.from('user_coupons')
|
||||
.select('id')
|
||||
.eq('user_id', userId)
|
||||
.eq('template_id', coupon.id)
|
||||
|
||||
if (checkError !== null) {
|
||||
console.error('检查优惠券失败:', checkError)
|
||||
return
|
||||
}
|
||||
|
||||
if (existingCoupons && existingCoupons.length >= coupon.per_user_limit) {
|
||||
uni.showToast({
|
||||
title: '已达领取上限',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 检查库存
|
||||
if (coupon.used_quantity >= coupon.total_quantity) {
|
||||
uni.showToast({
|
||||
title: '优惠券已领完',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 生成优惠券码
|
||||
const couponCode = generateCouponCode()
|
||||
const expireAt = coupon.end_time
|
||||
|
||||
try {
|
||||
const { error: insertError } = await supa
|
||||
.from('user_coupons')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
template_id: coupon.id,
|
||||
coupon_code: couponCode,
|
||||
expire_at: expireAt,
|
||||
status: 1,
|
||||
is_valid: true
|
||||
})
|
||||
|
||||
if (insertError !== null) {
|
||||
console.error('领取优惠券失败:', insertError)
|
||||
uni.showToast({
|
||||
title: '领取失败',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 更新优惠券模板的已领取数量
|
||||
const { error: updateError } = await supa
|
||||
.from('coupon_templates')
|
||||
.update({ used_quantity: coupon.used_quantity + 1 })
|
||||
.eq('id', coupon.id)
|
||||
|
||||
if (updateError !== null) {
|
||||
console.error('更新优惠券数量失败:', updateError)
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: '领取成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 重新加载数据
|
||||
loadCoupons()
|
||||
} catch (err) {
|
||||
console.error('领取优惠券异常:', err)
|
||||
}
|
||||
}
|
||||
|
||||
// 生成优惠券码
|
||||
const generateCouponCode = (): string => {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
||||
let result = ''
|
||||
for (let i = 0; i < 12; i++) {
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 跳转到领券中心
|
||||
const goToCouponCenter = () => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/mall/consumer/coupon-center'
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.coupons-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
.coupons-header {
|
||||
background-color: #ffffff;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
.header-tabs {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.header-tab {
|
||||
flex: 1;
|
||||
padding: 15px;
|
||||
text-align: center;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.header-tab.active {
|
||||
color: #007aff;
|
||||
}
|
||||
|
||||
.header-tab.active::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 2px;
|
||||
background-color: #007aff;
|
||||
}
|
||||
|
||||
.tab-text {
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
color: #666666;
|
||||
}
|
||||
|
||||
.header-tab.active .tab-text {
|
||||
color: #007aff;
|
||||
}
|
||||
|
||||
.coupons-list {
|
||||
flex: 1;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.empty-coupons {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 80px 20px;
|
||||
background-color: #ffffff;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.empty-icon {
|
||||
font-size: 80px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.empty-text {
|
||||
font-size: 16px;
|
||||
color: #666666;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.empty-subtext {
|
||||
font-size: 14px;
|
||||
color: #999999;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.get-coupons-btn {
|
||||
background-color: #007aff;
|
||||
color: #ffffff;
|
||||
padding: 10px 40px;
|
||||
border-radius: 25px;
|
||||
font-size: 14px;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.coupon-item {
|
||||
background-color: #ffffff;
|
||||
margin-bottom: 10px;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.coupon-left {
|
||||
width: 100px;
|
||||
background: linear-gradient(135deg, #ff6b6b, #ffa726);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 20px 10px;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.coupon-value {
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.coupon-condition {
|
||||
font-size: 11px;
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.coupon-right {
|
||||
flex: 1;
|
||||
padding: 15px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.coupon-info {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.coupon-name {
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
color: #333333;
|
||||
margin-bottom: 5px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.coupon-desc {
|
||||
font-size: 13px;
|
||||
color: #666666;
|
||||
margin-bottom: 8px;
|
||||
display: block;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.coupon-time {
|
||||
font-size: 12px;
|
||||
color: #999999;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.coupon-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.use-btn,
|
||||
.receive-btn {
|
||||
padding: 8px 20px;
|
||||
border-radius: 15px;
|
||||
font-size: 12px;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.use-btn {
|
||||
background-color: #007aff;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.receive-btn {
|
||||
background-color: #ff4757;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.status-text {
|
||||
font-size: 12px;
|
||||
color: #999999;
|
||||
font-style: italic;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user