You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

100 lines
2.9 KiB

export const useItemsStore = defineStore('items', () => {
const list = ref<any[]>([]);
const total = ref(0);
const page = ref(1);
const hasMore = ref(false);
const loading = ref(false);
const initialLoad = ref(true);
const ui = useUIStore();
async function fetchItems() {
loading.value = true;
try {
const params: Record<string, string> = {
page: String(page.value),
limit: '20',
};
if (ui.filterType !== 'all') {
if (ui.filterType === 'starred') params.starred = 'true';
else params.type = ui.filterType;
}
if (ui.currentCategoryId) params.categoryId = String(ui.currentCategoryId);
if (ui.currentView === 'inbox') params.categoryId = 'inbox';
if (ui.searchQuery) params.q = ui.searchQuery;
const res = await $fetch<any>('/api/items', { params });
if (res.code === 0) {
list.value = res.data.data;
total.value = res.data.total;
hasMore.value = res.data.hasMore;
}
} finally {
loading.value = false;
initialLoad.value = false;
}
}
async function loadMore() {
if (!hasMore.value || loading.value) return;
page.value++;
loading.value = true;
try {
const params: Record<string, string> = {
page: String(page.value),
limit: '20',
};
if (ui.filterType !== 'all') {
if (ui.filterType === 'starred') params.starred = 'true';
else params.type = ui.filterType;
}
if (ui.currentCategoryId) params.categoryId = String(ui.currentCategoryId);
if (ui.currentView === 'inbox') params.categoryId = 'inbox';
if (ui.searchQuery) params.q = ui.searchQuery;
const res = await $fetch<any>('/api/items', { params });
if (res.code === 0) {
list.value.push(...res.data.data);
total.value = res.data.total;
hasMore.value = res.data.hasMore;
}
} finally {
loading.value = false;
}
}
async function createItem(data: any) {
const res = await $fetch<any>('/api/items', {
method: 'POST',
body: data,
});
if (res.code === 0) {
list.value.unshift(res.data);
total.value++;
ui.showToast('✅ 收藏已保存');
}
return res;
}
async function toggleStar(id: number) {
const item = list.value.find(i => i.id === id);
if (!item) return;
const newStarred = !item.starred;
await $fetch(`/api/items/${id}`, {
method: 'PATCH',
body: { starred: newStarred },
});
item.starred = newStarred;
ui.showToast(newStarred ? '已加入星标' : '已移除星标');
}
async function deleteItem(id: number) {
await $fetch(`/api/items/${id}`, { method: 'DELETE' });
list.value = list.value.filter(i => i.id !== id);
total.value--;
ui.closeDetail();
ui.showToast('已删除');
}
return { list, total, page, hasMore, loading, initialLoad, fetchItems, loadMore, createItem, toggleStar, deleteItem };
});