BookShelf.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <template>
  2. <div :class="{ 'index-wrapper': true, night: isNight, day: !isNight }">
  3. <div class="navigation-wrapper">
  4. <div class="navigation-title-wrapper">
  5. <div class="navigation-title">阅读</div>
  6. <div class="navigation-sub-title">清风不识字,何故乱翻书</div>
  7. </div>
  8. <div class="search-wrapper">
  9. <el-input
  10. placeholder="搜索书籍,在线书籍自动加入书架"
  11. v-model="search"
  12. class="search-input"
  13. :prefix-icon="Search"
  14. @keyup.enter="searchBook"
  15. >
  16. </el-input>
  17. </div>
  18. <div class="bottom-wrapper">
  19. <div class="recent-wrapper">
  20. <div class="recent-title">最近阅读</div>
  21. <div class="reading-recent">
  22. <el-tag
  23. :type="readingRecent.name == '尚无阅读记录' ? 'warning' : ''"
  24. class="recent-book"
  25. size="large"
  26. @click="
  27. toDetail(
  28. readingRecent.url,
  29. readingRecent.name,
  30. readingRecent.author,
  31. readingRecent.chapterIndex,
  32. readingRecent.chapterPos,
  33. )
  34. "
  35. :class="{ 'no-point': readingRecent.url == '' }"
  36. >
  37. {{ readingRecent.name }}
  38. </el-tag>
  39. </div>
  40. </div>
  41. <div class="setting-wrapper">
  42. <div class="setting-title">基本设定</div>
  43. <div class="setting-item">
  44. <el-tag
  45. :type="connectType"
  46. size="large"
  47. class="setting-connect"
  48. :class="{ 'no-point': newConnect }"
  49. @click="setIP"
  50. >
  51. {{ connectStatus }}
  52. </el-tag>
  53. </div>
  54. </div>
  55. </div>
  56. <div class="bottom-icons">
  57. <a
  58. href="https://github.com/gedoor/legado_web_bookshelf"
  59. target="_blank"
  60. >
  61. <div class="bottom-icon">
  62. <img :src="githubUrl" alt="" />
  63. </div>
  64. </a>
  65. </div>
  66. </div>
  67. <div class="shelf-wrapper" ref="shelfWrapper">
  68. <book-items
  69. :books="books"
  70. @bookClick="handleBookClick"
  71. :isSearch="isSearching"
  72. ></book-items>
  73. </div>
  74. </div>
  75. </template>
  76. <script setup>
  77. import "@/assets/fonts/shelffont.css";
  78. import { useBookStore } from "@/store";
  79. import githubUrl from "@/assets/imgs/github.png";
  80. import { useLoading } from "@/hooks/loading";
  81. import { Search } from "@element-plus/icons-vue";
  82. import API from "@api";
  83. const store = useBookStore();
  84. const { connectStatus, connectType, newConnect, shelf } = storeToRefs(store);
  85. const theme = computed(() => {
  86. return store.config.theme;
  87. });
  88. const isNight = computed(() => theme.value == 6);
  89. const readingRecent = ref({
  90. name: "尚无阅读记录",
  91. author: "",
  92. url: "",
  93. chapterIndex: 0,
  94. chapterPos: 0,
  95. });
  96. const shelfWrapper = ref(null);
  97. const { showLoading, closeLoading, loadingWrapper } = useLoading(
  98. shelfWrapper,
  99. "正在获取书籍信息",
  100. );
  101. const books = ref([]);
  102. const search = ref("");
  103. const isSearching = ref(false);
  104. watchEffect(() => {
  105. if (isSearching.value && search.value != "") return;
  106. isSearching.value = false;
  107. books.value = [];
  108. if (search.value == "") {
  109. books.value = shelf.value;
  110. return;
  111. }
  112. books.value = shelf.value.filter((book) => {
  113. return (
  114. book.name.includes(search.value) || book.author.includes(search.value)
  115. );
  116. });
  117. });
  118. const searchBook = () => {
  119. if (search.value == "") return;
  120. books.value = [];
  121. store.clearSearchBooks();
  122. showLoading();
  123. isSearching.value = true;
  124. API.search(
  125. search.value,
  126. (data) => {
  127. closeLoading();
  128. try {
  129. store.setSearchBooks(JSON.parse(data));
  130. store.searchBooks.forEach((item) => books.value.push(item));
  131. } catch (e) {
  132. ElMessage.error("后端数据错误");
  133. throw e;
  134. }
  135. },
  136. () => {
  137. closeLoading();
  138. if (books.value.length == 0) {
  139. ElMessage.info("搜索结果为空");
  140. }
  141. },
  142. );
  143. };
  144. const setIP = () => {};
  145. const router = useRouter();
  146. const handleBookClick = async (book) => {
  147. const {
  148. bookUrl,
  149. name,
  150. author,
  151. durChapterIndex = 0,
  152. durChapterPos = 0,
  153. } = book;
  154. await API.saveBook(book);
  155. toDetail(bookUrl, name, author, durChapterIndex, durChapterPos);
  156. };
  157. const toDetail = (bookUrl, bookName, bookAuthor, chapterIndex, chapterPos) => {
  158. if (bookName === "尚无阅读记录") return;
  159. sessionStorage.setItem("bookUrl", bookUrl);
  160. sessionStorage.setItem("bookName", bookName);
  161. sessionStorage.setItem("bookAuthor", bookAuthor);
  162. sessionStorage.setItem("chapterIndex", chapterIndex);
  163. sessionStorage.setItem("chapterPos", chapterPos);
  164. readingRecent.value = {
  165. name: bookName,
  166. author: bookAuthor,
  167. url: bookUrl,
  168. chapterIndex: chapterIndex,
  169. chapterPos: chapterPos,
  170. };
  171. localStorage.setItem("readingRecent", JSON.stringify(readingRecent.value));
  172. router.push({
  173. path: "/chapter",
  174. });
  175. };
  176. onMounted(() => {
  177. //获取最近阅读书籍
  178. let readingRecentStr = localStorage.getItem("readingRecent");
  179. if (readingRecentStr != null) {
  180. readingRecent.value = JSON.parse(readingRecentStr);
  181. if (typeof readingRecent.value.chapterIndex == "undefined") {
  182. readingRecent.value.chapterIndex = 0;
  183. }
  184. }
  185. loadingWrapper(
  186. store
  187. .saveBookProgress()
  188. //确保各种网络情况下同步请求先完成
  189. .finally(fetchBookShelfData),
  190. );
  191. });
  192. const fetchBookShelfData = () => {
  193. return API.getBookShelf()
  194. .then((response) => {
  195. store.setConnectType("success");
  196. if (response.data.isSuccess) {
  197. //store.increaseBookNum(response.data.data.length);
  198. store.addBooks(
  199. response.data.data.sort(function (a, b) {
  200. var x = a["durChapterTime"] || 0;
  201. var y = b["durChapterTime"] || 0;
  202. return y - x;
  203. }),
  204. );
  205. } else {
  206. ElMessage.error(response.data.errorMsg);
  207. }
  208. store.setConnectStatus("已连接 ");
  209. store.setNewConnect(false);
  210. })
  211. .catch(function (error) {
  212. store.setConnectType("danger");
  213. store.setConnectStatus("连接失败");
  214. ElMessage.error("后端连接失败");
  215. store.setNewConnect(false);
  216. throw error;
  217. });
  218. };
  219. </script>
  220. <style lang="scss" scoped>
  221. .index-wrapper {
  222. height: 100%;
  223. width: 100%;
  224. display: flex;
  225. flex-direction: row;
  226. .navigation-wrapper {
  227. width: 260px;
  228. min-width: 260px;
  229. padding: 48px 36px;
  230. background-color: #f7f7f7;
  231. .navigation-title {
  232. font-size: 24px;
  233. font-weight: 500;
  234. font-family: FZZCYSK;
  235. }
  236. .navigation-sub-title {
  237. font-size: 16px;
  238. font-weight: 300;
  239. font-family: FZZCYSK;
  240. margin-top: 16px;
  241. color: #b1b1b1;
  242. }
  243. .search-wrapper {
  244. .search-input {
  245. border-radius: 50%;
  246. margin-top: 24px;
  247. :deep(.el-input__wrapper) {
  248. border-radius: 50px;
  249. border-color: #e3e3e3;
  250. }
  251. }
  252. }
  253. .bottom-wrapper {
  254. display: flex;
  255. flex-direction: column;
  256. }
  257. .recent-wrapper {
  258. margin-top: 36px;
  259. .recent-title {
  260. font-size: 14px;
  261. color: #b1b1b1;
  262. font-family: FZZCYSK;
  263. }
  264. .reading-recent {
  265. margin: 18px 0;
  266. .recent-book {
  267. font-size: 10px;
  268. // font-weight: 400;
  269. // margin: 12px 0;
  270. // font-weight: 500;
  271. // color: #6B7C87;
  272. cursor: pointer;
  273. // padding: 6px 18px;
  274. }
  275. }
  276. }
  277. .setting-wrapper {
  278. margin-top: 36px;
  279. .setting-title {
  280. font-size: 14px;
  281. color: #b1b1b1;
  282. font-family: FZZCYSK;
  283. }
  284. .no-point {
  285. pointer-events: none;
  286. }
  287. .setting-connect {
  288. font-size: 8px;
  289. margin-top: 16px;
  290. // color: #6B7C87;
  291. cursor: pointer;
  292. }
  293. }
  294. .bottom-icons {
  295. position: fixed;
  296. bottom: 0;
  297. height: 120px;
  298. width: 260px;
  299. align-items: center;
  300. display: flex;
  301. flex-direction: row;
  302. }
  303. }
  304. .shelf-wrapper {
  305. padding: 48px 48px;
  306. width: 100%;
  307. display: flex;
  308. flex-direction: column;
  309. box-sizing: border-box;
  310. overflow: hidden;
  311. }
  312. }
  313. @media screen and (max-width: 750px) {
  314. .index-wrapper {
  315. overflow-x: hidden;
  316. flex-direction: column;
  317. .navigation-wrapper {
  318. padding: 20px 24px;
  319. box-sizing: border-box;
  320. width: 100%;
  321. .navigation-title-wrapper {
  322. white-space: nowrap;
  323. display: flex;
  324. justify-content: space-between;
  325. align-items: flex-end;
  326. }
  327. .bottom-wrapper {
  328. flex-direction: row;
  329. > * {
  330. flex-grow: 1;
  331. margin-top: 18px;
  332. .reading-recent,
  333. .setting-item {
  334. margin-bottom: 0px;
  335. }
  336. }
  337. }
  338. .bottom-icons {
  339. display: none;
  340. }
  341. }
  342. .shelf-wrapper {
  343. padding: 0;
  344. flex-grow: 1;
  345. :deep(.el-loading-spinner) {
  346. display: none;
  347. }
  348. }
  349. }
  350. }
  351. .night {
  352. :deep(.navigation-wrapper) {
  353. background-color: #454545;
  354. .navigation-title {
  355. color: #aeaeae;
  356. }
  357. .search-wrapper {
  358. .search-input {
  359. .el-input__wrapper {
  360. background-color: #454545;
  361. }
  362. .el-input__inner {
  363. color: #b1b1b1;
  364. }
  365. }
  366. }
  367. }
  368. :deep(.shelf-wrapper) {
  369. background-color: #161819;
  370. }
  371. }
  372. </style>