BookShelf.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. try {
  128. store.setSearchBooks(JSON.parse(data));
  129. store.searchBooks.forEach((item) => books.value.push(item));
  130. } catch (e) {
  131. ElMessage.error("后端数据错误");
  132. throw e;
  133. }
  134. },
  135. () => {
  136. closeLoading();
  137. if (books.value.length == 0) {
  138. ElMessage.info("搜索结果为空");
  139. }
  140. }
  141. );
  142. };
  143. const setIP = () => {};
  144. const router = useRouter();
  145. const handleBookClick = async (book) => {
  146. const {
  147. bookUrl,
  148. name,
  149. author,
  150. durChapterIndex = 0,
  151. durChapterPos = 0,
  152. } = book;
  153. await API.saveBook(book);
  154. toDetail(bookUrl, name, author, durChapterIndex, durChapterPos);
  155. };
  156. const toDetail = (bookUrl, bookName, bookAuthor, chapterIndex, chapterPos) => {
  157. if (bookName === "尚无阅读记录") return;
  158. sessionStorage.setItem("bookUrl", bookUrl);
  159. sessionStorage.setItem("bookName", bookName);
  160. sessionStorage.setItem("bookAuthor", bookAuthor);
  161. sessionStorage.setItem("chapterIndex", chapterIndex);
  162. sessionStorage.setItem("chapterPos", chapterPos);
  163. readingRecent.value = {
  164. name: bookName,
  165. author: bookAuthor,
  166. url: bookUrl,
  167. chapterIndex: chapterIndex,
  168. chapterPos: chapterPos,
  169. };
  170. localStorage.setItem("readingRecent", JSON.stringify(readingRecent.value));
  171. router.push({
  172. path: "/chapter",
  173. });
  174. };
  175. onMounted(() => {
  176. //获取最近阅读书籍
  177. let readingRecentStr = localStorage.getItem("readingRecent");
  178. if (readingRecentStr != null) {
  179. readingRecent.value = JSON.parse(readingRecentStr);
  180. if (typeof readingRecent.value.chapterIndex == "undefined") {
  181. readingRecent.value.chapterIndex = 0;
  182. }
  183. }
  184. loadingWrapper(
  185. store
  186. .saveBookProgress()
  187. //确保各种网络情况下同步请求先完成
  188. .finally(fetchBookShelfData)
  189. );
  190. });
  191. const fetchBookShelfData = () => {
  192. return API.getBookShelf()
  193. .then((response) => {
  194. store.setConnectType("success");
  195. if (response.data.isSuccess) {
  196. //store.increaseBookNum(response.data.data.length);
  197. store.addBooks(
  198. response.data.data.sort(function (a, b) {
  199. var x = a["durChapterTime"] || 0;
  200. var y = b["durChapterTime"] || 0;
  201. return y - x;
  202. })
  203. );
  204. } else {
  205. ElMessage.error(response.data.errorMsg);
  206. }
  207. store.setConnectStatus("已连接 ");
  208. store.setNewConnect(false);
  209. })
  210. .catch(function (error) {
  211. store.setConnectType("danger");
  212. store.setConnectStatus("连接失败");
  213. ElMessage.error("后端连接失败");
  214. store.setNewConnect(false);
  215. throw error;
  216. });
  217. };
  218. </script>
  219. <style lang="scss" scoped>
  220. .index-wrapper {
  221. height: 100%;
  222. width: 100%;
  223. display: flex;
  224. flex-direction: row;
  225. .navigation-wrapper {
  226. width: 260px;
  227. min-width: 260px;
  228. padding: 48px 36px;
  229. background-color: #f7f7f7;
  230. .navigation-title {
  231. font-size: 24px;
  232. font-weight: 500;
  233. font-family: FZZCYSK;
  234. }
  235. .navigation-sub-title {
  236. font-size: 16px;
  237. font-weight: 300;
  238. font-family: FZZCYSK;
  239. margin-top: 16px;
  240. color: #b1b1b1;
  241. }
  242. .search-wrapper {
  243. .search-input {
  244. border-radius: 50%;
  245. margin-top: 24px;
  246. :deep(.el-input__wrapper) {
  247. border-radius: 50px;
  248. border-color: #e3e3e3;
  249. }
  250. }
  251. }
  252. .bottom-wrapper {
  253. display: flex;
  254. flex-direction: column;
  255. }
  256. .recent-wrapper {
  257. margin-top: 36px;
  258. .recent-title {
  259. font-size: 14px;
  260. color: #b1b1b1;
  261. font-family: FZZCYSK;
  262. }
  263. .reading-recent {
  264. margin: 18px 0;
  265. .recent-book {
  266. font-size: 10px;
  267. // font-weight: 400;
  268. // margin: 12px 0;
  269. // font-weight: 500;
  270. // color: #6B7C87;
  271. cursor: pointer;
  272. // padding: 6px 18px;
  273. }
  274. }
  275. }
  276. .setting-wrapper {
  277. margin-top: 36px;
  278. .setting-title {
  279. font-size: 14px;
  280. color: #b1b1b1;
  281. font-family: FZZCYSK;
  282. }
  283. .no-point {
  284. pointer-events: none;
  285. }
  286. .setting-connect {
  287. font-size: 8px;
  288. margin-top: 16px;
  289. // color: #6B7C87;
  290. cursor: pointer;
  291. }
  292. }
  293. .bottom-icons {
  294. position: fixed;
  295. bottom: 0;
  296. height: 120px;
  297. width: 260px;
  298. align-items: center;
  299. display: flex;
  300. flex-direction: row;
  301. }
  302. }
  303. .shelf-wrapper {
  304. padding: 48px 48px;
  305. width: 100%;
  306. display: flex;
  307. flex-direction: column;
  308. box-sizing: border-box;
  309. overflow: hidden;
  310. }
  311. }
  312. @media screen and (max-width: 750px) {
  313. .index-wrapper {
  314. overflow-x: hidden;
  315. flex-direction: column;
  316. .navigation-wrapper {
  317. padding: 20px 24px;
  318. box-sizing: border-box;
  319. width: 100%;
  320. .navigation-title-wrapper {
  321. white-space: nowrap;
  322. display: flex;
  323. justify-content: space-between;
  324. align-items: flex-end;
  325. }
  326. .bottom-wrapper {
  327. flex-direction: row;
  328. > * {
  329. flex-grow: 1;
  330. margin-top: 18px;
  331. .reading-recent,
  332. .setting-item {
  333. margin-bottom: 0px;
  334. }
  335. }
  336. }
  337. .bottom-icons {
  338. display: none;
  339. }
  340. }
  341. .shelf-wrapper {
  342. padding: 0;
  343. flex-grow: 1;
  344. :deep(.el-loading-spinner) {
  345. display: none;
  346. }
  347. }
  348. }
  349. }
  350. .night {
  351. :deep(.navigation-wrapper) {
  352. background-color: #454545;
  353. .navigation-title {
  354. color: #aeaeae;
  355. }
  356. .search-wrapper {
  357. .search-input {
  358. .el-input__wrapper {
  359. background-color: #454545;
  360. }
  361. .el-input__inner {
  362. color: #b1b1b1;
  363. }
  364. }
  365. }
  366. }
  367. :deep(.shelf-wrapper) {
  368. background-color: #161819;
  369. }
  370. }
  371. </style>