|
@@ -0,0 +1,254 @@
|
|
1
|
+<!--
|
|
2
|
+ * @Author: ysh
|
|
3
|
+ * @Date: 2025-03-06 14:38:35
|
|
4
|
+ * @LastEditors: Please set LastEditors
|
|
5
|
+ * @LastEditTime: 2025-03-11 16:16:33
|
|
6
|
+-->
|
|
7
|
+<template>
|
|
8
|
+ <div>
|
|
9
|
+ <div class="return-btn">
|
|
10
|
+ <div>
|
|
11
|
+ <el-button type="primary" size="mini" icon="el-icon-d-arrow-left"
|
|
12
|
+ @click="$router.push({ path: '/oa/study/myStudy' })">返回我的学习</el-button>
|
|
13
|
+ </div>
|
|
14
|
+ <div class="mt20">
|
|
15
|
+ 学习进度:
|
|
16
|
+ <el-progress :text-inside="true" :status="formatStatus(percentage)" text-color="#fff" :stroke-width="26"
|
|
17
|
+ :percentage="percentage"></el-progress>
|
|
18
|
+ </div>
|
|
19
|
+ <div class="mt20" style="color:#E23D28;font-size:12px;line-height:30px;">
|
|
20
|
+ tips:鼠标滚动过快将导致进度计算失败,请隔2秒滚动一次页面。若一直未到100%,请返回我的学习重新进入课程。
|
|
21
|
+ </div>
|
|
22
|
+ </div>
|
|
23
|
+ <div id="pdf-container">
|
|
24
|
+
|
|
25
|
+ <div id="viewer" class="pdfViewer"></div>
|
|
26
|
+ </div>
|
|
27
|
+ </div>
|
|
28
|
+</template>
|
|
29
|
+
|
|
30
|
+<script>
|
|
31
|
+import { listStudy, getStudy, delStudy, addStudy, updateStudy } from "@/api/oa/study/myStudy";
|
|
32
|
+import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist/build/pdf'
|
|
33
|
+import { PDFViewer } from 'pdfjs-dist/web/pdf_viewer'
|
|
34
|
+// 设置 PDF.js 的 Web Worker 路径
|
|
35
|
+GlobalWorkerOptions.workerSrc = require('pdfjs-dist/build/pdf.worker.min');
|
|
36
|
+export default {
|
|
37
|
+ async mounted() {
|
|
38
|
+ await this.getData();
|
|
39
|
+ this.initPDFViewer()
|
|
40
|
+ },
|
|
41
|
+ data() {
|
|
42
|
+ return {
|
|
43
|
+ baseUrl: process.env.VUE_APP_BASE_API,
|
|
44
|
+ studyId: '',
|
|
45
|
+ pdfPath: '',
|
|
46
|
+ pdfViewer: null,
|
|
47
|
+ resource: {},
|
|
48
|
+ totalPages: 0, //总页数
|
|
49
|
+ readPages: [], //已阅读页数
|
|
50
|
+ lastPoint: 0,
|
|
51
|
+ debounceTimer: null,
|
|
52
|
+ observers: new Map(),
|
|
53
|
+ isComplete: false,
|
|
54
|
+ percentage: 0,
|
|
55
|
+ }
|
|
56
|
+ },
|
|
57
|
+ methods: {
|
|
58
|
+ async getData() {
|
|
59
|
+ this.studyId = this.$route.params.id;
|
|
60
|
+ let resData = await getStudy(this.studyId);
|
|
61
|
+ let data = resData.data;
|
|
62
|
+ this.pdfPath = data.resource.sourcePath;
|
|
63
|
+ this.lastPoint = parseFloat(data.lastPoint);
|
|
64
|
+ this.resource = data.resource;
|
|
65
|
+ if (Number(this.lastPoint) == 100) {
|
|
66
|
+ this.isComplete = true;
|
|
67
|
+ this.$message.success('文档已阅读完毕')
|
|
68
|
+ }
|
|
69
|
+ },
|
|
70
|
+ // 初始化查看器
|
|
71
|
+ async initPDFViewer() {
|
|
72
|
+ this.pdfViewer = new PDFViewer({
|
|
73
|
+ container: document.getElementById('pdf-container'),
|
|
74
|
+ enhanceTextSelection: true,
|
|
75
|
+ textLayerMode: 1
|
|
76
|
+ })
|
|
77
|
+ // 先绑定监听页面变化事件
|
|
78
|
+ this.setupViewerEvents();
|
|
79
|
+ // 加载文档
|
|
80
|
+ const loadingTask = getDocument({
|
|
81
|
+ url: `/dev-api/profile/upload/${this.pdfPath}`,
|
|
82
|
+ cMapPacked: true
|
|
83
|
+ })
|
|
84
|
+ try {
|
|
85
|
+ const pdfDocument = await loadingTask.promise
|
|
86
|
+ this.pdfViewer.setDocument(pdfDocument);
|
|
87
|
+ this.totalPages = pdfDocument.numPages;
|
|
88
|
+ this.readPages = new Array(this.totalPages).fill(false);
|
|
89
|
+ let curProgress = Math.round((this.lastPoint / 100) * this.totalPages);
|
|
90
|
+ this.percentage = Math.round(curProgress / this.totalPages) * 100;
|
|
91
|
+ for (let i = 0; i < curProgress; i++) {
|
|
92
|
+ this.readPages[i] = true
|
|
93
|
+ }
|
|
94
|
+ this.$nextTick(() => {
|
|
95
|
+ this.setupPageObservers();
|
|
96
|
+ });
|
|
97
|
+ } catch (err) {
|
|
98
|
+ console.error('PDF加载失败:', err)
|
|
99
|
+ }
|
|
100
|
+ // 添加窗口缩放监听
|
|
101
|
+ window.addEventListener("resize", this.handleResize);
|
|
102
|
+ },
|
|
103
|
+ handleResize() {
|
|
104
|
+ if (this.pdfViewer) {
|
|
105
|
+ this.pdfViewer.update();
|
|
106
|
+ }
|
|
107
|
+ },
|
|
108
|
+ setupViewerEvents() {
|
|
109
|
+ const eventBus = this.pdfViewer.eventBus;
|
|
110
|
+ // 新增 pagesloaded 事件监听
|
|
111
|
+ eventBus.on('pagesloaded', () => {
|
|
112
|
+ this.scrollToLastPosition();
|
|
113
|
+ this.setupPageObservers()
|
|
114
|
+ });
|
|
115
|
+ },
|
|
116
|
+ scrollToLastPosition() {
|
|
117
|
+ if (!this.lastPoint || !this.totalPages) return
|
|
118
|
+ // 计算目标页码(向上取整)
|
|
119
|
+ const pageNumber = Math.min(
|
|
120
|
+ Math.ceil((this.lastPoint / 100) * this.totalPages),
|
|
121
|
+ this.totalPages
|
|
122
|
+ )
|
|
123
|
+ // 查找对应的页面元素
|
|
124
|
+ const pageElement = document.querySelector(
|
|
125
|
+ `.page[data-page-number="${pageNumber}"]`
|
|
126
|
+ )
|
|
127
|
+ if (pageElement) {
|
|
128
|
+ // 获取页面元素的垂直位置
|
|
129
|
+ const pageTop = pageElement.offsetTop
|
|
130
|
+ // 设置滚动位置(带20px顶部留白)
|
|
131
|
+ document.getElementById('pdf-container').scrollTo({
|
|
132
|
+ top: pageTop - 20,
|
|
133
|
+ behavior: 'auto'
|
|
134
|
+ })
|
|
135
|
+ }
|
|
136
|
+ },
|
|
137
|
+ setupPageObservers() {
|
|
138
|
+ // 清理旧观察者
|
|
139
|
+ this.observers.forEach((observer, page) => {
|
|
140
|
+ observer.disconnect();
|
|
141
|
+ delete page.dataset.timer;
|
|
142
|
+ });
|
|
143
|
+ this.observers.clear();
|
|
144
|
+ const pages = document.querySelectorAll('.page');
|
|
145
|
+ pages.forEach(page => {
|
|
146
|
+ const observer = new IntersectionObserver((entries) => {
|
|
147
|
+ entries.forEach(entry => {
|
|
148
|
+ const pageNumber = parseInt(page.dataset.pageNumber);
|
|
149
|
+ if (entry.isIntersecting) {
|
|
150
|
+ page.dataset.enterTime = Date.now();
|
|
151
|
+ page.dataset.timer = setTimeout(() => {
|
|
152
|
+ if (!this.readPages[pageNumber - 1]) {
|
|
153
|
+ this.$set(this.readPages, pageNumber - 1, true);
|
|
154
|
+ this.updateProgress();
|
|
155
|
+ }
|
|
156
|
+ }, 2000);
|
|
157
|
+ } else {
|
|
158
|
+ clearTimeout(page.dataset.timer);
|
|
159
|
+ delete page.dataset.enterTime;
|
|
160
|
+ }
|
|
161
|
+ });
|
|
162
|
+ }, { threshold: 0.5, root: this.pdfViewer.container });
|
|
163
|
+
|
|
164
|
+ this.observers.set(page, observer);
|
|
165
|
+ observer.observe(page);
|
|
166
|
+ });
|
|
167
|
+ },
|
|
168
|
+ updateProgress() {
|
|
169
|
+ if (!this.isComplete) {
|
|
170
|
+ const readCount = this.readPages.filter(Boolean).length;
|
|
171
|
+ const progress = Math.round(readCount / this.totalPages * 100);
|
|
172
|
+ this.percentage = progress;
|
|
173
|
+ // 防抖保存
|
|
174
|
+ if (!this.debounceTimer) {
|
|
175
|
+ this.debounceTimer = setTimeout(() => {
|
|
176
|
+ this.saveProgress(progress);
|
|
177
|
+ this.debounceTimer = null;
|
|
178
|
+ }, 2000);
|
|
179
|
+ }
|
|
180
|
+ }
|
|
181
|
+ },
|
|
182
|
+ async saveProgress(progress) {
|
|
183
|
+ try {
|
|
184
|
+ if (parseFloat(progress) == 100) {
|
|
185
|
+ this.$message.success('文档已阅读完毕!学时+' + this.resource.hours)
|
|
186
|
+ progress = 100
|
|
187
|
+ }
|
|
188
|
+ await updateStudy(
|
|
189
|
+ {
|
|
190
|
+ studyId: this.studyId,
|
|
191
|
+ lastPoint: progress,
|
|
192
|
+ getHours: Math.round((this.resource.hours * progress) / 100),
|
|
193
|
+ lastTime: this.parseTime(new Date(), '{y}-{m}-{d}')
|
|
194
|
+ });
|
|
195
|
+ } catch (error) {
|
|
196
|
+ console.error('保存进度失败:', error);
|
|
197
|
+ }
|
|
198
|
+ },
|
|
199
|
+ formatStatus(row) {
|
|
200
|
+ if (!row) {
|
|
201
|
+ row = 0
|
|
202
|
+ return 'exception'
|
|
203
|
+ }
|
|
204
|
+ if (row <= 20) {
|
|
205
|
+ return 'exception'
|
|
206
|
+ } else if (row > 20 && row <= 50) {
|
|
207
|
+ return 'warning'
|
|
208
|
+ } else if (row > 50 && row <= 80) {
|
|
209
|
+ return null
|
|
210
|
+ } else {
|
|
211
|
+ return 'success'
|
|
212
|
+ }
|
|
213
|
+ },
|
|
214
|
+ },
|
|
215
|
+ beforeDestroy() {
|
|
216
|
+ // 清理观察者和定时器
|
|
217
|
+ this.observers.forEach((observer, page) => {
|
|
218
|
+ observer.disconnect();
|
|
219
|
+ clearTimeout(page.dataset.timer);
|
|
220
|
+ });
|
|
221
|
+ window.removeEventListener("resize", this.handleResize);
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+}
|
|
225
|
+</script>
|
|
226
|
+
|
|
227
|
+<style lang="scss" scoped>
|
|
228
|
+@import '~pdfjs-dist/web/pdf_viewer.css';
|
|
229
|
+
|
|
230
|
+/* 修改后 */
|
|
231
|
+#pdf-container {
|
|
232
|
+ position: relative;
|
|
233
|
+ /* 改为相对定位,避免脱离文档流 */
|
|
234
|
+ width: 100%;
|
|
235
|
+ height: calc(100vh - 88px);
|
|
236
|
+ /* 根据实际布局调整高度 */
|
|
237
|
+ overflow: auto;
|
|
238
|
+ background-color: #cdcdcd;
|
|
239
|
+}
|
|
240
|
+
|
|
241
|
+.pdfViewer .page {
|
|
242
|
+ margin: 1em auto;
|
|
243
|
+ border: 1px solid #ddd;
|
|
244
|
+ box-shadow: 0 10px 10px rgba(0, 0, 0, 0.1);
|
|
245
|
+}
|
|
246
|
+
|
|
247
|
+.return-btn {
|
|
248
|
+ position: absolute;
|
|
249
|
+ left: 20px;
|
|
250
|
+ top: 100px;
|
|
251
|
+ z-index: 9999;
|
|
252
|
+ width: 230px;
|
|
253
|
+}
|
|
254
|
+</style>
|