2026-07-14 16:04:51 +08:00
|
|
|
import type { PageResponse, Product, ProductCategory, ProductSku } from '../types'
|
2026-07-07 16:18:23 +08:00
|
|
|
import { apiGet, apiPost, apiPut } from './http'
|
|
|
|
|
|
2026-07-14 16:04:51 +08:00
|
|
|
export interface ProductQuery {
|
|
|
|
|
keyword?: string
|
|
|
|
|
categoryId?: number
|
|
|
|
|
merchantId?: number
|
|
|
|
|
pageNo?: number
|
|
|
|
|
pageSize?: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function buildProductQuery(query: ProductQuery = {}) {
|
|
|
|
|
const params: Record<string, string | number> = {
|
|
|
|
|
pageNo: query.pageNo || 1,
|
|
|
|
|
pageSize: query.pageSize || 20
|
|
|
|
|
}
|
|
|
|
|
if (query.keyword) params.keyword = query.keyword
|
|
|
|
|
if (query.categoryId) params.categoryId = query.categoryId
|
|
|
|
|
if (query.merchantId) params.merchantId = query.merchantId
|
|
|
|
|
return params
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function listProducts(query: ProductQuery = {}) {
|
|
|
|
|
const page = await apiGet<PageResponse<Product>>('/api/admin/products/page', buildProductQuery(query))
|
|
|
|
|
return page.records
|
2026-07-07 16:18:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createProduct(data: Partial<Product>) {
|
|
|
|
|
return apiPost<Product>('/api/admin/products', data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function updateProduct(id: number, data: Partial<Product>) {
|
|
|
|
|
return apiPut<Product>(`/api/admin/products/${id}`, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function onShelfProduct(id: number) {
|
|
|
|
|
return apiPut<Product>(`/api/admin/products/${id}/on-shelf`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function offShelfProduct(id: number) {
|
|
|
|
|
return apiPut<Product>(`/api/admin/products/${id}/off-shelf`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function listProductCategories() {
|
|
|
|
|
return apiGet<ProductCategory[]>('/api/admin/product-categories')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createProductCategory(data: Partial<ProductCategory>) {
|
|
|
|
|
return apiPost<ProductCategory>('/api/admin/product-categories', data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function addSku(productId: number, data: Partial<ProductSku>) {
|
|
|
|
|
return apiPost<ProductSku>(`/api/admin/products/${productId}/skus`, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function updateSku(productId: number, skuId: number, data: Partial<ProductSku>) {
|
|
|
|
|
return apiPut<ProductSku>(`/api/admin/products/${productId}/skus/${skuId}`, data)
|
|
|
|
|
}
|