From 74b806b13d19cdce43df8b0511560dabd046cb57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BA=E7=8B=A8?= <18119604035@163.com> Date: Tue, 4 Jul 2023 15:00:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E8=AF=A6=E6=83=85=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E8=A7=86=E9=A2=91=E6=92=AD=E6=94=BE=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/course/compenents/hour.tsx | 19 +++- src/pages/course/compenents/videoHour.tsx | 16 ++- src/pages/course/index.tsx | 77 ++++--------- src/pages/course/video.tsx | 129 ++++++++-------------- 4 files changed, 91 insertions(+), 150 deletions(-) diff --git a/src/pages/course/compenents/hour.tsx b/src/pages/course/compenents/hour.tsx index 823e593..cf6c89e 100644 --- a/src/pages/course/compenents/hour.tsx +++ b/src/pages/course/compenents/hour.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; import styles from "./hour.module.scss"; import { durationFormat } from "../../../utils/index"; @@ -8,7 +8,6 @@ interface PropInterface { title: string; duration: number; record: any; - progress: number; onSuccess: (cid: number, id: number) => void; } @@ -18,9 +17,17 @@ export const HourCompenent: React.FC = ({ title, duration, record, - progress, onSuccess, }) => { + const [userProgress, setUserProgress] = useState(0); + useEffect(() => { + if (record?.finished_duration && record?.total_duration) { + setUserProgress((record.finished_duration * 100) / record.total_duration); + } else { + setUserProgress(0); + } + }, [record]); + return ( <>
= ({ 视频
- {progress > 0 && progress < 100 && ( + {userProgress > 0 && userProgress < 100 && (
学习到 @@ -42,14 +49,14 @@ export const HourCompenent: React.FC = ({
)} - {progress >= 100 && ( + {userProgress >= 100 && (
已学完{" "}
)}
- {title}{" "}({durationFormat(Number(duration))}) + {title} ({durationFormat(Number(duration))})
diff --git a/src/pages/course/compenents/videoHour.tsx b/src/pages/course/compenents/videoHour.tsx index 0855dc1..3fa208c 100644 --- a/src/pages/course/compenents/videoHour.tsx +++ b/src/pages/course/compenents/videoHour.tsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; import styles from "./hour.module.scss"; import { durationFormat } from "../../../utils/index"; @@ -9,7 +9,6 @@ interface PropInterface { title: string; duration: number; record: any; - progress: number; onSuccess: (cid: number, id: number) => void; } @@ -20,9 +19,16 @@ export const HourCompenent: React.FC = ({ title, duration, record, - progress, onSuccess, }) => { + const [userProgress, setUserProgress] = useState(0); + useEffect(() => { + if (record?.finished_duration && record?.total_duration) { + setUserProgress((record.finished_duration * 100) / record.total_duration); + } else { + setUserProgress(0); + } + }, [record]); return ( <>
= ({ 学习中
)} - {vid !== id && progress > 0 && progress < 100 && ( + {vid !== id && userProgress > 0 && userProgress < 100 && (
学习到 @@ -49,7 +55,7 @@ export const HourCompenent: React.FC = ({
)} - {vid !== id && progress >= 100 && ( + {vid !== id && userProgress >= 100 && (
已学完{" "}
diff --git a/src/pages/course/index.tsx b/src/pages/course/index.tsx index 3d1fb4c..4df38e7 100644 --- a/src/pages/course/index.tsx +++ b/src/pages/course/index.tsx @@ -128,34 +128,16 @@ const CoursePage = () => {
{hours[0].map((item: CourseHourModel) => (
- {learnHourRecord[item.id] ? ( - { - playVideo(cid, id); - }} - > - ) : ( - { - playVideo(cid, id); - }} - > - )} + { + playVideo(cid, id); + }} + >
))}
@@ -168,35 +150,16 @@ const CoursePage = () => {
{item.name}
{hours[item.id]?.map((it: CourseHourModel) => (
- {learnHourRecord[it.id] && ( - { - playVideo(cid, id); - }} - > - )} - {!learnHourRecord[it.id] && ( - { - playVideo(cid, id); - }} - > - )} + { + playVideo(cid, id); + }} + >
))} diff --git a/src/pages/course/video.tsx b/src/pages/course/video.tsx index 8e452d1..a9defbc 100644 --- a/src/pages/course/video.tsx +++ b/src/pages/course/video.tsx @@ -10,6 +10,14 @@ import { HourCompenent } from "./compenents/videoHour"; declare const window: any; +type LocalUserLearnHourRecordModel = { + [key: number]: UserLearnHourRecordModel; +}; + +type LocalCourseHour = { + [key: number]: CourseHourModel[]; +}; + const CoursePlayPage = () => { const navigate = useNavigate(); const params = useParams(); @@ -19,17 +27,17 @@ const CoursePlayPage = () => { const [playDuration, setPlayDuration] = useState(0); const [playendedStatus, setPlayendedStatus] = useState(false); const [lastSeeValue, setLastSeeValue] = useState({}); - const [course, setCourse] = useState({}); + const [course, setCourse] = useState(null); const [hour, setHour] = useState({}); const [loading, setLoading] = useState(false); const [isLastpage, setIsLastpage] = useState(false); const [totalHours, setTotalHours] = useState([]); const [playingTime, setPlayingTime] = useState(0); const [watchedSeconds, setWatchedSeconds] = useState(0); - const [chapters, setChapters] = useState([]); - const [hours, setHours] = useState({}); - const [learnRecord, setLearnRecord] = useState({}); - const [learnHourRecord, setLearnHourRecord] = useState({}); + const [chapters, setChapters] = useState([]); + const [hours, setHours] = useState(null); + const [learnHourRecord, setLearnHourRecord] = + useState({}); const myRef = useRef(0); const playRef = useRef(0); const watchRef = useRef(0); @@ -61,9 +69,6 @@ const CoursePlayPage = () => { Course.detail(Number(params.courseId)).then((res: any) => { setChapters(res.data.chapters); setHours(res.data.hours); - if (res.data.learn_record) { - setLearnRecord(res.data.learn_record); - } if (res.data.learn_hour_records) { setLearnHourRecord(res.data.learn_hour_records); } @@ -97,7 +102,8 @@ const CoursePlayPage = () => { setLoading(true); Course.play(Number(params.courseId), Number(params.hourId)) .then((res: any) => { - setCourse(res.data.course); + let courseItem: CourseModel = res.data.course; + setCourse(courseItem); setHour(res.data.hour); document.title = res.data.hour.title; let record = res.data.user_hour_record; @@ -276,87 +282,46 @@ const CoursePlayPage = () => {
- {chapters.length === 0 && JSON.stringify(hours) === "{}" && }{" "} - {chapters.length === 0 && JSON.stringify(hours) !== "{}" && ( -
- {hours[0].map((item: any, index: number) => ( + {chapters.length === 0 && !hours && } + {chapters.length === 0 && hours && ( +
+ {hours[0].map((item: CourseHourModel) => (
- {learnHourRecord[item.id] && ( - { - playVideo(cid, id); - }} - > - )} - {!learnHourRecord[item.id] && ( - { - playVideo(cid, id); - }} - > - )} + { + playVideo(cid, id); + }} + >
))}
)} - {chapters.length > 0 && JSON.stringify(hours) !== "{}" && ( + {chapters.length > 0 && hours && (
- {chapters.map((item: any, index: number) => ( + {chapters.map((item: ChapterModel) => (
{item.name}
- {hours[item.id] && - hours[item.id].map((it: any, int: number) => ( -
- {learnHourRecord[it.id] && ( - { - playVideo(cid, id); - }} - > - )} - {!learnHourRecord[it.id] && ( - { - playVideo(cid, id); - }} - > - )} -
- ))} + {hours[item.id]?.map((it: CourseHourModel) => ( +
+ { + playVideo(cid, id); + }} + > +
+ ))}
))}