FileWatch.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <div class="watcher_file">
  3. <div v-if="filePreviewList.includes(previewType)" class="preview_item">
  4. <PdfViewer
  5. :initCourseLearnPage="props.initCourseLearnPage"
  6. @pageChange="(params)=>HandlePageChange('pdf',params)"
  7. :showPage="props?.allowDragOrShowPage === 0 ? false : true"
  8. :pdf-url="props?.fileMessage?.url"
  9. v-if="previewType == 'pdf'"
  10. />
  11. <vue-office-excel
  12. class="preview_main"
  13. v-else-if="previewType == 'xlsx'"
  14. :src="props?.fileMessage?.url"
  15. :options="excelOptions"
  16. @rendered="renderedHandler"
  17. @error="errorHandler"
  18. />
  19. <vue-office-docx
  20. class="preview_main"
  21. v-else-if="previewType == 'docx'"
  22. :src="props?.fileMessage?.url"
  23. @rendered="renderedHandler"
  24. @error="errorHandler"
  25. />
  26. <div
  27. v-else-if="['mp4','avi','mpg','mpeg','wmv','mkv','mov','flv','mpg'].includes(previewType)"
  28. class="preview_main"
  29. >
  30. <!-- 视频时间的执行先后顺序
  31. @loadeddata (1) 视频初始加载一次
  32. @seeking (2)
  33. @timeupdate (3)
  34. videoPlayer.value.currentTime 的变更 会触发 (2)(3) 依次执行
  35. -->
  36. <video
  37. @loadeddata="HandleVideoLoaded"
  38. @seeking="props?.allowDragOrShowPage ? '' : HandleVideoDragStart()"
  39. style="background-color: black;"
  40. ref="videoPlayer"
  41. @timeupdate="handleTimeUpdate"
  42. @ratechange="handleRateChange"
  43. controls
  44. controlslist="nodownload nofullscreen"
  45. :src="props?.fileMessage?.url"
  46. />
  47. <img class="full_screen" :src="isFullScreen ? exitFullScreenIcon : fullScreenIcon" alt="" @click="HandleFullScreen">
  48. </div>
  49. <PptViewer
  50. :initCourseLearnPage="props.initCourseLearnPage"
  51. @pageChange="(params)=>HandlePageChange('ppt',params)"
  52. @fileLoaded="emit('pptLoaded')"
  53. v-else-if="['pptx'].includes(previewType)"
  54. :fileMessage="props?.fileMessage">
  55. </PptViewer>
  56. <img @click="HandleImgPreview" style="text-align: center;" v-else-if="['jpg','png','jpeg','gif','svg','webp'].includes(previewType)" :src="props?.fileMessage?.url"/>
  57. </div>
  58. <div style="text-align: center;" v-else-if="previewType == '加载中'">加载中...</div>
  59. <div style="text-align: center;" v-else>不支持预览 .{{ previewType }}文件</div>
  60. <el-image-viewer
  61. v-if="imgVisible"
  62. :url-list="[props?.fileMessage?.url]"
  63. show-progress
  64. @close="imgVisible = false"
  65. />
  66. <!-- 页面指示器 -->
  67. <div class="nav_group" v-if="totalPage > 0">
  68. <span :class="['pre_nav',{'first':currentPage === 1} ]" @click="currentPage == 1 ? '' : HandlePageJump('reduce')">
  69. <el-icon><ArrowLeftBold /></el-icon>
  70. </span>
  71. <span :class="['next_nav',{'end':currentPage === totalPage}]" @click="currentPage === totalPage ? '' : HandlePageJump('add')">
  72. <el-icon><ArrowRightBold /></el-icon>
  73. </span>
  74. </div>
  75. <!-- <div v-if="previewType == 'docx'" class="pageProcess">
  76. 进度:{{ progressPercent }} %
  77. </div> -->
  78. </div>
  79. </template>
  80. <script setup lang="ts">
  81. import PdfViewer from './PdfViewer/PdfViewer.vue'
  82. import PptViewer from './PptViewer/PptViewer.vue'
  83. import { ref, computed, onMounted, watch } from 'vue'
  84. import fullScreenIcon from '@/assets/icon/fullScreen.png'
  85. import exitFullScreenIcon from '@/assets/icon/exitFullScreen.png'
  86. //引入VueOfficeExcel组件
  87. import VueOfficeExcel from '@vue-office/excel'
  88. //引入相关样式
  89. import '@vue-office/excel/lib/index.css'
  90. //引入VueOfficeDocx组件
  91. import VueOfficeDocx from '@vue-office/docx'
  92. //引入相关样式
  93. import '@vue-office/docx/lib/index.css'
  94. interface FileMsg {
  95. name:String,
  96. url:String
  97. }
  98. const props = defineProps({
  99. fileMessage:{
  100. required:true,
  101. type:Object
  102. },
  103. allowDragOrShowPage:{
  104. type:null,
  105. required:false,
  106. default:true,
  107. },
  108. initRecord:{
  109. type:null,
  110. required:false,
  111. },
  112. updateVideoCurrentTime:{
  113. type:Number
  114. },
  115. initCourseLearnPage:{
  116. type:Number,
  117. required:false,
  118. default:0
  119. }
  120. })
  121. const emit = defineEmits(['processChange','pptLoaded','manulFullScreen'])
  122. const videoProcessUpdateFlag = ref() //进度更新状态标识
  123. const currentVideoTime = ref(0) //只记录用户的前进,不记录回退
  124. // const videoPlayer = ref()
  125. const videoPlayer = defineModel('videoPlayer')
  126. const defaultVideoRate = ref(1)
  127. const handleRateChange = (event)=>{
  128. let currentRate = event.target.playbackRate
  129. console.log('当前速率',currentRate)
  130. defaultVideoRate.value = currentRate
  131. }
  132. const handleTimeUpdate = ()=>{
  133. const video = videoPlayer.value;
  134. const currentTime = Math.floor(video?.currentTime); // 当前播放时间(秒)
  135. const duration = Math.floor(video?.duration); // 视频总时长(秒)
  136. // 播放的结尾的时候再触发一次 更新
  137. if(videoProcessUpdateFlag.value && currentTime !== duration){
  138. return
  139. }
  140. // 只记录用户的前进,不记录回放
  141. if(currentVideoTime.value < currentTime){
  142. currentVideoTime.value = currentTime
  143. }
  144. emit('processChange',{type:'video',
  145. videoDuration:duration,
  146. currentVideoTime:currentTime,
  147. latestWatchTime:currentVideoTime.value}) //记录 用户的实际播放位置 和 最新的前进进度
  148. // 获取视频播放倍数
  149. // 要保证 同步频率 和视频的播放速度一致 或者更快(整数倍)
  150. videoProcessUpdateFlag.value = setTimeout(()=>{
  151. console.log('进度更新了')
  152. console.log('视频当前时间-总时长',currentTime,'-',duration)
  153. clearTimeout(videoProcessUpdateFlag.value)
  154. videoProcessUpdateFlag.value = null
  155. /** 遇到小数的情况下
  156. * floor:
  157. 同步相对快
  158. 速率相对慢
  159. ceil:
  160. 同步相对慢
  161. 速率相对快
  162. */
  163. // 这个后续过程中是否会有问题,待确认 —— 待完善
  164. // 确保视频前进1s同步一次
  165. },Math.floor(1000 / defaultVideoRate.value))
  166. }
  167. const currentPage = ref(1)
  168. const totalPage = ref(0)
  169. const pageTotalHeight = ref(0)
  170. const currentReadHeight = ref(0)
  171. const progressPercent = computed(()=>{
  172. if(currentReadHeight.value == 0 || pageTotalHeight.value == 0){
  173. return 0
  174. }
  175. return Math.round((currentReadHeight.value / pageTotalHeight.value) * 100)
  176. })
  177. const filePreviewList = ref(['pptx','pdf','gif','jpeg','jpg','png','svg','webp','xlsx','docx','mp4','avi','mpg','mpeg','wmv','mkv','mov','flv','mpg'])
  178. const imgVisible = ref(false)
  179. /**
  180. *
  181. */
  182. const previewType = ref('加载中')
  183. const excelOptions = {
  184. xls: false, //预览xlsx文件设为false;预览xls文件设为true
  185. minColLength: 0, // excel最少渲染多少列,如果想实现xlsx文件内容有几列,就渲染几列,可以将此值设置为0.
  186. minRowLength: 0, // excel最少渲染多少行,如果想实现根据xlsx实际函数渲染,可以将此值设置为0.
  187. widthOffset: 10, //如果渲染出来的结果感觉单元格宽度不够,可以在默认渲染的列表宽度上再加 Npx宽
  188. heightOffset: 10, //在默认渲染的列表高度上再加 Npx高
  189. beforeTransformData: (workbookData) => {return workbookData}, //底层通过exceljs获取excel文件内容,通过该钩子函数,可以对获取的excel文件内容进行修改,比如某个单元格的数据显示不正确,可以在此自行修改每个单元格的value值。
  190. transformData: (workbookData) => {return workbookData}, //将获取到的excel数据进行处理之后且渲染到页面之前,可通过transformData对即将渲染的数据及样式进行修改,此时每个单元格的text值就是即将渲染到页面上的内容
  191. }
  192. const timeoutFlag = ref()
  193. const HandleScroll = (e)=>{
  194. if(timeoutFlag.value){
  195. clearTimeout(timeoutFlag.value)
  196. }
  197. timeoutFlag.value = setTimeout(() => {
  198. let totalHeight = e.target.scrollHeight //总高度
  199. let scrollTop = e.target.scrollTop //滚动高度
  200. let pageHeight = e.target.clientHeight // 客户端高度
  201. // 用户阅读进度 = ( pageHeight + scrollTop ) / totalHeight
  202. console.log('滚动事件',e)
  203. console.log('滚动高度',scrollTop)
  204. console.log('总高度',totalHeight)
  205. console.log('客户端高度',pageHeight)
  206. pageTotalHeight.value = Math.floor(Number(totalHeight)) || 0
  207. currentReadHeight.value = Math.floor( Number(pageHeight) + Number(scrollTop) ) || 0
  208. emit('processChange',{type:'docx',currentProcess:progressPercent.value})
  209. clearTimeout(timeoutFlag.value)
  210. timeoutFlag.value = null
  211. }, 200);
  212. }
  213. const excelScrollTimeOutFlag = ref()
  214. const HandleExcelScroll = (e,type)=>{
  215. if(excelScrollTimeOutFlag.value){
  216. clearTimeout(excelScrollTimeOutFlag.value)
  217. }
  218. excelScrollTimeOutFlag.value = setTimeout(() => {
  219. emit('processChange',{type:'excel'})
  220. clearTimeout(excelScrollTimeOutFlag.value)
  221. excelScrollTimeOutFlag.value = null
  222. }, 200);
  223. }
  224. const HandlePageChange = (type,params)=>{
  225. emit('processChange',{type,...params})
  226. }
  227. const isFullScreen = ref(false)
  228. const HandleVideoLoaded = ()=>{
  229. if(['mp4','avi','mpg','mpeg','wmv','mkv','mov','flv','mpg'].includes(previewType.value)){
  230. videoPlayer.value.currentTime = currentVideoTime.value
  231. }
  232. // 禁用右击事件
  233. videoPlayer.value.addEventListener('contextmenu', (event) => {
  234. event.preventDefault();
  235. });
  236. if (document.fullscreenElement) {
  237. isFullScreen.value = true
  238. }else{
  239. isFullScreen.value = false
  240. }
  241. }
  242. const HandleVideoDragStart = ()=>{
  243. console.log('拖拽开始',videoPlayer.value.currentTime)
  244. console.log('历史播放记录',currentVideoTime.value)
  245. // 只能拖拽已观看部分,不能拖拽未观看部分
  246. if(videoPlayer.value.currentTime > currentVideoTime.value){
  247. videoPlayer.value.currentTime = currentVideoTime.value
  248. }
  249. }
  250. const HandlePageRedirect = (dom)=>{
  251. // docx 的进度重定位放到这里
  252. // props.initCourseLearnPage
  253. // 根据百分比 换算滚动高度
  254. let totalHeight = dom.scrollHeight //总高度
  255. let pageHeight = dom.clientHeight // 客户端高度
  256. let realScroll = props.initCourseLearnPage * 0.01 * totalHeight - pageHeight
  257. let scrollTop = realScroll > 0 ? realScroll : 0
  258. dom.scrollTo({
  259. top: scrollTop,
  260. left: 0,
  261. behavior: 'smooth'
  262. });
  263. pageTotalHeight.value = Math.floor(Number(totalHeight)) || 0
  264. // 用户阅读进度 = ( pageHeight + scrollTop ) / totalHeight
  265. currentReadHeight.value = Math.floor( Number(pageHeight) + Number(scrollTop) ) || 0
  266. emit('processChange',{type:'docx',currentProcess:progressPercent.value})
  267. }
  268. const renderedHandler = (params)=> {
  269. console.log("渲染完成",params)
  270. const scrollDom = document.querySelectorAll('.preview_main')
  271. if(previewType.value == 'docx'){
  272. scrollDom[0].addEventListener('scroll',HandleScroll,false)
  273. HandlePageRedirect(scrollDom[0])
  274. }else if(previewType.value == 'xlsx'){
  275. console.log('excel表格渲染中')
  276. // 监听横向滚动
  277. const excelDomHorizontal = document.querySelector('.vue-office-excel-main .x-spreadsheet-sheet .x-spreadsheet-scrollbar.horizontal ')
  278. excelDomHorizontal?.addEventListener('scroll',(event)=>HandleExcelScroll(event,'horizontal'))
  279. // 监听纵向滚动
  280. const excelDomVertical = document.querySelector('.vue-office-excel-main .x-spreadsheet-sheet .x-spreadsheet-scrollbar.vertical ')
  281. excelDomVertical?.addEventListener('scroll',(event)=>HandleExcelScroll(event,'vertical'))
  282. }
  283. }
  284. const errorHandler = (err) => {
  285. console.log("渲染失败",err)
  286. }
  287. const HandleImgPreview = ()=>{
  288. imgVisible.value = true
  289. }
  290. const HandlePageJump = (type)=>{
  291. switch(type){
  292. case 'add':
  293. currentPage.value += 1
  294. break;
  295. case 'reduce':
  296. currentPage.value -= 1
  297. break
  298. }
  299. }
  300. const HandleFullScreen = ()=>{
  301. emit('manulFullScreen')
  302. isFullScreen.value = !isFullScreen.value
  303. }
  304. watch(()=>props?.fileMessage,(newVal)=>{
  305. let suffixName = newVal?.name?.split('.').pop()
  306. if(suffixName){
  307. previewType.value = suffixName
  308. }
  309. })
  310. watch(()=>props.initRecord,(newVal)=>{
  311. // 重置视频的进度
  312. currentVideoTime.value = newVal
  313. })
  314. watch(()=>props.updateVideoCurrentTime,(newVal)=>{
  315. // 这个不加也可以
  316. // videoPlayer.value.currentTime = newVal
  317. })
  318. </script>
  319. <style lang="scss" scoped>
  320. .pageProcess{
  321. position: fixed;
  322. top:40%;
  323. right: 60px;
  324. }
  325. .nav_group{
  326. position: absolute;
  327. width: calc(100% - 40px - 50px);
  328. display: flex;
  329. justify-content: space-between;
  330. bottom:50%;
  331. left: calc(40px);
  332. .pre_nav,
  333. .next_nav{
  334. line-height: 1;
  335. border-radius: 20px;
  336. padding:10px;
  337. background-color: #415058;
  338. color:white;
  339. }
  340. .first,
  341. .end{
  342. background-color: #c3c8d0;
  343. }
  344. }
  345. .page_index{
  346. position: absolute;
  347. bottom: 10px;
  348. display: flex;
  349. align-items: center;
  350. justify-content: center;
  351. gap:5px;
  352. width: 100%;
  353. }
  354. .watcher_file{
  355. // border:solid blue 1px;
  356. width: 100%;
  357. min-height: 100%;
  358. // overflow: auto;
  359. }
  360. .preview_item{
  361. min-height: 100%;
  362. height: 100%;
  363. // overflow: auto;
  364. display: flex;
  365. flex-direction: column;
  366. .preview_main{
  367. position: relative;
  368. min-height: 100%;
  369. // overflow: auto;
  370. border-radius: 8px;
  371. .full_screen{
  372. width: 15px;
  373. position: absolute;
  374. right:65px;
  375. bottom:40px;
  376. background-color: rgba(0,0,0,0.5);
  377. text-align: right;
  378. }
  379. video{
  380. width: 100%;
  381. min-height: 100%;
  382. }
  383. }
  384. }
  385. </style>
  386. <style lang="scss">
  387. .preview_main{
  388. .vue-office-excel-main{
  389. width: 100%;
  390. height: 100%;
  391. .x-spreadsheet{
  392. height: 100%;
  393. .x-spreadsheet-sheet{
  394. height: calc(100% - 40px);
  395. }
  396. }
  397. }
  398. .docx-wrapper{
  399. // width: 100%;
  400. padding-top:20px;
  401. padding-bottom: 20px;
  402. background-color: #e8eaee !important;
  403. overflow: auto;
  404. box-sizing: border-box;
  405. .docx{
  406. overflow: auto;
  407. margin-bottom: 0;
  408. // width: 80% !important;
  409. // width: auto !important;
  410. }
  411. }
  412. }
  413. </style>