189 lines
5.4 KiB
TypeScript
189 lines
5.4 KiB
TypeScript
import type { HomeDecor } from "./types"
|
|
import { mockResponse, type PaginatedResponse, type PaginationParams } from "./client"
|
|
|
|
// 模拟家居装饰数据
|
|
const mockHomeDecors: HomeDecor[] = [
|
|
{
|
|
id: "DEC001",
|
|
name: "星空投影灯",
|
|
type: "灯饰",
|
|
rarity: "稀有",
|
|
description: "可以在房间内投影出美丽的星空,营造浪漫氛围。",
|
|
releaseDate: "2023-10-20",
|
|
status: "已发布",
|
|
activatedCount: 1342,
|
|
image: "/placeholder.svg?height=300&width=300",
|
|
},
|
|
{
|
|
id: "DEC002",
|
|
name: "音乐主题壁纸",
|
|
type: "墙饰",
|
|
rarity: "普通",
|
|
description: "以音乐元素为主题的壁纸,适合洛天依的房间装饰。",
|
|
releaseDate: "2023-11-05",
|
|
status: "已发布",
|
|
activatedCount: 2156,
|
|
image: "/placeholder.svg?height=300&width=300",
|
|
},
|
|
{
|
|
id: "DEC003",
|
|
name: "音符地毯",
|
|
type: "地饰",
|
|
rarity: "稀有",
|
|
description: "音符形状的地毯,踩上去会发出悦耳的音符声。",
|
|
releaseDate: "2023-12-15",
|
|
status: "已发布",
|
|
activatedCount: 987,
|
|
image: "/placeholder.svg?height=300&width=300",
|
|
},
|
|
{
|
|
id: "DEC004",
|
|
name: "全息投影装置",
|
|
type: "科技装饰",
|
|
rarity: "传说",
|
|
description: "可以投影出洛天依的全息影像,实现虚拟互动。",
|
|
releaseDate: "2024-01-20",
|
|
status: "已发布",
|
|
activatedCount: 456,
|
|
image: "/placeholder.svg?height=300&width=300",
|
|
},
|
|
{
|
|
id: "DEC005",
|
|
name: "樱花主题家具套装",
|
|
type: "家具套装",
|
|
rarity: "史诗",
|
|
description: "以樱花为主题的家具套装,包含床、桌椅、柜子等多件家具。",
|
|
releaseDate: "",
|
|
status: "未发布",
|
|
activatedCount: 0,
|
|
image: "/placeholder.svg?height=300&width=300",
|
|
},
|
|
]
|
|
|
|
// 获取家居装饰列表
|
|
export const getHomeDecors = async (params?: PaginationParams): Promise<PaginatedResponse<HomeDecor>> => {
|
|
let filteredDecors = [...mockHomeDecors]
|
|
|
|
// 搜索过滤
|
|
if (params?.search) {
|
|
const search = params.search.toLowerCase()
|
|
filteredDecors = filteredDecors.filter(
|
|
(decor) =>
|
|
decor.name.toLowerCase().includes(search) ||
|
|
decor.id.toLowerCase().includes(search) ||
|
|
decor.type.toLowerCase().includes(search),
|
|
)
|
|
}
|
|
|
|
// 排序
|
|
if (params?.sortBy) {
|
|
filteredDecors.sort((a: any, b: any) => {
|
|
const aValue = a[params.sortBy!]
|
|
const bValue = b[params.sortBy!]
|
|
|
|
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
return params.sortOrder === "desc" ? bValue.localeCompare(aValue) : aValue.localeCompare(bValue)
|
|
}
|
|
|
|
return params.sortOrder === "desc" ? bValue - aValue : aValue - bValue
|
|
})
|
|
}
|
|
|
|
// 分页
|
|
const page = params?.page || 1
|
|
const pageSize = params?.pageSize || 10
|
|
const startIndex = (page - 1) * pageSize
|
|
const paginatedDecors = filteredDecors.slice(startIndex, startIndex + pageSize)
|
|
|
|
return mockResponse({
|
|
items: paginatedDecors,
|
|
total: filteredDecors.length,
|
|
page,
|
|
pageSize,
|
|
totalPages: Math.ceil(filteredDecors.length / pageSize),
|
|
})
|
|
}
|
|
|
|
// 获取单个家居装饰
|
|
export const getHomeDecor = async (id: string): Promise<HomeDecor> => {
|
|
const decor = mockHomeDecors.find((decor) => decor.id === id)
|
|
|
|
if (!decor) {
|
|
return mockResponse({} as HomeDecor, "家居装饰不存在")
|
|
}
|
|
|
|
return mockResponse(decor)
|
|
}
|
|
|
|
// 创建家居装饰
|
|
export const createHomeDecor = async (decorData: Partial<HomeDecor>): Promise<HomeDecor> => {
|
|
// 生成新的家居装饰ID
|
|
const decorId = "DEC" + String(mockHomeDecors.length + 1).padStart(3, "0")
|
|
|
|
const newDecor: HomeDecor = {
|
|
id: decorId,
|
|
name: decorData.name || "",
|
|
type: decorData.type || "",
|
|
rarity: decorData.rarity || "",
|
|
description: decorData.description || "",
|
|
releaseDate: decorData.releaseDate || "",
|
|
status: decorData.status || "未发布",
|
|
activatedCount: 0,
|
|
image: decorData.image || "/placeholder.svg?height=300&width=300",
|
|
}
|
|
|
|
mockHomeDecors.push(newDecor)
|
|
|
|
return mockResponse(newDecor)
|
|
}
|
|
|
|
// 更新家居装饰
|
|
export const updateHomeDecor = async (id: string, decorData: Partial<HomeDecor>): Promise<HomeDecor> => {
|
|
const decorIndex = mockHomeDecors.findIndex((decor) => decor.id === id)
|
|
|
|
if (decorIndex === -1) {
|
|
return mockResponse({} as HomeDecor, "家居装饰不存在")
|
|
}
|
|
|
|
const updatedDecor = {
|
|
...mockHomeDecors[decorIndex],
|
|
...decorData,
|
|
}
|
|
|
|
mockHomeDecors[decorIndex] = updatedDecor
|
|
|
|
return mockResponse(updatedDecor)
|
|
}
|
|
|
|
// 删除家居装饰
|
|
export const deleteHomeDecor = async (id: string): Promise<boolean> => {
|
|
const decorIndex = mockHomeDecors.findIndex((decor) => decor.id === id)
|
|
|
|
if (decorIndex === -1) {
|
|
return mockResponse(false, "家居装饰不存在")
|
|
}
|
|
|
|
// 已发布的家居装饰不能删除
|
|
if (mockHomeDecors[decorIndex].status === "已发布") {
|
|
return mockResponse(false, "已发布的家居装饰不能删除")
|
|
}
|
|
|
|
mockHomeDecors.splice(decorIndex, 1)
|
|
|
|
return mockResponse(true)
|
|
}
|
|
|
|
// 发布家居装饰
|
|
export const publishHomeDecor = async (id: string): Promise<HomeDecor> => {
|
|
const decorIndex = mockHomeDecors.findIndex((decor) => decor.id === id)
|
|
|
|
if (decorIndex === -1) {
|
|
return mockResponse({} as HomeDecor, "家居装饰不存在")
|
|
}
|
|
|
|
mockHomeDecors[decorIndex].status = "已发布"
|
|
mockHomeDecors[decorIndex].releaseDate = new Date().toISOString().split("T")[0]
|
|
|
|
return mockResponse(mockHomeDecors[decorIndex])
|
|
}
|