diff --git a/src/api/curriculum.ts b/src/api/curriculum.ts index ecaec40..ad3d7c7 100644 --- a/src/api/curriculum.ts +++ b/src/api/curriculum.ts @@ -18,8 +18,6 @@ export interface CourseDepData { } export interface HourPlayData { - /** 独特的识别 */ - unique_ident: number /** 时间 */ duration: number /** 格式 */ @@ -117,10 +115,9 @@ export const curriculum = { }, /** 学习记录 */ hourCache(courseId: number, data: HourCacheParam) { - return request(`/api/v1/course/${courseId}/hour/cache`, "PUT", data) + return request(`/api/v1/course/${courseId}/hour/cache`, "PUT", data) }, - /** 删除 */ - delCache(courseId: number) { - return request(`/api/v1/course/${courseId}/hour/cache`, "DELETE") + commit(courseId: number, data: HourCacheParam) { + return request(`/api/v1/course/${courseId}/hour/commit`, "PUT", data) } } diff --git a/src/app.tsx b/src/app.tsx index 778cdcf..14389ff 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,7 +1,6 @@ import Taro, {useDidShow, useDidHide} from '@tarojs/taro' import './app.scss' import {CustomWrapper} from "@tarojs/components"; -import unique_ident from "@/hooks/unique_ident"; import {Profile} from '@/store' import storageDep from "@/hooks/storageDep"; @@ -38,8 +37,6 @@ function App(props) { Taro.useLaunch(() => { updateApp() storageDep.remove() - unique_ident.put() - unique_ident.del() }) Taro.getSystemInfo({ diff --git a/src/components/video/video.tsx b/src/components/video/video.tsx index 6275ffa..f2730e3 100644 --- a/src/components/video/video.tsx +++ b/src/components/video/video.tsx @@ -68,6 +68,7 @@ const HVideo: FC = (opt: HVideoOptions) => { opt.onEnded() } else { video?.seek(currentTime) + unique_ident.remove() video?.play() } } @@ -96,7 +97,9 @@ const HVideo: FC = (opt: HVideoOptions) => { }) Taro.useUnload(() => { - unique_ident.del(currentTime, Date.now()) + unique_ident.put(undefined, currentTime) + unique_ident.remove() + unique_ident.upload().then() }) diff --git a/src/hooks/unique_ident.ts b/src/hooks/unique_ident.ts index 995bff8..87ae56b 100644 --- a/src/hooks/unique_ident.ts +++ b/src/hooks/unique_ident.ts @@ -5,44 +5,58 @@ const KEY = 'unique_ident' /** 设置学习记录缓存 */ -function set(data: HourCacheParam) { +async function set(courseId: number) { Taro.removeStorageSync(KEY) + const res = await curriculum.hourCache(courseId, {duration: 0} as HourCacheParam) Taro.setStorage({ key: KEY, - data + data: { + courseId, + duration: 0, + end_date: 0, + hour_id: 0, + start_date: Date.now(), + unique_ident: res + } }) } +function remove() { + Taro.removeStorageSync(KEY) +} + /** * 上传学习记录 - * @param duration 视频播放的学习时长 + * @param hour_id 课时id + * @param duration * @param start_date 记录当前的时间戳 - * @param upload 是否上传 */ -function put(duration?: number, start_date?: number, upload = true) { +function put(hour_id?: number, duration?: number, start_date?: number) { const data: HourCacheParam | undefined = Taro.getStorageSync(KEY) if (data) { - data.duration = duration || data.duration data.start_date = start_date || data.start_date + data.duration = duration || data.duration + data.hour_id = hour_id || data.hour_id Taro.setStorageSync(KEY, data) - if (upload) { - curriculum.hourCache(data.courseId, data) - } } } -function del(duration?: number, start_date?: number, upload = true) { - put(duration, start_date, upload) +async function upload() { const data: HourCacheParam | undefined = Taro.getStorageSync(KEY) if (data) { - curriculum.delCache(data.courseId) + await curriculum.commit(data.courseId, data) } - Taro.removeStorageSync(KEY) +} + +function get(): HourCacheParam | undefined { + return Taro.getStorageSync(KEY) } export default { set, put, - del + remove, + upload, + get } diff --git a/src/pages/business/videoInfo/components/course.tsx b/src/pages/business/videoInfo/components/course.tsx index a2ce75d..0b9d142 100644 --- a/src/pages/business/videoInfo/components/course.tsx +++ b/src/pages/business/videoInfo/components/course.tsx @@ -1,7 +1,7 @@ import {ScrollView, Text, View} from "@tarojs/components"; import {FC, useEffect, useState} from "react"; import HVideo from "@/components/video/video"; -import {CurEndParam, curriculum, HourPlayData} from "@/api"; +import {curriculum, HourPlayData} from "@/api"; import {Profile} from '@/store' import Taro from "@tarojs/taro"; import Judge from "@/components/topic/judge"; @@ -28,11 +28,10 @@ const Course: FC = ({id, courseId, preview, curEnd}) => { const [validate, setValidate] = useState(false) // 开启验证 const [record, setRecord] = useState([]) // 考题记录 const [testId, setTestId] = useState(null) - const [startRecording, setStartRecording] = useState(null) // 视频开始记录 const {user} = Profile.useContainer() async function onEnded() { - unique_ident.put(data?.duration, Date.now()) // 记录 + const startRecording = unique_ident.get() startRecording && await curriculum.curEnd(courseId, id, {...startRecording, duration: data?.duration!}) // 结束 if (testId) { @@ -64,31 +63,13 @@ const Course: FC = ({id, courseId, preview, curEnd}) => { } async function getData() { - unique_ident.put() - + unique_ident.put(id, Date.now()) const res = await curriculum.hourPlay(courseId, id) - if (res) { setData(res) setBreakpoint(res.timeList) setExamAll(res.hourExamQuestions || []) setTestId(res?.hour_test?.id || null) - - setStartRecording({ - duration: 0, - unique_ident: res.unique_ident, - start_date: Date.now() - }) - - unique_ident.set({ - courseId: Number(courseId), - user_id: user?.id!, - duration: 0, - start_date: Date.now(), - end_date: Date.now(), - hour_id: id, - unique_ident: res.unique_ident - }) } } diff --git a/src/pages/business/videoInfo/videoInfo.tsx b/src/pages/business/videoInfo/videoInfo.tsx index 466d5af..bec6f10 100644 --- a/src/pages/business/videoInfo/videoInfo.tsx +++ b/src/pages/business/videoInfo/videoInfo.tsx @@ -8,6 +8,7 @@ import Taro from "@tarojs/taro"; import eventsIndex from "@/hooks/eventsIndex"; import {formatMinute} from "@/utils/time"; import videoEvents from "@/hooks/videoEvents"; +import unique_ident from "@/hooks/unique_ident"; const VideoInfo: FC = () => { const {id, depId} = Taro.getCurrentInstance()?.router?.params as any @@ -40,7 +41,8 @@ const VideoInfo: FC = () => { } useEffect(() => { - getData(false) + getData(false).then() + unique_ident.set(id) }, [id]) diff --git a/src/static/img/illnessTop.png b/src/static/img/illnessTop.png index 35ee98e..6c3f026 100644 Binary files a/src/static/img/illnessTop.png and b/src/static/img/illnessTop.png differ diff --git a/types/curriculum.d.ts b/types/curriculum.d.ts index 31cb7df..4b05e7d 100644 --- a/types/curriculum.d.ts +++ b/types/curriculum.d.ts @@ -106,7 +106,6 @@ interface HourCacheParam { hour_id: number; /** 课时开始学习时间 */ start_date: number; - user_id: number; unique_ident: number }