index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <div class="preview">
  3. <div class="menu">
  4. <div class="title">目录</div>
  5. <CatalogueTree :nodes="nodes" />
  6. </div>
  7. <div class="content-wrapper">
  8. <div class="exit">
  9. <el-button icon="el-icon-close" @click="goBack">退出预览</el-button>
  10. </div>
  11. <template v-for="(data, index) in coursewareDataList">
  12. <div v-if="data.hasOwnProperty('row_list')" :key="`data-${index}`" class="content">
  13. <span class="content-title">
  14. <SvgIcon icon-class="menu-2" size="24" />
  15. <template v-for="(item, m) in menuList">
  16. <span :key="m">{{ item }}</span>
  17. <span v-if="index < menuList.length - 1" :key="`separator-${m}`" class="separator">/</span>
  18. </template>
  19. </span>
  20. <!-- 课件 -->
  21. <div
  22. class="courserware"
  23. :style="[
  24. {
  25. backgroundImage: data.background_image_url ? `url(${data.background_image_url})` : '',
  26. backgroundSize: data.background_image_url
  27. ? `${data.background_position.width}% ${data.background_position.height}%`
  28. : '',
  29. backgroundPosition: data.background_image_url
  30. ? `${data.background_position.left}% ${data.background_position.top}%`
  31. : '',
  32. },
  33. ]"
  34. >
  35. <template v-for="(row, i) in data.row_list">
  36. <div :key="i" class="row" :style="getMultipleColStyle(index, i)">
  37. <!-- 列 -->
  38. <template v-for="(col, j) in row.col_list">
  39. <div :key="j" :class="['col', `col-${i}-${j}`]" :style="computedColStyle(col)">
  40. <!-- 网格 -->
  41. <template v-for="(grid, k) in col.grid_list">
  42. <component
  43. :is="previewComponentMap[grid.type]"
  44. :id="grid.id"
  45. ref="preview"
  46. :key="k"
  47. :courseware_id="data.courseware_id"
  48. :class="[grid.id]"
  49. :style="{
  50. gridArea: grid.grid_area,
  51. height: grid.height,
  52. }"
  53. />
  54. </template>
  55. </div>
  56. </template>
  57. </div>
  58. </template>
  59. </div>
  60. </div>
  61. </template>
  62. </div>
  63. </div>
  64. </template>
  65. <script>
  66. import { GetCoursewareContent, GetBookChapterStruct, GetCoursewareList_Chapter } from '@/api/book';
  67. import { previewComponentMap } from './components/common/data';
  68. import CatalogueTree from '@/views/book/components/catalogueTree.vue';
  69. export default {
  70. name: 'PreviewPage',
  71. components: {
  72. CatalogueTree,
  73. },
  74. provide() {
  75. return {
  76. selectNode: this.selectNode,
  77. getCurChaterId: () => this.curChapterId,
  78. };
  79. },
  80. data() {
  81. const { chapter_id } = this.$route.query;
  82. return {
  83. book_id: this.$route.params.book_id,
  84. chapter_id,
  85. curChapterId: chapter_id,
  86. previewComponentMap,
  87. nodes: [],
  88. curPosition: [],
  89. courseware_list: [],
  90. coursewareDataList: [],
  91. };
  92. },
  93. computed: {
  94. menuList() {
  95. return this.curPosition.map((item, index) => this.getNodeName(index));
  96. },
  97. },
  98. created() {
  99. GetBookChapterStruct({ book_id: this.book_id, node_deep_mode: 0 }).then(({ nodes }) => {
  100. this.nodes = nodes ?? [];
  101. if (this.chapter_id) this.setCurPosition(this.chapter_id);
  102. });
  103. if (this.chapter_id) this.getCoursewareList_Chapter(this.chapter_id);
  104. },
  105. methods: {
  106. goBack() {
  107. this.$router.push(`/chapter?chapter_id=${this.chapter_id}&book_id=${this.book_id}`);
  108. },
  109. /**
  110. * 根据节点id查找节点在nodes中的位置
  111. * @param {array} nodes 节点数组
  112. * @param {string} id 节点id
  113. * @param {array} position 节点位置
  114. */
  115. findNodeIndexById(nodes, id, position = []) {
  116. for (let i = 0; i < nodes.length; i++) {
  117. const node = nodes[i];
  118. if (node.id === id) {
  119. position.push(i);
  120. return position;
  121. }
  122. if (node.nodes && node.nodes.length > 0) {
  123. const childPosition = this.findNodeIndexById(node.nodes, id, [...position, i]);
  124. if (childPosition.length > 0) {
  125. return childPosition;
  126. }
  127. }
  128. }
  129. return [];
  130. },
  131. setCurPosition(id) {
  132. this.curPosition = this.findNodeIndexById(this.nodes, id);
  133. },
  134. selectNode(id) {
  135. this.setCurPosition(id);
  136. this.getCoursewareList_Chapter(id);
  137. this.curChapterId = id;
  138. },
  139. getCoursewareList_Chapter(chapter_id) {
  140. GetCoursewareList_Chapter({ chapter_id }).then(({ courseware_list }) => {
  141. this.courseware_list = courseware_list ?? [];
  142. this.getCoursewareContent();
  143. });
  144. },
  145. async getCoursewareContent() {
  146. this.coursewareDataList = await Promise.all(
  147. this.courseware_list.map(async ({ courseware_id }) => {
  148. const { content } = await GetCoursewareContent({ id: courseware_id });
  149. if (content) {
  150. let _content = JSON.parse(content);
  151. _content.courseware_id = courseware_id;
  152. return _content;
  153. }
  154. return {};
  155. }),
  156. );
  157. },
  158. getNodeName(index) {
  159. let node = this.nodes;
  160. for (let i = 0; i <= index; i++) {
  161. node = i === 0 ? node[this.curPosition[i]] : node.nodes[this.curPosition[i]];
  162. }
  163. return node?.name;
  164. },
  165. getCatalogueName() {
  166. return this.curPosition.map((item, index) => this.getNodeName(index)).join(' / ');
  167. },
  168. getMultipleColStyle(index, i) {
  169. let row = this.coursewareDataList[index].row_list[i];
  170. let col = row.col_list;
  171. if (col.length <= 1) {
  172. return {
  173. gridTemplateColumns: '100%',
  174. };
  175. }
  176. let str = row.width_list
  177. .map((item) => {
  178. return `calc(${item} - ${(16 * (row.width_list.length - 1)) / row.width_list.length}px)`;
  179. })
  180. .join(' 16px ');
  181. let gridTemplateColumns = `${str}`;
  182. return {
  183. gridAutoFlow: 'column',
  184. gridTemplateColumns,
  185. gridTemplateRows: 'auto',
  186. };
  187. },
  188. /**
  189. * 分割整数为多个 1的倍数
  190. * @param {number} num
  191. * @param {number} parts
  192. */
  193. splitInteger(num, parts) {
  194. let base = Math.floor(num / parts);
  195. let arr = Array(parts).fill(base);
  196. let remainder = num - base * parts;
  197. for (let i = 0; remainder > 0; i = (i + 1) % parts) {
  198. arr[i] += 1;
  199. remainder -= 1;
  200. }
  201. return arr;
  202. },
  203. computedColStyle(col) {
  204. const grid = col.grid_list;
  205. let maxCol = 0; // 最大列数
  206. let rowList = new Map();
  207. grid.forEach(({ row }) => {
  208. rowList.set(row, (rowList.get(row) || 0) + 1);
  209. });
  210. let curMaxRow = 0; // 当前数量最大 row 的值
  211. rowList.forEach((value, key) => {
  212. if (value > maxCol) {
  213. maxCol = value;
  214. curMaxRow = key;
  215. }
  216. });
  217. // 计算 grid_template_areas
  218. let gridTemplateAreas = '';
  219. let gridArr = [];
  220. grid.forEach(({ grid_area, row }) => {
  221. if (!gridArr[row - 1]) {
  222. gridArr[row - 1] = [];
  223. }
  224. if (curMaxRow === row) {
  225. gridArr[row - 1].push(`${grid_area}`);
  226. } else {
  227. let filter = grid.filter((item) => item.row === row);
  228. let find = filter.findIndex((item) => item.grid_area === grid_area);
  229. let needNum = (maxCol - filter.length) * 3; // 需要的数量
  230. let str = '';
  231. if (filter.length === 1) {
  232. str = ` ${grid_area} `.repeat(needNum + 1);
  233. } else {
  234. let arr = this.splitInteger(needNum, filter.length);
  235. str = arr[find] === 0 ? ` ${grid_area} ` : ` ${grid_area} `.repeat(arr[find] + 1);
  236. }
  237. gridArr[row - 1].push(`${str}`);
  238. }
  239. });
  240. gridArr.forEach((item) => {
  241. gridTemplateAreas += `'${item.join(' ')}' `;
  242. });
  243. // 计算 grid_template_columns
  244. let gridTemplateColumns = '';
  245. let rowOneNum = grid.filter((item) => item.row === 1).length;
  246. grid.forEach((item) => {
  247. if (item.row === 1) {
  248. gridTemplateColumns += `calc(${item.width} - ${((rowOneNum - 1) * 16) / rowOneNum}px) `;
  249. }
  250. });
  251. return {
  252. width: col.width,
  253. gridTemplateAreas,
  254. gridTemplateColumns,
  255. gridTemplateRows: `${grid.map(({ height }) => height).join(' ')}`,
  256. };
  257. },
  258. },
  259. };
  260. </script>
  261. <style lang="scss" scoped>
  262. .preview {
  263. display: flex;
  264. height: 100%;
  265. .menu {
  266. width: 240px;
  267. padding: 24px;
  268. overflow: auto;
  269. background-color: #fff;
  270. .title {
  271. margin-bottom: 16px;
  272. font-weight: bold;
  273. }
  274. :deep .node {
  275. &-name {
  276. height: 38px;
  277. line-height: 38px;
  278. &.content {
  279. padding-left: 16px;
  280. font-size: 14px;
  281. border-bottom: 1px solid $border-color;
  282. }
  283. }
  284. .node {
  285. padding-left: 20px;
  286. margin-left: 0;
  287. }
  288. }
  289. }
  290. .content-wrapper {
  291. flex: 1;
  292. padding: 24px 60px;
  293. overflow: auto;
  294. background: url('~@/assets/mask_group.png') repeat 0 0;
  295. .exit {
  296. position: sticky;
  297. top: 0;
  298. display: flex;
  299. justify-content: flex-end;
  300. .el-button {
  301. height: 40px;
  302. font-size: 16px;
  303. font-weight: bold;
  304. border-width: 0;
  305. box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 8%);
  306. }
  307. }
  308. .content {
  309. margin-top: 24px;
  310. background-color: #fff;
  311. border: 3px solid #f44444;
  312. border-radius: 16px;
  313. &-title {
  314. display: inline-flex;
  315. column-gap: 24px;
  316. align-items: center;
  317. min-width: 280px;
  318. height: 64px;
  319. padding: 18px 24px;
  320. font-size: 20px;
  321. color: #fff;
  322. background-color: #f44444;
  323. border-top-left-radius: 12px;
  324. border-bottom-right-radius: 16px;
  325. }
  326. .courserware {
  327. display: flex;
  328. flex-direction: column;
  329. row-gap: 6px;
  330. width: 100%;
  331. min-height: 500px;
  332. padding: 24px;
  333. background-color: #fff;
  334. background-repeat: no-repeat;
  335. border-bottom-right-radius: 12px;
  336. border-bottom-left-radius: 12px;
  337. .row {
  338. display: grid;
  339. row-gap: 16px;
  340. .col {
  341. display: grid;
  342. gap: 16px;
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. </style>