| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import { defineStore } from "pinia";
- import API from "@api";
- export const useBookStore = defineStore("book", {
- state: () => {
- return {
- connectStatus: "正在连接后端服务器……",
- /**@type {"primary" | "success" |"danger"} */
- connectType: "primary",
- newConnect: true,
- /**@type {Array<{respondTime:number}>} */
- searchBooks: [],
- shelf: [],
- catalog: [],
- /**@type {{index: number,chapterPos:number}} */
- readingBook: { index: 0, chapterPos: 0 },
- popCataVisible: false,
- contentLoading: true,
- showContent: false,
- config: {
- theme: 0,
- font: 0,
- fontSize: 18,
- readWidth: 800,
- infiniteLoading: false,
- customFontName: "",
- jumpDuration: 1000,
- spacing: {
- paragraph: 1,
- line: 0.8,
- letter: 0,
- },
- },
- miniInterface: false,
- readSettingsVisible: false,
- };
- },
- getters: {
- bookProgress: (state) => {
- if (state.catalog.length == 0) return;
- // @ts-ignore
- const { index, chapterPos, bookName, bookAuthor } = state.readingBook;
- let title = state.catalog[index]?.title;
- if (!title) return;
- return {
- name: bookName,
- author: bookAuthor,
- durChapterIndex: index,
- durChapterPos: chapterPos,
- durChapterTime: new Date().getTime(),
- durChapterTitle: title,
- };
- },
- theme: (state) => {
- return state.config.theme
- },
- isNight: (state) => state.config.theme == 6,
- },
- actions: {
- setConnectStatus(connectStatus) {
- this.connectStatus = connectStatus;
- },
- setConnectType(connectType) {
- this.connectType = connectType;
- },
- setNewConnect(newConnect) {
- this.newConnect = newConnect;
- },
- addBooks(books) {
- this.shelf = books;
- },
- clearBooks() {
- this.shelf = [];
- },
- setCatalog(catalog) {
- this.catalog = catalog;
- },
- setPopCataVisible(visible) {
- this.popCataVisible = visible;
- },
- setContentLoading(loading) {
- this.contentLoading = loading;
- },
- setReadingBook(readingBook) {
- this.readingBook = readingBook;
- },
- setConfig(config) {
- this.config = Object.assign({}, this.config, config);
- },
- setReadSettingsVisible(visible) {
- this.readSettingsVisible = visible;
- },
- setShowContent(visible) {
- this.showContent = visible;
- },
- setMiniInterface(mini) {
- this.miniInterface = mini;
- },
- async setSearchBooks(books) {
- books.forEach((book) => {
- let findBook = this.shelf.find((item) => item.bookUrl == book.bookUrl);
- if (findBook === undefined) {
- this.searchBooks.push(book);
- }
- });
- },
- clearSearchBooks() {
- this.searchBooks = [];
- },
- //保存进度到app
- async saveBookProgress() {
- if (!this.bookProgress) return Promise.resolve();
- return API.saveBookProgress(this.bookProgress);
- },
- //读取阅读界面配置以初始化夜间模式 以免初次加载书架页面时闪屏
- async loadReadConfig() {
- return API.getReadConfig().then(response => response.data)
- .then(({isSuccess, data}) =>
- isSuccess && this.setConfig(JSON.parse(data))
- )
- }
- },
- });
|