setting.vue 19 KB

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