learningPreview.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <ele-modal
  3. :visible.sync="visible"
  4. title="学习预览"
  5. width="90vw"
  6. :fullscreen="true"
  7. append-to-body
  8. :close-on-click-modal="false"
  9. :maxable="true"
  10. @close="cancel"
  11. >
  12. <div class="preview-container">
  13. <!-- 左侧树形 -->
  14. <div class="left-tree">
  15. <div class="tree-header">{{ treeTitle }}</div>
  16. <el-tree
  17. ref="treeRef"
  18. :data="treeData"
  19. :props="defaultProps"
  20. node-key="id"
  21. default-expand-all
  22. highlight-current
  23. @node-click="handleNodeClick"
  24. >
  25. <span slot-scope="{ node, data }" class="custom-tree-node">
  26. <span :class="{ 'not-learned': data.isLeaf }">{{
  27. node.label
  28. }}</span>
  29. <span v-if="data.duration" class="node-duration">{{
  30. data.duration
  31. }}</span>
  32. </span>
  33. </el-tree>
  34. </div>
  35. <!-- 右侧内容 -->
  36. <div class="right-content">
  37. <div class="content-title">{{ currentTitle }}</div>
  38. <div class="iframe-wrapper">
  39. <!-- 附件预览:按 视频 / 图片 / 文档 分别渲染 -->
  40. <file-preview :ids="currentFileIds" class="preview-files" />
  41. <!-- 开始学习遮罩 -->
  42. <div v-if="showOverlay" class="start-overlay">
  43. <el-button type="primary" @click="handleStartLearn">
  44. 开始学习
  45. </el-button>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. <template v-slot:footer>
  51. <el-button @click="cancel">关闭</el-button>
  52. </template>
  53. </ele-modal>
  54. </template>
  55. <script>
  56. import FilePreview from '@/components/FilePreview/index.vue';
  57. import { learn } from '@/api/onlineTrainingPlan/index.js';
  58. export default {
  59. components: {
  60. FilePreview
  61. },
  62. data() {
  63. return {
  64. visible: false,
  65. treeTitle: '',
  66. currentTitle: '',
  67. // 当前选中培训资源的附件id集合
  68. currentFileIds: [],
  69. showOverlay: true,
  70. defaultProps: {
  71. children: 'children',
  72. label: 'label'
  73. },
  74. treeData: [],
  75. // 当前学习的任务ID
  76. currentTaskId: null,
  77. // 每分钟上报学习进度的定时器
  78. learnTimer: null
  79. };
  80. },
  81. beforeDestroy() {
  82. this.stopLearnTimer();
  83. },
  84. methods: {
  85. open(row, needReport = false) {
  86. this.visible = true;
  87. this.currentTaskId = row && row.id ? row.id : null;
  88. const resources = (row && row.trainingResourceList) || [];
  89. const materialName = (row && row.materialName) || '培训资料';
  90. const children = resources.map((item, index) => ({
  91. id: item.id || `resource-${index}`,
  92. label: item.resourceName || `培训资源${index + 1}`,
  93. duration: item.suggestDuration
  94. ? `总时长: ${item.suggestDuration} 分钟`
  95. : '',
  96. attachment: this.parseAttachment(item.attachment),
  97. isLeaf: true
  98. }));
  99. this.treeTitle = materialName;
  100. this.currentTitle = children.length ? children[0].label : materialName;
  101. this.currentFileIds = children.length ? children[0].attachment : [];
  102. this.treeData = [
  103. {
  104. id: row.id || 'material',
  105. label: materialName,
  106. children
  107. }
  108. ];
  109. this.showOverlay = true;
  110. // 仅在「开始学习」调用时开启定时上报
  111. if (needReport) {
  112. this.startLearnTimer();
  113. }
  114. this.$nextTick(() => {
  115. if (children.length) {
  116. this.$refs.treeRef.setCurrentKey(children[0].id);
  117. }
  118. });
  119. },
  120. // 每分钟上报一次学习进度
  121. startLearnTimer() {
  122. this.stopLearnTimer();
  123. if (!this.currentTaskId) {
  124. return;
  125. }
  126. this.learnTimer = setInterval(() => {
  127. this.reportLearn();
  128. }, 60 * 1000);
  129. },
  130. stopLearnTimer() {
  131. if (this.learnTimer) {
  132. clearInterval(this.learnTimer);
  133. this.learnTimer = null;
  134. }
  135. },
  136. // 上报当前学习进度,无需传入learnedTime
  137. async reportLearn() {
  138. if (!this.currentTaskId) {
  139. return;
  140. }
  141. try {
  142. await learn({ taskId: this.currentTaskId });
  143. } catch (error) {
  144. this.$message.error(error.message || '学习进度上报失败');
  145. }
  146. },
  147. cancel() {
  148. this.stopLearnTimer();
  149. this.visible = false;
  150. this.showOverlay = true;
  151. this.currentFileIds = [];
  152. this.currentTaskId = null;
  153. },
  154. // 附件id标准化,兼容字符串和数组
  155. parseAttachment(attachment) {
  156. if (!attachment) {
  157. return [];
  158. }
  159. const list = Array.isArray(attachment)
  160. ? attachment
  161. : String(attachment).split(',');
  162. return list
  163. .map((item) => String(item || '').trim())
  164. .filter((item) => item);
  165. },
  166. handleNodeClick(data) {
  167. this.currentTitle = data.label;
  168. this.currentFileIds = data.attachment || [];
  169. this.showOverlay = true;
  170. },
  171. handleStartLearn() {
  172. this.showOverlay = false;
  173. }
  174. }
  175. };
  176. </script>
  177. <style lang="scss" scoped>
  178. .preview-container {
  179. display: flex;
  180. height: calc(90vh - 160px);
  181. min-height: 500px;
  182. }
  183. .left-tree {
  184. width: 280px;
  185. flex-shrink: 0;
  186. border-right: 1px solid #ebeef5;
  187. overflow-y: auto;
  188. .tree-header {
  189. padding: 12px 15px;
  190. font-weight: bold;
  191. font-size: 14px;
  192. border-bottom: 1px solid #ebeef5;
  193. }
  194. ::v-deep .el-tree {
  195. padding: 10px;
  196. .el-tree-node__content {
  197. height: 36px;
  198. }
  199. }
  200. }
  201. .custom-tree-node {
  202. flex: 1;
  203. display: flex;
  204. align-items: center;
  205. justify-content: space-between;
  206. font-size: 13px;
  207. padding-right: 8px;
  208. .not-learned {
  209. color: #409eff;
  210. }
  211. .node-duration {
  212. color: #909399;
  213. font-size: 12px;
  214. margin-left: 8px;
  215. }
  216. }
  217. .right-content {
  218. flex: 1;
  219. display: flex;
  220. flex-direction: column;
  221. padding: 0 20px;
  222. .content-title {
  223. text-align: center;
  224. font-size: 16px;
  225. font-weight: bold;
  226. padding: 10px 0 20px;
  227. }
  228. .iframe-wrapper {
  229. flex: 1;
  230. position: relative;
  231. background-color: #eef5f9;
  232. border-radius: 4px;
  233. overflow: hidden;
  234. }
  235. .preview-files {
  236. width: 100%;
  237. height: 100%;
  238. }
  239. .preview-iframe {
  240. width: 100%;
  241. height: 100%;
  242. border: none;
  243. }
  244. .iframe-placeholder {
  245. width: 100%;
  246. height: 100%;
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. color: #909399;
  251. }
  252. .start-overlay {
  253. position: absolute;
  254. top: 0;
  255. left: 0;
  256. right: 0;
  257. bottom: 0;
  258. background-color: rgba(238, 245, 249, 0.95);
  259. display: flex;
  260. align-items: center;
  261. justify-content: center;
  262. z-index: 10;
  263. }
  264. }
  265. </style>