index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <template>
  2. <a-layout>
  3. <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible collapsedWidth="0">
  4. <div class="classTitle">
  5. <div>{{ classDetail.courseName }}</div>
  6. </div>
  7. <a-menu
  8. v-model:openKeys="openKeys"
  9. v-model:selectedKeys="selectedKeys"
  10. theme="dark"
  11. mode="inline"
  12. @click="menuClick"
  13. >
  14. <a-sub-menu :key="String(idx + 1)" v-for="(item, idx) in classTimeList">
  15. <template #title>{{ item.name }}</template>
  16. <a-menu-item :key="itemc.id" v-for="(itemc, idxc) in item.classHours">
  17. <span class="mr-2">
  18. <CheckSquareOutlined v-if="selectedKeys.includes(itemc.id)" />
  19. <BorderOutlined v-else />
  20. </span>
  21. <span>{{ itemc.name }}</span>
  22. </a-menu-item>
  23. </a-sub-menu>
  24. </a-menu>
  25. </a-layout-sider>
  26. <a-layout>
  27. <a-layout-header style="padding: 0 20px">
  28. <div class="flex-between">
  29. <div>
  30. <menu-unfold-outlined
  31. v-if="collapsed"
  32. class="trigger"
  33. @click="() => (collapsed = !collapsed)"
  34. style="font-size: 30px; color: #fff"
  35. />
  36. <menu-fold-outlined
  37. v-else
  38. class="trigger"
  39. @click="() => (collapsed = !collapsed)"
  40. style="font-size: 30px; color: #fff"
  41. />
  42. </div>
  43. <div @click="isLike">
  44. <a-tooltip title="收藏" style="cursor: pointer" :getPopupContainer="(trigger) => trigger.parentElement">
  45. <heart-outlined :style="{ 'font-size': '20px', color: classDetail.isCollect ? '#ff4d4f' : '#fff' }" />
  46. </a-tooltip>
  47. </div>
  48. </div>
  49. </a-layout-header>
  50. <a-layout-content style="overflow-y: auto">
  51. <a-card :bordered="false">
  52. <video
  53. style="height: 900px; width: 100%; background-color: #000"
  54. ref="videoRef"
  55. controls
  56. Controlslist="nodownload noplaybackrate noremoteplayback disablePictureInPicture"
  57. @timeupdate="timeUpdate"
  58. :currentTime="currentTimenew"
  59. :poster="videoPoster"
  60. >
  61. <source :src="videoUrl" type="video/mp4" />
  62. <source :src="videoUrl" type="video/ogg" />
  63. 您的浏览器不支持 HTML5 video 标签。
  64. </video>
  65. <rightMenu :idsObj="idsObj" :dataList="classTimeData" ref="rightNenuRef" @videoSpeed="videoSpeed"></rightMenu>
  66. </a-card>
  67. <div style="display: flex; justify-content: center">
  68. <a-card :bordered="false" class="mt-2" style="width: 1200px">
  69. <a-tabs v-model:activeKey="tabsActiveKey">
  70. <a-tab-pane key="1" tab="讲义" style="height: 900px">
  71. <handouts :itemObj="itemObj" :hourId="classHourData.id" v-if="classHourData"></handouts>
  72. </a-tab-pane>
  73. <a-tab-pane key="2" tab="字幕">
  74. <subtitleBox :url="danmuObj.url" @videoSpeed="videoSpeed"></subtitleBox>
  75. </a-tab-pane>
  76. <a-tab-pane key="3" tab="笔记">
  77. <note :idsObj="idsObj" ref="noteRef" @edit="noteEdit"></note>
  78. </a-tab-pane>
  79. <a-tab-pane key="4" tab="问答">
  80. <askDiv :idsObj="idsObj" ref="askDivRef" @edit="askEdit"></askDiv>
  81. </a-tab-pane>
  82. </a-tabs>
  83. </a-card>
  84. </div>
  85. <forumBtn :forumData="forumData" resourceType="0"></forumBtn>
  86. </a-layout-content>
  87. </a-layout>
  88. </a-layout>
  89. </template>
  90. <script setup name="classCentre">
  91. import classCentre from '@/api/student/classCentre'
  92. import rightMenu from './rightMenu.vue'
  93. import { useRoute, useRouter } from 'vue-router'
  94. import sysConfig from '@/config/index'
  95. import note from './note.vue'
  96. import askDiv from './ask.vue'
  97. import handouts from './handouts.vue'
  98. import subtitleBox from './subtitle.vue'
  99. const route = useRoute()
  100. const router = useRouter()
  101. const classDetail = ref({})
  102. const classTimeList = ref([])
  103. const classTimeData = ref([])
  104. const openKeys = ref(['1'])
  105. const selectedKeys = ref([])
  106. const collapsed = ref(false)
  107. const videoRef = ref()
  108. const currentTimenew = ref(0)
  109. const idsObj = computed(() => {
  110. let item = findNodeByKey(classTimeList.value, selectedKeys.value[0],classTimeList.value[0])
  111. return {
  112. courseId: route.query.id,
  113. chapterId: selectedKeys.value[0],
  114. hourId: classHourData.value?.id,
  115. ...item
  116. }
  117. })
  118. function findNodeByKey(list, id, parent = null) {
  119. for (const item of list) {
  120. const itemWithParent = {
  121. ...item,
  122. parent: parent
  123. }
  124. if (itemWithParent.id === id) {
  125. return itemWithParent
  126. }
  127. if (item.classHours && Array.isArray(item.classHours)) {
  128. const found = findNodeByKey(item.classHours, id, itemWithParent)
  129. if (found) return found
  130. }
  131. }
  132. return null
  133. }
  134. const videoPoster = computed(() => {
  135. return classTimeData.value.filter((r) => r.funcType == 0)[0]?.url
  136. })
  137. const getClassData = () => {
  138. classCentre.addViewCount({ courseId: route.query.id })
  139. classCentre.courseDetail({ courseId: route.query.id }).then((data) => {
  140. classDetail.value = data
  141. classCentre.coursechapterList({ courseId: data.courseId }).then((data) => {
  142. classTimeList.value = data
  143. selectedKeys.value = [data[0]?.classHours[0].id]
  144. if (selectedKeys.value[0]) {
  145. menuClick()
  146. }
  147. })
  148. })
  149. }
  150. const classHourData = ref()
  151. const menuClick = (e) => {
  152. rightNenuRef.value.onClose()
  153. classCentre.courseTimeDetail({ id: e?.key ? e.key : selectedKeys.value[0] }).then((data) => {
  154. classHourData.value = data
  155. classTimeData.value = data.courseRelates.map((r) => {
  156. return {
  157. ...r,
  158. url: sysConfig.FILE_URL + r.url
  159. }
  160. })
  161. videoRef.value.src = classTimeData.value.filter((r) => r.funcType == 1)[0]?.url
  162. footprintClassAdd()
  163. getVideoTime()
  164. })
  165. }
  166. const tabsActiveKey = ref('1')
  167. const noteRef = ref()
  168. const askDivRef = ref()
  169. const itemObj = computed(() => {
  170. let item = classTimeData.value.length > 0 ? classTimeData.value.filter((r) => r.funcType == 2)[0] : { url: '' }
  171. return item
  172. })
  173. const danmuObj = computed(() => {
  174. let item = classTimeData.value.length > 0 ? classTimeData.value.filter((r) => r.funcType == 3)[0] : { url: '' }
  175. return item
  176. })
  177. const videoUrl = ref('')
  178. const newsschedule = ref(0)
  179. const currTime = ref(null)
  180. const maxTime = ref(0)
  181. const initialtime = ref(0)
  182. const videoContext = ref()
  183. const timeStamp1 = ref()
  184. const allTime = ref()
  185. const biNum = ref()
  186. const currentTime = ref()
  187. const videoStart = () => {
  188. videoContext.value = videoRef.value
  189. if (initialtime.value > 0) {
  190. videoContext.value.currentTime = initialtime.value
  191. timeStamp1.value = initialtime.value
  192. }
  193. }
  194. const timeUpdate = (e) => {
  195. //获取当前播放时间 durationinitialtime
  196. if (timeStamp1.value != parseInt(e.target.currentTime)) {
  197. allTime.value = parseInt(e.target.duration) //视频总时长(秒)
  198. //对播放的时间进行整
  199. timeStamp1.value = parseInt(e.target.currentTime) //播放进度 (秒)
  200. biNum.value = Math.floor((timeStamp1.value / allTime.value) * 10000) / 100 //暂时没用到
  201. currentTime.value = e.target.currentTime
  202. if (e.srcElement.currentTime - currTime.value > 3) {
  203. addClassPlan(3)
  204. console.log('快进了')
  205. }
  206. if (currTime.value - e.srcElement.currentTime > 3) {
  207. addClassPlan(3)
  208. console.log('快退了')
  209. }
  210. currTime.value = e.srcElement.currentTime
  211. // if (videoJumpTime.value) {
  212. // videoJumpTime.value = false
  213. // newsschedule.value = currentTime.value
  214. // } else {
  215. // if (newsschedule.value <= currentTime.value) {
  216. // //请求接口获取的参数就是判断当前观看的时长是否小于当前时间 小于当前时间就禁止拖动进度条
  217. // if (e.srcElement.currentTime - currTime.value > 3) {
  218. // //这里拖动进度条时间 - 当前观看的时长 > 3秒 这边判定它为拖动进度条
  219. // e.srcElement.currentTime = currTime.value > maxTime.value ? currTime.value : maxTime.value
  220. // let newsVal = initialtime.value
  221. // if (newsVal) {
  222. // videoContext.value.currentTime = newsVal
  223. // }
  224. // //当前用户记录观看时间 大于 实施播放时间
  225. // if (currTime.value > newsVal) {
  226. // videoContext.value.currentTime = currTime.value
  227. // }
  228. // console.log('快进了')
  229. // }
  230. // currTime.value = e.srcElement.currentTime
  231. // maxTime.value = currTime.value > maxTime.value ? currTime.value : maxTime.value
  232. // }
  233. // }
  234. }
  235. }
  236. const rightNenuRef = ref()
  237. const noteEdit = (e) => {
  238. rightNenuRef.value.selectBtn({ key: 6, type: 2 }, e)
  239. }
  240. const askEdit = (e) => {
  241. rightNenuRef.value.selectBtn({ key: 7, type: 4 }, e)
  242. }
  243. const videoJumpTime = ref(false)
  244. const videoSpeed = (e) => {
  245. videoJumpTime.value = true
  246. currentTimenew.value = e.startTime
  247. }
  248. const forumData = computed(() => {
  249. let item = findNodeByKey(classTimeList.value, selectedKeys.value[0],classTimeList.value[0]) ?? {}
  250. if (!item.src) {
  251. item.src = classTimeData.value.filter((r) => r.funcType == 1)[0]?.url
  252. }
  253. return {
  254. id: classHourData.value?.id,
  255. title: item.parent?.name,
  256. videoUrl: btoa(encodeURIComponent(videoRef.value?.src ? videoRef.value.src : item.src)),
  257. courseId: route.query.id,
  258. chapterId: selectedKeys.value[0],
  259. courseName: classDetail.value?.courseName,
  260. chapterName: classHourData.value?.name
  261. }
  262. })
  263. const showPdf = ref(true)
  264. function errorHandler() {
  265. showPdf.value = false
  266. }
  267. const getVideoTime = () => {
  268. classCentre
  269. .theLastDetail({
  270. funcType: 1,
  271. type: 1,
  272. hourld: classHourData.value?.id
  273. })
  274. .then((data) => {
  275. if (data.endTime) {
  276. initialtime.value = parseFloat(data.endTime) / 1000
  277. currentTimenew.value = parseFloat(data.endTime) / 1000
  278. }
  279. })
  280. }
  281. // 毫秒转换时分秒
  282. function msToHMS(ms) {
  283. if (typeof ms !== 'number' || isNaN(ms)) return '00:00:00'
  284. let totalSeconds = Math.floor(ms / 1000)
  285. let hours = String(Math.floor(totalSeconds / 3600)).padStart(2, '0')
  286. let minutes = String(Math.floor((totalSeconds % 3600) / 60)).padStart(2, '0')
  287. let seconds = String(totalSeconds % 60).padStart(2, '0')
  288. return `${hours}:${minutes}:${seconds}`
  289. }
  290. // 时分秒转换毫秒
  291. function hmsToMs(hms) {
  292. if (!hms) return 0
  293. const parts = hms.split(':').map((p) => parseFloat(p.replace(',', '.')))
  294. // 支持 hh:mm:ss 或 mm:ss
  295. let ms = 0
  296. if (parts.length === 3) {
  297. ms = (parts[0] * 3600 + parts[1] * 60 + parts[2]) * 1000
  298. } else if (parts.length === 2) {
  299. ms = (parts[0] * 60 + parts[1]) * 1000
  300. } else if (parts.length === 1) {
  301. ms = parts[0] * 1000
  302. }
  303. return ms
  304. }
  305. onBeforeUnmount(() => {
  306. addClassPlan(1)
  307. })
  308. const nowTimesStr = Date.now()
  309. const outNowTimesStr = ref()
  310. const addClassPlan = (type) => {
  311. outNowTimesStr.value = Date.now()
  312. let progress = parseFloat((videoRef.value?.currentTime / videoRef.value?.duration) * 100)
  313. if (
  314. (progress || progress == 0) &&
  315. (initialtime.value || initialtime.value == 0) &&
  316. (videoRef.value.currentTime || videoRef.value.currentTime == 0)
  317. ) {
  318. classCentre
  319. .classPlanAdd({
  320. startTime: parseFloat(initialtime.value),
  321. endTime: Math.round(videoRef.value.currentTime * 1000),
  322. progress: Math.round(progress),
  323. hourId: classHourData.value?.id,
  324. stayTime: outNowTimesStr.value - nowTimesStr,
  325. type: type,
  326. funcType: type
  327. })
  328. .then((data) => {})
  329. }
  330. }
  331. const isLike = () => {
  332. classCentre
  333. .classCollectAdd(
  334. {
  335. courseId: route.query.id
  336. },
  337. classDetail.value.isCollect
  338. )
  339. .then(() => {
  340. classCentre.courseDetail({ courseId: route.query.id }).then((data) => {
  341. classDetail.value = data
  342. })
  343. })
  344. }
  345. //足迹
  346. const footprintClassAdd = () => {
  347. classCentre.footprintClassAdd({
  348. hourName: classHourData.value.name,
  349. chapterName: classHourData.value.name,
  350. courseName: classDetail.value.courseName,
  351. hourId: classHourData.value.id,
  352. chapterId: selectedKeys.value[0],
  353. courseId: classDetail.value.courseId,
  354. fileId: classHourData.value.courseRelates.filter((r) => r.funcType == 1)[0].relateId,
  355. fileName: classHourData.value.courseRelates.filter((r) => r.funcType == 1)[0].name,
  356. filePath: classHourData.value.courseRelates.filter((r) => r.funcType == 1)[0].url
  357. })
  358. }
  359. onMounted(() => {
  360. getClassData()
  361. getVideoTime()
  362. nextTick(() => {
  363. videoStart()
  364. })
  365. })
  366. </script>
  367. <style scoped lang="less">
  368. .classTitle {
  369. height: 64px;
  370. width: 200px;
  371. font-size: 18px;
  372. color: #ccc;
  373. display: flex;
  374. justify-content: center;
  375. align-items: center;
  376. }
  377. .flex-between {
  378. display: flex;
  379. justify-content: space-between;
  380. }
  381. </style>