|
|
@@ -41,7 +41,7 @@
|
|
|
</template>
|
|
|
|
|
|
<script setup>
|
|
|
-import { defineProps, computed, ref, onMounted, onUnmounted } from 'vue'
|
|
|
+import { defineProps, computed, ref, onMounted, onUnmounted, defineEmits } from 'vue'
|
|
|
import videoImage01 from '@/assets/icon/videoImage01.png'
|
|
|
import videoImage02 from '@/assets/icon/videoImage02.png'
|
|
|
// 消息提示
|
|
|
@@ -52,9 +52,13 @@ const props = defineProps({
|
|
|
imagePath: { type: String, required: true },
|
|
|
altText: { type: String, default: '课程图片' },
|
|
|
autoPlay: { type: Boolean, default: true },
|
|
|
- interval: { type: Number, default: 3000 }
|
|
|
+ interval: { type: Number, default: 3000 },
|
|
|
+ courseId: { type: String } // 课程ID,用于保存进度
|
|
|
})
|
|
|
|
|
|
+// 定义emits
|
|
|
+const emits = defineEmits(['saveProgress'])
|
|
|
+
|
|
|
// 计算属性:将逗号分隔的图片路径字符串转换为数组
|
|
|
const images = computed(() => {
|
|
|
if (!props.imagePath) return []
|
|
|
@@ -67,13 +71,68 @@ const currentIndex = ref(0)
|
|
|
const carouselWrapper = ref(null)
|
|
|
const autoplayTimer = ref(null)
|
|
|
|
|
|
+// 记录已经保存的进度百分比
|
|
|
+const savedProgress = ref([])
|
|
|
+// 节流时间间隔(毫秒)
|
|
|
+const THROTTLE_TIME = 3000
|
|
|
+
|
|
|
+// 定义节流函数
|
|
|
+const throttle = (fn, delay) => {
|
|
|
+ let lastCall = 0
|
|
|
+ return function (...args) {
|
|
|
+ const now = Date.now()
|
|
|
+ if (now - lastCall >= delay) {
|
|
|
+ lastCall = now
|
|
|
+ return fn.apply(this, args)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 保存进度(带节流)
|
|
|
+const saveProgress = throttle(async (progress) => {
|
|
|
+ try {
|
|
|
+ // 保存到localStorage,下次加载续播
|
|
|
+ if (props.courseId) {
|
|
|
+ localStorage.setItem(
|
|
|
+ `imageProgress_${props.courseId}`,
|
|
|
+ JSON.stringify({
|
|
|
+ progress: progress,
|
|
|
+ currentIndex: currentIndex.value,
|
|
|
+ timestamp: Date.now()
|
|
|
+ })
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ // 通过emit事件通知父组件保存进度
|
|
|
+ emits('saveProgress', "image", progress)
|
|
|
+ savedProgress.value.push(progress)
|
|
|
+ } catch (error) {
|
|
|
+ console.error(`保存进度失败:`, error)
|
|
|
+ }
|
|
|
+}, THROTTLE_TIME)
|
|
|
+
|
|
|
// 下一张图片 - 取消循环切换,在最后一张时显示提示
|
|
|
const nextSlide = () => {
|
|
|
if (currentIndex.value < images.value.length - 1) {
|
|
|
currentIndex.value++
|
|
|
+ // 计算当前进度
|
|
|
+ const progressPercentage = Math.round(((currentIndex.value + 1) / images.value.length) * 100);
|
|
|
+ // 保存进度
|
|
|
+ saveProgress(progressPercentage);
|
|
|
+ // 检查是否到达最后一张图片
|
|
|
+ if (currentIndex.value === images.value.length - 1) {
|
|
|
+ // 保存100%进度
|
|
|
+ if (!savedProgress.value.includes(100)) {
|
|
|
+ saveProgress(100);
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
// 已经是最后一张图片,显示提示信息
|
|
|
ElMessage.warning('已播放到最后一张图片')
|
|
|
+ // 确保在最后一张图片时保存100%进度
|
|
|
+ if (!savedProgress.value.includes(100)) {
|
|
|
+ saveProgress(100);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -90,6 +149,17 @@ const prevSlide = () => {
|
|
|
// 跳转到指定图片
|
|
|
const goToSlide = (index) => {
|
|
|
currentIndex.value = index
|
|
|
+ // 计算当前进度
|
|
|
+ const progressPercentage = Math.round(((currentIndex.value + 1) / images.value.length) * 100);
|
|
|
+ // 保存进度
|
|
|
+ saveProgress(progressPercentage);
|
|
|
+ // 检查是否到达最后一张图片
|
|
|
+ if (currentIndex.value === images.value.length - 1) {
|
|
|
+ // 保存100%进度
|
|
|
+ if (!savedProgress.value.includes(100)) {
|
|
|
+ saveProgress(100);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -99,6 +169,33 @@ onUnmounted(() => {
|
|
|
clearInterval(autoplayTimer.value)
|
|
|
}
|
|
|
})
|
|
|
+
|
|
|
+// 组件挂载时
|
|
|
+onMounted(() => {
|
|
|
+ // 加载保存的进度
|
|
|
+ loadSavedProgress();
|
|
|
+})
|
|
|
+
|
|
|
+// 加载保存的进度
|
|
|
+const loadSavedProgress = () => {
|
|
|
+ if (props.courseId) {
|
|
|
+ try {
|
|
|
+ const savedData = localStorage.getItem(`imageProgress_${props.courseId}`);
|
|
|
+ if (savedData) {
|
|
|
+ const { progress, currentIndex: savedIndex } = JSON.parse(savedData);
|
|
|
+ if (savedIndex !== undefined && savedIndex >= 0 && savedIndex < images.value.length) {
|
|
|
+ currentIndex.value = savedIndex;
|
|
|
+ // 检查是否已有保存的进度点
|
|
|
+ if (progress >= 10) savedProgress.value.push(10);
|
|
|
+ if (progress >= 50) savedProgress.value.push(50);
|
|
|
+ if (progress >= 100) savedProgress.value.push(100);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('读取保存的进度失败:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
</script>
|
|
|
|
|
|
<style scoped lang="scss">
|