// stores/book.ts import { defineStore } from 'pinia' import type { IBookDetail, IBook, IReaderSettings, IReadProgress } from '@/types/book' interface BookState { currentBook: IBookDetail | null myBooks: IBook[] readerSettings: IReaderSettings readProgressCache: Record } export const useBookStore = defineStore('book', { state: (): BookState => ({ // 当前查看的书籍 currentBook: null, // 书单列表 myBooks: [], // 阅读器设置 readerSettings: { fontSize: 1, lineHeight: 34, theme: 'default', readMode: 'scroll' }, // 阅读进度缓存 readProgressCache: {} }), getters: { /** * 获取指定书籍的阅读进度 */ getReadProgress: (state) => (bookId: number) => { return state.readProgressCache[bookId] } }, actions: { /** * 设置当前书籍 */ setCurrentBook(book: IBookDetail) { this.currentBook = book }, /** * 更新阅读设置 */ updateReaderSettings(settings: Partial) { Object.assign(this.readerSettings, settings) // 持久化到本地存储 uni.setStorageSync('readerSettings', this.readerSettings) }, /** * 缓存阅读进度 */ cacheReadProgress(bookId: number, progress: IReadProgress) { this.readProgressCache[bookId] = progress }, /** * 从本地存储恢复设置 */ restoreSettings() { try { const settings = uni.getStorageSync('readerSettings') if (settings) { this.readerSettings = settings } } catch (e) { console.error('Failed to restore reader settings:', e) } }, /** * 重置store */ reset() { this.currentBook = null this.myBooks = [] this.readProgressCache = {} } } })