From 56d7ddc7d4443c63504d548bc7db70806db04982 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Fri, 13 Dec 2024 08:37:31 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=202.=E4=BC=98=E5=8C=96=E6=9C=8D=E5=8A=A1=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=9B=91=E6=8E=A7=203.=E4=BC=98=E5=8C=96ChapterUserTr?= =?UTF-8?q?ait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Tasks/ServerMonitorTask.php | 6 +- app/Services/Logic/Chapter/ChapterInfo.php | 3 +- .../Logic/Chapter/ChapterUserTrait.php | 61 +++++++++++++++++++ app/Services/Logic/ChapterTrait.php | 47 -------------- app/Services/Logic/Course/ChapterList.php | 14 ++--- app/Services/Logic/Search/Article.php | 6 -- app/Services/Logic/Search/Course.php | 6 -- app/Services/Logic/Search/Question.php | 20 +----- .../Logic/User/Console/ProfileInfo.php | 2 - app/Services/Search/ArticleDocument.php | 5 +- app/Services/Search/CourseDocument.php | 3 - app/Services/Search/QuestionDocument.php | 6 +- 12 files changed, 79 insertions(+), 100 deletions(-) create mode 100644 app/Services/Logic/Chapter/ChapterUserTrait.php diff --git a/app/Console/Tasks/ServerMonitorTask.php b/app/Console/Tasks/ServerMonitorTask.php index 8f054931..9c4eeaad 100644 --- a/app/Console/Tasks/ServerMonitorTask.php +++ b/app/Console/Tasks/ServerMonitorTask.php @@ -177,13 +177,13 @@ class ServerMonitorTask extends Task $searcher = new CourseSearcher(); - $user = $searcher->search('id:1'); + $course = $searcher->search('id:1'); $benchmark->stop(); $elapsedTime = $benchmark->getElapsedTime(); - if (empty($user)) { + if (empty($course)) { return "xunsearch搜索失败"; } @@ -242,4 +242,4 @@ class ServerMonitorTask extends Task return $coreCount * $processorCount; } -} \ No newline at end of file +} diff --git a/app/Services/Logic/Chapter/ChapterInfo.php b/app/Services/Logic/Chapter/ChapterInfo.php index a5221e81..49d223a9 100644 --- a/app/Services/Logic/Chapter/ChapterInfo.php +++ b/app/Services/Logic/Chapter/ChapterInfo.php @@ -31,8 +31,9 @@ class ChapterInfo extends LogicService protected $user; use CourseTrait; - use CourseUserTrait; use ChapterTrait; + use CourseUserTrait; + use ChapterUserTrait; public function handle($id) { diff --git a/app/Services/Logic/Chapter/ChapterUserTrait.php b/app/Services/Logic/Chapter/ChapterUserTrait.php new file mode 100644 index 00000000..ff84fbee --- /dev/null +++ b/app/Services/Logic/Chapter/ChapterUserTrait.php @@ -0,0 +1,61 @@ +id == 0) return; + + $chapterUser = null; + + /** + * @var CourseUserModel $courseUser + */ + $courseUser = $this->courseUser; + + if ($courseUser) { + $chapterUserRepo = new ChapterUserRepo(); + $chapterUser = $chapterUserRepo->findChapterUser($chapter->id, $user->id); + } + + $this->chapterUser = $chapterUser; + + if ($chapterUser) { + $this->joinedChapter = true; + } + + if ($this->ownedCourse || $chapter->free) { + $this->ownedChapter = true; + } + } + +} diff --git a/app/Services/Logic/ChapterTrait.php b/app/Services/Logic/ChapterTrait.php index 877c111c..933b96cd 100644 --- a/app/Services/Logic/ChapterTrait.php +++ b/app/Services/Logic/ChapterTrait.php @@ -7,31 +7,11 @@ namespace App\Services\Logic; -use App\Models\Chapter as ChapterModel; -use App\Models\ChapterUser as ChapterUserModel; -use App\Models\CourseUser as CourseUserModel; -use App\Models\User as UserModel; -use App\Repos\ChapterUser as ChapterUserRepo; use App\Validators\Chapter as ChapterValidator; trait ChapterTrait { - /** - * @var bool - */ - protected $ownedChapter = false; - - /** - * @var bool - */ - protected $joinedChapter = false; - - /** - * @var ChapterUserModel|null - */ - protected $chapterUser; - public function checkChapterVod($id) { $validator = new ChapterValidator(); @@ -67,31 +47,4 @@ trait ChapterTrait return $validator->checkChapterCache($id); } - public function setChapterUser(ChapterModel $chapter, UserModel $user) - { - if ($user->id == 0) return; - - $chapterUser = null; - - /** - * @var CourseUserModel $courseUser - */ - $courseUser = $this->courseUser; - - if ($courseUser) { - $chapterUserRepo = new ChapterUserRepo(); - $chapterUser = $chapterUserRepo->findChapterUser($chapter->id, $user->id); - } - - $this->chapterUser = $chapterUser; - - if ($chapterUser) { - $this->joinedChapter = true; - } - - if ($this->ownedCourse || $chapter->free) { - $this->ownedChapter = true; - } - } - } diff --git a/app/Services/Logic/Course/ChapterList.php b/app/Services/Logic/Course/ChapterList.php index c94eda71..3d23dae9 100644 --- a/app/Services/Logic/Course/ChapterList.php +++ b/app/Services/Logic/Course/ChapterList.php @@ -50,14 +50,14 @@ class ChapterList extends LogicService protected function handleLoginUserChapters(array $chapters, CourseModel $course, UserModel $user) { - $mapping = $this->getLearningMapping($course->id, $user->id, $this->courseUser->plan_id); + $mappings = $this->getLearningMappings($course->id, $user->id, $this->courseUser->plan_id); foreach ($chapters as &$chapter) { foreach ($chapter['children'] as &$lesson) { $owned = ($this->ownedCourse || $lesson['free'] == 1) && $lesson['published'] == 1; $lesson['me'] = [ - 'progress' => $mapping[$lesson['id']]['progress'] ?? 0, - 'duration' => $mapping[$lesson['id']]['duration'] ?? 0, + 'progress' => $mappings[$lesson['id']]['progress'] ?? 0, + 'duration' => $mappings[$lesson['id']]['duration'] ?? 0, 'owned' => $owned ? 1 : 0, 'logged' => 1, ]; @@ -84,7 +84,7 @@ class ChapterList extends LogicService return $chapters; } - protected function getLearningMapping($courseId, $userId, $planId) + protected function getLearningMappings($courseId, $userId, $planId) { $courseRepo = new CourseRepo(); @@ -92,17 +92,17 @@ class ChapterList extends LogicService if ($userLearnings->count() == 0) return []; - $mapping = []; + $mappings = []; foreach ($userLearnings as $learning) { - $mapping[$learning->chapter_id] = [ + $mappings[$learning->chapter_id] = [ 'progress' => $learning->progress, 'duration' => $learning->duration, 'consumed' => $learning->consumed, ]; } - return $mapping; + return $mappings; } } diff --git a/app/Services/Logic/Search/Article.php b/app/Services/Logic/Search/Article.php index 0197a3ac..fc49f0a5 100644 --- a/app/Services/Logic/Search/Article.php +++ b/app/Services/Logic/Search/Article.php @@ -68,12 +68,6 @@ class Article extends Handler $owner = json_decode($item['owner'], true); $tags = json_decode($item['tags'], true); - $owner['avatar'] = $owner['avatar'] ?: kg_default_user_avatar_path(); - - if (!empty($owner['avatar']) && !Text::startsWith($owner['avatar'], 'http')) { - $owner['avatar'] = $baseUrl . $owner['avatar']; - } - if (!empty($item['cover']) && !Text::startsWith($item['cover'], 'http')) { $item['cover'] = $baseUrl . $item['cover']; } diff --git a/app/Services/Logic/Search/Course.php b/app/Services/Logic/Search/Course.php index 0926573f..1ee9e493 100644 --- a/app/Services/Logic/Search/Course.php +++ b/app/Services/Logic/Search/Course.php @@ -73,12 +73,6 @@ class Course extends Handler $teacher = json_decode($item['teacher'], true); $tags = json_decode($item['tags'], true); - $teacher['avatar'] = $teacher['avatar'] ?: kg_default_user_avatar_path(); - - if (!empty($teacher['avatar']) && !Text::startsWith($teacher['avatar'], 'http')) { - $teacher['avatar'] = $baseUrl . $teacher['avatar']; - } - if (!empty($item['cover']) && !Text::startsWith($item['cover'], 'http')) { $item['cover'] = $baseUrl . $item['cover']; } diff --git a/app/Services/Logic/Search/Question.php b/app/Services/Logic/Search/Question.php index a8788988..0f6db7d4 100644 --- a/app/Services/Logic/Search/Question.php +++ b/app/Services/Logic/Search/Question.php @@ -64,33 +64,17 @@ class Question extends Handler foreach ($pager->items as $item) { + $lastAnswer = json_decode($item['last_answer'], true); + $acceptAnswer = json_decode($item['accept_answer'], true); $lastReplier = json_decode($item['last_replier'], true); $category = json_decode($item['category'], true); $owner = json_decode($item['owner'], true); $tags = json_decode($item['tags'], true); - $owner['avatar'] = $owner['avatar'] ?: kg_default_user_avatar_path(); - - if (!empty($owner['avatar']) && !Text::startsWith($owner['avatar'], 'http')) { - $owner['avatar'] = $baseUrl . $owner['avatar']; - } - if (!empty($item['cover']) && !Text::startsWith($item['cover'], 'http')) { $item['cover'] = $baseUrl . $item['cover']; } - $lastAnswer = json_decode($item['last_answer'], true); - - if (!empty($lastAnswer['cover']) && !Text::startsWith($lastAnswer['cover'], 'http')) { - $lastAnswer['cover'] = $baseUrl . $lastAnswer['cover']; - } - - $acceptAnswer = json_decode($item['accept_answer'], true); - - if (!empty($acceptAnswer['cover']) && !Text::startsWith($acceptAnswer['cover'], 'http')) { - $acceptAnswer['cover'] = $baseUrl . $acceptAnswer['cover']; - } - $items[] = [ 'id' => (int)$item['id'], 'title' => (string)$item['title'], diff --git a/app/Services/Logic/User/Console/ProfileInfo.php b/app/Services/Logic/User/Console/ProfileInfo.php index 3fdafc7b..910a5938 100644 --- a/app/Services/Logic/User/Console/ProfileInfo.php +++ b/app/Services/Logic/User/Console/ProfileInfo.php @@ -22,8 +22,6 @@ class ProfileInfo extends LogicService protected function handleUser(UserModel $user) { - $user->avatar = kg_cos_user_avatar_url($user->avatar); - $user->area = $this->handleArea($user->area); return [ diff --git a/app/Services/Search/ArticleDocument.php b/app/Services/Search/ArticleDocument.php index 659044f4..d9615ec0 100644 --- a/app/Services/Search/ArticleDocument.php +++ b/app/Services/Search/ArticleDocument.php @@ -57,6 +57,8 @@ class ArticleDocument extends Injectable $category = $this->handleCategory($article->category_id); } + $article->cover = ArticleModel::getCoverPath($article->cover); + return [ 'id' => $article->id, 'title' => $article->title, @@ -81,12 +83,9 @@ class ArticleDocument extends Injectable $user = $userRepo->findById($id); - $user->avatar = UserModel::getAvatarPath($user->avatar); - return kg_json_encode([ 'id' => $user->id, 'name' => $user->name, - 'avatar' => $user->avatar, ]); } diff --git a/app/Services/Search/CourseDocument.php b/app/Services/Search/CourseDocument.php index b38d356f..a1b6f774 100644 --- a/app/Services/Search/CourseDocument.php +++ b/app/Services/Search/CourseDocument.php @@ -102,12 +102,9 @@ class CourseDocument extends Injectable $user = $userRepo->findById($id); - $user->avatar = UserModel::getAvatarPath($user->avatar); - return kg_json_encode([ 'id' => $user->id, 'name' => $user->name, - 'avatar' => $user->avatar, ]); } diff --git a/app/Services/Search/QuestionDocument.php b/app/Services/Search/QuestionDocument.php index 3c8a5e2a..4f1d70e7 100644 --- a/app/Services/Search/QuestionDocument.php +++ b/app/Services/Search/QuestionDocument.php @@ -76,6 +76,8 @@ class QuestionDocument extends Injectable $acceptAnswer = $this->handleAnswer($question->accept_answer_id); } + $question->cover = QuestionModel::getCoverPath($question->cover); + return [ 'id' => $question->id, 'title' => $question->title, @@ -108,12 +110,9 @@ class QuestionDocument extends Injectable $user = $userRepo->findById($id); - $user->avatar = UserModel::getAvatarPath($user->avatar); - return kg_json_encode([ 'id' => $user->id, 'name' => $user->name, - 'avatar' => $user->avatar, ]); } @@ -138,7 +137,6 @@ class QuestionDocument extends Injectable return kg_json_encode([ 'id' => $answer->id, 'summary' => $answer->summary, - 'cover' => $answer->cover, ]); }