consumer模块完成度85%,测试连接supabase
This commit is contained in:
228
utils/supabaseService.uts
Normal file
228
utils/supabaseService.uts
Normal file
@@ -0,0 +1,228 @@
|
||||
import { createClient } from '@/components/supadb/aksupa.uts'
|
||||
import { SUPA_URL, SUPA_KEY } from '@/ak/config.uts'
|
||||
import type { AkReqResponse } from '@/uni_modules/ak-req/index.uts'
|
||||
|
||||
// 创建 Supabase 客户端
|
||||
const supa = createClient(SUPA_URL, SUPA_KEY)
|
||||
|
||||
// 类型定义
|
||||
export interface Category {
|
||||
id: string
|
||||
name: string
|
||||
icon: string
|
||||
description: string
|
||||
color: string
|
||||
created_at?: string
|
||||
}
|
||||
|
||||
export interface Product {
|
||||
id: string
|
||||
category_id: string
|
||||
name: string
|
||||
specification: string
|
||||
price: number
|
||||
original_price?: number
|
||||
image?: string
|
||||
manufacturer: string
|
||||
sales: number
|
||||
badge?: string
|
||||
shop_id?: string
|
||||
shop_name?: string
|
||||
created_at?: string
|
||||
}
|
||||
|
||||
export interface PaginatedResponse<T> {
|
||||
data: T[]
|
||||
total: number
|
||||
page: number
|
||||
limit: number
|
||||
hasmore: boolean
|
||||
}
|
||||
|
||||
class SupabaseService {
|
||||
// 获取所有分类
|
||||
async getCategories(): Promise<Category[]> {
|
||||
try {
|
||||
const response = await supa
|
||||
.from('categories')
|
||||
.select('*')
|
||||
.order('name', { ascending: true })
|
||||
.execute()
|
||||
|
||||
if (response.error) {
|
||||
console.error('获取分类失败:', response.error)
|
||||
return []
|
||||
}
|
||||
|
||||
return response.data as Category[]
|
||||
} catch (error) {
|
||||
console.error('获取分类异常:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// 获取指定分类的商品
|
||||
async getProductsByCategory(
|
||||
categoryId: string,
|
||||
page: number = 1,
|
||||
limit: number = 20
|
||||
): Promise<PaginatedResponse<Product>> {
|
||||
try {
|
||||
const response = await supa
|
||||
.from('products')
|
||||
.select('*', { count: 'exact' })
|
||||
.eq('category_id', categoryId)
|
||||
.order('sales', { 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,
|
||||
page: number = 1,
|
||||
limit: number = 20
|
||||
): Promise<PaginatedResponse<Product>> {
|
||||
try {
|
||||
const response = await supa
|
||||
.from('products')
|
||||
.select('*', { count: 'exact' })
|
||||
.or(`name.ilike.%${keyword}%,manufacturer.ilike.%${keyword}%,specification.ilike.%${keyword}%`)
|
||||
.order('sales', { 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 getProductById(productId: string): Promise<Product | null> {
|
||||
try {
|
||||
const response = await supa
|
||||
.from('products')
|
||||
.select('*')
|
||||
.eq('id', productId)
|
||||
.single()
|
||||
.execute()
|
||||
|
||||
if (response.error) {
|
||||
console.error('获取商品详情失败:', response.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return response.data as Product
|
||||
} catch (error) {
|
||||
console.error('获取商品详情异常:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 获取热销商品
|
||||
async getHotProducts(limit: number = 10): Promise<Product[]> {
|
||||
try {
|
||||
const response = await supa
|
||||
.from('products')
|
||||
.select('*')
|
||||
.order('sales', { 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 {
|
||||
const response = await supa
|
||||
.from('products')
|
||||
.select('*')
|
||||
.not('badge', 'is', null)
|
||||
.order('sales', { ascending: false })
|
||||
.limit(limit)
|
||||
.execute()
|
||||
|
||||
if (response.error) {
|
||||
console.error('获取推荐商品失败:', response.error)
|
||||
return []
|
||||
}
|
||||
|
||||
return response.data as Product[]
|
||||
} catch (error) {
|
||||
console.error('获取推荐商品异常:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例实例
|
||||
export const supabaseService = new SupabaseService()
|
||||
|
||||
// 默认导出
|
||||
export default supabaseService
|
||||
Reference in New Issue
Block a user