解决优惠券获取链路报错
This commit is contained in:
@@ -1,15 +1,25 @@
|
|||||||
<template>
|
<template>
|
||||||
<view class="coupons-page">
|
<view class="coupons-page">
|
||||||
<view class="coupon-list">
|
<scroll-view class="coupon-list" :scroll-y="true">
|
||||||
<view v-if="coupons.length === 0" class="empty-state">
|
<view v-if="loading" class="empty-state">
|
||||||
|
<text class="empty-text">加载中...</text>
|
||||||
|
</view>
|
||||||
|
|
||||||
|
<view v-else-if="loadError" class="empty-state">
|
||||||
|
<text class="empty-icon">⚠️</text>
|
||||||
|
<text class="empty-text">优惠券加载失败</text>
|
||||||
|
<button class="retry-btn" @click="loadCoupons">重新加载</button>
|
||||||
|
</view>
|
||||||
|
|
||||||
|
<view v-else-if="coupons.length === 0" class="empty-state">
|
||||||
<text class="empty-icon">🎫</text>
|
<text class="empty-icon">🎫</text>
|
||||||
<text class="empty-text">暂无优惠券</text>
|
<text class="empty-text">暂无优惠券</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view v-else v-for="(coupon, index) in coupons" :key="index" class="coupon-item">
|
<view v-else v-for="coupon in coupons" :key="coupon.id" class="coupon-item">
|
||||||
<view class="coupon-left">
|
<view class="coupon-left">
|
||||||
<text class="coupon-amount">{{ coupon.amount }}</text>
|
<text class="coupon-amount">{{ coupon.displayAmount }}</text>
|
||||||
<text class="coupon-type">优惠券</text>
|
<text class="coupon-type">{{ coupon.typeLabel }}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="coupon-right">
|
<view class="coupon-right">
|
||||||
<text class="coupon-title">{{ coupon.title }}</text>
|
<text class="coupon-title">{{ coupon.title }}</text>
|
||||||
@@ -17,7 +27,7 @@
|
|||||||
<button class="use-btn" @click="useCoupon(coupon)">去使用</button>
|
<button class="use-btn" @click="useCoupon(coupon)">去使用</button>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</scroll-view>
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -28,28 +38,45 @@ import type { UserCoupon } from '@/utils/supabaseService.uts'
|
|||||||
|
|
||||||
type Coupon = {
|
type Coupon = {
|
||||||
title: string
|
title: string
|
||||||
amount: string
|
displayAmount: string
|
||||||
|
typeLabel: string
|
||||||
expiry: string
|
expiry: string
|
||||||
id: string
|
id: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const coupons = ref<Coupon[]>([])
|
const coupons = ref<Coupon[]>([])
|
||||||
|
const loading = ref<boolean>(true)
|
||||||
|
const loadError = ref<boolean>(false)
|
||||||
|
|
||||||
const loadCoupons = async () => {
|
const loadCoupons = async () => {
|
||||||
uni.showLoading({ title: '加载中...' })
|
loading.value = true
|
||||||
|
loadError.value = false
|
||||||
try {
|
try {
|
||||||
const userCoupons = await supabaseService.getUserCoupons(1)
|
const userCoupons = await supabaseService.getUserCoupons(1)
|
||||||
const couponList: Coupon[] = []
|
const couponList: Coupon[] = []
|
||||||
for (let i = 0; i < userCoupons.length; i++) {
|
for (let i = 0; i < userCoupons.length; i++) {
|
||||||
const item = userCoupons[i]
|
const item = userCoupons[i]
|
||||||
const amountVal = item.amount ?? 0
|
const amountVal = item.amount ?? 0
|
||||||
|
const discountType = item.discount_type ?? 1
|
||||||
const expiryVal = (item.expire_at != null && item.expire_at !== '')
|
const expiryVal = (item.expire_at != null && item.expire_at !== '')
|
||||||
? item.expire_at.substring(0, 10)
|
? item.expire_at.substring(0, 10)
|
||||||
: '长期有效'
|
: '长期有效'
|
||||||
|
|
||||||
|
let displayAmount = ''
|
||||||
|
let typeLabel = '优惠券'
|
||||||
|
if (discountType == 2) {
|
||||||
|
displayAmount = Math.round(amountVal * 100) / 10 + '折'
|
||||||
|
typeLabel = '折扣券'
|
||||||
|
} else {
|
||||||
|
displayAmount = '¥' + amountVal.toString()
|
||||||
|
typeLabel = '优惠券'
|
||||||
|
}
|
||||||
|
|
||||||
const coupon: Coupon = {
|
const coupon: Coupon = {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
title: (item.template_name != null && item.template_name !== '') ? item.template_name : '优惠券',
|
title: (item.template_name != null && item.template_name !== '') ? item.template_name : '优惠券',
|
||||||
amount: `¥${amountVal}`,
|
displayAmount: displayAmount,
|
||||||
|
typeLabel: typeLabel,
|
||||||
expiry: expiryVal
|
expiry: expiryVal
|
||||||
} as Coupon
|
} as Coupon
|
||||||
couponList.push(coupon)
|
couponList.push(coupon)
|
||||||
@@ -57,9 +84,14 @@ const loadCoupons = async () => {
|
|||||||
coupons.value = couponList
|
coupons.value = couponList
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('加载优惠券失败', e)
|
console.error('加载优惠券失败', e)
|
||||||
|
loadError.value = true
|
||||||
coupons.value = []
|
coupons.value = []
|
||||||
|
uni.showToast({
|
||||||
|
title: '优惠券加载失败,请稍后重试',
|
||||||
|
icon: 'none'
|
||||||
|
})
|
||||||
} finally {
|
} finally {
|
||||||
uni.hideLoading()
|
loading.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,9 +108,15 @@ const useCoupon = (coupon: Coupon) => {
|
|||||||
|
|
||||||
<style>
|
<style>
|
||||||
.coupons-page {
|
.coupons-page {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
padding: 15px;
|
padding: 15px;
|
||||||
background-color: #f5f5f5;
|
background-color: #f5f5f5;
|
||||||
flex: 1;
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
.empty-state {
|
.empty-state {
|
||||||
@@ -97,6 +135,22 @@ const useCoupon = (coupon: Coupon) => {
|
|||||||
.empty-text {
|
.empty-text {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
color: #999;
|
color: #999;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.retry-btn {
|
||||||
|
font-size: 14px;
|
||||||
|
background-color: #FF5722;
|
||||||
|
color: white;
|
||||||
|
padding: 6px 16px;
|
||||||
|
border-radius: 15px;
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.coupon-list {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.coupon-item {
|
.coupon-item {
|
||||||
|
|||||||
@@ -750,11 +750,25 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
scrollToSection(tabKey: string) {
|
scrollToSection(tabKey: string) {
|
||||||
const offsets = this.sectionOffsets
|
const sectionId = 'section-' + tabKey
|
||||||
if (offsets == null) return
|
const query = uni.createSelectorQuery()
|
||||||
const offset = offsets.getNumber(tabKey)
|
query.select('#' + sectionId).boundingClientRect()
|
||||||
if (offset != null && offset >= 0) {
|
query.select('.page-scroll').scrollOffset()
|
||||||
let target = tabKey == 'goods' ? 0 : Math.max(0, offset - this.customHeaderHeight)
|
query.exec((res: any) => {
|
||||||
|
if (res == null) return
|
||||||
|
let rect: any = null
|
||||||
|
let scrollInfo: any = null
|
||||||
|
if (Array.isArray(res)) {
|
||||||
|
rect = res[0]
|
||||||
|
scrollInfo = res[1]
|
||||||
|
}
|
||||||
|
if (rect == null || rect.top == null) return
|
||||||
|
const nodeTop = rect.top as number
|
||||||
|
const scrollTop = (scrollInfo != null && scrollInfo.scrollTop != null) ? scrollInfo.scrollTop as number : this.currentScrollTop
|
||||||
|
let target = 0
|
||||||
|
if (tabKey != 'goods') {
|
||||||
|
target = Math.max(0, nodeTop + scrollTop - this.customHeaderHeight)
|
||||||
|
}
|
||||||
this.isProgrammaticScrolling = true
|
this.isProgrammaticScrolling = true
|
||||||
this.targetScrollTop = -1
|
this.targetScrollTop = -1
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -762,12 +776,12 @@ export default {
|
|||||||
setTimeout(() => { this.isProgrammaticScrolling = false }, 400)
|
setTimeout(() => { this.isProgrammaticScrolling = false }, 400)
|
||||||
}, 20)
|
}, 20)
|
||||||
this.activeTab = tabKey
|
this.activeTab = tabKey
|
||||||
}
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
measureSections() {
|
measureSections() {
|
||||||
const sectionIds = ['section-goods', 'section-comments', 'section-detail', 'section-recommend']
|
const sectionIds = ['section-goods', 'section-comments', 'section-detail', 'section-recommend']
|
||||||
const query = uni.createSelectorQuery().in(this)
|
const query = uni.createSelectorQuery()
|
||||||
for (let i = 0; i < sectionIds.length; i++) {
|
for (let i = 0; i < sectionIds.length; i++) {
|
||||||
query.select('#' + sectionIds[i]).boundingClientRect()
|
query.select('#' + sectionIds[i]).boundingClientRect()
|
||||||
}
|
}
|
||||||
@@ -782,7 +796,6 @@ export default {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.sectionOffsets = offsets
|
this.sectionOffsets = offsets
|
||||||
this.customHeaderHeight = this.statusBarHeight + this.headerMainRowHeight + this.headerTabsRowHeight
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -387,19 +387,12 @@ export type UserAddress = {
|
|||||||
|
|
||||||
export type UserCoupon = {
|
export type UserCoupon = {
|
||||||
id: string
|
id: string
|
||||||
user_id: string
|
template_name: string
|
||||||
template_id: string
|
amount: number
|
||||||
coupon_code: string
|
min_spend: number
|
||||||
status: number // 1: unused, 2: used, 3: expired
|
|
||||||
received_at: string
|
|
||||||
expire_at: string
|
expire_at: string
|
||||||
used_at?: string
|
status: number
|
||||||
// join fields from template or view
|
discount_type?: number
|
||||||
template_name?: string
|
|
||||||
amount?: number
|
|
||||||
min_spend?: number
|
|
||||||
name?: string
|
|
||||||
title?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ChatRoom = {
|
export type ChatRoom = {
|
||||||
@@ -7714,12 +7707,9 @@ class SupabaseService {
|
|||||||
return empty
|
return empty
|
||||||
}
|
}
|
||||||
|
|
||||||
// 假设有一个视图或者直接关联 ml_user_coupons 和 ml_coupon_templates
|
|
||||||
// 这里简化处理,尝试直接从 ml_user_coupons 读取,并且加入 template 信息
|
|
||||||
// 如果没有 view,可能需要改为两个查询或者使用 left join
|
|
||||||
const response = await supa
|
const response = await supa
|
||||||
.from('ml_user_coupons')
|
.from('ml_user_coupons')
|
||||||
.select('*, template:ml_coupon_templates(name, amount, min_spend)')
|
.select('*, template:ml_coupon_templates(name, discount_value, min_order_amount, coupon_type, discount_type)')
|
||||||
.eq('user_id', userId!)
|
.eq('user_id', userId!)
|
||||||
.eq('status', status.toString())
|
.eq('status', status.toString())
|
||||||
.order('expire_at', { ascending: true })
|
.order('expire_at', { ascending: true })
|
||||||
@@ -7727,32 +7717,23 @@ class SupabaseService {
|
|||||||
|
|
||||||
if (response.error != null) {
|
if (response.error != null) {
|
||||||
console.error('获取优惠券失败:', response.error)
|
console.error('获取优惠券失败:', response.error)
|
||||||
const empty: UserCoupon[] = []
|
throw new Error(response.error.message != null ? response.error.message as string : '获取优惠券失败')
|
||||||
return empty
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安全处理返回数据 - 安卓端可能是 UTSJSONObject 或 UTSArray
|
|
||||||
const rawData: any[] = []
|
const rawData: any[] = []
|
||||||
const respData = response.data
|
const respData = response.data
|
||||||
console.log('[getUserCoupons] 原始数据类型:', typeof respData, '是否数组:', Array.isArray(respData))
|
|
||||||
if (respData != null) {
|
if (respData != null) {
|
||||||
if (Array.isArray(respData)) {
|
if (Array.isArray(respData)) {
|
||||||
const arr = respData as any[]
|
const arr = respData as any[]
|
||||||
console.log('[getUserCoupons] 数组长度:', arr.length)
|
|
||||||
for (let i = 0; i < arr.length; i++) {
|
for (let i = 0; i < arr.length; i++) {
|
||||||
rawData.push(arr[i])
|
rawData.push(arr[i])
|
||||||
}
|
}
|
||||||
} else if (respData instanceof UTSJSONObject) {
|
} else if (respData instanceof UTSJSONObject) {
|
||||||
// 单个对象情况,包装成数组
|
|
||||||
console.log('[getUserCoupons] 单个对象,包装成数组')
|
|
||||||
rawData.push(respData)
|
rawData.push(respData)
|
||||||
} else {
|
} else {
|
||||||
// 尝试 JSON 转换
|
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(JSON.stringify(respData))
|
const parsed = JSON.parse(JSON.stringify(respData))
|
||||||
console.log('[getUserCoupons] JSON转换后是否数组:', Array.isArray(parsed))
|
|
||||||
if (Array.isArray(parsed)) {
|
if (Array.isArray(parsed)) {
|
||||||
console.log('[getUserCoupons] 转换后数组长度:', parsed.length)
|
|
||||||
for (let i = 0; i < parsed.length; i++) {
|
for (let i = 0; i < parsed.length; i++) {
|
||||||
rawData.push(parsed[i])
|
rawData.push(parsed[i])
|
||||||
}
|
}
|
||||||
@@ -7762,39 +7743,22 @@ class SupabaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('[getUserCoupons] 最终rawData长度:', rawData.length)
|
|
||||||
|
|
||||||
// 映射数据,将 template 的字段展平
|
|
||||||
const coupons: UserCoupon[] = []
|
const coupons: UserCoupon[] = []
|
||||||
for (let i = 0; i < rawData.length; i++) {
|
for (let i = 0; i < rawData.length; i++) {
|
||||||
const item = rawData[i]
|
const item = rawData[i]
|
||||||
let template: any | null = null
|
let template: any | null = null
|
||||||
let itemId = ''
|
let itemId = ''
|
||||||
let itemUserId = ''
|
|
||||||
let itemTmplId = ''
|
|
||||||
let itemCode = ''
|
|
||||||
let itemStatus = 0
|
|
||||||
let itemRecv = ''
|
|
||||||
let itemExpire = ''
|
let itemExpire = ''
|
||||||
|
|
||||||
if (item instanceof UTSJSONObject) {
|
if (item instanceof UTSJSONObject) {
|
||||||
template = item.get('template') as any | null
|
template = item.get('template') as any | null
|
||||||
itemId = item.getString('id') ?? ''
|
itemId = item.getString('id') ?? ''
|
||||||
itemUserId = item.getString('user_id') ?? ''
|
|
||||||
itemTmplId = item.getString('template_id') ?? ''
|
|
||||||
itemCode = item.getString('coupon_code') ?? ''
|
|
||||||
itemStatus = item.getNumber('status') ?? 0
|
|
||||||
itemRecv = item.getString('received_at') ?? ''
|
|
||||||
itemExpire = item.getString('expire_at') ?? ''
|
itemExpire = item.getString('expire_at') ?? ''
|
||||||
} else {
|
} else {
|
||||||
const iObj = JSON.parse(JSON.stringify(item)) as UTSJSONObject
|
const iObj = JSON.parse(JSON.stringify(item)) as UTSJSONObject
|
||||||
template = iObj.get('template') as any | null
|
template = iObj.get('template') as any | null
|
||||||
itemId = iObj.getString('id') ?? ''
|
itemId = iObj.getString('id') ?? ''
|
||||||
itemUserId = iObj.getString('user_id') ?? ''
|
|
||||||
itemTmplId = iObj.getString('template_id') ?? ''
|
|
||||||
itemCode = iObj.getString('coupon_code') ?? ''
|
|
||||||
itemStatus = iObj.getNumber('status') ?? 0
|
|
||||||
itemRecv = iObj.getString('received_at') ?? ''
|
|
||||||
itemExpire = iObj.getString('expire_at') ?? ''
|
itemExpire = iObj.getString('expire_at') ?? ''
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -7803,30 +7767,29 @@ class SupabaseService {
|
|||||||
let tName = ''
|
let tName = ''
|
||||||
let tAmount = 0
|
let tAmount = 0
|
||||||
let tMin = 0
|
let tMin = 0
|
||||||
|
let tDiscountType = 1
|
||||||
|
|
||||||
if (template instanceof UTSJSONObject) {
|
if (template instanceof UTSJSONObject) {
|
||||||
tName = template.getString('name') ?? '优惠券'
|
tName = template.getString('name') ?? '优惠券'
|
||||||
tAmount = template.getNumber('amount') ?? 0
|
tAmount = template.getNumber('discount_value') ?? 0
|
||||||
tMin = template.getNumber('min_spend') ?? 0
|
tMin = template.getNumber('min_order_amount') ?? 0
|
||||||
|
tDiscountType = template.getNumber('discount_type') ?? 1
|
||||||
} else {
|
} else {
|
||||||
const tObj = JSON.parse(JSON.stringify(template)) as UTSJSONObject
|
const tObj = JSON.parse(JSON.stringify(template)) as UTSJSONObject
|
||||||
tName = tObj.getString('name') ?? '优惠券'
|
tName = tObj.getString('name') ?? '优惠券'
|
||||||
tAmount = tObj.getNumber('amount') ?? 0
|
tAmount = tObj.getNumber('discount_value') ?? 0
|
||||||
tMin = tObj.getNumber('min_spend') ?? 0
|
tMin = tObj.getNumber('min_order_amount') ?? 0
|
||||||
|
tDiscountType = tObj.getNumber('discount_type') ?? 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建真正的 UserCoupon 对象,而不是 UTSJSONObject
|
|
||||||
const couponItem: UserCoupon = {
|
const couponItem: UserCoupon = {
|
||||||
id: itemId,
|
id: itemId,
|
||||||
user_id: itemUserId,
|
|
||||||
template_id: itemTmplId,
|
|
||||||
coupon_code: itemCode,
|
|
||||||
status: itemStatus,
|
|
||||||
received_at: itemRecv,
|
|
||||||
expire_at: itemExpire,
|
|
||||||
template_name: tName,
|
template_name: tName,
|
||||||
amount: tAmount,
|
amount: tAmount,
|
||||||
min_spend: tMin
|
min_spend: tMin,
|
||||||
|
expire_at: itemExpire,
|
||||||
|
status: status,
|
||||||
|
discount_type: tDiscountType
|
||||||
}
|
}
|
||||||
|
|
||||||
coupons.push(couponItem)
|
coupons.push(couponItem)
|
||||||
@@ -7835,8 +7798,7 @@ class SupabaseService {
|
|||||||
return coupons
|
return coupons
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('获取优惠券异常:', e)
|
console.error('获取优惠券异常:', e)
|
||||||
const empty: UserCoupon[] = []
|
throw e
|
||||||
return empty
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user