bookStore.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { defineStore } from "pinia";
  2. import API from "@api";
  3. export const useBookStore = defineStore("book", {
  4. state: () => {
  5. return {
  6. connectStatus: "正在连接后端服务器……",
  7. connectType: "primary",
  8. newConnect: true,
  9. /**@type {Array<{respondTime:number}>} */
  10. searchBooks: [],
  11. shelf: [],
  12. catalog: [],
  13. /**@type {{index: number,chapterPos:number}} */
  14. readingBook: { index: 0, chapterPos: 0 },
  15. popCataVisible: false,
  16. contentLoading: true,
  17. showContent: false,
  18. config: {
  19. theme: 0,
  20. font: 0,
  21. fontSize: 18,
  22. readWidth: 800,
  23. infiniteLoading: false,
  24. customFontName: "",
  25. jumpDuration: 1000,
  26. spacing: {
  27. paragraph: 1,
  28. line: 0.8,
  29. letter: 0,
  30. },
  31. },
  32. miniInterface: false,
  33. readSettingsVisible: false,
  34. };
  35. },
  36. getters: {
  37. bookProgress: (state) => {
  38. if (state.catalog.length == 0) return;
  39. // @ts-ignore
  40. const { index, chapterPos, bookName, bookAuthor } = state.readingBook;
  41. let title = state.catalog[index]?.title;
  42. if (!title) return;
  43. return {
  44. name: bookName,
  45. author: bookAuthor,
  46. durChapterIndex: index,
  47. durChapterPos: chapterPos,
  48. durChapterTime: new Date().getTime(),
  49. durChapterTitle: title,
  50. };
  51. },
  52. theme: (state) => {
  53. return state.config.theme
  54. },
  55. isDark: (state) => state.config.theme == 6,
  56. },
  57. actions: {
  58. setConnectStatus(connectStatus) {
  59. this.connectStatus = connectStatus;
  60. },
  61. setConnectType(connectType) {
  62. this.connectType = connectType;
  63. },
  64. setNewConnect(newConnect) {
  65. this.newConnect = newConnect;
  66. },
  67. addBooks(books) {
  68. this.shelf = books;
  69. },
  70. clearBooks() {
  71. this.shelf = [];
  72. },
  73. setCatalog(catalog) {
  74. this.catalog = catalog;
  75. },
  76. setPopCataVisible(visible) {
  77. this.popCataVisible = visible;
  78. },
  79. setContentLoading(loading) {
  80. this.contentLoading = loading;
  81. },
  82. setReadingBook(readingBook) {
  83. this.readingBook = readingBook;
  84. },
  85. setConfig(config) {
  86. this.config = Object.assign({}, this.config, config);
  87. },
  88. setReadSettingsVisible(visible) {
  89. this.readSettingsVisible = visible;
  90. },
  91. setShowContent(visible) {
  92. this.showContent = visible;
  93. },
  94. setMiniInterface(mini) {
  95. this.miniInterface = mini;
  96. },
  97. async setSearchBooks(books) {
  98. books.forEach((book) => {
  99. let findBook = this.shelf.find((item) => item.bookUrl == book.bookUrl);
  100. if (findBook === undefined) {
  101. this.searchBooks.push(book);
  102. }
  103. });
  104. },
  105. clearSearchBooks() {
  106. this.searchBooks = [];
  107. },
  108. //保存进度到app
  109. async saveBookProgress() {
  110. if (!this.bookProgress) return Promise.resolve();
  111. return API.saveBookProgress(this.bookProgress);
  112. },
  113. },
  114. });