YNUtdPlatform/pages/YNEduApp/learn/learn.vue

270 lines
7.4 KiB
Vue

<template>
<view>
<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" />
</div>
</view>
</template>
<script>
import config from '@/config'
import { pathToBase64, base64ToPath } from 'image-tools'
import { updStudyDuration } from '@/api/eduApp'
export default {
props: {
states: {
type: Object,
default: () => {}
}
},
data() {
return {
isLoading: false,
studyId: '', // 学习id
stageId: '', // 阶段id
stageContentId: '', // 阶段内容id
stageType: '', // 阶段类型
studyCourseId: '',
sourceId: '', // 素材id
path: '', // 视频路径
studyDuration: 0, // 学习时长
allStudyDuration: 0, // 总时长
studyPercentage: 0, // 学习进度
// 当前播放时间
currentTime: 0,
showModal: false, // 是否显示弹窗
content: '是否确认结束学习?',
isEnd: false,
video: null,
isHide: false,
oldTime: 0,
// 失败次数
failCount: 3
}
},
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)
this.studyDuration = this.currentTime = Number(opt.studyDuration)
this.allStudyDuration = Number(opt.allStudyDuration)
this.isEnd = opt.isEnd
if (opt.isEnd) {
this.studyDuration = 0
}
this.video = uni.createVideoContext('myVideo')
this.$verificationToken()
},
onHide() {
console.log('🚀 ~ onHide ~ 页面隐藏')
if (!this.isEnd && !this.isHide) {
// 关闭页面时,修改项目进度
this.handleEnd()
}
},
onUnload() {
console.log('🚀 ~ onUnload ~ 页面关闭')
if (!this.isEnd && !this.isHide) {
// 关闭页面时,修改项目进度
this.handleEnd()
}
},
methods: {
videoErrorCallback(e) {
console.log('视频错误信息:', e)
},
// 视频播放时间更新
videoTimeUpdate(e) {
let newTime = Math.ceil(e.detail.currentTime)
if (this.currentTime != newTime) {
this.currentTime = newTime
console.log('🚀 ~ 视频播放时间: ~ this.currentTime:', this.currentTime)
// 每隔 10 秒记录一次学习时长
if (this.currentTime % 10 == 0) {
console.log('学习时长:', this.currentTime)
this.updStudyDuration()
}
}
},
openModal() {
this.showModal = true
},
// 结束学习
handleEnd() {
if (this.isLoading) {
uni.showToast({
title: '正在提交中, 请稍后',
icon: 'none'
})
return
}
this.isLoading = true
if (this.isEnd) {
uni.reLaunch({
url: '/pages/YNEduApp/learnProj/learnProjDetail?id=' + this.studyId
})
return
}
// 手动暂停视频
this.video.pause()
// 获取当前播放时间 - 用于记录学习时长
console.log('当前播放时间:', this.currentTime, this.allStudyDuration)
this.studyDuration = this.currentTime
// 计算学习进度
this.studyPercentage =
Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2) > 100
? 100
: Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2)
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)
this.$verificationToken()
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)
uni.reLaunch({
url: '/pages/YNEduApp/learnProj/learnProjDetail?id=' + this.studyId
})
setTimeout(() => {
this.isLoading = false
this.showModal = false
}, 1000)
},
fail: err => {
console.log('🚀 ~ handleEnd ~ err:', err)
}
})
},
// 更新学习进度
updStudyDuration() {
// 获取当前播放时间 - 用于记录学习时长
console.log('当前播放时间:', this.currentTime, this.allStudyDuration)
this.studyDuration = this.currentTime
// 计算学习进度
this.studyPercentage =
Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2) > 100
? 100
: Math.ceil((this.studyDuration / this.allStudyDuration) * 100).toFixed(2)
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)
this.$verificationToken()
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)
},
fail: err => {
console.log('🚀 ~ handleEnd ~ err:', err)
}
})
}
}
}
</script>
<style lang="scss" scoped>
// 隐藏 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;
}
/* 隐藏视频控件的进度条 */
#video::-webkit-media-controls-progress-bar {
display: none !important;
}
/* 如果需要隐藏其他控件 */
#video::-webkit-media-controls-panel {
display: none !important;
}
#video::-webkit-media-controls-play-button {
display: block !important;
}
</style>