From 74c08316f10784c06f77e8f87d91f44b900f2e4f Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Sun, 12 Feb 2023 17:09:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=AE=B0=E5=BF=86=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Tasks/SyncLearningTask.php | 13 ++++++++--- app/Repos/Chapter.php | 3 +++ public/static/home/js/chapter.vod.player.js | 26 +++++++++++++++++---- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/Console/Tasks/SyncLearningTask.php b/app/Console/Tasks/SyncLearningTask.php index a1b9dbe0..c8a86463 100644 --- a/app/Console/Tasks/SyncLearningTask.php +++ b/app/Console/Tasks/SyncLearningTask.php @@ -48,10 +48,12 @@ class SyncLearningTask extends Task */ protected function handleLearning($itemKey) { + $cache = $this->getCache(); + /** * @var LearningModel $cacheLearning */ - $cacheLearning = $this->cache->get($itemKey); + $cacheLearning = $cache->get($itemKey); if (!$cacheLearning) return; @@ -76,7 +78,7 @@ class SyncLearningTask extends Task $this->updateChapterUser($dbLearning); } - $this->cache->delete($itemKey); + $cache->delete($itemKey); } /** @@ -111,7 +113,12 @@ class SyncLearningTask extends Task $progress = floor(100 * $chapterUser->duration / $duration); - $chapterUser->position = floor($learning->position); + /** + * 过于接近结束位置当作已结束处理,播放位置为起点0 + */ + $playPosition = $duration - $learning->position > 10 ? floor($learning->position) : 0; + + $chapterUser->position = $playPosition; $chapterUser->progress = $progress < 100 ? $progress : 100; $chapterUser->consumed = $chapterUser->duration > 0.3 * $duration ? 1 : 0; diff --git a/app/Repos/Chapter.php b/app/Repos/Chapter.php index e98fcc62..8f1e63e1 100644 --- a/app/Repos/Chapter.php +++ b/app/Repos/Chapter.php @@ -99,6 +99,9 @@ class Chapter extends Repository */ public function findByFileId($fileId) { + /** + * @var ChapterVodModel $vod + */ $vod = ChapterVodModel::findFirst([ 'conditions' => 'file_id = :file_id:', 'bind' => ['file_id' => $fileId], diff --git a/public/static/home/js/chapter.vod.player.js b/public/static/home/js/chapter.vod.player.js index 3aaa7f69..afcf7af0 100644 --- a/public/static/home/js/chapter.vod.player.js +++ b/public/static/home/js/chapter.vod.player.js @@ -7,6 +7,7 @@ layui.use(['jquery', 'helper'], function () { var intervalTime = 15000; var userId = window.user.id; var requestId = helper.getRequestId(); + var chapterId = $('input[name="chapter.id"]').val(); var planId = $('input[name="chapter.plan_id"]').val(); var lastPosition = $('input[name="chapter.position"]').val(); var learningUrl = $('input[name="chapter.learning_url"]').val(); @@ -57,15 +58,30 @@ layui.use(['jquery', 'helper'], function () { player.toggle(); }); - var position = parseInt(lastPosition); + var position = getLastPosition(); /** - * 过于接近结束位置当作已结束处理 + * 上次播放位置 */ - if (position > 0 && player.video.duration - position > 10) { + if (position > 0) { player.seek(position); } + function getPositionKey() { + return 'chapter:' + chapterId + ':position'; + } + + function getLastPosition() { + var key = getPositionKey(); + var value = localStorage.getItem(key); + return value != null ? parseInt(value) : lastPosition; + } + + function setLastPosition(value) { + var key = getPositionKey(); + localStorage.setItem(key, value); + } + function clearLearningInterval() { if (interval != null) { clearInterval(interval); @@ -87,11 +103,13 @@ layui.use(['jquery', 'helper'], function () { } function ended() { - clearLearningInterval(); learning(); + setLastPosition(0); + clearLearningInterval(); } function learning() { + setLastPosition(player.video.currentTime); if (userId !== '0' && planId !== '0') { $.ajax({ type: 'POST',