chapter.vue 6.6 KB

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