BookD.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <script>
  2. import {getBook, getBookContent, getBookListOfRecommend, setBookStatus} from '@/api/knowledge.js';
  3. export default {
  4. name: 'KnowledgeBookDetail',
  5. data() {
  6. return {
  7. book: {id: '', catalogueList: []},
  8. section: [],
  9. recommend: [],
  10. containerHeight: 0,
  11. selected: {},
  12. };
  13. },
  14. computed: {
  15. collection() {
  16. return +this.book['isBookshelf'] === 1;
  17. },
  18. },
  19. created() {
  20. this.book.id = this.$route.query.id;
  21. this.onPreview();
  22. },
  23. mounted() {
  24. setTimeout(() => {
  25. try { this.containerHeight = `${this.$el.parentElement.getBoundingClientRect().height - 10}px`; } catch (e) { }
  26. }, 200);
  27. },
  28. methods: {
  29. async getRecommend() {
  30. try {
  31. this.recommend = await getBookListOfRecommend(this.book.id);
  32. } catch (e) {}
  33. },
  34. async getBook() {
  35. this.book = await getBook(this.book.id);
  36. let selected;
  37. const get = (list, level = 1) => list.map(item => {
  38. const children = Array.isArray(item.children) ? get(item.children, level + 1) : [];
  39. const isLeaf = !children.length;
  40. const section = {
  41. id: item.catalogueId,
  42. name: item.catalogueName,
  43. children, isLeaf, level,
  44. content: isLeaf ? '' : void 0,
  45. dirty: !isLeaf,
  46. }
  47. if (section.isLeaf && !selected) selected = section;
  48. return section;
  49. });
  50. this.section = get(this.book.catalogueList);
  51. await this.load(selected);
  52. },
  53. async load(section) {
  54. if (!section.isLeaf) return;
  55. this.selected = {...section};
  56. if (!section.dirty) {
  57. try {
  58. section.content = await getBookContent({bookId: this.book.id, catalogueId: section.id});
  59. section.dirty = true;
  60. this.$set(this.selected, 'content', section.content);
  61. this.$set(this.selected, 'dirty', section.dirty);
  62. } catch (e) {}
  63. }
  64. this.$nextTick(() => {
  65. document.querySelector(`#scrolling`).scrollTo({top: 0, behavior: 'smooth'});
  66. });
  67. },
  68. async collected() {
  69. try {
  70. const isBookshelf = this.collection ? '0' : '1';
  71. await setBookStatus(this.book.id, {isBookshelf});
  72. this.$set(this.book, 'isBookshelf', isBookshelf);
  73. this.$message.success('操作成功');
  74. } catch (e) {
  75. }
  76. },
  77. async onPreview(book) {
  78. if (book) {
  79. await this.$router.push({path: `/index/knowledge/book/detail?id=${book.id}`});
  80. this.book = book;
  81. }
  82. try {
  83. await this.getBook();
  84. await this.getRecommend();
  85. } catch (e) {
  86. }
  87. },
  88. },
  89. };
  90. </script>
  91. <template>
  92. <div class="knowledge-book-detail" :style="{height: containerHeight}">
  93. <div class="detail-wrapper">
  94. <div class="header">
  95. <div class="book-wrapper">
  96. <div class="cover">
  97. <div class="name">{{ book.bookName }}</div>
  98. </div>
  99. </div>
  100. <div class="description-wrapper">
  101. <div>
  102. <div style="margin-bottom: 6px;font-size: 18px;font-weight: 700;">{{ book.bookName }}</div>
  103. <div class="content">
  104. <span v-if="book.dynasty">{{ book.dynasty }} · </span>
  105. <span>{{ book.author || '佚名' }}</span>
  106. </div>
  107. </div>
  108. <div style="text-indent: 2em;">{{ book.briefIntroduction }}</div>
  109. <el-button type="primary" plain round size="small" :icon="collection ? 'el-icon-remove-outline' : 'el-icon-circle-plus-outline'" @click="collected()">
  110. {{ collection ? '移出' : '加入' }}书架
  111. </el-button>
  112. </div>
  113. </div>
  114. <el-divider content-position="left">阅读<span v-if="book.readProgress">:{{ book.readProgress }}%</span>
  115. </el-divider>
  116. <div class="book-content-container" :style="{maxHeight: containerHeight}" :class="{scrollable: true}">
  117. <div class="catalogue">
  118. <div v-for="item in section" :key="item.id"
  119. :class="['level-' + item.level, item.isLeaf ? 'section' : 'title']">
  120. <span :class="{active: item.id === selected.id}" @click="load(item)">{{ item.name }}</span>
  121. <div v-for="item in item.children" :key="item.id"
  122. :class="['level-' + item.level, item.isLeaf ? 'section' : 'title']">
  123. <span :class="{active: item.id === selected.id}" @click="load(item)">{{ item.name }}</span>
  124. </div>
  125. </div>
  126. </div>
  127. <div class="content" id="scrolling" :style="{height: containerHeight}" v-loading="!selected.dirty">
  128. <div class="text" v-html="selected.content"></div>
  129. <el-backtop style="right: 300px;" target="#scrolling"></el-backtop>
  130. </div>
  131. </div>
  132. </div>
  133. <el-card header="相关医书">
  134. <div class="book-wrapper" style="margin-bottom: 24px;" v-for="book in recommend" :key="book.id"
  135. @click="onPreview(book)">
  136. <div class="cover">
  137. <div class="name">{{ book.bookName }}</div>
  138. </div>
  139. <div class="content" style="font-weight: 700;">{{ book.bookName }}</div>
  140. <div class="content">
  141. <span v-if="book.dynasty">{{ book.dynasty }}</span>
  142. <span>{{ book.author || '佚名' }}</span>
  143. </div>
  144. </div>
  145. </el-card>
  146. </div>
  147. </template>
  148. <style lang="scss" scoped>
  149. .knowledge-book-detail {
  150. display: flex;
  151. overflow: hidden;
  152. .detail-wrapper {
  153. flex: auto;
  154. overflow-y: auto;
  155. height: 100%;
  156. padding: 0 24px;
  157. .header {
  158. display: flex;
  159. }
  160. .description-wrapper {
  161. display: flex;
  162. flex-direction: column;
  163. justify-content: space-evenly;
  164. margin-left: 24px;
  165. .el-button {
  166. max-width: 120px;
  167. }
  168. }
  169. }
  170. .el-card {
  171. flex: none;
  172. display: flex;
  173. flex-direction: column;
  174. width: 262px;
  175. overflow: hidden;
  176. &::v-deep {
  177. .el-card__header {
  178. flex: none;
  179. }
  180. .el-card__body {
  181. flex: auto;
  182. overflow-y: auto;
  183. }
  184. }
  185. }
  186. .el-divider {
  187. &::v-deep {
  188. .el-divider__text {
  189. background-color: #f5f7f9;
  190. }
  191. }
  192. }
  193. }
  194. .knowledge-book-detail {
  195. &::v-deep {
  196. .el-collapse-item__header {
  197. padding-left: 12px;
  198. }
  199. .el-collapse-item__content {
  200. padding-bottom: 6px;
  201. }
  202. .el-collapse-item__wrap {
  203. padding: 6px 12px;
  204. }
  205. }
  206. }
  207. .book-content-container {
  208. display: flex;
  209. &.scrollable > div {
  210. overflow-y: auto;
  211. }
  212. .catalogue {
  213. flex: 1 0;
  214. user-select: none;
  215. .title {
  216. margin: 24px 0;
  217. color: #213547;
  218. > span {
  219. pointer-events: none;
  220. }
  221. }
  222. .section {
  223. margin: 6px 0;
  224. color: #3c3c3cb3;
  225. &:hover, .active {
  226. cursor: pointer;
  227. color: #5386f6;
  228. }
  229. }
  230. }
  231. .content {
  232. flex: 4 4;
  233. > .text {
  234. padding: 24px;
  235. line-height: 2.5;
  236. white-space: pre-wrap;
  237. }
  238. }
  239. ::v-deep.el-loading-mask {
  240. background-color: transparent;
  241. }
  242. }
  243. .book-wrapper {
  244. $zoom: 0.6;
  245. cursor: pointer;
  246. .cover {
  247. position: relative;
  248. width: 272px * $zoom;
  249. height: 400px * $zoom;
  250. background: url("../../assets/book-cover.png") no-repeat center / 100%;
  251. }
  252. .name {
  253. position: absolute;
  254. top: 30px * $zoom;
  255. right: 40px * $zoom;
  256. display: flex;
  257. justify-content: center;
  258. align-items: center;
  259. width: 48px * $zoom;
  260. height: 234px * $zoom;
  261. font-size: max(24px * $zoom, 12px);
  262. font-weight: 700;
  263. letter-spacing: 0.3em;
  264. -ms-writing-mode: tb-rl;
  265. writing-mode: vertical-rl;
  266. }
  267. .content {
  268. margin: 8px 0;
  269. font-size: 16px;
  270. span + span::before {
  271. content: '·';
  272. margin: 0 3px;
  273. }
  274. }
  275. }
  276. </style>