解决优惠券获取链路报错

This commit is contained in:
2026-06-04 21:54:16 +08:00
parent fc808bd562
commit 5d0c0ae95a
4 changed files with 123 additions and 2074 deletions

View File

@@ -387,19 +387,12 @@ export type UserAddress = {
export type UserCoupon = {
id: string
user_id: string
template_id: string
coupon_code: string
status: number // 1: unused, 2: used, 3: expired
received_at: string
template_name: string
amount: number
min_spend: number
expire_at: string
used_at?: string
// join fields from template or view
template_name?: string
amount?: number
min_spend?: number
name?: string
title?: string
status: number
discount_type?: number
}
export type ChatRoom = {
@@ -7714,12 +7707,9 @@ class SupabaseService {
return empty
}
// 假设有一个视图或者直接关联 ml_user_coupons 和 ml_coupon_templates
// 这里简化处理,尝试直接从 ml_user_coupons 读取,并且加入 template 信息
// 如果没有 view可能需要改为两个查询或者使用 left join
const response = await supa
.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('status', status.toString())
.order('expire_at', { ascending: true })
@@ -7727,32 +7717,23 @@ class SupabaseService {
if (response.error != null) {
console.error('获取优惠券失败:', response.error)
const empty: UserCoupon[] = []
return empty
throw new Error(response.error.message != null ? response.error.message as string : '获取优惠券失败')
}
// 安全处理返回数据 - 安卓端可能是 UTSJSONObject 或 UTSArray
const rawData: any[] = []
const respData = response.data
console.log('[getUserCoupons] 原始数据类型:', typeof respData, '是否数组:', Array.isArray(respData))
if (respData != null) {
if (Array.isArray(respData)) {
const arr = respData as any[]
console.log('[getUserCoupons] 数组长度:', arr.length)
for (let i = 0; i < arr.length; i++) {
rawData.push(arr[i])
}
} else if (respData instanceof UTSJSONObject) {
// 单个对象情况,包装成数组
console.log('[getUserCoupons] 单个对象,包装成数组')
rawData.push(respData)
} else {
// 尝试 JSON 转换
try {
const parsed = JSON.parse(JSON.stringify(respData))
console.log('[getUserCoupons] JSON转换后是否数组:', Array.isArray(parsed))
if (Array.isArray(parsed)) {
console.log('[getUserCoupons] 转换后数组长度:', parsed.length)
for (let i = 0; i < parsed.length; i++) {
rawData.push(parsed[i])
}
@@ -7762,39 +7743,22 @@ class SupabaseService {
}
}
}
console.log('[getUserCoupons] 最终rawData长度:', rawData.length)
// 映射数据,将 template 的字段展平
const coupons: UserCoupon[] = []
for (let i = 0; i < rawData.length; i++) {
const item = rawData[i]
let template: any | null = null
let itemId = ''
let itemUserId = ''
let itemTmplId = ''
let itemCode = ''
let itemStatus = 0
let itemRecv = ''
let itemExpire = ''
if (item instanceof UTSJSONObject) {
template = item.get('template') as any | null
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') ?? ''
} else {
const iObj = JSON.parse(JSON.stringify(item)) as UTSJSONObject
template = iObj.get('template') as any | null
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') ?? ''
}
@@ -7803,30 +7767,29 @@ class SupabaseService {
let tName = ''
let tAmount = 0
let tMin = 0
let tDiscountType = 1
if (template instanceof UTSJSONObject) {
tName = template.getString('name') ?? '优惠券'
tAmount = template.getNumber('amount') ?? 0
tMin = template.getNumber('min_spend') ?? 0
tAmount = template.getNumber('discount_value') ?? 0
tMin = template.getNumber('min_order_amount') ?? 0
tDiscountType = template.getNumber('discount_type') ?? 1
} else {
const tObj = JSON.parse(JSON.stringify(template)) as UTSJSONObject
tName = tObj.getString('name') ?? '优惠券'
tAmount = tObj.getNumber('amount') ?? 0
tMin = tObj.getNumber('min_spend') ?? 0
tAmount = tObj.getNumber('discount_value') ?? 0
tMin = tObj.getNumber('min_order_amount') ?? 0
tDiscountType = tObj.getNumber('discount_type') ?? 1
}
// 创建真正的 UserCoupon 对象,而不是 UTSJSONObject
const couponItem: UserCoupon = {
id: itemId,
user_id: itemUserId,
template_id: itemTmplId,
coupon_code: itemCode,
status: itemStatus,
received_at: itemRecv,
expire_at: itemExpire,
template_name: tName,
amount: tAmount,
min_spend: tMin
min_spend: tMin,
expire_at: itemExpire,
status: status,
discount_type: tDiscountType
}
coupons.push(couponItem)
@@ -7835,8 +7798,7 @@ class SupabaseService {
return coupons
} catch (e) {
console.error('获取优惠券异常:', e)
const empty: UserCoupon[] = []
return empty
throw e
}
}