bookStore.js 2.7 KB

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