diff --git a/app/Builders/CategoryTreeList.php b/app/Builders/CategoryTreeList.php index 25735823..82f22690 100644 --- a/app/Builders/CategoryTreeList.php +++ b/app/Builders/CategoryTreeList.php @@ -2,43 +2,64 @@ namespace App\Builders; +use App\Models\Category as CategoryModel; +use Phalcon\Mvc\Model\Resultset; +use Phalcon\Mvc\Model\ResultsetInterface; + class CategoryTreeList extends Builder { - public function handleTreeList($categories) + public function handle() { - $list = []; + $topCategories = $this->findChildCategories(0); - foreach ($categories as $category) { - if ($category['parent_id'] == 0) { - $key = $category['id']; - $list[$key] = [ - 'id' => $category['id'], - 'name' => $category['name'], - 'priority' => $category['priority'], - 'children' => [], - ]; - } else { - $key = $category['parent_id']; - $list[$key]['children'][] = [ - 'id' => $category['id'], - 'name' => $category['name'], - 'priority' => $category['priority'], - ]; - } + if ($topCategories->count() == 0) { + return []; } - usort($list, function ($a, $b) { - return $a['priority'] > $b['priority']; - }); + $list = []; - foreach ($list as $key => $value) { - usort($list[$key]['children'], function ($a, $b) { - return $a['priority'] > $b['priority']; - }); + foreach ($topCategories as $category) { + $list[] = [ + 'id' => $category->id, + 'name' => $category->name, + 'children' => $this->handleChildren($category), + ]; } return $list; } + protected function handleChildren(CategoryModel $category) + { + $subCategories = $this->findChildCategories($category->id); + + if ($subCategories->count() == 0) { + return []; + } + + $list = []; + + foreach ($subCategories as $category) { + $list[] = [ + 'id' => $category->id, + 'name' => $category->name, + ]; + } + + return $list; + } + + /** + * @param int $categoryId + * @return ResultsetInterface|Resultset|CategoryModel[] + */ + protected function findChildCategories($categoryId = 0) + { + return CategoryModel::query() + ->where('parent_id = :parent_id:', ['parent_id' => $categoryId]) + ->andWhere('deleted = 0') + ->execute(); + } + } diff --git a/app/Builders/Chapter.php b/app/Builders/Chapter.php deleted file mode 100644 index d55ee8cc..00000000 --- a/app/Builders/Chapter.php +++ /dev/null @@ -1,19 +0,0 @@ - $chapter['id'], - 'title' => $chapter['title'], - 'summary' => $chapter['summary'], - 'priority' => $chapter['priority'], - 'children' => [], - ]; - } else { - $key = $chapter['parent_id']; - $list[$key]['children'][] = $this->handleChapter($chapter); - } + $chapters = $this->findChapters($courseId); + + if ($chapters->count() == 0) { + return []; } - usort($list, function ($a, $b) { - return $a['priority'] > $b['priority']; - }); - - foreach ($list as $key => $value) { - usort($list[$key]['children'], function ($a, $b) { - return $a['priority'] > $b['priority']; - }); + foreach ($chapters as $chapter) { + $list[] = [ + 'id' => $chapter->id, + 'title' => $chapter->title, + 'summary' => $chapter->summary, + 'priority' => $chapter->priority, + 'children' => $this->handleChildren($chapter), + ]; } return $list; } /** - * @param array $chapter + * @param ChapterModel $chapter * @return array */ - protected function handleChapter($chapter) + protected function handleChildren(ChapterModel $chapter) { - $attrs = json_decode($chapter['attrs'], true); + $lessons = $this->findLessons($chapter->id); - if ($attrs['model'] == CourseModel::MODEL_VOD) { - unset($attrs['file_id'], $attrs['file_status']); + if ($lessons->count() == 0) { + return []; } - return [ - 'id' => $chapter['id'], - 'title' => $chapter['title'], - 'summary' => $chapter['summary'], - 'priority' => $chapter['priority'], - 'free' => $chapter['free'], - 'attrs' => $attrs, - ]; + $list = []; + + foreach ($lessons as $lesson) { + + /** + * @var $attrs array + */ + $attrs = $lesson->attrs; + + if ($attrs['model'] == CourseModel::MODEL_VOD) { + unset($attrs['file_id'], $attrs['file_status']); + } + + $list[] = [ + 'id' => $lesson->id, + 'title' => $lesson->title, + 'summary' => $lesson->summary, + 'free' => $lesson->free, + 'attrs' => $attrs, + ]; + } + + return $list; + } + + /** + * @param int $courseId + * @return ResultsetInterface|Resultset|ChapterModel[] + */ + protected function findChapters($courseId) + { + return ChapterModel::query() + ->where('course_id = :course_id:', ['course_id' => $courseId]) + ->andWhere('parent_id = 0 AND deleted = 0') + ->orderBy('priority ASC, id ASC') + ->execute(); + } + + /** + * @param int $chapterId + * @return ResultsetInterface|Resultset|ChapterModel[] + */ + protected function findLessons($chapterId) + { + return ChapterModel::query() + ->where('parent_id = :parent_id:', ['parent_id' => $chapterId]) + ->andWhere('deleted = 0') + ->orderBy('priority ASC, id ASC') + ->execute(); } } diff --git a/app/Builders/CommentList.php b/app/Builders/CommentList.php index 84a953c7..8f3b2a4f 100644 --- a/app/Builders/CommentList.php +++ b/app/Builders/CommentList.php @@ -9,40 +9,40 @@ use App\Repos\User as UserRepo; class CommentList extends Builder { - public function handleCourses($comments) + public function handleCourses(array $comments) { $courses = $this->getCourses($comments); foreach ($comments as $key => $comment) { - $comments[$key]['course'] = $courses[$comment['course_id']] ?? []; + $comments[$key]['course'] = $courses[$comment['course_id']] ?? new \stdClass(); } return $comments; } - public function handleChapters($comments) + public function handleChapters(array $comments) { $chapters = $this->getChapters($comments); foreach ($comments as $key => $comment) { - $comments[$key]['chapter'] = $chapters[$comment['chapter_id']] ?? []; + $comments[$key]['chapter'] = $chapters[$comment['chapter_id']] ?? new \stdClass(); } return $comments; } - public function handleUsers($comments) + public function handleUsers(array $comments) { $users = $this->getUsers($comments); foreach ($comments as $key => $comment) { - $comments[$key]['user'] = $users[$comment['user_id']] ?? []; + $comments[$key]['user'] = $users[$comment['user_id']] ?? new \stdClass(); } return $comments; } - public function getCourses($comments) + public function getCourses(array $comments) { $ids = kg_array_column($comments, 'course_id'); @@ -59,7 +59,7 @@ class CommentList extends Builder return $result; } - public function getChapters($comments) + public function getChapters(array $comments) { $ids = kg_array_column($comments, 'chapter_id'); @@ -76,7 +76,7 @@ class CommentList extends Builder return $result; } - public function getUsers($comments) + public function getUsers(array $comments) { $ids = kg_array_column($comments, 'user_id'); diff --git a/app/Builders/ConsultList.php b/app/Builders/ConsultList.php index 8649c6f1..d2f932a8 100644 --- a/app/Builders/ConsultList.php +++ b/app/Builders/ConsultList.php @@ -8,29 +8,29 @@ use App\Repos\User as UserRepo; class ConsultList extends Builder { - public function handleCourses($consults) + public function handleCourses(array $consults) { $courses = $this->getCourses($consults); foreach ($consults as $key => $consult) { - $consults[$key]['course'] = $courses[$consult['course_id']] ?? []; + $consults[$key]['course'] = $courses[$consult['course_id']] ?? new \stdClass(); } return $consults; } - public function handleUsers($consults) + public function handleUsers(array $consults) { $users = $this->getUsers($consults); foreach ($consults as $key => $consult) { - $consults[$key]['user'] = $users[$consult['user_id']] ?? []; + $consults[$key]['user'] = $users[$consult['user_id']] ?? new \stdClass(); } return $consults; } - public function getCourses($consults) + public function getCourses(array $consults) { $ids = kg_array_column($consults, 'course_id'); @@ -47,7 +47,7 @@ class ConsultList extends Builder return $result; } - public function getUsers($consults) + public function getUsers(array $consults) { $ids = kg_array_column($consults, 'user_id'); diff --git a/app/Builders/Course.php b/app/Builders/Course.php deleted file mode 100644 index a85731e7..00000000 --- a/app/Builders/Course.php +++ /dev/null @@ -1,21 +0,0 @@ -cover = kg_ci_img_url($course->cover); - - return $course; - } - -} diff --git a/app/Builders/CourseChapterUser.php b/app/Builders/CourseChapterUser.php index 49401f26..618b50c0 100644 --- a/app/Builders/CourseChapterUser.php +++ b/app/Builders/CourseChapterUser.php @@ -49,7 +49,7 @@ class CourseChapterUser extends Builder $attrs = json_decode($chapter['attrs'], true); - $me = $chapter['me'] ?? []; + $me = $chapter['me'] ?? new \stdClass(); $clickable = $chapter['published']; diff --git a/app/Builders/CourseFavoriteList.php b/app/Builders/CourseFavoriteList.php index 91ecd68f..9d431f04 100644 --- a/app/Builders/CourseFavoriteList.php +++ b/app/Builders/CourseFavoriteList.php @@ -8,38 +8,39 @@ use App\Repos\User as UserRepo; class CourseFavoriteList extends Builder { - public function handleCourses($relations) + public function handleCourses(array $relations) { $courses = $this->getCourses($relations); foreach ($relations as $key => $value) { - $relations[$key]['course'] = $courses[$value['course_id']]; + $relations[$key]['course'] = $courses[$value['course_id']] ?? new \stdClass(); } return $relations; } - public function handleUsers($relations) + public function handleUsers(array $relations) { $users = $this->getUsers($relations); foreach ($relations as $key => $value) { - $relations[$key]['user'] = $users[$value['user_id']]; + $relations[$key]['user'] = $users[$value['user_id']] ?? new \stdClass(); } return $relations; } - public function getCourses($relations) + public function getCourses(array $relations) { $ids = kg_array_column($relations, 'course_id'); $courseRepo = new CourseRepo(); $columns = [ - 'id', 'title', 'cover', 'summary', - 'market_price', 'vip_price', 'model', 'level', 'attrs', - 'user_count', 'lesson_count', 'review_count', 'favorite_count', + 'id', 'title', 'cover', + 'market_price', 'vip_price', + 'rating', 'model', 'level', 'attrs', + 'user_count', 'lesson_count', ]; $courses = $courseRepo->findByIds($ids, $columns); @@ -57,7 +58,7 @@ class CourseFavoriteList extends Builder return $result; } - public function getUsers($relations) + public function getUsers(array $relations) { $ids = kg_array_column($relations, 'user_id'); diff --git a/app/Builders/CourseList.php b/app/Builders/CourseList.php index 43be2cd8..0dd1580d 100644 --- a/app/Builders/CourseList.php +++ b/app/Builders/CourseList.php @@ -8,35 +8,37 @@ use App\Repos\User as UserRepo; class CourseList extends Builder { - public function handleCategories($courses) + public function handleCategories(array $courses) { $categories = $this->getCategories(); foreach ($courses as $key => $course) { - $courses[$key]['category'] = $categories[$course['category_id']] ?? []; + $courses[$key]['category'] = $categories[$course['category_id']] ?? new \stdClass(); } return $courses; } - public function handleTeachers($courses) + public function handleTeachers(array $courses) { $teachers = $this->getTeachers($courses); foreach ($courses as $key => $course) { - $courses[$key]['teacher'] = $teachers[$course['teacher_id']] ?? []; + $courses[$key]['teacher'] = $teachers[$course['teacher_id']] ?? new \stdClass(); } return $courses; } - protected function getCategories() + public function getCategories() { $cache = new CategoryListCache(); $items = $cache->get(); - if (!$items) return null; + if (empty($items)) { + return []; + } $result = []; @@ -50,7 +52,7 @@ class CourseList extends Builder return $result; } - protected function getTeachers($courses) + public function getTeachers($courses) { $ids = kg_array_column($courses, 'teacher_id'); diff --git a/app/Builders/CourseUserList.php b/app/Builders/CourseUserList.php index bcc5e59b..66dcb9d1 100644 --- a/app/Builders/CourseUserList.php +++ b/app/Builders/CourseUserList.php @@ -13,7 +13,7 @@ class CourseUserList extends Builder $courses = $this->getCourses($relations); foreach ($relations as $key => $value) { - $relations[$key]['course'] = $courses[$value['course_id']]; + $relations[$key]['course'] = $courses[$value['course_id']] ?? new \stdClass(); } return $relations; @@ -24,7 +24,7 @@ class CourseUserList extends Builder $users = $this->getUsers($relations); foreach ($relations as $key => $value) { - $relations[$key]['user'] = $users[$value['user_id']]; + $relations[$key]['user'] = $users[$value['user_id']] ?? new \stdClass(); } return $relations; @@ -37,9 +37,10 @@ class CourseUserList extends Builder $courseRepo = new CourseRepo(); $columns = [ - 'id', 'title', 'cover', 'summary', - 'market_price', 'vip_price', 'model', 'level', 'attrs', - 'user_count', 'lesson_count', 'review_count', 'favorite_count', + 'id', 'title', 'cover', + 'market_price', 'vip_price', + 'rating', 'model', 'level', 'attrs', + 'user_count', 'lesson_count', ]; $courses = $courseRepo->findByIds($ids, $columns); diff --git a/app/Builders/LearningList.php b/app/Builders/LearningList.php index 4afedb21..03eb682f 100644 --- a/app/Builders/LearningList.php +++ b/app/Builders/LearningList.php @@ -14,7 +14,7 @@ class LearningList extends Builder $courses = $this->getCourses($relations); foreach ($relations as $key => $value) { - $relations[$key]['course'] = $courses[$value['course_id']]; + $relations[$key]['course'] = $courses[$value['course_id']] ?? new \stdClass(); } return $relations; @@ -25,7 +25,7 @@ class LearningList extends Builder $chapters = $this->getChapters($relations); foreach ($relations as $key => $value) { - $relations[$key]['chapter'] = $chapters[$value['chapter_id']]; + $relations[$key]['chapter'] = $chapters[$value['chapter_id']] ?? new \stdClass(); } return $relations; @@ -36,7 +36,7 @@ class LearningList extends Builder $users = $this->getUsers($relations); foreach ($relations as $key => $value) { - $relations[$key]['user'] = $users[$value['user_id']]; + $relations[$key]['user'] = $users[$value['user_id']] ?? new \stdClass(); } return $relations; @@ -48,7 +48,7 @@ class LearningList extends Builder $courseRepo = new CourseRepo(); - $courses = $courseRepo->findByIds($ids, ['id', 'title', 'cover']); + $courses = $courseRepo->findByIds($ids, ['id', 'title']); $result = []; @@ -82,7 +82,7 @@ class LearningList extends Builder $userRepo = new UserRepo(); - $users = $userRepo->findByIds($ids, ['id', 'name', 'avatar']); + $users = $userRepo->findByIds($ids, ['id', 'name']); $result = []; diff --git a/app/Builders/NavTreeList.php b/app/Builders/NavTreeList.php index 1d2decee..d7e1ae4f 100644 --- a/app/Builders/NavTreeList.php +++ b/app/Builders/NavTreeList.php @@ -2,45 +2,76 @@ namespace App\Builders; +use App\Models\Nav as NavModel; +use Phalcon\Mvc\Model\Resultset; +use Phalcon\Mvc\Model\ResultsetInterface; + class NavTreeList extends Builder { - public function handleTreeList($navs) + public function handle($position = 'top') { - $list = []; + $topNavs = $this->findTopNavs($position); - foreach ($navs as $nav) { - if ($nav['parent_id'] == 0) { - $key = $nav['id']; - $list[$key] = [ - 'id' => $nav['id'], - 'name' => $nav['name'], - 'priority' => $nav['priority'], - 'children' => [], - ]; - } else { - $key = $nav['parent_id']; - $list[$key]['children'][] = [ - 'id' => $nav['id'], - 'name' => $nav['name'], - 'priority' => $nav['priority'], - 'target' => $nav['target'], - 'url' => $nav['url'], - ]; - } + if ($topNavs->count() == 0) { + return []; } - usort($list, function ($a, $b) { - return $a['priority'] > $b['priority']; - }); + $list = []; - foreach ($list as $key => $value) { - usort($list[$key]['children'], function ($a, $b) { - return $a['priority'] > $b['priority']; - }); + foreach ($topNavs as $nav) { + $list[] = [ + 'id' => $nav->id, + 'name' => $nav->name, + 'children' => $this->handleChildren($nav), + ]; } return $list; } + protected function handleChildren(NavModel $nav) + { + $subNavs = $this->findSubNavs($nav->id); + + if ($subNavs->count() == 0) { + return []; + } + + $list = []; + + foreach ($subNavs as $nav) { + $list[] = [ + 'id' => $nav->id, + 'name' => $nav->name, + ]; + } + + return $list; + } + + /** + * @param int $navId + * @return ResultsetInterface|Resultset|NavModel[] + */ + protected function findSubNavs($navId) + { + return NavModel::query() + ->where('parent_id = :parent_id:', ['parent_id' => $navId]) + ->andWhere('deleted = 0') + ->execute(); + } + + /** + * @param string $position + * @return ResultsetInterface|Resultset|NavModel[] + */ + protected function findTopNavs($position) + { + return NavModel::query() + ->where('position = :position:', ['position' => $position]) + ->andWhere('deleted = 0') + ->execute(); + } + } diff --git a/app/Builders/OrderList.php b/app/Builders/OrderList.php index 5d45fad8..fb63c54d 100644 --- a/app/Builders/OrderList.php +++ b/app/Builders/OrderList.php @@ -8,7 +8,7 @@ use App\Repos\User as UserRepo; class OrderList extends Builder { - protected $baseUrl; + protected $imgBaseUrl; public function __construct() { @@ -24,7 +24,7 @@ class OrderList extends Builder $users = $this->getUsers($orders); foreach ($orders as $key => $order) { - $orders[$key]['user'] = $users[$order['user_id']]; + $orders[$key]['user'] = $users[$order['user_id']] ?? new \stdClass(); } return $orders; @@ -37,9 +37,7 @@ class OrderList extends Builder public function handleItems(array $orders) { foreach ($orders as $key => $order) { - $itemInfo = $this->handleItem($order); - $orders[$key]['item_info'] = $itemInfo; } diff --git a/app/Builders/RefundList.php b/app/Builders/RefundList.php index 17e56a9b..855d29d8 100644 --- a/app/Builders/RefundList.php +++ b/app/Builders/RefundList.php @@ -8,29 +8,29 @@ use App\Repos\User as UserRepo; class RefundList extends Builder { - public function handleOrders($trades) + public function handleOrders(array $trades) { $orders = $this->getOrders($trades); foreach ($trades as $key => $trade) { - $trades[$key]['order'] = $orders[$trade['order_id']]; + $trades[$key]['order'] = $orders[$trade['order_id']] ?? new \stdClass(); } return $trades; } - public function handleUsers($refunds) + public function handleUsers(array $refunds) { $users = $this->getUsers($refunds); foreach ($refunds as $key => $refund) { - $refunds[$key]['user'] = $users[$refund['user_id']]; + $refunds[$key]['user'] = $users[$refund['user_id']] ?? new \stdClass(); } return $refunds; } - public function getOrders($trades) + public function getOrders(array $trades) { $ids = kg_array_column($trades, 'order_id'); @@ -47,7 +47,7 @@ class RefundList extends Builder return $result; } - public function getUsers($refunds) + public function getUsers(array $refunds) { $ids = kg_array_column($refunds, 'user_id'); diff --git a/app/Builders/ReviewList.php b/app/Builders/ReviewList.php index d8d609dd..322a69ac 100644 --- a/app/Builders/ReviewList.php +++ b/app/Builders/ReviewList.php @@ -8,29 +8,29 @@ use App\Repos\User as UserRepo; class ReviewList extends Builder { - public function handleCourses($reviews) + public function handleCourses(array $reviews) { $courses = $this->getCourses($reviews); foreach ($reviews as $key => $review) { - $reviews[$key]['course'] = $courses[$review['course_id']] ?? []; + $reviews[$key]['course'] = $courses[$review['course_id']] ?? new \stdClass(); } return $reviews; } - public function handleUsers($reviews) + public function handleUsers(array $reviews) { $users = $this->getUsers($reviews); foreach ($reviews as $key => $review) { - $reviews[$key]['user'] = $users[$review['user_id']] ?? []; + $reviews[$key]['user'] = $users[$review['user_id']] ?? new \stdClass(); } return $reviews; } - public function getCourses($reviews) + public function getCourses(array $reviews) { $ids = kg_array_column($reviews, 'course_id'); @@ -47,7 +47,7 @@ class ReviewList extends Builder return $result; } - public function getUsers($reviews) + public function getUsers(array $reviews) { $ids = kg_array_column($reviews, 'user_id'); diff --git a/app/Builders/SlideList.php b/app/Builders/SlideList.php deleted file mode 100644 index b371bf7f..00000000 --- a/app/Builders/SlideList.php +++ /dev/null @@ -1,19 +0,0 @@ - $slide) { - $slides[$key]['cover'] = $baseUrl . $slide['cover']; - } - - return $slides; - } - -} diff --git a/app/Builders/TradeList.php b/app/Builders/TradeList.php index f878ffc1..8e032972 100644 --- a/app/Builders/TradeList.php +++ b/app/Builders/TradeList.php @@ -8,12 +8,12 @@ use App\Repos\User as UserRepo; class TradeList extends Builder { - public function handleOrders($trades) + public function handleOrders(array $trades) { $orders = $this->getOrders($trades); foreach ($trades as $key => $trade) { - $trades[$key]['order'] = $orders[$trade['order_id']]; + $trades[$key]['order'] = $orders[$trade['order_id']] ?? new \stdClass(); } return $trades; @@ -24,7 +24,7 @@ class TradeList extends Builder $users = $this->getUsers($trades); foreach ($trades as $key => $trade) { - $trades[$key]['user'] = $users[$trade['user_id']]; + $trades[$key]['user'] = $users[$trade['user_id']] ?? new \stdClass(); } return $trades; diff --git a/app/Builders/User.php b/app/Builders/User.php deleted file mode 100644 index 80589fae..00000000 --- a/app/Builders/User.php +++ /dev/null @@ -1,21 +0,0 @@ -avatar = kg_ci_img_url($user->avatar); - - return $user; - } - -} diff --git a/app/Builders/UserList.php b/app/Builders/UserList.php index 58515b27..b033ec1b 100644 --- a/app/Builders/UserList.php +++ b/app/Builders/UserList.php @@ -8,7 +8,7 @@ use App\Repos\Role as RoleRepo; class UserList extends Builder { - public function handleUsers($users) + public function handleUsers(array $users) { $baseUrl = kg_ci_base_url(); @@ -19,7 +19,7 @@ class UserList extends Builder return $users; } - public function handleAdminRoles($users) + public function handleAdminRoles(array $users) { $roles = $this->getAdminRoles($users); @@ -30,7 +30,7 @@ class UserList extends Builder return $users; } - public function handleEduRoles($users) + public function handleEduRoles(array $users) { $roles = $this->getEduRoles(); @@ -41,7 +41,7 @@ class UserList extends Builder return $users; } - protected function getAdminRoles($users) + protected function getAdminRoles(array $users) { $ids = kg_array_column($users, 'admin_role'); diff --git a/app/Caches/CategoryTreeList.php b/app/Caches/CategoryTreeList.php index 2854d648..32ca33bc 100644 --- a/app/Caches/CategoryTreeList.php +++ b/app/Caches/CategoryTreeList.php @@ -3,8 +3,6 @@ namespace App\Caches; use App\Builders\CategoryTreeList as CategoryTreeListBuilder; -use App\Models\Category as CategoryModel; -use Phalcon\Mvc\Model\Resultset; class CategoryTreeList extends Cache { @@ -23,31 +21,11 @@ class CategoryTreeList extends Cache public function getContent($id = null) { - /** - * @var Resultset $categories - */ - $categories = CategoryModel::query() - ->where('published = 1 AND deleted = 0') - ->execute(); - - if ($categories->count() == 0) { - return []; - } - - return $this->handleContent($categories); - } - - /** - * @param Resultset $categories - * @return array - */ - protected function handleContent($categories) - { - $items = $categories->toArray(); - $builder = new CategoryTreeListBuilder(); - return $builder->handleTreeList($items); + $list = $builder->handle(); + + return $list ?: []; } } diff --git a/app/Caches/CourseChapterList.php b/app/Caches/CourseChapterList.php index 74f1082d..62b12f4c 100644 --- a/app/Caches/CourseChapterList.php +++ b/app/Caches/CourseChapterList.php @@ -3,13 +3,11 @@ namespace App\Caches; use App\Builders\ChapterTreeList as ChapterTreeListBuilder; -use App\Repos\Course as CourseRepo; -use Phalcon\Mvc\Model\Resultset; class CourseChapterList extends Cache { - protected $lifetime = 1 * 86400; + protected $lifetime = 7 * 86400; public function getLifetime() { @@ -23,28 +21,11 @@ class CourseChapterList extends Cache public function getContent($id = null) { - $courseRepo = new CourseRepo(); - - $chapters = $courseRepo->findChapters($id); - - if ($chapters->count() == 0) { - return []; - } - - return $this->handleContent($chapters); - } - - /** - * @param Resultset $chapters - * @return array - */ - protected function handleContent($chapters) - { - $items = $chapters->toArray(); - $builder = new ChapterTreeListBuilder(); - return $builder->handleTreeList($items); + $list = $builder->handle($id); + + return $list ?: []; } } diff --git a/app/Caches/CoursePackageList.php b/app/Caches/CoursePackageList.php index f99a6e9a..3462825d 100644 --- a/app/Caches/CoursePackageList.php +++ b/app/Caches/CoursePackageList.php @@ -45,7 +45,6 @@ class CoursePackageList extends Cache $result[] = [ 'id' => $package->id, 'title' => $package->title, - 'summary' => $package->summary, 'market_price' => $package->market_price, 'vip_price' => $package->vip_price, ]; diff --git a/app/Caches/CourseRecommendedList.php b/app/Caches/CourseRecommendedList.php new file mode 100644 index 00000000..ce3b03a3 --- /dev/null +++ b/app/Caches/CourseRecommendedList.php @@ -0,0 +1,75 @@ +lifetime; + } + + public function getKey($id = null) + { + return "course_recommended_list:{$id}"; + } + + public function getContent($id = null) + { + $courses = $this->findCourses(5); + + 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, + 'cover' => $course->cover, + 'market_price' => $course->market_price, + 'vip_price' => $course->vip_price, + 'rating' => $course->rating, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, + ]; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|CourseModel[] + */ + public function findCourses($limit = 5) + { + return CourseModel::query() + ->where('published = 1 AND deleted = 0') + ->orderBy('RAND()') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/CourseTeacherList.php b/app/Caches/CourseTeacherList.php index 051160cf..f13d08c2 100644 --- a/app/Caches/CourseTeacherList.php +++ b/app/Caches/CourseTeacherList.php @@ -47,7 +47,6 @@ class CourseTeacherList extends Cache 'name' => $user->name, 'avatar' => $user->avatar, 'title' => $user->title, - 'about' => $user->about, ]; } diff --git a/app/Caches/CourseTopicList.php b/app/Caches/CourseTopicList.php new file mode 100644 index 00000000..18d2f914 --- /dev/null +++ b/app/Caches/CourseTopicList.php @@ -0,0 +1,67 @@ +lifetime; + } + + public function getKey($id = null) + { + return "course_topic_list:{$id}"; + } + + public function getContent($id = null) + { + $topics = $this->findTopics(5); + + if ($topics->count() == 0) { + return []; + } + + return $this->handleContent($topics); + } + + /** + * @param TopicModel[] $topics + * @return array + */ + public function handleContent($topics) + { + $result = []; + + foreach ($topics as $topic) { + + $result[] = [ + 'id' => $topic->id, + 'title' => $topic->title, + ]; + } + + return $result; + } + + /** + * @param int $limit + * @return ResultsetInterface|Resultset|TopicModel[] + */ + public function findTopics($limit = 5) + { + return TopicModel::query() + ->where('published = 1 AND deleted = 0') + ->orderBy('RAND()') + ->limit($limit) + ->execute(); + } + +} diff --git a/app/Caches/IndexFreeCourseList.php b/app/Caches/IndexFreeCourseList.php index 6ae162a6..89698328 100644 --- a/app/Caches/IndexFreeCourseList.php +++ b/app/Caches/IndexFreeCourseList.php @@ -4,7 +4,6 @@ namespace App\Caches; use App\Models\Category as CategoryModel; use App\Models\Course as CourseModel; -use App\Repos\User as UserRepo; use App\Services\Category as CategoryService; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -41,7 +40,7 @@ class IndexFreeCourseList extends Cache $categories = $this->findCategories($categoryLimit); if ($categories->count() == 0) { - return null; + return []; } foreach ($categories as $category) { @@ -57,21 +56,16 @@ class IndexFreeCourseList extends Cache continue; } - $teacherMappings = $this->getTeacherMappings($courses); - $categoryCourses = []; foreach ($courses as $course) { - - $teacher = $teacherMappings[$course->teacher_id]; - $categoryCourses[] = [ 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'teacher' => $teacher, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, + 'rating' => $course->rating, 'model' => $course->model, 'level' => $course->level, 'user_count' => $course->user_count, @@ -87,31 +81,6 @@ class IndexFreeCourseList extends Cache return $result; } - /** - * @param Resultset|CourseModel[] $courses - * @return array - */ - protected function getTeacherMappings($courses) - { - $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); - - $userRepo = new UserRepo(); - - $teachers = $userRepo->findByIds($teacherIds); - - $mappings = []; - - foreach ($teachers as $teacher) { - $mappings[$teacher->id] = [ - 'id' => $teacher->id, - 'name' => $teacher->name, - 'avatar' => $teacher->avatar, - ]; - } - - return $mappings; - } - /** * @param int $limit * @return ResultsetInterface|Resultset|CategoryModel[] diff --git a/app/Caches/IndexLiveList.php b/app/Caches/IndexLiveList.php index 26bc5c30..906d3960 100644 --- a/app/Caches/IndexLiveList.php +++ b/app/Caches/IndexLiveList.php @@ -5,7 +5,6 @@ namespace App\Caches; use App\Models\ChapterLive as ChapterLiveModel; use App\Repos\Chapter as ChapterRepo; use App\Repos\Course as CourseRepo; -use App\Repos\User as UserRepo; use Phalcon\Mvc\Model\Resultset; /** @@ -44,8 +43,6 @@ class IndexLiveList extends Cache $beginTime = strtotime('today'); $endTime = strtotime("+30 days"); - $result = []; - /** * @var Resultset|ChapterLiveModel[] $lives */ @@ -55,9 +52,11 @@ class IndexLiveList extends Cache ->execute(); if ($lives->count() == 0) { - return $result; + return []; } + $result = []; + $chapterIds = kg_array_column($lives->toArray(), 'chapter_id'); $chapterRepo = new ChapterRepo(); @@ -82,18 +81,6 @@ class IndexLiveList extends Cache $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) { @@ -108,7 +95,6 @@ class IndexLiveList extends Cache $chapter = $chapterMappings[$live->chapter_id]; $course = $courseMappings[$chapter->course_id]; - $teacher = $teacherMappings[$course->teacher_id]; $chapterInfo = [ 'id' => $chapter->id, @@ -121,13 +107,9 @@ class IndexLiveList extends Cache 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'teacher' => [ - 'id' => $teacher->id, - 'name' => $teacher->name, - 'avatar' => $teacher->avatar, - ], 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, + 'rating' => $course->rating, 'model' => $course->model, 'level' => $course->level, 'user_count' => $course->user_count, diff --git a/app/Caches/IndexNewCourseList.php b/app/Caches/IndexNewCourseList.php index 1fb34b3c..4a155ac0 100644 --- a/app/Caches/IndexNewCourseList.php +++ b/app/Caches/IndexNewCourseList.php @@ -4,7 +4,6 @@ namespace App\Caches; use App\Models\Category as CategoryModel; use App\Models\Course as CourseModel; -use App\Repos\User as UserRepo; use App\Services\Category as CategoryService; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -41,7 +40,7 @@ class IndexNewCourseList extends Cache $categories = $this->findCategories($categoryLimit); if ($categories->count() == 0) { - return null; + return []; } foreach ($categories as $category) { @@ -57,21 +56,16 @@ class IndexNewCourseList extends Cache continue; } - $teacherMappings = $this->getTeacherMappings($courses); - $categoryCourses = []; foreach ($courses as $course) { - - $teacher = $teacherMappings[$course->teacher_id]; - $categoryCourses[] = [ 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'teacher' => $teacher, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, + 'rating' => $course->rating, 'model' => $course->model, 'level' => $course->level, 'user_count' => $course->user_count, @@ -87,31 +81,6 @@ class IndexNewCourseList extends Cache return $result; } - /** - * @param Resultset|CourseModel[] $courses - * @return array - */ - protected function getTeacherMappings($courses) - { - $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); - - $userRepo = new UserRepo(); - - $teachers = $userRepo->findByIds($teacherIds); - - $mappings = []; - - foreach ($teachers as $teacher) { - $mappings[$teacher->id] = [ - 'id' => $teacher->id, - 'name' => $teacher->name, - 'avatar' => $teacher->avatar, - ]; - } - - return $mappings; - } - /** * @param int $limit * @return ResultsetInterface|Resultset|CategoryModel[] diff --git a/app/Caches/IndexVipCourseList.php b/app/Caches/IndexVipCourseList.php index d1e19cfa..b5a90d65 100644 --- a/app/Caches/IndexVipCourseList.php +++ b/app/Caches/IndexVipCourseList.php @@ -4,7 +4,6 @@ namespace App\Caches; use App\Models\Category as CategoryModel; use App\Models\Course as CourseModel; -use App\Repos\User as UserRepo; use App\Services\Category as CategoryService; use Phalcon\Mvc\Model\Resultset; use Phalcon\Mvc\Model\ResultsetInterface; @@ -41,7 +40,7 @@ class IndexVipCourseList extends Cache $categories = $this->findCategories($categoryLimit); if ($categories->count() == 0) { - return null; + return []; } foreach ($categories as $category) { @@ -57,21 +56,16 @@ class IndexVipCourseList extends Cache continue; } - $teacherMappings = $this->getTeacherMappings($courses); - $categoryCourses = []; foreach ($courses as $course) { - - $teacher = $teacherMappings[$course->teacher_id]; - $categoryCourses[] = [ 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'teacher' => $teacher, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, + 'rating' => $course->rating, 'model' => $course->model, 'level' => $course->level, 'user_count' => $course->user_count, @@ -87,31 +81,6 @@ class IndexVipCourseList extends Cache return $result; } - /** - * @param Resultset|CourseModel[] $courses - * @return array - */ - protected function getTeacherMappings($courses) - { - $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); - - $userRepo = new UserRepo(); - - $teachers = $userRepo->findByIds($teacherIds); - - $mappings = []; - - foreach ($teachers as $teacher) { - $mappings[$teacher->id] = [ - 'id' => $teacher->id, - 'name' => $teacher->name, - 'avatar' => $teacher->avatar, - ]; - } - - return $mappings; - } - /** * @param int $limit * @return ResultsetInterface|Resultset|CategoryModel[] diff --git a/app/Caches/NavTreeList.php b/app/Caches/NavTreeList.php index abda0cbd..0a244677 100644 --- a/app/Caches/NavTreeList.php +++ b/app/Caches/NavTreeList.php @@ -3,8 +3,6 @@ namespace App\Caches; use App\Builders\NavTreeList as NavTreeListBuilder; -use App\Models\Nav as NavModel; -use Phalcon\Mvc\Model\Resultset; class NavTreeList extends Cache { @@ -23,45 +21,11 @@ class NavTreeList extends Cache public function getContent($id = null) { - /** - * @var Resultset $navs - */ - $navs = NavModel::query() - ->where('published = 1 AND deleted = 0') - ->orderBy('position ASC, priority ASC') - ->execute(); - - if ($navs->count() == 0) { - return []; - } - - return $this->handleContent($navs); - } - - /** - * @param Resultset $navs - * @return array - */ - protected function handleContent($navs) - { - $list = [ - 'top' => [], - 'bottom' => [], - ]; - - foreach ($navs->toArray() as $nav) { - if ($nav['position'] == 'top') { - $list['top'][] = $nav; - } elseif ($nav['position'] == 'bottom') { - $list['bottom'][] = $nav; - } - } - $builder = new NavTreeListBuilder(); return [ - 'top' => $builder->handleTreeList($list['top']), - 'bottom' => $builder->handleTreeList($list['bottom']), + 'top' => $builder->handle('top'), + 'bottom' => $builder->handle('bottom'), ]; } diff --git a/app/Caches/PackageCourseList.php b/app/Caches/PackageCourseList.php index 132843ad..c213c507 100644 --- a/app/Caches/PackageCourseList.php +++ b/app/Caches/PackageCourseList.php @@ -4,8 +4,6 @@ namespace App\Caches; use App\Models\Course as CourseModel; use App\Repos\Package as PackageRepo; -use App\Repos\User as UserRepo; -use Phalcon\Mvc\Model\Resultset; class PackageCourseList extends Cache { @@ -43,17 +41,11 @@ class PackageCourseList extends Cache { $result = []; - $teacherMappings = $this->getTeacherMappings($courses); - foreach ($courses as $course) { - - $teacher = $teacherMappings[$course->teacher_id]; - $result[] = [ 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'teacher' => $teacher, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, 'rating' => $course->rating, @@ -67,29 +59,4 @@ class PackageCourseList extends Cache return $result; } - /** - * @param Resultset|CourseModel[] $courses - * @return array - */ - protected function getTeacherMappings($courses) - { - $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); - - $userRepo = new UserRepo(); - - $teachers = $userRepo->findByIds($teacherIds); - - $mappings = []; - - foreach ($teachers as $teacher) { - $mappings[$teacher->id] = [ - 'id' => $teacher->id, - 'name' => $teacher->name, - 'avatar' => $teacher->avatar, - ]; - } - - return $mappings; - } - } diff --git a/app/Caches/TopicCourseList.php b/app/Caches/TopicCourseList.php index d8a46f6a..2ff4868e 100644 --- a/app/Caches/TopicCourseList.php +++ b/app/Caches/TopicCourseList.php @@ -4,8 +4,6 @@ namespace App\Caches; use App\Models\Course as CourseModel; use App\Repos\Topic as TopicRepo; -use App\Repos\User as UserRepo; -use Phalcon\Mvc\Model\Resultset; class TopicCourseList extends Cache { @@ -43,17 +41,11 @@ class TopicCourseList extends Cache { $result = []; - $teacherMappings = $this->getTeacherMappings($courses); - foreach ($courses as $course) { - - $teacher = $teacherMappings[$course->teacher_id]; - $result[] = [ 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'teacher' => $teacher, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, 'rating' => $course->rating, @@ -67,29 +59,4 @@ class TopicCourseList extends Cache return $result; } - /** - * @param Resultset|CourseModel[] $courses - * @return array - */ - protected function getTeacherMappings($courses) - { - $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); - - $userRepo = new UserRepo(); - - $teachers = $userRepo->findByIds($teacherIds); - - $mappings = []; - - foreach ($teachers as $teacher) { - $mappings[$teacher->id] = [ - 'id' => $teacher->id, - 'name' => $teacher->name, - 'avatar' => $teacher->avatar, - ]; - } - - return $mappings; - } - } diff --git a/app/Console/Tasks/OrderTask.php b/app/Console/Tasks/OrderTask.php index 15943105..b0285153 100644 --- a/app/Console/Tasks/OrderTask.php +++ b/app/Console/Tasks/OrderTask.php @@ -105,7 +105,7 @@ class OrderTask extends Task $courseUser = new CourseUserModel(); if ($courseUser->create($data) === false) { - throw new \RuntimeException('Create Course User Failed'); + throw new \RuntimeException('Create CourseQuery User Failed'); } $this->handleCourseHistory($data['course_id'], $data['user_id']); @@ -134,7 +134,7 @@ class OrderTask extends Task $courseUser = new CourseUserModel(); if ($courseUser->create($data) === false) { - throw new \RuntimeException('Create Course User Failed'); + throw new \RuntimeException('Create CourseQuery User Failed'); } $this->handleCourseHistory($data['course_id'], $data['user_id']); diff --git a/app/Console/Tasks/RefundTask.php b/app/Console/Tasks/RefundTask.php index 8880cf6c..8c1d6eca 100644 --- a/app/Console/Tasks/RefundTask.php +++ b/app/Console/Tasks/RefundTask.php @@ -183,7 +183,7 @@ class RefundTask extends Task $courseUser->deleted = 1; if ($courseUser->update() === false) { - throw new \RuntimeException('Delete Course User Failed'); + throw new \RuntimeException('Delete CourseQuery User Failed'); } } } @@ -211,7 +211,7 @@ class RefundTask extends Task $courseUser->deleted = 1; if ($courseUser->update() === false) { - throw new \RuntimeException('Delete Course User Failed'); + throw new \RuntimeException('Delete CourseQuery User Failed'); } } } diff --git a/app/Http/Web/Controllers/AccountController.php b/app/Http/Web/Controllers/AccountController.php index 50776a8a..d5d55018 100644 --- a/app/Http/Web/Controllers/AccountController.php +++ b/app/Http/Web/Controllers/AccountController.php @@ -54,12 +54,12 @@ class AccountController extends Controller $service->register(); - $location = $this->request->getHTTPReferer(); - - return $this->jsonSuccess([ - 'location' => $location, + $content = [ + 'location' => $this->request->getHTTPReferer(), 'msg' => '注册账户成功', - ]); + ]; + + return $this->jsonSuccess($content); } } diff --git a/app/Http/Web/Controllers/CourseController.php b/app/Http/Web/Controllers/CourseController.php index 80f88380..a5015f9c 100644 --- a/app/Http/Web/Controllers/CourseController.php +++ b/app/Http/Web/Controllers/CourseController.php @@ -2,13 +2,18 @@ namespace App\Http\Web\Controllers; -use App\Http\Web\Services\Course as CourseService; +use App\Http\Web\Services\CourseQuery as CourseQueryService; +use App\Services\Frontend\Course\ChapterList as CourseChapterListService; use App\Services\Frontend\Course\ConsultList as CourseConsultListService; -use App\Services\Frontend\Course\CourseFavorite as CourseFavoriteService; use App\Services\Frontend\Course\CourseInfo as CourseInfoService; use App\Services\Frontend\Course\CourseList as CourseListService; -use App\Services\Frontend\Course\CourseRelated as CourseRelatedService; +use App\Services\Frontend\Course\Favorite as CourseFavoriteService; +use App\Services\Frontend\Course\PackageList as CoursePackageListService; +use App\Services\Frontend\Course\RecommendedList as CourseRecommendedListService; +use App\Services\Frontend\Course\RelatedList as CourseRelatedListService; use App\Services\Frontend\Course\ReviewList as CourseReviewListService; +use App\Services\Frontend\Course\TeacherList as CourseTeacherListService; +use App\Services\Frontend\Course\TopicList as CourseTopicListService; /** * @RoutePrefix("/course") @@ -25,17 +30,19 @@ class CourseController extends Controller $pager = $courseListService->handle(); - $courseService = new CourseService(); + $courseQueryService = new CourseQueryService(); - $topCategories = $courseService->handleTopCategories(); - $subCategories = $courseService->handleSubCategories(); - $levels = $courseService->handleLevels(); - - dd($topCategories, $subCategories, $levels); + $topCategories = $courseQueryService->handleTopCategories(); + $subCategories = $courseQueryService->handleSubCategories(); + $models = $courseQueryService->handleModels(); + $levels = $courseQueryService->handleLevels(); + $sorts = $courseQueryService->handleSorts(); $this->view->setVar('top_categories', $topCategories); $this->view->setVar('sub_categories', $subCategories); + $this->view->setVar('models', $models); $this->view->setVar('levels', $levels); + $this->view->setVar('sorts', $sorts); $this->view->setVar('pager', $pager); } @@ -46,14 +53,81 @@ class CourseController extends Controller { $courseInfoService = new CourseInfoService(); - $courseInfo = $courseInfoService->handle($id); + $course = $courseInfoService->handle($id); - $courseRelatedService = new CourseRelatedService(); + $this->view->setVar('course', $course); + } - $relatedCourses = $courseRelatedService->handle($id); + /** + * @Get("/{id:[0-9]+}/teachers", name="web.course.teachers") + */ + public function teachersAction($id) + { + $service = new CourseTeacherListService(); - $this->view->setVar('course_info', $courseInfo); - $this->view->setVar('related_courses', $relatedCourses); + $teachers = $service->handle($id); + + return $this->jsonSuccess(['teachers' => $teachers]); + } + + /** + * @Get("/{id:[0-9]+}/chapters", name="web.course.chapters") + */ + public function chaptersAction($id) + { + $service = new CourseChapterListService(); + + $chapters = $service->handle($id); + + return $this->jsonSuccess(['chapters' => $chapters]); + } + + /** + * @Get("/{id:[0-9]+}/packages", name="web.course.packages") + */ + public function packagesAction($id) + { + $service = new CoursePackageListService(); + + $packages = $service->handle($id); + + return $this->jsonSuccess(['packages' => $packages]); + } + + /** + * @Get("/{id:[0-9]+}/recommended", name="web.course.recommended") + */ + public function recommendedAction($id) + { + $service = new CourseRecommendedListService(); + + $courses = $service->handle($id); + + return $this->jsonSuccess(['courses' => $courses]); + } + + /** + * @Get("/{id:[0-9]+}/related", name="web.course.related") + */ + public function relatedAction($id) + { + $service = new CourseRelatedListService(); + + $courses = $service->handle($id); + + return $this->jsonSuccess(['courses' => $courses]); + } + + /** + * @Get("/{id:[0-9]+}/topics", name="web.course.topics") + */ + public function topicsAction($id) + { + $service = new CourseTopicListService(); + + $topics = $service->handle($id); + + return $this->jsonSuccess(['topics' => $topics]); } /** diff --git a/app/Http/Web/Controllers/IndexController.php b/app/Http/Web/Controllers/IndexController.php index cdf6a988..e3ebb7b3 100644 --- a/app/Http/Web/Controllers/IndexController.php +++ b/app/Http/Web/Controllers/IndexController.php @@ -17,11 +17,11 @@ class IndexController extends Controller $indexService = new IndexService(); - $this->view->setVar('slide_list', $indexService->getSlideList()); - $this->view->setVar('live_list', $indexService->getLiveList()); - $this->view->setVar('new_course_list', $indexService->getNewCourseList()); - $this->view->setVar('free_course_list', $indexService->getFreeCourseList()); - $this->view->setVar('vip_course_list', $indexService->getVipCourseList()); + $this->view->setVar('slides', $indexService->getSlides()); + $this->view->setVar('lives', $indexService->getLives()); + $this->view->setVar('new_courses', $indexService->getNewCourses()); + $this->view->setVar('free_courses', $indexService->getFreeCourses()); + $this->view->setVar('vip_courses', $indexService->getVipCourses()); } } diff --git a/app/Http/Web/Services/Course.php b/app/Http/Web/Services/CourseQuery.php similarity index 61% rename from app/Http/Web/Services/Course.php rename to app/Http/Web/Services/CourseQuery.php index f6b43d8e..03ea5778 100644 --- a/app/Http/Web/Services/Course.php +++ b/app/Http/Web/Services/CourseQuery.php @@ -5,9 +5,16 @@ namespace App\Http\Web\Services; use App\Models\Course as CourseModel; use App\Services\Category as CategoryService; -class Course extends Service +class CourseQuery extends Service { + protected $baseUrl; + + public function __construct() + { + $this->baseUrl = $this->url->get(['for' => 'web.course.list']); + } + public function handleTopCategories() { $params = $this->getQueryParams(); @@ -92,22 +99,50 @@ class Course extends Service return $result; } - public function handleLevels() + public function handleModels() { $params = $this->getQueryParams(); - $defaultParams = $params; - - if (isset($defaultParams['level'])) { - unset($defaultParams['level']); + if (isset($params['model'])) { + unset($params['model']); } - $baseUrl = $this->url->get(['for' => 'web.course.list']); - $defaultItem = [ 'id' => 0, 'name' => '全部', - 'href' => $baseUrl . $this->buildQueryParams($defaultParams), + 'href' => $this->baseUrl . $this->buildQueryParams($params), + ]; + + $result = []; + + $result[] = $defaultItem; + + $models = CourseModel::modelTypes(); + + foreach ($models as $key => $value) { + $params['model'] = $key; + $result[] = [ + 'id' => $key, + 'name' => $value, + 'href' => $this->baseUrl . $this->buildQueryParams($params), + ]; + } + + return $result; + } + + public function handleLevels() + { + $params = $this->getQueryParams(); + + if (isset($params['level'])) { + unset($params['level']); + } + + $defaultItem = [ + 'id' => 0, + 'name' => '全部', + 'href' => $this->baseUrl . $this->buildQueryParams($params), ]; $result = []; @@ -121,7 +156,7 @@ class Course extends Service $result[] = [ 'id' => $key, 'name' => $value, - 'href' => $baseUrl . $this->buildQueryParams($params), + 'href' => $this->baseUrl . $this->buildQueryParams($params), ]; } @@ -130,6 +165,22 @@ class Course extends Service public function handleSorts() { + $params = $this->getQueryParams(); + + $result = []; + + $sorts = CourseModel::sortTypes(); + + foreach ($sorts as $key => $value) { + $params['sort'] = $key; + $result[] = [ + 'id' => $key, + 'name' => $value, + 'href' => $this->baseUrl . $this->buildQueryParams($params), + ]; + } + + return $result; } protected function getQueryParams() @@ -138,18 +189,33 @@ class Course extends Service $params = []; + $validator = new \App\Validators\CourseQuery(); + if (!empty($query['tc'])) { + $validator->checkTopCategory($query['tc']); $params['tc'] = $query['tc']; } if (!empty($query['sc'])) { + $validator->checkSubCategory($query['sc']); $params['sc'] = $query['sc']; } + if (!empty($query['model'])) { + $validator->checkModel($query['model']); + $params['model'] = $query['model']; + } + if (!empty($query['level'])) { + $validator->checkLevel($query['level']); $params['level'] = $query['level']; } + if (!empty($query['sort'])) { + $validator->checkSort($query['sort']); + $params['sort'] = $query['sort']; + } + return $params; } diff --git a/app/Http/Web/Services/Index.php b/app/Http/Web/Services/Index.php index 2c72da6a..94fb2bf0 100644 --- a/app/Http/Web/Services/Index.php +++ b/app/Http/Web/Services/Index.php @@ -2,46 +2,46 @@ namespace App\Http\Web\Services; -use App\Caches\IndexFreeCourseList; -use App\Caches\IndexLiveList; -use App\Caches\IndexNewCourseList; -use App\Caches\IndexSlideList; -use App\Caches\IndexVipCourseList; +use App\Caches\IndexFreeCourseList as IndexFreeCourseListCache; +use App\Caches\IndexLiveList as IndexLiveListCache; +use App\Caches\IndexNewCourseList as IndexNewCourseListCache; +use App\Caches\IndexSlideList as IndexSlideListCache; +use App\Caches\IndexVipCourseList as IndexVipCourseListCache; class Index extends Service { - public function getSlideList() + public function getSlides() { - $cache = new IndexSlideList(); + $cache = new IndexSlideListCache(); return $cache->get(); } - public function getLiveList() + public function getLives() { - $cache = new IndexLiveList(); + $cache = new IndexLiveListCache(); return $cache->get(); } - public function getNewCourseList() + public function getNewCourses() { - $cache = new IndexNewCourseList(); + $cache = new IndexNewCourseListCache(); return $cache->get(); } - public function getFreeCourseList() + public function getFreeCourses() { - $cache = new IndexFreeCourseList(); + $cache = new IndexFreeCourseListCache(); return $cache->get(); } - public function getVipCourseList() + public function getVipCourses() { - $cache = new IndexVipCourseList(); + $cache = new IndexVipCourseListCache(); return $cache->get(); } diff --git a/app/Models/Course.php b/app/Models/Course.php index e1499ff6..e3d8f9c5 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -342,6 +342,15 @@ class Course extends Model ]; } + public static function sortTypes() + { + return [ + 'score' => '综合', + 'rating' => '好评', + 'pop' => '人气', + ]; + } + public static function studyExpiryOptions() { return [ diff --git a/app/Repos/Course.php b/app/Repos/Course.php index 6d427851..c5c05c33 100644 --- a/app/Repos/Course.php +++ b/app/Repos/Course.php @@ -83,11 +83,14 @@ class Course extends Repository } switch ($sort) { + case 'score': + $orderBy = 'c.score DESC'; + break; case 'rating': $orderBy = 'c.rating DESC'; break; - case 'score': - $orderBy = 'c.score DESC'; + case 'pop': + $orderBy = 'c.user_count DESC'; break; default: $orderBy = 'c.id DESC'; diff --git a/app/Services/Frontend/Course/ChapterList.php b/app/Services/Frontend/Course/ChapterList.php index 5eae58de..9e6e1874 100644 --- a/app/Services/Frontend/Course/ChapterList.php +++ b/app/Services/Frontend/Course/ChapterList.php @@ -2,82 +2,65 @@ namespace App\Services\Frontend\Course; -use App\Builders\ChapterTreeList as ChapterListBuilder; +use App\Caches\CourseChapterList as CourseChapterListCache; use App\Models\Course as CourseModel; use App\Models\User as UserModel; use App\Repos\Course as CourseRepo; use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; -use Phalcon\Mvc\Model\Resultset; class ChapterList extends Service { use CourseTrait; - /** - * @var CourseModel - */ - protected $course; - - /** - * @var UserModel - */ - protected $user; - - public function getChapters($id) + public function handle($id) { - $this->course = $this->checkCourse($id); + $course = $this->checkCourse($id); - $this->user = $this->getCurrentUser(); + $user = $this->getCurrentUser(); - $this->setCourseUser($this->course, $this->user); + $this->setCourseUser($course, $user); - $courseRepo = new CourseRepo(); - - $chapters = $courseRepo->findChapters($id); - - return $this->handleChapters($chapters); + return $this->getChapters($course, $user); } - /** - * @param Resultset $chapters - * @return array - */ - protected function handleChapters($chapters) + protected function getChapters(CourseModel $course, UserModel $user) { - if ($chapters->count() == 0) { + $cache = new CourseChapterListCache(); + + $chapters = $cache->get($course->id); + + if (empty($chapters)) { return []; } - $builder = new ChapterListBuilder(); - - $items = $chapters->toArray(); - - $treeList = $builder->handleTreeList($items); - - $learningMapping = $this->getLearningMapping($this->course, $this->user); - - foreach ($treeList 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, + ]; + } } } - return $treeList; + return $chapters; } protected function getLearningMapping(CourseModel $course, UserModel $user) { - if ($user->id == 0) { - return []; - } - $courseRepo = new CourseRepo(); $userLearnings = $courseRepo->findUserLearnings($course->id, $user->id); diff --git a/app/Services/Frontend/Course/ConsultList.php b/app/Services/Frontend/Course/ConsultList.php index 618dd12a..ec304b34 100644 --- a/app/Services/Frontend/Course/ConsultList.php +++ b/app/Services/Frontend/Course/ConsultList.php @@ -11,7 +11,6 @@ use App\Repos\Consult as ConsultRepo; use App\Repos\Course as CourseRepo; use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; -use Phalcon\Mvc\Model\Resultset; class ConsultList extends Service { @@ -28,9 +27,9 @@ class ConsultList extends Service use CourseTrait; - public function handle($courseId) + public function handle($id) { - $this->course = $this->checkCourse($courseId); + $this->course = $this->checkCourse($id); $this->user = $this->getCurrentUser(); @@ -65,7 +64,7 @@ class ConsultList extends Service $users = $builder->getUsers($consults); - $votes = $this->getConsultVotes($this->course->id, $this->user->id); + $votes = $this->getConsultVotes($this->course, $this->user); $items = []; @@ -95,18 +94,15 @@ class ConsultList extends Service return $pager; } - protected function getConsultVotes($courseId, $userId) + protected function getConsultVotes(CourseModel $course, UserModel $user) { - if (!$courseId || !$userId) { + if ($course->id == 0 || !$user->id == 0) { return []; } $courseRepo = new CourseRepo(); - /** - * @var Resultset $votes - */ - $votes = $courseRepo->findUserConsultVotes($courseId, $userId); + $votes = $courseRepo->findUserConsultVotes($course->id, $user->id); if ($votes->count() == 0) { return []; diff --git a/app/Services/Frontend/Course/CourseInfo.php b/app/Services/Frontend/Course/CourseInfo.php index 46b0a776..a3f441e8 100644 --- a/app/Services/Frontend/Course/CourseInfo.php +++ b/app/Services/Frontend/Course/CourseInfo.php @@ -2,11 +2,8 @@ namespace App\Services\Frontend\Course; -use App\Caches\CourseChapterList as CourseChapterListCache; -use App\Caches\CourseTeacherList as CourseTeacherListCache; use App\Models\Course as CourseModel; use App\Models\User as UserModel; -use App\Repos\Course as CourseRepo; use App\Repos\CourseFavorite as CourseFavoriteRepo; use App\Services\Category as CategoryService; use App\Services\Frontend\CourseTrait; @@ -80,77 +77,17 @@ class CourseInfo extends Service $me['owned'] = $this->ownedCourse ? 1 : 0; } - $result['category_paths'] = $this->getCategoryPaths($course); - $result['teachers'] = $this->getTeachers($course); - $result['chapters'] = $this->getChapters($course, $user); + $result['paths'] = $this->getPaths($course); $result['me'] = $me; return $result; } - protected function getCategoryPaths(CourseModel $course) + protected function getPaths(CourseModel $course) { $service = new CategoryService(); return $service->getCategoryPaths($course->category_id); } - protected function getTeachers(CourseModel $course) - { - $cache = new CourseTeacherListCache(); - - return $cache->get($course->id); - } - - protected function getChapters(CourseModel $course, UserModel $user) - { - $cache = new CourseChapterListCache(); - - $chapters = $cache->get($course->id); - - 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, - ]; - } - } - } - - return $chapters; - } - - protected function getLearningMapping(CourseModel $course, UserModel $user) - { - $courseRepo = new CourseRepo(); - - $userLearnings = $courseRepo->findUserLearnings($course->id, $user->id); - - if ($userLearnings->count() == 0) { - return []; - } - - $mapping = []; - - foreach ($userLearnings as $learning) { - $mapping[$learning['chapter_id']] = [ - 'progress' => $learning['progress'], - ]; - } - - return $mapping; - } - } diff --git a/app/Services/Frontend/Course/CourseList.php b/app/Services/Frontend/Course/CourseList.php index 827a0f73..68428f2b 100644 --- a/app/Services/Frontend/Course/CourseList.php +++ b/app/Services/Frontend/Course/CourseList.php @@ -67,7 +67,6 @@ class CourseList extends Service 'id' => $course['id'], 'title' => $course['title'], 'cover' => $course['cover'], - 'summary' => $course['summary'], 'market_price' => (float)$course['market_price'], 'vip_price' => (float)$course['vip_price'], 'rating' => (float)$course['rating'], diff --git a/app/Services/Frontend/Course/CourseFavorite.php b/app/Services/Frontend/Course/Favorite.php similarity index 92% rename from app/Services/Frontend/Course/CourseFavorite.php rename to app/Services/Frontend/Course/Favorite.php index 38080552..b17d16ca 100644 --- a/app/Services/Frontend/Course/CourseFavorite.php +++ b/app/Services/Frontend/Course/Favorite.php @@ -3,14 +3,14 @@ namespace App\Services\Frontend\Course; use App\Models\Course as CourseModel; -use App\Models\CourseFavorite as FavoriteModel; +use App\Models\CourseFavorite as CourseFavoriteModel; use App\Models\User as UserModel; use App\Repos\CourseFavorite as CourseFavoriteRepo; use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; use App\Validators\UserDailyLimit as UserDailyLimitValidator; -class CourseFavorite extends Service +class Favorite extends Service { use CourseTrait; @@ -31,7 +31,7 @@ class CourseFavorite extends Service if (!$favorite) { - $favorite = new FavoriteModel(); + $favorite = new CourseFavoriteModel(); $favorite->course_id = $course->id; $favorite->user_id = $user->id; diff --git a/app/Services/Frontend/Course/PackageList.php b/app/Services/Frontend/Course/PackageList.php index 327a905d..3f6127f8 100644 --- a/app/Services/Frontend/Course/PackageList.php +++ b/app/Services/Frontend/Course/PackageList.php @@ -20,7 +20,7 @@ class PackageList extends Service $packages = $cache->get($course->id); - if (!$packages) { + if (empty($packages)) { return []; } @@ -28,7 +28,7 @@ class PackageList extends Service $result = []; - foreach ($packages->toArray() as $package) { + foreach ($packages as $package) { $courses = $cache->get($package['id']); diff --git a/app/Services/Frontend/Course/RecommendedList.php b/app/Services/Frontend/Course/RecommendedList.php new file mode 100644 index 00000000..6a9227e6 --- /dev/null +++ b/app/Services/Frontend/Course/RecommendedList.php @@ -0,0 +1,25 @@ +checkCourse($id); + + $cache = new CourseRecommendedListCache(); + + $result = $cache->get($course->id); + + return $result ?: []; + } + +} diff --git a/app/Services/Frontend/Course/CourseRelated.php b/app/Services/Frontend/Course/RelatedList.php similarity index 92% rename from app/Services/Frontend/Course/CourseRelated.php rename to app/Services/Frontend/Course/RelatedList.php index de60931d..aeddb875 100644 --- a/app/Services/Frontend/Course/CourseRelated.php +++ b/app/Services/Frontend/Course/RelatedList.php @@ -6,7 +6,7 @@ use App\Caches\CourseRelatedList as CourseRelatedListCache; use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; -class CourseRelated extends Service +class RelatedList extends Service { use CourseTrait; diff --git a/app/Services/Frontend/Course/ReviewList.php b/app/Services/Frontend/Course/ReviewList.php index 69f913a3..ef1f31a9 100644 --- a/app/Services/Frontend/Course/ReviewList.php +++ b/app/Services/Frontend/Course/ReviewList.php @@ -64,7 +64,7 @@ class ReviewList extends Service $users = $builder->getUsers($reviews); - $votes = $this->getReviewVotes($this->course->id, $this->user->id); + $votes = $this->getReviewVotes($this->course, $this->user); $items = []; @@ -94,15 +94,15 @@ class ReviewList extends Service return $pager; } - protected function getReviewVotes($courseId, $userId) + protected function getReviewVotes(CourseModel $course, UserModel $user) { - if (!$courseId || !$userId) { + if ($course->id == 0 || $user->id == 0) { return []; } $courseRepo = new CourseRepo(); - $votes = $courseRepo->findUserReviewVotes($courseId, $userId); + $votes = $courseRepo->findUserReviewVotes($course->id, $user->id); if ($votes->count() == 0) { return []; diff --git a/app/Services/Frontend/Course/TeacherList.php b/app/Services/Frontend/Course/TeacherList.php index 57639423..e78fed8a 100644 --- a/app/Services/Frontend/Course/TeacherList.php +++ b/app/Services/Frontend/Course/TeacherList.php @@ -11,7 +11,7 @@ class TeacherList extends Service use CourseTrait; - public function getTeachers($id) + public function handle($id) { $course = $this->checkCourse($id); diff --git a/app/Services/Frontend/Course/TopicList.php b/app/Services/Frontend/Course/TopicList.php new file mode 100644 index 00000000..b900a0e4 --- /dev/null +++ b/app/Services/Frontend/Course/TopicList.php @@ -0,0 +1,25 @@ +checkCourse($id); + + $cache = new CourseTopicListCache(); + + $result = $cache->get($course->id); + + return $result ?: []; + } + +} diff --git a/app/Validators/CourseQuery.php b/app/Validators/CourseQuery.php index 067a3f3d..0c24ebec 100644 --- a/app/Validators/CourseQuery.php +++ b/app/Validators/CourseQuery.php @@ -2,20 +2,34 @@ namespace App\Validators; +use App\Caches\Category as CategoryCache; +use App\Exceptions\BadRequest as BadRequestException; use App\Models\Course as CourseModel; -use App\Repos\Category as CategoryRepo; class CourseQuery extends Validator { - public function checkCategory($id) + public function checkTopCategory($id) { - $categoryRepo = new CategoryRepo(); + $categoryCache = new CategoryCache(); - $category = $categoryRepo->findById($id); + $category = $categoryCache->get($id); if (!$category) { - return false; + throw new BadRequestException('course_query.invalid_top_category'); + } + + return $category->id; + } + + public function checkSubCategory($id) + { + $categoryCache = new CategoryCache(); + + $category = $categoryCache->get($id); + + if (!$category) { + throw new BadRequestException('course_query.invalid_sub_category'); } return $category->id; @@ -26,38 +40,32 @@ class CourseQuery extends Validator $types = CourseModel::levelTypes(); if (!isset($types[$level])) { - return $level; + throw new BadRequestException('course_query.invalid_level'); } - return false; + return $level; } public function checkModel($model) { - $types = CourseModel::levelTypes(); + $types = CourseModel::modelTypes(); if (!isset($types[$model])) { - return $model; + throw new BadRequestException('course_query.invalid_model'); } - return false; + return $model; } public function checkSort($sort) { - switch ($sort) { - case 'rating': - $orderBy = 'rating DESC'; - break; - case 'score': - $orderBy = 'score DESC'; - break; - default: - $orderBy = 'id DESC'; - break; + $types = CourseModel::sortTypes(); + + if (!isset($types[$sort])) { + throw new BadRequestException('course_query.invalid_sort'); } - return $orderBy; + return $sort; } } diff --git a/config/errors.php b/config/errors.php index 24b696e6..eab61d30 100644 --- a/config/errors.php +++ b/config/errors.php @@ -295,4 +295,13 @@ $error['user_daily_limit.reach_review_limit'] = '超出日评价限额'; $error['user_daily_limit.reach_order_limit'] = '超出日订单限额'; $error['user_daily_limit.reach_vote_limit'] = '超出日投票限额'; +/** + * 课程查询 + */ +$error['course_query.invalid_top_category'] = '无效的方向类别'; +$error['course_query.invalid_sub_category'] = '无效的分类类别'; +$error['course_query.invalid_model'] = '无效的模型类别'; +$error['course_query.invalid_level'] = '无效的难度类别'; +$error['course_query.invalid_sort'] = '无效的排序类别'; + return $error;