@ -1,12 +1,12 @@
|
||||
// src/lib/favorites.ts — 收藏(服务端持久化 + 本地缓存兜底)
|
||||
//
|
||||
// 设计要点(应对"收藏量极大"场景):
|
||||
// - 列表卡片打勾:只把「当前可见页的 id」发给后端 /auth/favorites/check,
|
||||
// - 列表卡片打勾:只把「当前可见页的 id」发给后端 /me/favorites/checks,
|
||||
// 问哪些已收藏。复杂度与「一屏多少张」成正比,与「用户收藏了多少」彻底解耦,
|
||||
// 即便收藏 10 万也只传输约 24~50 个 id(常数级)。
|
||||
// - 个人中心 Saved looks:走 /auth/favorites 的「服务端分页」,每次只取一页(默认 24 条),
|
||||
// - 个人中心 Saved looks:走 /me/favorites 的「服务端分页」,每次只取一页(默认 24 条),
|
||||
// DOM 永远轻量,不存在一次渲染上万张卡片的问题。
|
||||
// - 收藏/取消:本地先乐观更新(UI 即时),再后台同步到 /api/v1/auth/favorites(POST/DELETE),
|
||||
// - 收藏/取消:本地先乐观更新(UI 即时),再后台同步到 /api/v1/me/favorites(POST/DELETE),
|
||||
// 即便网络失败也不回滚本地,保证前端不"卡死"。
|
||||
// - 未登录:纯本地 localStorage,行为与旧版一致(按当前用户名隔离)。
|
||||
//
|
||||
@ -148,7 +148,7 @@ export function toggleImageFavorite(args: {
|
||||
export function removeFavorite(id: string): void {
|
||||
setLocal(getLocal().filter((f) => f.id !== id))
|
||||
if (serverMode()) {
|
||||
authJson("DELETE", `/auth/favorites/${encodeURIComponent(id)}`).catch(() => {
|
||||
authJson("DELETE", `/me/favorites/${encodeURIComponent(id)}`).catch(() => {
|
||||
/* 失败不回滚本地 */
|
||||
})
|
||||
}
|
||||
@ -181,7 +181,7 @@ export async function fetchFavoritesPage(
|
||||
const qs = kind ? `&kind=${kind}` : ""
|
||||
const r = await authJson<{ items: ServerFavorite[]; total: number; last_page: number }>(
|
||||
"GET",
|
||||
`/auth/favorites?page=${page}&per_page=${perPage}${qs}`,
|
||||
`/me/favorites?page=${page}&per_page=${perPage}${qs}`,
|
||||
)
|
||||
const items = (r?.items ?? []).map((it) => ({
|
||||
id: String(it.id),
|
||||
@ -215,7 +215,7 @@ export async function fetchFavoritesPage(
|
||||
export async function checkFavorited(ids: string[]): Promise<Set<string>> {
|
||||
if (!serverMode() || ids.length === 0) return new Set()
|
||||
try {
|
||||
const arr = await authJson<string[]>("POST", "/auth/favorites/check", { ids })
|
||||
const arr = await authJson<string[]>("POST", "/me/favorites/checks", { ids })
|
||||
return new Set(Array.isArray(arr) ? arr.map(String) : [])
|
||||
} catch {
|
||||
return new Set()
|
||||
@ -260,11 +260,11 @@ function syncServer(item: Omit<Favorite, "addedAt">, newState: boolean): void {
|
||||
parent_uid: item.parentUid || "",
|
||||
}
|
||||
if (newState) {
|
||||
authJson("POST", "/auth/favorites", payload).catch(() => {
|
||||
authJson("POST", "/me/favorites", payload).catch(() => {
|
||||
/* 失败不回滚本地 */
|
||||
})
|
||||
} else {
|
||||
authJson("DELETE", `/auth/favorites/${encodeURIComponent(item.id)}`).catch(() => {
|
||||
authJson("DELETE", `/me/favorites/${encodeURIComponent(item.id)}`).catch(() => {
|
||||
/* 失败不回滚本地 */
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user