From 25653b1cf78857e2c24be3823450a010c7d7da56 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Tue, 28 Apr 2020 19:07:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=A6=96=E9=A1=B5=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E8=AF=BE=E7=A8=8B=EF=BC=8C=E6=96=B0=E4=B8=8A=E8=AF=BE?= =?UTF-8?q?=E7=A8=8B=EF=BC=8C=E5=85=8D=E8=B4=B9=E8=AF=BE=E7=A8=8B=EF=BC=8C?= =?UTF-8?q?=E4=BC=9A=E5=91=98=E8=AF=BE=E7=A8=8B=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Builders/CourseList.php | 87 ++--------- app/Caches/CourseRelatedList.php | 1 - app/Caches/CourseUser.php | 39 ----- app/Caches/HotCourseList.php | 72 --------- app/Caches/IndexFreeCourseList.php | 114 ++++++++++++++ ...eacherList.php => IndexHotTeacherList.php} | 2 +- app/Caches/IndexLiveCourseList.php | 141 ++++++++++++++++++ app/Caches/IndexNewCourseList.php | 113 ++++++++++++++ app/Caches/IndexVipCourseList.php | 114 ++++++++++++++ app/Caches/LatestCourseList.php | 77 ---------- app/Caches/PackageCourseList.php | 1 - app/Caches/TopicCourseList.php | 1 - app/Console/Tasks/SyncChapterCounterTask.php | 9 +- app/Console/Tasks/SyncCourseCounterTask.php | 9 +- .../Admin/Controllers/CourseController.php | 2 + app/Http/Admin/Services/Chapter.php | 2 +- app/Http/Admin/Services/Course.php | 10 +- app/Http/Admin/Views/course/edit_basic.volt | 27 +++- app/Http/Admin/Views/course/edit_sale.volt | 2 +- app/Http/Admin/Views/course/list.volt | 16 +- app/Http/Admin/Views/course/search.volt | 18 +++ app/Http/Admin/Views/slide/edit.volt | 2 +- app/Providers/Volt.php | 2 +- app/Repos/Course.php | 13 +- app/Services/Frontend/Course/CourseInfo.php | 35 +++-- 25 files changed, 609 insertions(+), 300 deletions(-) delete mode 100644 app/Caches/CourseUser.php delete mode 100644 app/Caches/HotCourseList.php create mode 100644 app/Caches/IndexFreeCourseList.php rename app/Caches/{HotTeacherList.php => IndexHotTeacherList.php} (89%) create mode 100644 app/Caches/IndexLiveCourseList.php create mode 100644 app/Caches/IndexNewCourseList.php create mode 100644 app/Caches/IndexVipCourseList.php delete mode 100644 app/Caches/LatestCourseList.php diff --git a/app/Builders/CourseList.php b/app/Builders/CourseList.php index 4cf907c6..43be2cd8 100644 --- a/app/Builders/CourseList.php +++ b/app/Builders/CourseList.php @@ -3,111 +3,56 @@ namespace App\Builders; use App\Caches\CategoryList as CategoryListCache; -use App\Repos\CourseCategory as CourseCategoryRepo; use App\Repos\User as UserRepo; class CourseList extends Builder { - public function handleCourses($courses) - { - $baseUrl = kg_ci_base_url(); - - $list = []; - - foreach ($courses as $course) { - - $course['categories'] = []; - $course['cover'] = $baseUrl . $course['cover']; - $course['attrs'] = json_decode($course['attrs'], true); - - $result[] = [ - 'id' => $course['id'], - 'title' => $course['title'], - 'cover' => $course['cover'], - 'summary' => $course['summary'], - 'categories' => $course['categories'], - 'market_price' => (float)$course['market_price'], - 'vip_price' => (float)$course['vip_price'], - 'study_expiry' => $course->study_expiry, - 'refund_expiry' => $course->refund_expiry, - 'rating' => (float)$course['rating'], - 'score' => (float)$course['score'], - 'model' => $course['model'], - 'level' => $course['level'], - 'attrs' => $course['attrs'], - 'user_count' => $course['user_count'], - 'lesson_count' => $course['lesson_count'], - 'comment_count' => $course['comment_count'], - 'review_count' => $course['review_count'], - 'favorite_count' => $course['favorite_count'], - ]; - } - - return $list; - } - public function handleCategories($courses) { - $categories = $this->getCategories($courses); + $categories = $this->getCategories(); foreach ($courses as $key => $course) { - $courses[$key]['categories'] = $categories[$course['id']] ?? []; + $courses[$key]['category'] = $categories[$course['category_id']] ?? []; } return $courses; } - public function handleUsers($courses) + public function handleTeachers($courses) { - $users = $this->getUsers($courses); + $teachers = $this->getTeachers($courses); foreach ($courses as $key => $course) { - $courses[$key]['user'] = $users[$course['user_id']] ?? []; + $courses[$key]['teacher'] = $teachers[$course['teacher_id']] ?? []; } return $courses; } - protected function getCategories($courses) + protected function getCategories() { - $categoryListCache = new CategoryListCache(); + $cache = new CategoryListCache(); - $categoryList = $categoryListCache->get(); + $items = $cache->get(); - if (!$categoryList) { - return []; - } - - $mapping = []; - - foreach ($categoryList as $category) { - $mapping[$category['id']] = [ - 'id' => $category['id'], - 'name' => $category['name'], - ]; - } - - $courseIds = kg_array_column($courses, 'id'); - - $courseCategoryRepo = new CourseCategoryRepo(); - - $relations = $courseCategoryRepo->findByCourseIds($courseIds); + if (!$items) return null; $result = []; - foreach ($relations as $relation) { - $categoryId = $relation->category_id; - $courseId = $relation->course_id; - $result[$courseId][] = $mapping[$categoryId] ?? []; + foreach ($items as $item) { + $result[$item['id']] = [ + 'id' => $item['id'], + 'name' => $item['name'], + ]; } return $result; } - protected function getUsers($courses) + protected function getTeachers($courses) { - $ids = kg_array_column($courses, 'user_id'); + $ids = kg_array_column($courses, 'teacher_id'); $userRepo = new UserRepo(); diff --git a/app/Caches/CourseRelatedList.php b/app/Caches/CourseRelatedList.php index 67f80c90..70c797eb 100644 --- a/app/Caches/CourseRelatedList.php +++ b/app/Caches/CourseRelatedList.php @@ -47,7 +47,6 @@ class CourseRelatedList extends Cache 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'summary' => $course->summary, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, 'rating' => $course->rating, diff --git a/app/Caches/CourseUser.php b/app/Caches/CourseUser.php deleted file mode 100644 index 433a9425..00000000 --- a/app/Caches/CourseUser.php +++ /dev/null @@ -1,39 +0,0 @@ -lifetime; - } - - /** - * id = {$courseId}_{$userId} - * - * @param string $id - * @return string - */ - public function getKey($id = null) - { - return "course_user:{$id}"; - } - - public function getContent($id = null) - { - list($courseId, $userId) = explode('_', $id); - - $courseUserRepo = new CourseUserRepo(); - - $courseUser = $courseUserRepo->findCourseUser($courseId, $userId); - - return $courseUser ?: null; - } - -} diff --git a/app/Caches/HotCourseList.php b/app/Caches/HotCourseList.php deleted file mode 100644 index c488bbb2..00000000 --- a/app/Caches/HotCourseList.php +++ /dev/null @@ -1,72 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return 'hot_course_list'; - } - - public function getContent($id = null) - { - $courses = $this->findHotCourses($id); - - if ($courses->count() == 0) { - return []; - } - - return $this->handleContent($courses); - } - - /** - * @param CourseModel[] $courses - * @return array - */ - public function handleContent($courses) - { - $result = []; - - foreach ($courses as $course) { - $result[] = [ - 'id' => $course->id, - 'title' => $course->title, - 'summary' => $course->summary, - 'cover' => $course->cover, - 'market_price' => $course->market_price, - 'vip_price' => $course->vip_price, - 'model' => $course->model, - 'level' => $course->level, - ]; - } - - return $result; - } - - /** - * @param int $limit - * @return ResultsetInterface|Resultset|CourseModel[] - */ - protected function findHotCourses($limit = 10) - { - return CourseModel::query() - ->where('deleted = 0') - ->orderBy('score DESC') - ->limit($limit) - ->execute(); - } - -} diff --git a/app/Caches/IndexFreeCourseList.php b/app/Caches/IndexFreeCourseList.php new file mode 100644 index 00000000..3ed9377c --- /dev/null +++ b/app/Caches/IndexFreeCourseList.php @@ -0,0 +1,114 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_free_course_list'; + } + + public function getContent($id = null) + { + $result = []; + + $categoryLimit = 5; + + $courseLimit = 5; + + $categories = $this->findCategories($categoryLimit); + + if ($categories->count() == 0) { + return null; + } + + foreach ($categories as $category) { + + $categoryItem = [ + 'id' => $category->id, + 'name' => $category->name, + ]; + + $courses = $this->findCategoryCourses($category->id, $courseLimit); + + if ($courses->count() == 0) continue; + + $categoryCourses = []; + + foreach ($courses as $course) { + $categoryCourses[] = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, + ]; + } + + $categoryItem['courses'] = $categoryCourses; + + $result[] = $categoryItem; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|CategoryModel[] + */ + protected function findCategories($limit = 5) + { + return CategoryModel::query() + ->andWhere('published = 1') + ->orderBy('priority ASC') + ->limit($limit) + ->execute(); + } + + /** + * @param int $categoryId + * @param int $limit + * @return ResultsetInterface|Resultset|CourseModel[] + */ + protected function findCategoryCourses($categoryId, $limit = 10) + { + $categoryService = new CategoryService(); + + $categoryIds = $categoryService->getChildCategoryIds($categoryId); + + return CourseModel::query() + ->inWhere('category_id', $categoryIds) + ->andWhere('published = 1') + ->andWhere('market_price = 0') + ->orderBy('id DESC') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/HotTeacherList.php b/app/Caches/IndexHotTeacherList.php similarity index 89% rename from app/Caches/HotTeacherList.php rename to app/Caches/IndexHotTeacherList.php index d16c5579..ce412a8f 100644 --- a/app/Caches/HotTeacherList.php +++ b/app/Caches/IndexHotTeacherList.php @@ -2,7 +2,7 @@ namespace App\Caches; -class HotTeacherList extends Cache +class IndexHotTeacherList extends Cache { protected $lifetime = 365 * 86400; diff --git a/app/Caches/IndexLiveCourseList.php b/app/Caches/IndexLiveCourseList.php new file mode 100644 index 00000000..0521286f --- /dev/null +++ b/app/Caches/IndexLiveCourseList.php @@ -0,0 +1,141 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_live_course_list'; + } + + public function getContent($id = null) + { + /** + * 限制输出多少天数(一维限额) + */ + $dayLimit = 3; + + /** + * 限制每天维度下的输出数(二维限额) + */ + $perDayLimit = 5; + + $beginTime = strtotime('today'); + $endTime = strtotime("+30 days"); + + $result = []; + + /** + * @var Resultset|ChapterLiveModel[] $lives + */ + $lives = ChapterLiveModel::query() + ->betweenWhere('start_time', $beginTime, $endTime) + ->orderBy('start_time ASC') + ->execute(); + + if ($lives->count() == 0) { + return $result; + } + + $chapterIds = kg_array_column($lives->toArray(), 'chapter_id'); + + $chapterRepo = new ChapterRepo(); + + $chapters = $chapterRepo->findByIds($chapterIds); + + $chapterMappings = []; + + foreach ($chapters as $chapter) { + $chapterMappings[$chapter->id] = $chapter; + } + + $courseIds = kg_array_column($lives->toArray(), 'course_id'); + + $courseRepo = new CourseRepo(); + + $courses = $courseRepo->findByIds($courseIds); + + $courseMappings = []; + + foreach ($courses as $course) { + $courseMappings[$course->id] = $course; + } + + $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); + + $userRepo = new UserRepo(); + + $teachers = $userRepo->findByIds($teacherIds); + + $teacherMappings = []; + + foreach ($teachers as $teacher) { + $teacherMappings[$teacher->id] = $teacher; + } + + foreach ($lives as $live) { + + if (count($result) >= $dayLimit) { + break; + } + + $day = date('y-m-d', $live->start_time); + + if (isset($result[$day]) && count($result[$day]) >= $perDayLimit) { + continue; + } + + $chapter = $chapterMappings[$live->chapter_id]; + $course = $courseMappings[$chapter->course_id]; + $teacher = $teacherMappings[$course->teacher_id]; + + $chapterInfo = [ + 'id' => $chapter->id, + 'title' => $chapter->title, + ]; + + $courseInfo = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + ]; + + $teacherInfo = [ + 'id' => $teacher->id, + 'name' => $teacher->name, + 'avatar' => $teacher->avatar, + ]; + + $result[$day][] = [ + 'course' => $courseInfo, + 'chapter' => $chapterInfo, + 'teacher' => $teacherInfo, + 'start_time' => $live->start_time, + ]; + } + + return $result; + } + +} diff --git a/app/Caches/IndexNewCourseList.php b/app/Caches/IndexNewCourseList.php new file mode 100644 index 00000000..44a9ade4 --- /dev/null +++ b/app/Caches/IndexNewCourseList.php @@ -0,0 +1,113 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_new_course_list'; + } + + public function getContent($id = null) + { + $result = []; + + $categoryLimit = 5; + + $courseLimit = 5; + + $categories = $this->findCategories($categoryLimit); + + if ($categories->count() == 0) { + return null; + } + + foreach ($categories as $category) { + + $categoryItem = [ + 'id' => $category->id, + 'name' => $category->name, + ]; + + $courses = $this->findCategoryCourses($category->id, $courseLimit); + + if ($courses->count() == 0) continue; + + $categoryCourses = []; + + foreach ($courses as $course) { + $categoryCourses[] = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, + ]; + } + + $categoryItem['courses'] = $categoryCourses; + + $result[] = $categoryItem; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|CategoryModel[] + */ + protected function findCategories($limit = 5) + { + return CategoryModel::query() + ->andWhere('published = 1') + ->orderBy('priority ASC') + ->limit($limit) + ->execute(); + } + + /** + * @param int $categoryId + * @param int $limit + * @return ResultsetInterface|Resultset|CourseModel[] + */ + protected function findCategoryCourses($categoryId, $limit = 10) + { + $categoryService = new CategoryService(); + + $categoryIds = $categoryService->getChildCategoryIds($categoryId); + + return CourseModel::query() + ->inWhere('category_id', $categoryIds) + ->andWhere('published = 1') + ->orderBy('id DESC') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/IndexVipCourseList.php b/app/Caches/IndexVipCourseList.php new file mode 100644 index 00000000..c1fed8ac --- /dev/null +++ b/app/Caches/IndexVipCourseList.php @@ -0,0 +1,114 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_vip_course_list'; + } + + public function getContent($id = null) + { + $result = []; + + $categoryLimit = 5; + + $courseLimit = 5; + + $categories = $this->findCategories($categoryLimit); + + if ($categories->count() == 0) { + return null; + } + + foreach ($categories as $category) { + + $categoryItem = [ + 'id' => $category->id, + 'name' => $category->name, + ]; + + $courses = $this->findCategoryCourses($category->id, $courseLimit); + + if ($courses->count() == 0) continue; + + $categoryCourses = []; + + foreach ($courses as $course) { + $categoryCourses[] = [ + 'id' => $course->id, + 'title' => $course->title, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, + ]; + } + + $categoryItem['courses'] = $categoryCourses; + + $result[] = $categoryItem; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|CategoryModel[] + */ + protected function findCategories($limit = 5) + { + return CategoryModel::query() + ->andWhere('published = 1') + ->orderBy('priority ASC') + ->limit($limit) + ->execute(); + } + + /** + * @param int $categoryId + * @param int $limit + * @return ResultsetInterface|Resultset|CourseModel[] + */ + protected function findCategoryCourses($categoryId, $limit = 10) + { + $categoryService = new CategoryService(); + + $categoryIds = $categoryService->getChildCategoryIds($categoryId); + + return CourseModel::query() + ->inWhere('category_id', $categoryIds) + ->andWhere('published = 1') + ->andWhere('vip_price >= 0') + ->orderBy('id DESC') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/LatestCourseList.php b/app/Caches/LatestCourseList.php deleted file mode 100644 index a110ec3a..00000000 --- a/app/Caches/LatestCourseList.php +++ /dev/null @@ -1,77 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return 'latest_course_list'; - } - - public function getContent($id = null) - { - $courses = $this->findLatestCourses(5); - - if ($courses->count() == 0) { - return []; - } - - return $this->handleContent($courses); - } - - /** - * @param CourseModel[] $courses - * @return array - */ - public function handleContent($courses) - { - $result = []; - - $baseUrl = kg_ci_base_url(); - - foreach ($courses as $course) { - - $course->cover = $baseUrl . $course->cover; - - $result[] = [ - 'id' => $course->id, - 'title' => $course->title, - 'summary' => $course->summary, - 'cover' => $course->cover, - 'market_price' => $course->market_price, - 'vip_price' => $course->vip_price, - 'model' => $course->model, - 'level' => $course->level, - ]; - } - - return $result; - } - - /** - * @param int $limit - * @return ResultsetInterface|Resultset|CourseModel[] - */ - protected function findLatestCourses($limit = 10) - { - return CourseModel::query() - ->where('deleted = 0') - ->orderBy('create_time DESC') - ->limit($limit) - ->execute(); - } - -} diff --git a/app/Caches/PackageCourseList.php b/app/Caches/PackageCourseList.php index 05860e42..c213c507 100644 --- a/app/Caches/PackageCourseList.php +++ b/app/Caches/PackageCourseList.php @@ -46,7 +46,6 @@ class PackageCourseList extends Cache 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'summary' => $course->summary, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, 'rating' => $course->rating, diff --git a/app/Caches/TopicCourseList.php b/app/Caches/TopicCourseList.php index f02ffbbc..2ff4868e 100644 --- a/app/Caches/TopicCourseList.php +++ b/app/Caches/TopicCourseList.php @@ -46,7 +46,6 @@ class TopicCourseList extends Cache 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'summary' => $course->summary, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, 'rating' => $course->rating, diff --git a/app/Console/Tasks/SyncChapterCounterTask.php b/app/Console/Tasks/SyncChapterCounterTask.php index cd32fff6..61e79eba 100644 --- a/app/Console/Tasks/SyncChapterCounterTask.php +++ b/app/Console/Tasks/SyncChapterCounterTask.php @@ -2,6 +2,7 @@ namespace App\Console\Tasks; +use App\Caches\Chapter as ChapterCache; use App\Caches\ChapterCounter as ChapterCounterCache; use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\Chapter as ChapterRepo; @@ -45,11 +46,13 @@ class SyncChapterCounterTask extends Task return; } - $cache = new ChapterCounterCache(); + $chapterCache = new ChapterCache(); + + $chapterCounterCache = new ChapterCounterCache(); foreach ($chapters as $chapter) { - $counter = $cache->get($chapter->id); + $counter = $chapterCounterCache->get($chapter->id); if ($counter) { @@ -60,6 +63,8 @@ class SyncChapterCounterTask extends Task $chapter->oppose_count = $counter['oppose_count']; $chapter->update(); + + $chapterCache->rebuild($chapter->id); } } diff --git a/app/Console/Tasks/SyncCourseCounterTask.php b/app/Console/Tasks/SyncCourseCounterTask.php index 4ddc1a5b..9cda6066 100644 --- a/app/Console/Tasks/SyncCourseCounterTask.php +++ b/app/Console/Tasks/SyncCourseCounterTask.php @@ -2,6 +2,7 @@ namespace App\Console\Tasks; +use App\Caches\Course as CourseCache; use App\Caches\CourseCounter as CourseCounterCache; use App\Library\Cache\Backend\Redis as RedisCache; use App\Repos\Course as CourseRepo; @@ -45,11 +46,13 @@ class SyncCourseCounterTask extends Task return; } - $cache = new CourseCounterCache(); + $courseCounterCache = new CourseCounterCache(); + + $courseCache = new CourseCache(); foreach ($courses as $course) { - $counter = $cache->get($course->id); + $counter = $courseCounterCache->get($course->id); if ($counter) { @@ -61,6 +64,8 @@ class SyncCourseCounterTask extends Task $course->favorite_count = $counter['favorite_count']; $course->update(); + + $courseCache->rebuild($course->id); } } diff --git a/app/Http/Admin/Controllers/CourseController.php b/app/Http/Admin/Controllers/CourseController.php index 8e0612f9..6818a763 100644 --- a/app/Http/Admin/Controllers/CourseController.php +++ b/app/Http/Admin/Controllers/CourseController.php @@ -18,8 +18,10 @@ class CourseController extends Controller $courseService = new CourseService(); $xmCategories = $courseService->getXmCategories(0); + $xmTeachers = $courseService->getXmTeachers(0); $this->view->setVar('xm_categories', $xmCategories); + $this->view->setVar('xm_teachers', $xmTeachers); } /** diff --git a/app/Http/Admin/Services/Chapter.php b/app/Http/Admin/Services/Chapter.php index 7598d036..30daec62 100644 --- a/app/Http/Admin/Services/Chapter.php +++ b/app/Http/Admin/Services/Chapter.php @@ -44,13 +44,13 @@ class Chapter extends Service $data['course_id'] = $course->id; $data['title'] = $validator->checkTitle($post['title']); $data['summary'] = $validator->checkSummary($post['summary']); - $data['free'] = $validator->checkFreeStatus($post['free']); $chapterRepo = new ChapterRepo(); if (isset($post['parent_id'])) { $parent = $validator->checkParent($post['parent_id']); $data['parent_id'] = $parent->id; + $data['free'] = $validator->checkFreeStatus($post['free']); $data['priority'] = $chapterRepo->maxLessonPriority($post['parent_id']); } else { $data['priority'] = $chapterRepo->maxChapterPriority($post['course_id']); diff --git a/app/Http/Admin/Services/Course.php b/app/Http/Admin/Services/Course.php index 85f39c6f..11abf1b9 100644 --- a/app/Http/Admin/Services/Course.php +++ b/app/Http/Admin/Services/Course.php @@ -36,6 +36,11 @@ class Course extends Service $params['category_id'] = count($xmCategoryIds) > 1 ? $xmCategoryIds : $xmCategoryIds[0]; } + if (!empty($params['xm_teacher_ids'])) { + $xmTeacherIds = explode(',', $params['xm_teacher_ids']); + $params['teacher_id'] = count($xmTeacherIds) > 1 ? $xmTeacherIds : $xmTeacherIds[0]; + } + $params['deleted'] = $params['deleted'] ?? 0; $sort = $pagerQuery->getSort(); @@ -516,9 +521,10 @@ class Course extends Service $pipeA = $pager->items->toArray(); $pipeB = $builder->handleCategories($pipeA); - $pipeC = $builder->objects($pipeB); + $pipeC = $builder->handleTeachers($pipeB); + $pipeD = $builder->objects($pipeC); - $pager->items = $pipeC; + $pager->items = $pipeD; } return $pager; diff --git a/app/Http/Admin/Views/course/edit_basic.volt b/app/Http/Admin/Views/course/edit_basic.volt index 49cbe2de..9bd5fa4f 100644 --- a/app/Http/Admin/Views/course/edit_basic.volt +++ b/app/Http/Admin/Views/course/edit_basic.volt @@ -13,7 +13,7 @@ {% if course.cover %} {% else %} - + {{ image('id':'cover-img','class':'kg-cover','src':'admin/img/default_cover.png') }} {% endif %} @@ -49,7 +49,7 @@
- +
@@ -85,4 +85,27 @@ data: {{ xm_teachers|json_encode }} }); + layui.use(['jquery', 'layer'], function () { + + var $ = layui.jquery; + var layer = layui.layer; + + $('.kg-submit').on('click', function () { + + var xm_category_ids = $('input[name=xm_category_ids]'); + var xm_teacher_ids = $('input[name=xm_teacher_ids]'); + + if (xm_category_ids.val() === '') { + layer.msg('请选择分类', {icon: 2}); + return false; + } + + if (xm_teacher_ids.val() === '') { + layer.msg('请选择讲师', {icon: 2}); + return false; + } + }); + + }) + diff --git a/app/Http/Admin/Views/course/edit_sale.volt b/app/Http/Admin/Views/course/edit_sale.volt index 2b3271af..61e8f244 100644 --- a/app/Http/Admin/Views/course/edit_sale.volt +++ b/app/Http/Admin/Views/course/edit_sale.volt @@ -66,7 +66,7 @@ form.on('radio(price-mode)', function (data) { var priceBlock = $('#price-block'); - if (data.value == 'free') { + if (data.value === 'free') { priceBlock.hide(); } else { priceBlock.show(); diff --git a/app/Http/Admin/Views/course/list.volt b/app/Http/Admin/Views/course/list.volt index 94cbc3e6..010d55da 100644 --- a/app/Http/Admin/Views/course/list.volt +++ b/app/Http/Admin/Views/course/list.volt @@ -8,10 +8,16 @@ {% endif %} {%- endmacro %} -{%- macro category_info(items) %} - {% for item in items %} - {{ item.name }} - {% endfor %} +{%- macro category_info(category) %} + {% if category %} + 分类:{{ category.name }} + {% endif %} +{%- endmacro %} + +{%- macro teacher_info(teacher) %} + {% if teacher %} + 讲师:{{ teacher.name }} + {% endif %} {%- endmacro %}
@@ -54,7 +60,7 @@

标题:{{ item.title }} {{ model_info(item.model) }}

-

分类:{{ category_info(item.categories) }}

+

{{ category_info(item.category) }}  {{ teacher_info(item.teacher) }}

diff --git a/app/Http/Admin/Views/course/search.volt b/app/Http/Admin/Views/course/search.volt index a3d9db02..a15b1bb6 100644 --- a/app/Http/Admin/Views/course/search.volt +++ b/app/Http/Admin/Views/course/search.volt @@ -25,6 +25,13 @@
+
+ +
+
+
+
+
@@ -85,4 +92,15 @@ data: {{ xm_categories|json_encode }} }); + xmSelect.render({ + el: '#xm-teacher-ids', + name: 'xm_teacher_ids', + max: 5, + prop: { + name: 'name', + value: 'id' + }, + data: {{ xm_teachers|json_encode }} + }); + \ No newline at end of file diff --git a/app/Http/Admin/Views/slide/edit.volt b/app/Http/Admin/Views/slide/edit.volt index 67a6e390..1840a1f4 100644 --- a/app/Http/Admin/Views/slide/edit.volt +++ b/app/Http/Admin/Views/slide/edit.volt @@ -20,7 +20,7 @@ {% if slide.cover %} {% else %} - + {{ image('id':'cover-img','class':'kg-cover','src':'admin/img/default_cover.png') }} {% endif %}
diff --git a/app/Providers/Volt.php b/app/Providers/Volt.php index bc820280..a9af4740 100644 --- a/app/Providers/Volt.php +++ b/app/Providers/Volt.php @@ -38,7 +38,7 @@ class Volt extends Provider return 'kg_js_include(' . $resolvedArgs . ')'; }); - $compiler->addFunction('ci_image', function ($resolvedArgs) { + $compiler->addFunction('ci_img_url', function ($resolvedArgs) { return 'kg_ci_img_url(' . $resolvedArgs . ')'; }); diff --git a/app/Repos/Course.php b/app/Repos/Course.php index ba0f0db2..6d427851 100644 --- a/app/Repos/Course.php +++ b/app/Repos/Course.php @@ -31,13 +31,16 @@ class Course extends Repository $builder = $this->modelsManager->createBuilder(); if (!empty($where['category_id'])) { + $where['category_id'] = is_array($where['category_id']) ? $where['category_id'] : [$where['category_id']]; $builder->addFrom(CourseModel::class, 'c'); $builder->join(CourseCategoryModel::class, 'c.id = cc.course_id', 'cc'); - if (is_array($where['category_id'])) { - $builder->inWhere('cc.category_id', $where['category_id']); - } else { - $builder->where('cc.category_id = :category_id:', ['category_id' => $where['category_id']]); - } + $builder->inWhere('cc.category_id', $where['category_id']); + } elseif (!empty($where['teacher_id'])) { + $where['teacher_id'] = is_array($where['teacher_id']) ? $where['teacher_id'] : [$where['teacher_id']]; + $builder->addFrom(CourseModel::class, 'c'); + $builder->join(CourseUserModel::class, 'c.id = cu.course_id', 'cu'); + $builder->inWhere('cu.user_id', $where['teacher_id']); + $builder->andWhere('cu.role_type = :role_type:', ['role_type' => CourseUserModel::ROLE_TEACHER]); } else { $builder->addFrom(CourseModel::class, 'c'); $builder->where('1 = 1'); diff --git a/app/Services/Frontend/Course/CourseInfo.php b/app/Services/Frontend/Course/CourseInfo.php index 97294509..089c6bbf 100644 --- a/app/Services/Frontend/Course/CourseInfo.php +++ b/app/Services/Frontend/Course/CourseInfo.php @@ -19,7 +19,7 @@ class CourseInfo extends Service public function getCourse($id) { - $course = $this->checkCourse($id); + $course = $this->checkCourseCache($id); $user = $this->getCurrentUser(); @@ -49,6 +49,7 @@ class CourseInfo extends Service 'lesson_count' => $course->lesson_count, 'review_count' => $course->review_count, 'comment_count' => $course->comment_count, + 'consult_count' => $course->consult_count, 'favorite_count' => $course->favorite_count, ]; @@ -107,16 +108,24 @@ class CourseInfo extends Service $chapters = $cache->get($course->id); - $learningMapping = $this->getLearningMapping($course, $user); - - foreach ($chapters as &$chapter) { - foreach ($chapter['children'] as &$lesson) { - $owned = ($this->ownedCourse || $lesson['free']) ? 1 : 0; - $progress = $learningMapping[$lesson['id']]['progress'] ?? 0; - $lesson['me'] = [ - 'owned' => $owned, - 'progress' => $progress, - ]; + if ($user->id == 0) { + foreach ($chapters as &$chapter) { + foreach ($chapter['children'] as &$lesson) { + $lesson['me'] = [ + 'owned' => $this->ownedCourse || $lesson['free'] ? 1 : 0, + 'progress' => 0, + ]; + } + } + } else { + $mapping = $this->getLearningMapping($course, $user); + foreach ($chapters as &$chapter) { + foreach ($chapter['children'] as &$lesson) { + $lesson['me'] = [ + 'owned' => $this->ownedCourse || $lesson['free'] ? 1 : 0, + 'progress' => $mapping[$lesson['id']]['progress'] ?? 0, + ]; + } } } @@ -125,10 +134,6 @@ class CourseInfo extends Service protected function getLearningMapping(CourseModel $course, UserModel $user) { - if ($user->id == 0) { - return []; - } - $courseRepo = new CourseRepo(); $userLearnings = $courseRepo->findUserLearnings($course->id, $user->id);