setting.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <template>
  2. <div class="setting">
  3. <div class="breadcrumb">
  4. <ul>
  5. <li>
  6. <span class="pointer" @click="$router.push('/')">教材管理</span>
  7. </li>
  8. <li>
  9. <span class="separator">></span>
  10. <span class="breadcrumb-name">{{ data.name }}</span>
  11. </li>
  12. </ul>
  13. </div>
  14. <main class="setting-wrapper">
  15. <div class="basic-info">
  16. <div class="editor">
  17. <div class="title">基本信息</div>
  18. <el-button @click="edit"><SvgIcon icon-class="edit" /> 编辑</el-button>
  19. </div>
  20. <el-image :src="data.picture_url" class="cover-image">
  21. <div slot="error" class="image-slot">
  22. <i class="el-icon-picture-outline"></i>
  23. </div>
  24. </el-image>
  25. <div class="name">{{ data.name }}</div>
  26. <div class="brief-introduction">{{ data.description }}</div>
  27. </div>
  28. <div class="catalogue-wrapper" :style="{ backgroundColor: isEdit ? 'inherit' : '#fff' }">
  29. <!-- 显示区域 -->
  30. <template v-if="!isEdit">
  31. <div class="catalogue-top">
  32. <div class="title">目录</div>
  33. <div class="operation">
  34. <!-- <el-button><SvgIcon icon-class="palette" /> 主色设置</el-button> -->
  35. <el-button @click="isEdit = true"><SvgIcon icon-class="edit" /> 编辑目录</el-button>
  36. <el-button @click="enterPreview"><SvgIcon icon-class="browse" /> 预览</el-button>
  37. </div>
  38. </div>
  39. <div v-for="{ id, name, nodes: children } in nodes" :key="id" class="catalogue">
  40. <!-- 一级目录 -->
  41. <div class="catalogue-title">{{ name }}</div>
  42. <template v-for="item in children">
  43. <!-- 二级目录或内容 -->
  44. <div
  45. :key="item.id"
  46. :class="['catalogue-item', item.is_leaf_chapter === 'true' ? 'content' : 'subdirectory']"
  47. >
  48. <span class="name">{{ item.name }}</span>
  49. <span class="time">{{ item.time }} {{ item.editor }}</span>
  50. <span v-if="item.is_leaf_chapter === 'true'" class="edit" @click="editBookContent(item.id)">编辑</span>
  51. </div>
  52. <!-- 二级目录的内容 -->
  53. <template v-if="item.is_leaf_chapter === 'false' && item.nodes?.length > 0">
  54. <div v-for="li in item.nodes" :key="li.id">
  55. <div :class="['catalogue-item', 'children']">
  56. <span class="name">{{ li?.name }}</span>
  57. <span class="time">{{ li?.time }} {{ li.editor }}</span>
  58. <span v-if="li.is_leaf_chapter === 'true'" class="edit" @click="editBookContent(li.id)">编辑</span>
  59. </div>
  60. </div>
  61. </template>
  62. </template>
  63. </div>
  64. </template>
  65. <!-- 编辑区域 -->
  66. <div v-else class="catalogue-edit">
  67. <div class="catalogue-edit-top">
  68. <span class="title">编辑目录</span>
  69. <div class="operation">
  70. <el-button class="cancel" @click="cancel">取消</el-button>
  71. <el-button type="primary" @click="complete">完成</el-button>
  72. </div>
  73. </div>
  74. <div class="nodes">
  75. <template v-for="item in nodes">
  76. <!-- 一级目录 -->
  77. <div v-if="item.id === curEditNodeId" :key="item.id" class="nodes-edit">
  78. <el-input v-model="item.name" placeholder="请输入标题" />
  79. <el-button type="primary" class="confirm-edit" @click="confirmEditNode(item.id, '', 'false')">
  80. 确定
  81. </el-button>
  82. <el-button @click="cancelEditNode(item.id)">取消</el-button>
  83. </div>
  84. <div v-else :key="item.id" class="nodes-item">
  85. <span class="title">{{ item.name }}</span>
  86. <span class="operation">
  87. <el-dropdown trigger="click">
  88. <span class="el-dropdown-link">
  89. <SvgIcon icon-class="add-rectangle" />
  90. </span>
  91. <el-dropdown-menu slot="dropdown">
  92. <el-dropdown-item @click.native="createChildCatalogue(item.id, 'false')">子目录</el-dropdown-item>
  93. <el-dropdown-item @click.native="createChildCatalogue(item.id, 'true')">内容</el-dropdown-item>
  94. </el-dropdown-menu>
  95. </el-dropdown>
  96. <SvgIcon icon-class="edit" @click="setCurEditNodeId(item.id)" />
  97. <SvgIcon icon-class="delete-2" @click="deleteChapter(item.id, 'false')" />
  98. </span>
  99. </div>
  100. <!-- 子目录及内容 -->
  101. <template v-if="item.is_leaf_chapter === 'false' && item.nodes?.length > 0">
  102. <!-- 子目录 -->
  103. <template v-for="li in item.nodes">
  104. <div v-if="li.id === curEditNodeId" :key="li.id" class="nodes-edit">
  105. <el-input v-model="li.name" placeholder="请输入标题" />
  106. <el-button
  107. type="primary"
  108. class="confirm-edit"
  109. @click="confirmEditNode(li.id, item.id, li.is_leaf_chapter)"
  110. >
  111. 确定
  112. </el-button>
  113. <el-button @click="cancelEditNode(li.id)">取消</el-button>
  114. </div>
  115. <div
  116. v-else
  117. :key="li.id"
  118. :class="['nodes-item', 'children', li.is_leaf_chapter === 'true' ? '' : 'subdirectory']"
  119. >
  120. <span class="name">{{ li.name }}</span>
  121. <span class="operation">
  122. <el-dropdown trigger="click">
  123. <span class="el-dropdown-link">
  124. <SvgIcon icon-class="add-rectangle" />
  125. </span>
  126. <el-dropdown-menu slot="dropdown">
  127. <el-dropdown-item
  128. v-if="li.is_leaf_chapter === 'true'"
  129. @click.native="createChildCatalogue(item.id, 'false')"
  130. >子目录</el-dropdown-item
  131. >
  132. <el-dropdown-item
  133. @click.native="
  134. createChildCatalogue(li.is_leaf_chapter === 'true' ? item.id : li.id, 'true')
  135. "
  136. >内容</el-dropdown-item
  137. >
  138. </el-dropdown-menu>
  139. </el-dropdown>
  140. <SvgIcon icon-class="edit" @click="setCurEditNodeId(li.id)" />
  141. <SvgIcon icon-class="delete-2" @click="deleteChapter(li.id, 'false')" />
  142. </span>
  143. </div>
  144. <!-- 内容 -->
  145. <template v-if="li.is_leaf_chapter === 'false' && li.nodes?.length > 0">
  146. <template v-for="child in li.nodes">
  147. <div v-if="child.id === curEditNodeId" :key="child.id" class="nodes-edit">
  148. <el-input v-model="child.name" placeholder="请输入标题" />
  149. <el-button
  150. type="primary"
  151. class="confirm-edit"
  152. @click="confirmEditNode(child.id, li.id, child.is_leaf_chapter)"
  153. >
  154. 确定
  155. </el-button>
  156. <el-button @click="cancelEditNode(child.id)">取消</el-button>
  157. </div>
  158. <div v-else :key="child.id" :class="['nodes-item', 'children']" :style="{ paddingLeft: '32px' }">
  159. <span class="name">{{ child.name }}</span>
  160. <span class="operation">
  161. <el-dropdown trigger="click">
  162. <span class="el-dropdown-link">
  163. <SvgIcon icon-class="add-rectangle" />
  164. </span>
  165. <el-dropdown-menu slot="dropdown">
  166. <el-dropdown-item
  167. @click.native="
  168. createChildCatalogue(child.is_leaf_chapter === 'true' ? li.id : child.id, 'true')
  169. "
  170. >内容</el-dropdown-item
  171. >
  172. </el-dropdown-menu>
  173. </el-dropdown>
  174. <SvgIcon icon-class="edit" @click="setCurEditNodeId(child.id)" />
  175. <SvgIcon icon-class="delete-2" @click="deleteChapter(child.id, 'true')" />
  176. </span>
  177. </div>
  178. </template>
  179. </template>
  180. </template>
  181. </template>
  182. </template>
  183. </div>
  184. <el-button type="primary" @click="createCatalogue"><SvgIcon icon-class="add-rectangle" /> 新建目录</el-button>
  185. </div>
  186. </div>
  187. </main>
  188. </div>
  189. </template>
  190. <script>
  191. import { GetBook, GetBookChapterStruct, AddChapterToBook, UpdateChapter, DeleteChapter } from '@/api/book';
  192. export default {
  193. name: 'SettingPage',
  194. data() {
  195. return {
  196. book_id: this.$route.params.book_id,
  197. isEdit: false, // 是否编辑状态
  198. curEditNodeId: '', // 当前编辑的节点id
  199. data: {
  200. picture_url: '',
  201. picture_id: '',
  202. name: '',
  203. author: '',
  204. current_price: '',
  205. original_price: '',
  206. label_name_list: '',
  207. type_id: '',
  208. description: '',
  209. },
  210. nodes: [],
  211. loading: false,
  212. };
  213. },
  214. created() {
  215. GetBook({ id: this.book_id }).then(({ name, picture_id, picture_url, author, type_id, price, description }) => {
  216. this.data = {
  217. name,
  218. picture_id,
  219. picture_url,
  220. author,
  221. type_id,
  222. current_price: price,
  223. original_price: '0.00',
  224. label_name_list: [],
  225. description,
  226. };
  227. });
  228. this.getBookChapterStruct();
  229. },
  230. methods: {
  231. edit() {
  232. this.$router.push({ path: '/book/create', query: { book_id: this.book_id } });
  233. },
  234. enterPreview() {
  235. this.$router.push(`/preview/courseware/${this.book_id}`);
  236. },
  237. editBookContent(chapter_id) {
  238. this.$router.push({ path: '/chapter', query: { chapter_id, book_id: this.book_id } });
  239. },
  240. getBookChapterStruct() {
  241. GetBookChapterStruct({ book_id: this.book_id, node_deep_mode: 0 }).then(({ nodes }) => {
  242. this.nodes = nodes ?? [];
  243. this.loading = false;
  244. });
  245. },
  246. /**
  247. * @description 确认编辑节点
  248. * @param {string} id 节点id
  249. * @param {string} parent_id 父节点id
  250. * @param {string} is_leaf_chapter 是否是内容目录
  251. */
  252. confirmEditNode(id, parent_id, is_leaf_chapter) {
  253. const position = this.findNodeIndexById(this.nodes, id);
  254. let nodes = this.nodes;
  255. for (let i = 0; i < position.length - 1; i++) {
  256. nodes = nodes[position[i]].nodes;
  257. }
  258. const name = nodes[position[position.length - 1]].name;
  259. if (name.length <= 0) {
  260. this.$message.error('请输入名称');
  261. // 非编辑状态下删除临时节点
  262. if (!this.isEdit) {
  263. this.cancelEditNode(id);
  264. }
  265. return;
  266. }
  267. if (nodes[position[position.length - 1]]?.temporary) {
  268. this.addChapterToBook(name, parent_id, is_leaf_chapter);
  269. } else {
  270. this.updateChapter(id, name);
  271. }
  272. },
  273. addChapterToBook(name, parent_id, is_leaf) {
  274. if (this.loading) return;
  275. this.loading = true;
  276. AddChapterToBook({ name, book_id: this.book_id, parent_id, is_leaf }).then(() => {
  277. this.getBookChapterStruct();
  278. });
  279. },
  280. updateChapter(id, name) {
  281. if (this.loading) return;
  282. this.loading = true;
  283. UpdateChapter({ id, name }).then(() => {
  284. this.getBookChapterStruct();
  285. this.curEditNodeId = '';
  286. });
  287. },
  288. /**
  289. * @description 删除目录
  290. * @param {string} id 目录id
  291. * @param {'true' | 'false'} is_force_delete 是否强制删除
  292. */
  293. deleteChapter(id, is_force_delete) {
  294. this.$confirm('确定删除该目录吗?', '提示', {
  295. confirmButtonText: '确定',
  296. cancelButtonText: '取消',
  297. type: 'warning',
  298. })
  299. .then(() => {
  300. DeleteChapter({ id, is_force_delete })
  301. .then(() => {
  302. this.$message.success('删除成功');
  303. this.getBookChapterStruct();
  304. })
  305. .catch(() => {});
  306. })
  307. .catch(() => {});
  308. },
  309. createCatalogue() {
  310. if (this.curEditNodeId) {
  311. this.cancelEditNode(this.curEditNodeId);
  312. }
  313. let id = new Date().getTime();
  314. const level_index = this.nodes.length;
  315. this.nodes.push({
  316. id,
  317. name: '',
  318. is_leaf_chapter: 'false',
  319. level_index,
  320. temporary: true, // 临时节点
  321. nodes: [],
  322. });
  323. this.curEditNodeId = id;
  324. },
  325. /**
  326. * 创建子目录或内容
  327. * @param {string} parent_id 父目录id
  328. * @param {string} is_leaf_chapter 是否是内容目录
  329. */
  330. createChildCatalogue(parent_id, is_leaf_chapter) {
  331. if (this.curEditNodeId) {
  332. this.cancelEditNode(this.curEditNodeId);
  333. }
  334. let id = new Date().getTime();
  335. let position = this.findNodeIndexById(this.nodes, parent_id);
  336. let parent = this.nodes;
  337. for (let i = 0; i < position.length; i++) {
  338. if (i === position.length - 1) {
  339. parent = parent[position[i]];
  340. } else {
  341. parent = parent[position[i]].nodes;
  342. }
  343. }
  344. const level_index = position.join('-');
  345. if (!Object.hasOwn(parent, 'nodes')) {
  346. this.$set(parent, 'nodes', []);
  347. }
  348. parent.nodes.push({
  349. id,
  350. name: '',
  351. is_leaf_chapter,
  352. level_index,
  353. temporary: true, // 临时节点
  354. nodes: [],
  355. });
  356. this.curEditNodeId = id;
  357. },
  358. /**
  359. * 设置当前编辑节点id
  360. */
  361. setCurEditNodeId(id) {
  362. this.curEditNodeId = id;
  363. },
  364. /**
  365. * 根据节点id查找节点在nodes中的位置
  366. * @param {array} nodes 节点数组
  367. * @param {string} id 节点id
  368. * @param {array} position 节点位置
  369. */
  370. findNodeIndexById(nodes, id, position = []) {
  371. for (let i = 0; i < nodes.length; i++) {
  372. const node = nodes[i];
  373. if (node.id === id) {
  374. position.push(i);
  375. return position;
  376. }
  377. if (node.nodes && node.nodes.length > 0) {
  378. const childPosition = this.findNodeIndexById(node.nodes, id, [...position, i]);
  379. if (childPosition.length > 0) {
  380. return childPosition;
  381. }
  382. }
  383. }
  384. return [];
  385. },
  386. /**
  387. * 取消编辑节点
  388. * @param {string} id 节点id
  389. */
  390. cancelEditNode(id) {
  391. const indexArr = this.findNodeIndexById(this.nodes, id);
  392. let nodes = this.nodes;
  393. for (let i = 0; i < indexArr.length - 1; i++) {
  394. nodes = nodes[indexArr[i]].nodes;
  395. }
  396. // 删除临时节点
  397. if (nodes[indexArr[indexArr.length - 1]]?.temporary) {
  398. nodes.splice(indexArr[indexArr.length - 1], 1);
  399. }
  400. this.curEditNodeId = '';
  401. },
  402. // 取消编辑
  403. cancel() {
  404. this.isEdit = false;
  405. if (this.curEditNodeId) {
  406. this.cancelEditNode(this.curEditNodeId);
  407. this.getBookChapterStruct();
  408. }
  409. },
  410. // 完成编辑
  411. complete() {
  412. this.isEdit = false;
  413. // 如果有正在编辑的节点,先保存
  414. if (this.curEditNodeId) {
  415. document.querySelector('.confirm-edit').click();
  416. }
  417. },
  418. },
  419. };
  420. </script>
  421. <style lang="scss" scoped>
  422. %button {
  423. padding: 7px 15px;
  424. :deep > span {
  425. display: flex;
  426. column-gap: 8px;
  427. align-items: center;
  428. }
  429. }
  430. .setting {
  431. min-height: 100%;
  432. padding: 8px 24px;
  433. .breadcrumb {
  434. position: sticky;
  435. top: 8px;
  436. z-index: 1;
  437. display: flex;
  438. align-items: center;
  439. font-size: 14px;
  440. > ul {
  441. display: flex;
  442. li {
  443. font-weight: 400;
  444. color: #949494;
  445. .separator {
  446. margin: 0 8px;
  447. color: #c9cdd4;
  448. }
  449. &:nth-last-child(1) {
  450. font-weight: bold;
  451. color: $font-color;
  452. }
  453. }
  454. }
  455. }
  456. .setting-wrapper {
  457. display: flex;
  458. column-gap: 24px;
  459. justify-content: center;
  460. min-height: calc(100vh - 118px);
  461. margin-top: 16px;
  462. .basic-info {
  463. display: flex;
  464. flex-direction: column;
  465. row-gap: 16px;
  466. width: 276px;
  467. padding: 24px;
  468. background-color: #fff;
  469. border-radius: 4px;
  470. .editor {
  471. display: flex;
  472. align-items: center;
  473. justify-content: space-between;
  474. .title {
  475. font-weight: bold;
  476. color: #000;
  477. }
  478. .el-button {
  479. @extend %button;
  480. }
  481. }
  482. .cover-image {
  483. width: 228px;
  484. height: 320px;
  485. background-color: #f3f3f3;
  486. border: 1px solid #e0e0e0;
  487. :deep .image-slot {
  488. display: flex;
  489. align-items: center;
  490. justify-content: center;
  491. width: 100%;
  492. height: 100%;
  493. i {
  494. font-size: 40px;
  495. color: #c0c4cc;
  496. }
  497. }
  498. }
  499. .name {
  500. font-weight: bold;
  501. color: #000;
  502. }
  503. .brief-introduction {
  504. font-size: 14px;
  505. color: #666;
  506. word-break: break-all;
  507. }
  508. }
  509. .catalogue-wrapper {
  510. display: flex;
  511. flex-direction: column;
  512. row-gap: 16px;
  513. width: 916px;
  514. padding: 24px;
  515. border-radius: 4px;
  516. .catalogue-top {
  517. display: flex;
  518. align-items: center;
  519. justify-content: space-between;
  520. .title {
  521. font-weight: bold;
  522. color: #000;
  523. }
  524. .operation {
  525. .el-button {
  526. @extend %button;
  527. }
  528. }
  529. }
  530. .catalogue {
  531. display: flex;
  532. flex-direction: column;
  533. row-gap: 8px;
  534. &-title {
  535. padding: 8px 0;
  536. font-weight: bold;
  537. color: #000;
  538. }
  539. &-item {
  540. display: flex;
  541. column-gap: 8px;
  542. padding: 8px 52px 8px 16px;
  543. font-size: 14px;
  544. border-bottom: 1px solid #ebebeb;
  545. &:hover {
  546. background-color: #f3f3f3;
  547. }
  548. &.subdirectory {
  549. .name {
  550. font-weight: bold;
  551. }
  552. }
  553. &.children {
  554. padding-left: 32px;
  555. }
  556. .name {
  557. flex: 1;
  558. color: #000;
  559. }
  560. .time,
  561. .edit {
  562. color: #929292;
  563. }
  564. .edit {
  565. margin-left: 24px;
  566. cursor: pointer;
  567. }
  568. }
  569. }
  570. .catalogue-edit {
  571. display: flex;
  572. flex-direction: column;
  573. row-gap: 16px;
  574. &-top {
  575. display: flex;
  576. align-items: center;
  577. justify-content: space-between;
  578. .title {
  579. font-weight: bold;
  580. color: #000;
  581. }
  582. .operation {
  583. .cancel {
  584. background-color: #e7e7e7;
  585. }
  586. .el-button + .el-button {
  587. margin-left: 16px;
  588. }
  589. }
  590. }
  591. .nodes {
  592. display: flex;
  593. flex-direction: column;
  594. row-gap: 8px;
  595. &-edit {
  596. display: flex;
  597. column-gap: 8px;
  598. .el-button + .el-button {
  599. margin-left: 0;
  600. }
  601. .el-input {
  602. :deep .el-input__inner {
  603. background-color: #fff;
  604. }
  605. }
  606. }
  607. &-item {
  608. display: flex;
  609. align-items: center;
  610. justify-content: space-between;
  611. padding-right: 16px;
  612. .title {
  613. flex: 1;
  614. font-weight: bold;
  615. color: #000;
  616. }
  617. &.children {
  618. padding: 8px 16px;
  619. background-color: #fff;
  620. &:hover {
  621. background-color: #f3f3f3;
  622. }
  623. .name {
  624. font-size: 14px;
  625. }
  626. &.subdirectory {
  627. .name {
  628. font-weight: bold;
  629. }
  630. }
  631. }
  632. .operation {
  633. display: flex;
  634. column-gap: 8px;
  635. .svg-icon {
  636. cursor: pointer;
  637. }
  638. }
  639. }
  640. }
  641. }
  642. }
  643. }
  644. }
  645. </style>