consumer模块完成度85%,连接服务器supabase,新建相关表

This commit is contained in:
2026-01-29 17:28:47 +08:00
parent a4fa00c935
commit ab038ec029
15 changed files with 4475 additions and 4793 deletions

View File

@@ -19,12 +19,14 @@ export interface Product {
id: string
category_id: string
name: string
description?: string
specification: string
price: number
original_price?: number
image?: string
manufacturer: string
sales: number
stock: number
badge?: string
shop_id?: string
shop_name?: string
@@ -111,14 +113,27 @@ class SupabaseService {
async searchProducts(
keyword: string,
page: number = 1,
limit: number = 20
limit: number = 20,
sortBy: string = 'sales',
ascending: boolean = false
): Promise<PaginatedResponse<Product>> {
try {
const response = await supa
let query = supa
.from('products')
.select('*', { count: 'exact' })
.or(`name.ilike.%${keyword}%,manufacturer.ilike.%${keyword}%,specification.ilike.%${keyword}%`)
.order('sales', { ascending: false })
// 根据sortBy和ascending设置排序
if (sortBy === 'price') {
query = query.order('price', { ascending })
} else if (sortBy === 'sales') {
query = query.order('sales', { ascending: false }) // 销量总是降序
} else {
// 默认按销量降序
query = query.order('sales', { ascending: false })
}
const response = await query
.page(page)
.limit(limit)
.execute()
@@ -175,7 +190,7 @@ class SupabaseService {
}
}
// 获取热销商品
// 获取热销商品(按销量排序)
async getHotProducts(limit: number = 10): Promise<Product[]> {
try {
const response = await supa
@@ -197,13 +212,58 @@ class SupabaseService {
}
}
// 获取推荐商品带badge的商品
async getRecommendedProducts(limit: number = 10): Promise<Product[]> {
// 获取按价格排序的商品(升序:从低到高
async getProductsByPrice(limit: number = 10, ascending: boolean = true): Promise<Product[]> {
try {
const response = await supa
.from('products')
.select('*')
.not('badge', 'is', null)
.order('price', { ascending })
.limit(limit)
.execute()
if (response.error) {
console.error('获取价格排序商品失败:', response.error)
return []
}
return response.data as Product[]
} catch (error) {
console.error('获取价格排序商品异常:', error)
return []
}
}
// 获取新品(按创建时间排序,最新的在前)
async getProductsByNewest(limit: number = 10): Promise<Product[]> {
try {
const response = await supa
.from('products')
.select('*')
.order('created_at', { ascending: false })
.limit(limit)
.execute()
if (response.error) {
console.error('获取新品失败:', response.error)
return []
}
return response.data as Product[]
} catch (error) {
console.error('获取新品异常:', error)
return []
}
}
// 获取推荐商品带badge的商品
async getRecommendedProducts(limit: number = 10): Promise<Product[]> {
try {
// 直接使用 neq 空字符串查询,忽略 null 值null 表示没有 badge不应被推荐
const response = await supa
.from('products')
.select('*')
.neq('badge', '')
.order('sales', { ascending: false })
.limit(limit)
.execute()
@@ -213,12 +273,37 @@ class SupabaseService {
return []
}
return response.data as Product[]
console.log('推荐商品查询结果条数:', response.data?.length || 0)
return response.data as Product[] || []
} catch (error) {
console.error('获取推荐商品异常:', error)
return []
}
}
// 获取特价商品badge为'特价'
async getDiscountProducts(limit: number = 10): Promise<Product[]> {
try {
const response = await supa
.from('products')
.select('*')
.eq('badge', '特价')
.order('sales', { ascending: false })
.limit(limit)
.execute()
if (response.error) {
console.error('获取特价商品失败:', response.error)
return []
}
console.log('特价商品查询结果条数:', response.data?.length || 0)
return response.data as Product[] || []
} catch (error) {
console.error('获取特价商品异常:', error)
return []
}
}
}
// 导出单例实例