consumer模块完成90%,完善店铺商品优惠券领取

This commit is contained in:
2026-02-05 17:27:22 +08:00
parent 0ee4577b31
commit 06b7369494
22 changed files with 2096 additions and 1286 deletions

View File

@@ -256,6 +256,53 @@ class SupabaseService {
}
}
// 获取店铺商品
async getProductsByMerchantId(
merchantId: string,
page: number = 1,
limit: number = 20
): Promise<PaginatedResponse<Product>> {
try {
const response = await supa
.from('ml_products_detail_view')
.select('*', { count: 'exact' })
.eq('merchant_id', merchantId)
.eq('status', 1)
.order('created_at', { ascending: false })
.page(page)
.limit(limit)
.execute()
if (response.error) {
console.error('获取店铺商品失败:', response.error)
return {
data: [],
total: 0,
page,
limit,
hasmore: false
}
}
return {
data: response.data as Product[],
total: response.total || 0,
page,
limit,
hasmore: response.hasmore || false
}
} catch (error) {
console.error('获取店铺商品异常:', error)
return {
data: [],
total: 0,
page,
limit,
hasmore: false
}
}
}
// 搜索商品
async searchProducts(
keyword: string,
@@ -2121,9 +2168,69 @@ class SupabaseService {
return false
}
}
// 获取店铺/商品可用优惠券
async getAvailableCoupons(merchantId: string): Promise<any[]> {
return this.fetchShopCoupons(merchantId)
}
// ALIAS for Cache busting: 获取店铺优惠券
async fetchShopCoupons(merchantId: string): Promise<any[]> {
try {
// 查询该商家的优惠券 + 平台通用券 (merchant_id is null)
// 注意:这里简化逻辑,实际可能需要联合查询用户是否已领取
const response = await supa
.from('ml_coupon_templates')
.select('*')
.or(`merchant_id.eq.${merchantId},merchant_id.is.null`)
.eq('status', 1)
.gt('end_time', new Date().toISOString())
.order('discount_value', { ascending: false })
.execute()
if (response.error) {
console.error('Fetch coupons failed:', response.error)
return []
}
return response.data
} catch (e) {
console.error('Fetch coupons error:', e)
return []
}
}
// 领取优惠券
async claimCoupon(templateId: string, userId: string): Promise<boolean> {
return this.claimShopCoupon(templateId, userId)
}
// ALIAS for Cache busting
async claimShopCoupon(templateId: string, userId: string): Promise<boolean> {
try {
// 检查是否已领取 (根据业务规则,这里假设不做严格限制或由数据库约束处理)
// 简单插入
const response = await supa
.from('ml_user_coupons')
.insert({
user_id: userId,
template_id: templateId,
coupon_code: 'C' + Date.now() + Math.floor(Math.random() * 1000), // 简单生成
status: 1, // 1 = unused based on mock data comment
expire_at: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString() // Default 30 days if not fetched from template (Simplified)
})
.execute()
return !response.error
} catch(e) {
console.error('Claim coupon error:', e)
return false
}
}
}
// 导出单例实例
console.log('Supabase Service Initialized - Version with Coupons')
export const supabaseService = new SupabaseService()
// 默认导出