bookStore.js 3.3 KB

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