重构优惠券页面ui

This commit is contained in:
2026-06-05 10:34:02 +08:00
parent 5d0c0ae95a
commit 48fb499138
3 changed files with 1530 additions and 106 deletions

View File

@@ -387,12 +387,21 @@ export type UserAddress = {
export type UserCoupon = {
id: string
template_id: string
template_name: string
amount: number
min_spend: number
expire_at: string
status: number
discount_type?: number
discount_type: number
coupon_type: number
used_at: string
received_at: string
merchant_id: string
shop_name: string
shop_logo: string
scope_type: string
consumer_deleted_at?: string
}
export type ChatRoom = {
@@ -7699,7 +7708,7 @@ class SupabaseService {
}
// 获取用户优惠券列表
async getUserCoupons(status: number = 1): Promise<UserCoupon[]> {
async getUserCoupons(status?: number): Promise<UserCoupon[]> {
try {
const userId = this.getCurrentUserId()
if (userId == null) {
@@ -7707,13 +7716,16 @@ class SupabaseService {
return empty
}
const response = await supa
let query = supa
.from('ml_user_coupons')
.select('*, template:ml_coupon_templates(name, discount_value, min_order_amount, coupon_type, discount_type)')
.select('*, template:ml_coupon_templates(name, discount_value, min_order_amount, coupon_type, discount_type, merchant_id, applicable_products)')
.eq('user_id', userId!)
.eq('status', status.toString())
.order('expire_at', { ascending: true })
.execute()
if (status != null) {
query = query.eq('status', status.toString())
}
const response = await query.order('expire_at', { ascending: true }).execute()
if (response.error != null) {
console.error('获取优惠券失败:', response.error)
@@ -7745,56 +7757,164 @@ class SupabaseService {
}
const coupons: UserCoupon[] = []
const merchantIds: string[] = []
for (let i = 0; i < rawData.length; i++) {
const item = rawData[i]
let template: any | null = null
let itemId = ''
let itemTemplateId = ''
let itemStatus = 1
let itemUsedAt = ''
let itemReceivedAt = ''
let itemExpire = ''
if (item instanceof UTSJSONObject) {
template = item.get('template') as any | null
itemId = item.getString('id') ?? ''
itemTemplateId = item.getString('template_id') ?? ''
itemStatus = item.getNumber('status') ?? 1
itemUsedAt = item.getString('used_at') ?? ''
itemReceivedAt = 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') ?? ''
itemTemplateId = iObj.getString('template_id') ?? ''
itemStatus = iObj.getNumber('status') ?? 1
itemUsedAt = iObj.getString('used_at') ?? ''
itemReceivedAt = iObj.getString('received_at') ?? ''
itemExpire = iObj.getString('expire_at') ?? ''
}
if (template == null) template = new UTSJSONObject()
let tName = ''
let tAmount = 0
let tMin = 0
let tDiscountType = 1
let tCouponType = 1
let tMerchantId = ''
let tScopeType = 'store'
if (template instanceof UTSJSONObject) {
tName = template.getString('name') ?? '优惠券'
tAmount = template.getNumber('discount_value') ?? 0
tMin = template.getNumber('min_order_amount') ?? 0
tDiscountType = template.getNumber('discount_type') ?? 1
tCouponType = template.getNumber('coupon_type') ?? 1
tMerchantId = template.getString('merchant_id') ?? ''
const applicableProducts = template.get('applicable_products')
if (applicableProducts != null) {
let hasProducts = false
if (Array.isArray(applicableProducts)) {
const arr = applicableProducts as any[]
if (arr.length > 0) hasProducts = true
} else if (typeof applicableProducts === 'string') {
const s = applicableProducts as string
if (s != '' && s != '[]') {
try {
const parsed = JSON.parse(s)
if (Array.isArray(parsed) && (parsed as any[]).length > 0) {
hasProducts = true
}
} catch (e) {}
}
}
if (hasProducts) {
tScopeType = 'product'
}
}
} else {
const tObj = JSON.parse(JSON.stringify(template)) as UTSJSONObject
tName = tObj.getString('name') ?? '优惠券'
tAmount = tObj.getNumber('discount_value') ?? 0
tMin = tObj.getNumber('min_order_amount') ?? 0
tDiscountType = tObj.getNumber('discount_type') ?? 1
tCouponType = tObj.getNumber('coupon_type') ?? 1
tMerchantId = tObj.getString('merchant_id') ?? ''
const applicableProducts = tObj.get('applicable_products')
if (applicableProducts != null) {
let hasProducts = false
if (Array.isArray(applicableProducts)) {
const arr = applicableProducts as any[]
if (arr.length > 0) hasProducts = true
} else if (typeof applicableProducts === 'string') {
const s = applicableProducts as string
if (s != '' && s != '[]') {
try {
const parsed = JSON.parse(s)
if (Array.isArray(parsed) && (parsed as any[]).length > 0) {
hasProducts = true
}
} catch (e) {}
}
}
if (hasProducts) {
tScopeType = 'product'
}
}
}
if (tMerchantId != '' && merchantIds.indexOf(tMerchantId) == -1) {
merchantIds.push(tMerchantId)
}
const couponItem: UserCoupon = {
id: itemId,
template_id: itemTemplateId,
template_name: tName,
amount: tAmount,
min_spend: tMin,
expire_at: itemExpire,
status: status,
discount_type: tDiscountType
status: itemStatus,
discount_type: tDiscountType,
coupon_type: tCouponType,
used_at: itemUsedAt,
received_at: itemReceivedAt,
merchant_id: tMerchantId,
shop_name: '',
shop_logo: '',
scope_type: tScopeType
}
coupons.push(couponItem)
}
// 批量查询店铺信息
if (merchantIds.length > 0) {
try {
const shopRes = await supa
.from('ml_shops')
.select('merchant_id, shop_name, shop_logo')
.in('merchant_id', merchantIds as any[])
.execute()
if (shopRes.error == null && shopRes.data != null) {
const shopRows = shopRes.data as any[]
for (let i = 0; i < shopRows.length; i++) {
const shopObj = normalizeUtsObject(shopRows[i])
const mid = shopObj.getString('merchant_id') ?? ''
const sname = shopObj.getString('shop_name') ?? ''
const slogo = shopObj.getString('shop_logo') ?? ''
if (mid != '') {
for (let j = 0; j < coupons.length; j++) {
if (coupons[j].merchant_id == mid) {
coupons[j].shop_name = sname
coupons[j].shop_logo = slogo
}
}
}
}
}
} catch (shopErr) {
console.error('查询店铺信息失败:', shopErr)
}
}
return coupons
} catch (e) {
console.error('获取优惠券异常:', e)
@@ -7802,6 +7922,38 @@ class SupabaseService {
}
}
// 软删除用户优惠券(消费者端自行管理)
async softDeleteUserCoupons(ids: string[]): Promise<boolean> {
try {
const userId = this.getCurrentUserId()
if (userId == null || ids.length == 0) {
return false
}
const nowIso = new Date().toISOString()
const response = await supa
.from('ml_user_coupons')
.update({
consumer_deleted_at: nowIso,
consumer_deleted_by: userId,
consumer_delete_reason: 'user_delete'
} as any)
.in('id', ids as any[])
.eq('user_id', userId!)
.execute()
if (response.error != null) {
console.error('软删除优惠券失败:', response.error)
return false
}
return true
} catch (e) {
console.error('软删除优惠券异常:', e)
return false
}
}
// 获取可用优惠券数量
async getUserCouponCount(): Promise<number> {
try {