chapter.vue 8.3 KB

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