chapter.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <div class="chapter">
  3. <div class="chapter-top">
  4. <div class="catalogue">
  5. <i class="el-icon-arrow-left" @click="goBack"></i>
  6. <div class="name">
  7. <span>{{ getCatalogueName() }}</span>
  8. </div>
  9. <el-popover v-model="visibleStatus" placement="bottom" trigger="click">
  10. <CatalogueTree :nodes="nodes" />
  11. <span slot="reference" class="pointer"><SvgIcon icon-class="menu" /> 目录</span>
  12. </el-popover>
  13. </div>
  14. <div class="operation">
  15. <el-button type="primary" class="add" @click="addCoursewareToBook">
  16. <SvgIcon icon-class="artboard" /> 添加教材内容
  17. </el-button>
  18. <el-button class="preview">
  19. <SvgIcon icon-class="browse" @click="enterPreview" /><span @click="enterPreview">预览</span>
  20. <el-button type="primary"><SvgIcon icon-class="save" />保存</el-button>
  21. </el-button>
  22. </div>
  23. </div>
  24. <div
  25. v-for="({ courseware_id }, i) in courseware_list"
  26. :key="courseware_id"
  27. class="book-content"
  28. @dblclick="enterCourseware(courseware_id)"
  29. >
  30. <div class="book-content-top">
  31. <span class="book-content-title">教材内容 {{ i + 1 }}</span>
  32. <SvgIcon icon-class="delete" @click="deleteCourseware(courseware_id)" />
  33. <SvgIcon icon-class="setup" />
  34. </div>
  35. <div class="tip">
  36. <span>双击开始编辑教材内容 {{ i + 1 }}</span>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import { GetBookChapterStruct, GetCoursewareList_Chapter, AddCoursewareToBook, DeleteCourseware } from '@/api/book';
  43. import CatalogueTree from './components/catalogueTree.vue';
  44. export default {
  45. name: 'ChapterPage',
  46. components: {
  47. CatalogueTree,
  48. },
  49. provide() {
  50. return {
  51. selectNode: this.selectNode,
  52. getCurChaterId: () => this.curChapterId,
  53. };
  54. },
  55. data() {
  56. const { book_id, chapter_id } = this.$route.query;
  57. return {
  58. chapter_id,
  59. curChapterId: chapter_id, // 当前章节id
  60. book_id,
  61. visibleStatus: false,
  62. curPosition: [], // 当前位置
  63. nodes: [],
  64. courseware_list: [],
  65. };
  66. },
  67. created() {
  68. GetBookChapterStruct({ book_id: this.book_id, node_deep_mode: 0 }).then(({ nodes }) => {
  69. this.nodes = nodes ?? [];
  70. this.setCurPosition(this.chapter_id);
  71. });
  72. this.getCoursewareList_Chapter(this.chapter_id);
  73. },
  74. methods: {
  75. goBack() {
  76. this.$router.push(`/book/setting/${this.book_id}`);
  77. },
  78. enterPreview() {
  79. this.$router.push(`/preview/courseware/${this.book_id}?chapter_id=${this.curChapterId}`);
  80. },
  81. enterCourseware(courseware_id) {
  82. this.$router.push({
  83. path: `/courseware/create/${courseware_id}`,
  84. query: { book_id: this.book_id, chapter_id: this.chapter_id },
  85. });
  86. },
  87. getNodeName(index) {
  88. let node = this.nodes;
  89. for (let i = 0; i <= index; i++) {
  90. node = i === 0 ? node[this.curPosition[i]] : node.nodes[this.curPosition[i]];
  91. }
  92. return node?.name;
  93. },
  94. getCatalogueName() {
  95. return this.curPosition.map((item, index) => this.getNodeName(index)).join(' / ');
  96. },
  97. getCoursewareList_Chapter(chapter_id) {
  98. GetCoursewareList_Chapter({ chapter_id }).then(({ courseware_list }) => {
  99. this.courseware_list = courseware_list ?? [];
  100. });
  101. },
  102. /**
  103. * 根据节点id查找节点在nodes中的位置
  104. * @param {array} nodes 节点数组
  105. * @param {string} id 节点id
  106. * @param {array} position 节点位置
  107. */
  108. findNodeIndexById(nodes, id, position = []) {
  109. for (let i = 0; i < nodes.length; i++) {
  110. const node = nodes[i];
  111. if (node.id === id) {
  112. position.push(i);
  113. return position;
  114. }
  115. if (node.nodes && node.nodes.length > 0) {
  116. const childPosition = this.findNodeIndexById(node.nodes, id, [...position, i]);
  117. if (childPosition.length > 0) {
  118. return childPosition;
  119. }
  120. }
  121. }
  122. return [];
  123. },
  124. setCurPosition(id) {
  125. this.curPosition = this.findNodeIndexById(this.nodes, id);
  126. },
  127. selectNode(id) {
  128. this.setCurPosition(id);
  129. this.getCoursewareList_Chapter(id);
  130. this.curChapterId = id;
  131. this.visibleStatus = false;
  132. },
  133. addCoursewareToBook() {
  134. AddCoursewareToBook({ book_id: this.book_id, chapter_id: this.curChapterId, name: '教材内容' }).then(() => {
  135. this.getCoursewareList_Chapter(this.curChapterId);
  136. this.$message.success('添加成功');
  137. });
  138. },
  139. deleteCourseware(id) {
  140. this.$confirm('是否删除该教材内容?', '提示', {
  141. confirmButtonText: '确定',
  142. cancelButtonText: '取消',
  143. type: 'warning',
  144. })
  145. .then(() => {
  146. DeleteCourseware({ id })
  147. .then(() => {
  148. this.getCoursewareList_Chapter(id);
  149. this.$message.success('删除成功');
  150. })
  151. .catch(() => {
  152. this.$message.error('删除失败');
  153. });
  154. })
  155. .catch(() => {});
  156. },
  157. },
  158. };
  159. </script>
  160. <style lang="scss" scoped>
  161. .chapter {
  162. display: flex;
  163. flex-direction: column;
  164. row-gap: 16px;
  165. width: 100%;
  166. height: 100%;
  167. padding: 24px;
  168. background: url('~@/assets/mask_group.png') repeat 0 0;
  169. &-top {
  170. display: flex;
  171. align-items: center;
  172. justify-content: space-between;
  173. margin-bottom: 8px;
  174. .catalogue {
  175. display: flex;
  176. column-gap: 8px;
  177. align-items: center;
  178. min-width: 240px;
  179. height: 40px;
  180. padding: 8px 16px;
  181. font-size: 14px;
  182. font-weight: bold;
  183. background-color: #fff;
  184. border-radius: 4px;
  185. box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 8%);
  186. .name {
  187. flex: 1;
  188. margin-right: 6px;
  189. }
  190. }
  191. .operation {
  192. display: flex;
  193. .el-button {
  194. height: 40px;
  195. font-size: 16px;
  196. font-weight: bold;
  197. :deep > span {
  198. display: flex;
  199. column-gap: 4px;
  200. align-items: center;
  201. }
  202. }
  203. .preview {
  204. padding: 3px;
  205. :deep > span {
  206. padding-left: 12px;
  207. > .el-button {
  208. height: 32px;
  209. margin-left: 12px;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. .book-content {
  216. height: 550px;
  217. padding: 8px 12px;
  218. cursor: pointer;
  219. background: rgba(241, 246, 255, 44%);
  220. border: 1px solid #005aff;
  221. border-radius: 4px;
  222. &-top {
  223. display: flex;
  224. column-gap: 8px;
  225. align-items: center;
  226. .book-content-title {
  227. font-size: 14px;
  228. font-weight: bold;
  229. color: #1c6cff;
  230. }
  231. .svg-icon {
  232. color: #babbbe;
  233. }
  234. }
  235. .tip {
  236. display: flex;
  237. align-items: center;
  238. justify-content: center;
  239. width: 100%;
  240. height: calc(100% - 42px);
  241. font-size: 14px;
  242. font-weight: bold;
  243. color: #000;
  244. }
  245. }
  246. }
  247. </style>