From c6aaa868bc4d48ae9da653c18766c8d3ac0f65bd Mon Sep 17 00:00:00 2001 From: none Date: Tue, 4 Jul 2023 14:31:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/course/index.tsx | 203 ++++++++++++++++++------------------- src/playedu.d.ts | 58 +++++++++++ 2 files changed, 158 insertions(+), 103 deletions(-) create mode 100644 src/playedu.d.ts diff --git a/src/pages/course/index.tsx b/src/pages/course/index.tsx index e3423dc..3d1fb4c 100644 --- a/src/pages/course/index.tsx +++ b/src/pages/course/index.tsx @@ -7,15 +7,29 @@ import { course as vod } from "../../api/index"; import { Empty } from "../../components"; import { HourCompenent } from "./compenents/hour"; +type LocalUserLearnHourRecordModel = { + [key: number]: UserLearnHourRecordModel; +}; + +type LocalCourseHour = { + [key: number]: CourseHourModel[]; +}; + const CoursePage = () => { const { courseId } = useParams(); const navigate = useNavigate(); - const [loading, setLoading] = useState(false); - const [course, setCourse] = useState({}); - const [chapters, setChapters] = useState([]); - const [hours, setHours] = useState({}); - const [learnRecord, setLearnRecord] = useState({}); - const [learnHourRecord, setLearnHourRecord] = useState({}); + + const [course, setCourse] = useState(null); + const [chapters, setChapters] = useState([]); + const [hours, setHours] = useState(null); + const [learnRecord, setLearnRecord] = useState( + null + ); + const [learnHourRecord, setLearnHourRecord] = + useState({}); + + const [courseTypeText, setCourseTypeText] = useState(""); + const [userCourseProgress, setUserCourseProgress] = useState(0); useEffect(() => { if (courseId) { @@ -25,19 +39,40 @@ const CoursePage = () => { const getDetail = (cid: number) => { vod.detail(cid).then((res: any) => { - document.title = res.data.course.title; - setCourse(res.data.course); + let courseItem: CourseModel = res.data.course; + + document.title = courseItem.title || "课程详情"; + + setCourse(courseItem); 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); } }); }; + useEffect(() => { + if (course) { + setCourseTypeText(course.is_required === 1 ? "必修课" : "选修课"); + } + }, [course]); + + useEffect(() => { + if (learnRecord?.progress) { + setUserCourseProgress(learnRecord.progress / 100); + } else if (learnHourRecord) { + setUserCourseProgress(1); + } else { + setUserCourseProgress(0); + } + }, [learnRecord, learnHourRecord]); + const playVideo = (cid: number, id: number) => { navigate(`/course/${cid}/hour/${id}`); }; @@ -52,7 +87,7 @@ const CoursePage = () => { />
-
{course.title}
+
{course?.title}
@@ -60,77 +95,40 @@ const CoursePage = () => { {learnRecord ? learnRecord.finished_count || 0 : 0} {" "} - / {course.class_hour} + / {course?.class_hour}
- {course.is_required === 1 && ( -
必修课
- )} - {course.is_required === 0 && ( -
选修课
- )} +
{courseTypeText}
- {JSON.stringify(learnRecord) === "{}" && - JSON.stringify(learnHourRecord) === "{}" && ( - - 0% - - )} - {JSON.stringify(learnRecord) === "{}" && - JSON.stringify(learnHourRecord) !== "{}" && ( - - 1% - - )} - {JSON.stringify(learnRecord) !== "{}" && - JSON.stringify(learnHourRecord) !== "{}" && ( - - - {Math.floor(learnRecord.progress / 100)}% - - - )} + + {userCourseProgress}% +
- {course.short_desc && ( + {course?.short_desc && ( <>
{course.short_desc}
)}
- {chapters.length === 0 && JSON.stringify(hours) === "{}" && }{" "} - {chapters.length === 0 && JSON.stringify(hours) !== "{}" && ( + {chapters.length === 0 && !hours && } + + {chapters.length === 0 && hours && (
- {hours[0].map((item: any, index: number) => ( + {hours[0].map((item: CourseHourModel) => (
- {learnHourRecord[item.id] && ( + {learnHourRecord[item.id] ? ( { playVideo(cid, id); }} > - )} - {!learnHourRecord[item.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) => ( +
+ {learnHourRecord[it.id] && ( + { + playVideo(cid, id); + }} + > + )} + {!learnHourRecord[it.id] && ( + { + playVideo(cid, id); + }} + > + )} +
+ ))}
))}
diff --git a/src/playedu.d.ts b/src/playedu.d.ts new file mode 100644 index 0000000..d78058e --- /dev/null +++ b/src/playedu.d.ts @@ -0,0 +1,58 @@ +declare global { + interface CourseModel { + id: number; + title: string; + thumb: string; + short_desc: string; + is_required: number; + charge: number; + class_hour: number; + } + + interface ChapterModel { + id: number; + name: string; + course_id: number; + sort: number; + } + + interface CourseHourModel { + id: number; + course_id: number; + chapter_id: number; + duration: number; + rid: number; + sort: number; + title: string; + type: string; + } + + interface UserLearnRecordModel { + id: number; + user_id: number; + course_id: number; + hour_count: number; + finished_at: string; + finished_count: number; + is_finished: number; + progress: number; + created_at: string; + updated_at: string; + } + + interface UserLearnHourRecordModel { + id: number; + user_id: number; + course_id: number; + hour_id: number; + is_finished: number; + finished_duration: number; + real_duration: number; + total_duration: number; + finished_at: string; + created_at: string; + updated_at: string; + } +} + +export {};