import {FC, useEffect, useMemo, useState} from "react"; import Taro, {useDidShow} from "@tarojs/taro"; import {ScrollView, Swiper, SwiperItem, View} from "@tarojs/components"; import {Courses, CoursesKey, publicApi} from "@/api/public"; import VideoCover from "@/components/videoCover/videoCover"; import {formatMinute} from "@/utils/time"; import {userApi} from "@/api"; import Empty from "@/components/empty/empty"; import {Profile} from "@/store"; import LoginView from "@/components/loginView"; interface Props { categoryKey: CoursesKey setCategoryKey: (categoryKey: CoursesKey) => void } export const VideoList: FC = ({categoryKey, setCategoryKey}) => { const [data, setData] = useState({ is_required: [], is_not_required: [], }) const [page, setPage] = useState(1) const [records, setRecords] = useState([]) const {token} = Profile.useContainer() const {windowHeight, textBarHeight, statusBarHeight} = Taro.getApp().globalData const pageHeight = windowHeight - textBarHeight - statusBarHeight function screen(oldData: Curriculum[], data: Curriculum[]): Curriculum[] { return data.reduce((pre, cur) => { const index = pre.findIndex(d => d.id === cur.id) if (index === -1) { pre.push(cur) } else { pre.splice(index, 1, cur) } return pre }, oldData) } async function getData() { try { const res = await publicApi.course({page: page, pageSize: 10}) const old: Courses = JSON.parse(JSON.stringify(data)) setData({ is_required: screen(old.is_required, res.is_required || []), is_not_required: screen(old.is_not_required, res.is_not_required || []), }) } catch (e) { } } async function getRecords() { try { const {user_course_records} = await userApi.learningRecord() setRecords(user_course_records) } catch (e) { } } function rateOfLearning(id: number, class_hour: number): JSX.Element { const find = records.find(d => d?.course_id === id) if (find) { if (class_hour === find.finished_count) { return 已完成 } return ({`共${class_hour}节/已学${find.finished_count}节`}) } return ({`共${class_hour}节/已学0节`}) } function rateOfPercent(id: number, class_hour: number): string { const find = records.find(d => d?.course_id === id) if (find) { if (class_hour === find.finished_count) { return '100%' } return ` ${((find.finished_count / class_hour) * 100).toFixed(0)}%` } return '0%' } const fetchData = () => { getData().then() getRecords().then() } useDidShow(fetchData) useEffect(fetchData, [page]) function changeSwiper(e) { const index = e.detail.current const categoryKeys: CoursesKey[] = Object.keys(data) as CoursesKey[] setCategoryKey(categoryKeys[index]) } const categoryIndex = useMemo(() => { const index = Object.keys(data).findIndex(d => d === categoryKey) return index < 0 ? 0 : index }, [data, categoryKey]) return ( { Object.entries(data).map(([key, value]) => setPage(page + 1)} enhanced showScrollbar={false}> { !token && key === 'is_required' ? : value?.length ? <> { value?.map(c => ) } 暂无更多 : } ) } ) }