YNUtdPlatform/pages/YNEduApp/learn/learn.vue

203 lines
5.3 KiB
Vue
Raw Normal View History

2024-08-13 19:03:13 +08:00
<template>
<view>
2024-08-26 21:46:08 +08:00
<div class="video">
<video
id="myVideo"
:src="path"
:initial-time="studyDuration"
:show-progress="false"
:enable-progress-gesture="false"
@error="videoErrorCallback"
@timeupdate="videoTimeUpdate"
/>
</div>
<u-modal
:show="showModal"
title="提示"
:content="content"
showCancelButton
@cancel="showModal = false"
@confirm="handleEnd"
/>
<div class="btn">
<u-button type="primary" shape="circle" text="结束学习" size="small" @click="openModal" />
2024-08-13 19:03:13 +08:00
</div>
</view>
</template>
<script>
2024-08-26 21:46:08 +08:00
import config from '@/config'
import { updStudyDuration } from '@/api/eduApp'
2024-08-13 19:03:13 +08:00
export default {
2024-08-26 21:46:08 +08:00
props: {
states: {
type: Object,
default: () => {}
}
},
2024-08-13 19:03:13 +08:00
data() {
return {
2024-09-04 19:20:45 +08:00
isLoading: false,
2024-08-26 21:46:08 +08:00
studyId: '', // 学习id
stageId: '', // 阶段id
stageContentId: '', // 阶段内容id
stageType: '', // 阶段类型
studyCourseId: '',
sourceId: '', // 素材id
path: '', // 视频路径
studyDuration: 0, // 学习时长
allStudyDuration: 0, // 总时长
studyPercentage: 0, // 学习进度
// 当前播放时间
currentTime: 0,
showModal: false, // 是否显示弹窗
2024-09-04 15:18:01 +08:00
content: '是否确认结束学习?',
isEnd: false
2024-08-13 19:03:13 +08:00
}
},
2024-08-26 21:46:08 +08:00
onLoad(opt) {
opt = JSON.parse(opt.params)
console.log('🚀 ~ onLoad ~ :', opt)
this.studyId = opt.studyId
this.stageId = opt.stageId
this.stageContentId = opt.stageContentId
this.stageType = opt.stageType
this.studyCourseId = opt.studyCourseId
this.sourceId = opt.sourceId
this.path = config.fileUrl + opt.path
console.log('🚀 ~ onLoad ~ this.path:', this.path)
2024-08-30 17:35:33 +08:00
this.studyDuration = this.currentTime = Number(opt.studyDuration)
this.allStudyDuration = Number(opt.allStudyDuration)
2024-09-04 15:18:01 +08:00
this.isEnd = opt.isEnd
if (opt.isEnd) {
this.studyDuration = 0
}
},
onHide() {
console.log('🚀 ~ onHide ~ 页面隐藏')
if (!this.isEnd) {
// 关闭页面时,修改项目进度
this.handleEnd()
}
2024-08-26 21:46:08 +08:00
},
2024-08-28 17:23:03 +08:00
onUnload() {
console.log('🚀 ~ onUnload ~ 页面关闭')
2024-09-04 15:18:01 +08:00
if (!this.isEnd) {
// 关闭页面时,修改项目进度
this.handleEnd()
}
2024-08-28 17:23:03 +08:00
},
2024-08-13 19:03:13 +08:00
methods: {
2024-08-26 21:46:08 +08:00
videoErrorCallback(e) {
console.log('视频错误信息:', e)
},
// 视频播放时间更新
videoTimeUpdate(e) {
console.log('视频播放时间:', e.detail.currentTime)
2024-08-30 17:35:33 +08:00
this.currentTime = Math.ceil(e.detail.currentTime)
2024-08-13 19:03:13 +08:00
},
2024-08-26 21:46:08 +08:00
openModal() {
this.showModal = true
},
// 结束学习
handleEnd() {
2024-09-04 19:20:45 +08:00
if (this.isLoading) {
uni.showToast({
title: '正在提交中, 请稍后',
icon: 'none'
})
return
}
this.isLoading = true
2024-09-04 15:18:01 +08:00
if (this.isEnd) {
uni.reLaunch({
url: '/pages/YNEduApp/learnProj/learnProjDetail?id=' + this.studyId
})
return
}
2024-08-26 21:46:08 +08:00
// 手动暂停视频
const video = uni.createVideoContext('myVideo')
video.pause()
// 获取当前播放时间 - 用于记录学习时长
2024-08-30 17:35:33 +08:00
console.log('当前播放时间:', this.currentTime, this.allStudyDuration)
2024-08-26 21:46:08 +08:00
this.studyDuration = this.currentTime
// 计算学习进度
2024-09-04 15:18:01 +08:00
this.studyPercentage =
Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2) > 100
? 100
: Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2)
2024-08-26 21:46:08 +08:00
console.log('🚀 ~ handleEnd ~ this.studyPercentage:', this.studyPercentage)
const params = {
userId: uni.getStorageSync('userId'),
studyId: this.studyId,
stageId: this.stageId,
stageContentId: this.stageContentId,
stageType: this.stageType,
studyCourseId: this.studyCourseId,
sourceId: this.sourceId,
studyDuration: this.studyDuration,
studyPercentage: this.studyPercentage
}
console.log('🚀 ~ handleEnd ~ params:', params)
2024-08-28 09:51:05 +08:00
// updStudyDuration(params).then(res => {
// console.log('🚀 ~ handleEnd ~ res:', res)
// this.showModal = false
// uni.navigateBack()
// })
uni.request({
url: config.baseUrl + '/exam-student/student/updStudyDuration',
method: 'post',
data: params,
header: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: uni.getStorageSync('access_token')
},
success: res => {
console.log('🚀 ~ handleEnd ~ res:', res)
2024-09-04 15:18:01 +08:00
uni.reLaunch({
2024-08-28 17:23:03 +08:00
url: '/pages/YNEduApp/learnProj/learnProjDetail?id=' + this.studyId
})
2024-09-04 19:20:45 +08:00
setTimeout(() => {
this.isLoading = false
this.showModal = false
}, 1000)
2024-08-28 09:51:05 +08:00
},
fail: err => {
console.log('🚀 ~ handleEnd ~ err:', err)
}
2024-08-26 21:46:08 +08:00
})
}
2024-08-13 19:03:13 +08:00
}
}
</script>
<style lang="scss" scoped>
2024-08-26 21:46:08 +08:00
// 隐藏 video 默认控制条
::v-deep .uni-video-controls {
display: none !important;
}
.video {
width: 100%;
height: 260px;
uni-video {
width: 100%;
height: 100%;
}
}
.btn {
margin: 60px auto;
width: 100px;
}
::v-deep .u-modal__content {
flex-direction: column;
align-items: center;
2024-08-13 19:03:13 +08:00
}
</style>