diff --git a/app/Caches/IndexArticleList.php b/app/Caches/IndexArticleList.php new file mode 100644 index 00000000..0348b0a4 --- /dev/null +++ b/app/Caches/IndexArticleList.php @@ -0,0 +1,44 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_article_list'; + } + + public function getContent($id = null) + { + $articleRepo = new ArticleRepo(); + + $where = ['published' => ArticleModel::PUBLISH_APPROVED]; + + $pager = $articleRepo->paginate($where, 'latest', 1, 10); + + $service = new ArticleListService(); + + $pager = $service->handleArticles($pager); + + return $pager->items ?: []; + } + +} diff --git a/app/Caches/IndexLiveList.php b/app/Caches/IndexLiveList.php index 2088f9e5..6eaab7bf 100644 --- a/app/Caches/IndexLiveList.php +++ b/app/Caches/IndexLiveList.php @@ -7,14 +7,14 @@ namespace App\Caches; +use App\Models\Chapter as ChapterModel; 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; +use Phalcon\Mvc\Model\ResultsetInterface; -/** - * 直播课程 - */ class IndexLiveList extends Cache { @@ -32,32 +32,11 @@ class IndexLiveList extends Cache public function getContent($id = null) { - /** - * 限制输出多少天数(一维限额) - */ - $dayLimit = 3; + $limit = 8; - /** - * 限制每天维度下的输出数(二维限额) - */ - $perDayLimit = 10; + $lives = $this->findChapterLives(); - $beginTime = strtotime('today'); - $endTime = strtotime("+30 days"); - - /** - * @var Resultset|ChapterLiveModel[] $lives - */ - $lives = ChapterLiveModel::query() - ->betweenWhere('start_time', $beginTime, $endTime) - ->orderBy('start_time ASC') - ->execute(); - - if ($lives->count() == 0) { - return []; - } - - $result = []; + if ($lives->count() == 0) return []; $chapterIds = kg_array_column($lives->toArray(), 'chapter_id'); @@ -77,53 +56,85 @@ class IndexLiveList extends Cache $courses = $courseRepo->findByIds($courseIds); + $teacherIds = kg_array_column($courses->toArray(), 'teacher_id'); + + $userRepo = new UserRepo(); + + $users = $userRepo->findByIds($teacherIds); + $courseMapping = []; foreach ($courses as $course) { $courseMapping[$course->id] = $course; } + $userMapping = []; + + foreach ($users as $user) { + $userMapping[$user->id] = $user; + } + + $result = []; + + $flag = []; + 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 = $chapterMapping[$live->chapter_id]; $course = $courseMapping[$chapter->course_id]; + $teacher = $userMapping[$course->teacher_id]; + + $teacherInfo = [ + 'id' => $teacher->id, + 'name' => $teacher->name, + 'title' => $teacher->title, + 'avatar' => $teacher->avatar, + ]; $chapterInfo = [ 'id' => $chapter->id, 'title' => $chapter->title, - 'start_time' => $live->start_time, - 'end_time' => $live->end_time, ]; $courseInfo = [ '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, + 'teacher' => $teacherInfo, ]; - $result[$day][] = [ - 'course' => $courseInfo, - 'chapter' => $chapterInfo, - ]; + if (!isset($flag[$course->id]) && count($flag) < $limit) { + $flag[$course->id] = 1; + $result[] = [ + 'id' => $live->id, + 'status' => $live->status, + 'start_time' => $live->start_time, + 'end_time' => $live->end_time, + 'course' => $courseInfo, + 'chapter' => $chapterInfo, + ]; + } } return $result; } + /** + * @return ResultsetInterface|Resultset|ChapterLiveModel[] + */ + protected function findChapterLives() + { + $startTime = strtotime('today'); + $endTime = strtotime('+30 days'); + + return $this->modelsManager->createBuilder() + ->columns('cl.*') + ->addFrom(ChapterLiveModel::class, 'cl') + ->join(ChapterModel::class, 'cl.chapter_id = c.id', 'c') + ->betweenWhere('start_time', $startTime, $endTime) + ->orderBy('start_time ASC') + ->getQuery() + ->execute(); + } + } diff --git a/app/Caches/IndexQuestionList.php b/app/Caches/IndexQuestionList.php new file mode 100644 index 00000000..db028c91 --- /dev/null +++ b/app/Caches/IndexQuestionList.php @@ -0,0 +1,44 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'index_question_list'; + } + + public function getContent($id = null) + { + $questionRepo = new QuestionRepo(); + + $where = ['published' => QuestionModel::PUBLISH_APPROVED]; + + $pager = $questionRepo->paginate($where, 'latest', 1, 10); + + $service = new QuestionListService(); + + $pager = $service->handleQuestions($pager); + + return $pager->items ?: []; + } + +} diff --git a/app/Caches/IndexSimpleVipCourseList.php b/app/Caches/IndexSimpleVipCourseList.php index 27c55f90..cda3478f 100644 --- a/app/Caches/IndexSimpleVipCourseList.php +++ b/app/Caches/IndexSimpleVipCourseList.php @@ -66,6 +66,7 @@ class IndexSimpleVipCourseList extends Cache { return CourseModel::query() ->where('published = 1') + ->andWhere('market_price > vip_price') ->andWhere('vip_price >= 0') ->orderBy('score DESC') ->limit($limit) diff --git a/app/Caches/IndexVipCourseList.php b/app/Caches/IndexVipCourseList.php index 84b2c1f6..201edb71 100644 --- a/app/Caches/IndexVipCourseList.php +++ b/app/Caches/IndexVipCourseList.php @@ -114,6 +114,7 @@ class IndexVipCourseList extends Cache return CourseModel::query() ->inWhere('category_id', $categoryIds) ->andWhere('published = 1') + ->andWhere('market_price > vip_price') ->andWhere('vip_price >= 0') ->orderBy('score DESC') ->limit($limit) diff --git a/app/Http/Api/Controllers/AnswerController.php b/app/Http/Api/Controllers/AnswerController.php index 284e2b57..79c1c416 100644 --- a/app/Http/Api/Controllers/AnswerController.php +++ b/app/Http/Api/Controllers/AnswerController.php @@ -54,12 +54,11 @@ class AnswerController extends Controller $answer = $service->handle(); - $content = [ - 'answer' => $answer, - 'msg' => '创建回答成功', - ]; + $service = new AnswerInfoService(); - return $this->jsonSuccess($content); + $answer = $service->handle($answer->id); + + return $this->jsonSuccess(['answer' => $answer]); } /** @@ -71,12 +70,7 @@ class AnswerController extends Controller $answer = $service->handle($id); - $content = [ - 'answer' => $answer, - 'msg' => '更新回答成功', - ]; - - return $this->jsonSuccess($content); + return $this->jsonSuccess(['answer' => $answer]); } /** @@ -105,20 +99,6 @@ class AnswerController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/unaccept", name="api.answer.unaccept") - */ - public function unacceptAction($id) - { - $service = new AnswerAcceptService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '采纳成功' : '取消采纳成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - /** * @Post("/{id:[0-9]+}/like", name="api.answer.like") */ @@ -133,18 +113,4 @@ class AnswerController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/like", name="api.answer.unlike") - */ - public function unlikeAction($id) - { - $service = new AnswerLikeService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '点赞成功' : '取消点赞成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - } diff --git a/app/Http/Api/Controllers/ArticleController.php b/app/Http/Api/Controllers/ArticleController.php index 8366281d..fa636c57 100644 --- a/app/Http/Api/Controllers/ArticleController.php +++ b/app/Http/Api/Controllers/ArticleController.php @@ -82,20 +82,6 @@ class ArticleController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/unfavorite", name="api.article.unfavorite") - */ - public function unfavoriteAction($id) - { - $service = new ArticleFavoriteService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '收藏成功' : '取消收藏成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - /** * @Post("/{id:[0-9]+}/like", name="api.article.like") */ @@ -110,18 +96,4 @@ class ArticleController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/like", name="api.article.unlike") - */ - public function unlikeAction($id) - { - $service = new ArticleLikeService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '点赞成功' : '取消点赞成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - } diff --git a/app/Http/Api/Controllers/CommentController.php b/app/Http/Api/Controllers/CommentController.php index 66d61821..4e9031c2 100644 --- a/app/Http/Api/Controllers/CommentController.php +++ b/app/Http/Api/Controllers/CommentController.php @@ -102,18 +102,4 @@ class CommentController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/like", name="home.comment.like") - */ - public function unlikeAction($id) - { - $service = new CommentLikeService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '点赞成功' : '取消点赞成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - } diff --git a/app/Http/Api/Controllers/ConsultController.php b/app/Http/Api/Controllers/ConsultController.php index bccb3d2a..4a9b1386 100644 --- a/app/Http/Api/Controllers/ConsultController.php +++ b/app/Http/Api/Controllers/ConsultController.php @@ -89,18 +89,4 @@ class ConsultController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/unlike", name="api.consult.unlike") - */ - public function unlikeAction($id) - { - $service = new ConsultLikeService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '点赞成功' : '取消点赞成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - } diff --git a/app/Http/Api/Controllers/CourseController.php b/app/Http/Api/Controllers/CourseController.php index d3acfd28..5c449652 100644 --- a/app/Http/Api/Controllers/CourseController.php +++ b/app/Http/Api/Controllers/CourseController.php @@ -120,18 +120,4 @@ class CourseController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/unfavorite", name="api.course.unfavorite") - */ - public function unfavoriteAction($id) - { - $service = new CourseFavoriteService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '收藏成功' : '取消收藏成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - } diff --git a/app/Http/Api/Controllers/IndexController.php b/app/Http/Api/Controllers/IndexController.php index d5d56468..eb9557d9 100644 --- a/app/Http/Api/Controllers/IndexController.php +++ b/app/Http/Api/Controllers/IndexController.php @@ -7,6 +7,9 @@ namespace App\Http\Api\Controllers; +use App\Caches\IndexArticleList; +use App\Caches\IndexLiveList; +use App\Caches\IndexQuestionList; use App\Caches\IndexSimpleFeaturedCourseList; use App\Caches\IndexSimpleFreeCourseList; use App\Caches\IndexSimpleNewCourseList; @@ -31,6 +34,42 @@ class IndexController extends Controller return $this->jsonSuccess(['slides' => $slides]); } + /** + * @Get("/articles", name="api.index.articles") + */ + public function articlesAction() + { + $cache = new IndexArticleList(); + + $articles = $cache->get(); + + return $this->jsonSuccess(['articles' => $articles]); + } + + /** + * @Get("/questions", name="api.index.questions") + */ + public function questionsAction() + { + $cache = new IndexQuestionList(); + + $questions = $cache->get(); + + return $this->jsonSuccess(['questions' => $questions]); + } + + /** + * @Get("/lives", name="api.index.lives") + */ + public function livesAction() + { + $cache = new IndexLiveList(); + + $lives = $cache->get(); + + return $this->jsonSuccess(['lives' => $lives]); + } + /** * @Get("/courses/featured", name="api.index.featured_courses") */ diff --git a/app/Http/Api/Controllers/QuestionController.php b/app/Http/Api/Controllers/QuestionController.php index fa30b2e6..d76c82bb 100644 --- a/app/Http/Api/Controllers/QuestionController.php +++ b/app/Http/Api/Controllers/QuestionController.php @@ -95,20 +95,6 @@ class QuestionController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/unfavorite", name="api.question.unfavorite") - */ - public function unfavoriteAction($id) - { - $service = new QuestionFavoriteService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '收藏成功' : '取消收藏成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - /** * @Post("/{id:[0-9]+}/like", name="api.question.like") */ @@ -123,18 +109,4 @@ class QuestionController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/like", name="api.question.unlike") - */ - public function unlikeAction($id) - { - $service = new QuestionLikeService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '点赞成功' : '取消点赞成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - } diff --git a/app/Http/Api/Controllers/ReviewController.php b/app/Http/Api/Controllers/ReviewController.php index c41a6e1d..1c88a173 100644 --- a/app/Http/Api/Controllers/ReviewController.php +++ b/app/Http/Api/Controllers/ReviewController.php @@ -89,18 +89,4 @@ class ReviewController extends Controller return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); } - /** - * @Post("/{id:[0-9]+}/unlike", name="api.review.unlike") - */ - public function unlikeAction($id) - { - $service = new ReviewLikeService(); - - $data = $service->handle($id); - - $msg = $data['action'] == 'do' ? '点赞成功' : '取消点赞成功'; - - return $this->jsonSuccess(['data' => $data, 'msg' => $msg]); - } - } diff --git a/app/Http/Api/Controllers/SearchController.php b/app/Http/Api/Controllers/SearchController.php index 39b86369..4b1cd272 100644 --- a/app/Http/Api/Controllers/SearchController.php +++ b/app/Http/Api/Controllers/SearchController.php @@ -7,10 +7,11 @@ namespace App\Http\Api\Controllers; -use App\Services\Logic\Search\Article as ArticleSearchService; -use App\Services\Logic\Search\Course as CourseSearchService; -use App\Services\Logic\Search\Group as GroupSearchService; -use App\Services\Logic\Search\User as UserSearchService; +use App\Services\Logic\Search\Article as ArticleSearch; +use App\Services\Logic\Search\Course as CourseSearch; +use App\Services\Logic\Search\Group as GroupSearch; +use App\Services\Logic\Search\Question as QuestionSearch; +use App\Services\Logic\Search\User as UserSearch; /** * @RoutePrefix("/api/search") @@ -45,22 +46,25 @@ class SearchController extends Controller /** * @param string $type - * @return ArticleSearchService|CourseSearchService|GroupSearchService|UserSearchService + * @return ArticleSearch|QuestionSearch|CourseSearch|GroupSearch|UserSearch */ protected function getSearchService($type) { switch ($type) { case 'article': - $service = new ArticleSearchService(); + $service = new ArticleSearch(); + break; + case 'question': + $service = new QuestionSearch(); break; case 'group': - $service = new GroupSearchService(); + $service = new GroupSearch(); break; case 'user': - $service = new UserSearchService(); + $service = new UserSearch(); break; default: - $service = new CourseSearchService(); + $service = new CourseSearch(); break; } diff --git a/app/Http/Home/Views/course/show_catalog.volt b/app/Http/Home/Views/course/show_catalog.volt index 3f55aab4..b1b2cabc 100644 --- a/app/Http/Home/Views/course/show_catalog.volt +++ b/app/Http/Home/Views/course/show_catalog.volt @@ -57,7 +57,7 @@ {%- endmacro %} {%- macro live_status_info(lesson) %} - {% if lesson.attrs.stream.status == 'active' %} + {% if lesson.attrs.start_time < time() and lesson.attrs.end_time > time() %} {{ date('m月d日 H:i',lesson.attrs.start_time) }} 直播中 {% elseif lesson.attrs.start_time > time() %} {{ date('m月d日 H:i',lesson.attrs.start_time) }} 倒计时 diff --git a/app/Library/AppInfo.php b/app/Library/AppInfo.php index ce5f1a17..2e94c37e 100644 --- a/app/Library/AppInfo.php +++ b/app/Library/AppInfo.php @@ -16,7 +16,7 @@ class AppInfo protected $link = 'https://koogua.com'; - protected $version = '1.4.3'; + protected $version = '1.4.4'; public function __get($name) { diff --git a/app/Services/Logic/Article/ArticleList.php b/app/Services/Logic/Article/ArticleList.php index c89fabd3..fdfd9e8a 100644 --- a/app/Services/Logic/Article/ArticleList.php +++ b/app/Services/Logic/Article/ArticleList.php @@ -72,8 +72,6 @@ class ArticleList extends LogicService 'source_type' => $article['source_type'], 'source_url' => $article['source_url'], 'tags' => $article['tags'], - 'category' => $category, - 'owner' => $owner, 'private' => $article['private'], 'published' => $article['published'], 'closed' => $article['closed'], @@ -83,6 +81,8 @@ class ArticleList extends LogicService 'favorite_count' => $article['favorite_count'], 'create_time' => $article['create_time'], 'update_time' => $article['update_time'], + 'category' => $category, + 'owner' => $owner, ]; } diff --git a/app/Services/Logic/Comment/CommentLike.php b/app/Services/Logic/Comment/CommentLike.php index 43e9eaf0..481ae526 100644 --- a/app/Services/Logic/Comment/CommentLike.php +++ b/app/Services/Logic/Comment/CommentLike.php @@ -50,7 +50,7 @@ class CommentLike extends LogicService $isFirstTime = false; - $commentLike->comment_id = $commentLike->deleted == 1 ? 0 : 1; + $commentLike->deleted = $commentLike->deleted == 1 ? 0 : 1; $commentLike->update(); } diff --git a/app/Services/Logic/Live/LiveList.php b/app/Services/Logic/Live/LiveList.php index 18490016..9db5d708 100644 --- a/app/Services/Logic/Live/LiveList.php +++ b/app/Services/Logic/Live/LiveList.php @@ -52,12 +52,17 @@ class LiveList extends LogicService $items = []; foreach ($lives as $live) { + + $course = $courses[$live['course_id']] ?? new \stdClass(); + $chapter = $chapters[$live['chapter_id']] ?? new \stdClass(); + $items[] = [ - 'course' => $courses[$live['course_id']] ?? new \stdClass(), - 'chapter' => $chapters[$live['chapter_id']] ?? new \stdClass(), + 'id' => $live['id'], 'status' => $live['status'], 'start_time' => $live['start_time'], 'end_time' => $live['end_time'], + 'course' => $course, + 'chapter' => $chapter, ]; } diff --git a/app/Services/Logic/Question/QuestionList.php b/app/Services/Logic/Question/QuestionList.php index fa130993..19ad8af4 100644 --- a/app/Services/Logic/Question/QuestionList.php +++ b/app/Services/Logic/Question/QuestionList.php @@ -47,6 +47,8 @@ class QuestionList extends LogicService $builder = new QuestionListBuilder(); + $categories = $builder->getCategories(); + $questions = $pager->items->toArray(); $users = $builder->getUsers($questions); @@ -57,6 +59,8 @@ class QuestionList extends LogicService $question['tags'] = json_decode($question['tags'], true); + $category = $categories[$question['category_id']] ?? new \stdClass(); + $owner = $users[$question['owner_id']] ?? new \stdClass(); $lastReplier = $users[$question['last_replier_id']] ?? new \stdClass(); @@ -81,6 +85,7 @@ class QuestionList extends LogicService 'create_time' => $question['create_time'], 'update_time' => $question['update_time'], 'last_replier' => $lastReplier, + 'category' => $category, 'owner' => $owner, ]; } diff --git a/app/Services/Logic/Search/Article.php b/app/Services/Logic/Search/Article.php index 6c86f128..0197a3ac 100644 --- a/app/Services/Logic/Search/Article.php +++ b/app/Services/Logic/Search/Article.php @@ -64,6 +64,16 @@ class Article extends Handler foreach ($pager->items as $item) { + $category = json_decode($item['category'], true); + $owner = json_decode($item['owner'], true); + $tags = json_decode($item['tags'], true); + + $owner['avatar'] = $owner['avatar'] ?: kg_default_user_avatar_path(); + + if (!empty($owner['avatar']) && !Text::startsWith($owner['avatar'], 'http')) { + $owner['avatar'] = $baseUrl . $owner['avatar']; + } + if (!empty($item['cover']) && !Text::startsWith($item['cover'], 'http')) { $item['cover'] = $baseUrl . $item['cover']; } @@ -78,9 +88,9 @@ class Article extends Handler 'like_count' => (int)$item['like_count'], 'favorite_count' => (int)$item['favorite_count'], 'comment_count' => (int)$item['comment_count'], - 'tags' => json_decode($item['tags'], true), - 'owner' => json_decode($item['owner'], true), - 'category' => json_decode($item['category'], true), + 'category' => $category, + 'owner' => $owner, + 'tags' => $tags, ]; } diff --git a/app/Services/Logic/Search/Course.php b/app/Services/Logic/Search/Course.php index 73f1c376..92fa1338 100644 --- a/app/Services/Logic/Search/Course.php +++ b/app/Services/Logic/Search/Course.php @@ -10,6 +10,7 @@ namespace App\Services\Logic\Search; use App\Library\Paginator\Adapter\XunSearch as XunSearchPaginator; use App\Library\Paginator\Query as PagerQuery; use App\Services\Search\CourseSearcher as CourseSearcherService; +use Phalcon\Text; class Course extends Handler { @@ -63,7 +64,24 @@ class Course extends Handler foreach ($pager->items as $item) { - $item['cover'] = $baseUrl . $item['cover']; + /** + * 后补的字段,给默认值防止出错 + */ + $item['tags'] = $item['tags'] ?: '[]'; + + $category = json_decode($item['category'], true); + $teacher = json_decode($item['teacher'], true); + $tags = json_decode($item['tags'], true); + + $teacher['avatar'] = $teacher['avatar'] ?: kg_default_user_avatar_path(); + + if (!empty($teacher['avatar']) && !Text::startsWith($teacher['avatar'], 'http')) { + $teacher['avatar'] = $baseUrl . $teacher['avatar']; + } + + if (!empty($item['cover']) && !Text::startsWith($item['cover'], 'http')) { + $item['cover'] = $baseUrl . $item['cover']; + } $items[] = [ 'id' => (int)$item['id'], @@ -78,8 +96,9 @@ class Course extends Handler 'lesson_count' => (int)$item['lesson_count'], 'review_count' => (int)$item['review_count'], 'favorite_count' => (int)$item['favorite_count'], - 'teacher' => json_decode($item['teacher'], true), - 'category' => json_decode($item['category'], true), + 'category' => $category, + 'teacher' => $teacher, + 'tags' => $tags, ]; } diff --git a/app/Services/Logic/Search/Group.php b/app/Services/Logic/Search/Group.php index b070aeec..215e06b6 100644 --- a/app/Services/Logic/Search/Group.php +++ b/app/Services/Logic/Search/Group.php @@ -10,6 +10,7 @@ namespace App\Services\Logic\Search; use App\Library\Paginator\Adapter\XunSearch as XunSearchPaginator; use App\Library\Paginator\Query as PagerQuery; use App\Services\Search\GroupSearcher as GroupSearcherService; +use Phalcon\Text; class Group extends Handler { @@ -63,7 +64,11 @@ class Group extends Handler foreach ($pager->items as $item) { - $item['avatar'] = $baseUrl . $item['avatar']; + $owner = json_decode($item['owner'], true); + + if (!empty($item['avatar']) && !Text::startsWith($item['avatar'], 'http')) { + $item['avatar'] = $baseUrl . $item['avatar']; + } $items[] = [ 'id' => (int)$item['id'], @@ -73,7 +78,7 @@ class Group extends Handler 'about' => (string)$item['about'], 'user_count' => (int)$item['user_count'], 'msg_count' => (int)$item['msg_count'], - 'owner' => json_decode($item['owner'], true), + 'owner' => $owner, ]; } diff --git a/app/Services/Logic/Search/Question.php b/app/Services/Logic/Search/Question.php index 0803b76d..a8788988 100644 --- a/app/Services/Logic/Search/Question.php +++ b/app/Services/Logic/Search/Question.php @@ -64,6 +64,17 @@ class Question extends Handler foreach ($pager->items as $item) { + $lastReplier = json_decode($item['last_replier'], true); + $category = json_decode($item['category'], true); + $owner = json_decode($item['owner'], true); + $tags = json_decode($item['tags'], true); + + $owner['avatar'] = $owner['avatar'] ?: kg_default_user_avatar_path(); + + if (!empty($owner['avatar']) && !Text::startsWith($owner['avatar'], 'http')) { + $owner['avatar'] = $baseUrl . $owner['avatar']; + } + if (!empty($item['cover']) && !Text::startsWith($item['cover'], 'http')) { $item['cover'] = $baseUrl . $item['cover']; } @@ -71,13 +82,13 @@ class Question extends Handler $lastAnswer = json_decode($item['last_answer'], true); if (!empty($lastAnswer['cover']) && !Text::startsWith($lastAnswer['cover'], 'http')) { - $item['last_answer'] = $baseUrl . $lastAnswer['cover']; + $lastAnswer['cover'] = $baseUrl . $lastAnswer['cover']; } $acceptAnswer = json_decode($item['accept_answer'], true); if (!empty($acceptAnswer['cover']) && !Text::startsWith($acceptAnswer['cover'], 'http')) { - $item['accept_answer'] = $baseUrl . $acceptAnswer['cover']; + $acceptAnswer['cover'] = $baseUrl . $acceptAnswer['cover']; } $items[] = [ @@ -95,12 +106,12 @@ class Question extends Handler 'answer_count' => (int)$item['answer_count'], 'comment_count' => (int)$item['comment_count'], 'favorite_count' => (int)$item['favorite_count'], - 'category' => json_decode($item['category'], true), - 'tags' => json_decode($item['tags'], true), - 'owner' => json_decode($item['owner'], true), - 'last_replier' => json_decode($item['last_replier'], true), - 'last_answer' => $item['last_answer'], - 'accept_answer' => $item['accept_answer'], + 'accept_answer' => $acceptAnswer, + 'last_answer' => $lastAnswer, + 'last_replier' => $lastReplier, + 'category' => $category, + 'owner' => $owner, + 'tags' => $tags, ]; } diff --git a/app/Services/Search/ArticleDocument.php b/app/Services/Search/ArticleDocument.php index cd912b30..659044f4 100644 --- a/app/Services/Search/ArticleDocument.php +++ b/app/Services/Search/ArticleDocument.php @@ -8,6 +8,7 @@ namespace App\Services\Search; use App\Models\Article as ArticleModel; +use App\Models\User as UserModel; use App\Repos\Category as CategoryRepo; use App\Repos\User as UserRepo; use Phalcon\Di\Injectable; @@ -80,9 +81,12 @@ class ArticleDocument extends Injectable $user = $userRepo->findById($id); + $user->avatar = UserModel::getAvatarPath($user->avatar); + return kg_json_encode([ 'id' => $user->id, 'name' => $user->name, + 'avatar' => $user->avatar, ]); } diff --git a/app/Services/Search/CourseDocument.php b/app/Services/Search/CourseDocument.php index 39d9e747..52247a04 100644 --- a/app/Services/Search/CourseDocument.php +++ b/app/Services/Search/CourseDocument.php @@ -7,9 +7,10 @@ namespace App\Services\Search; -use App\Models\Category as CategoryModel; use App\Models\Course as CourseModel; use App\Models\User as UserModel; +use App\Repos\Category as CategoryRepo; +use App\Repos\User as UserRepo; use Phalcon\Di\Injectable; class CourseDocument extends Injectable @@ -44,24 +45,20 @@ class CourseDocument extends Injectable $course->attrs = kg_json_encode($course->attrs); } + if (is_array($course->tags) || is_object($course->tags)) { + $course->tags = kg_json_encode($course->tags); + } + $teacher = '{}'; if ($course->teacher_id > 0) { - $record = UserModel::findFirst($course->teacher_id); - $teacher = kg_json_encode([ - 'id' => $record->id, - 'name' => $record->name, - ]); + $teacher = $this->handleUser($course->teacher_id); } $category = '{}'; if ($course->category_id > 0) { - $record = CategoryModel::findFirst($course->category_id); - $category = kg_json_encode([ - 'id' => $record->id, - 'name' => $record->name, - ]); + $category = $this->handleCategory($course->category_id); } $course->cover = CourseModel::getCoverPath($course->cover); @@ -83,6 +80,7 @@ class CourseDocument extends Injectable 'model' => $course->model, 'level' => $course->level, 'attrs' => $course->attrs, + 'tags' => $course->tags, 'category' => $category, 'teacher' => $teacher, 'user_count' => $course->user_count, @@ -92,4 +90,31 @@ class CourseDocument extends Injectable ]; } + protected function handleUser($id) + { + $userRepo = new UserRepo(); + + $user = $userRepo->findById($id); + + $user->avatar = UserModel::getAvatarPath($user->avatar); + + return kg_json_encode([ + 'id' => $user->id, + 'name' => $user->name, + 'avatar' => $user->avatar, + ]); + } + + protected function handleCategory($id) + { + $categoryRepo = new CategoryRepo(); + + $category = $categoryRepo->findById($id); + + return kg_json_encode([ + 'id' => $category->id, + 'name' => $category->name, + ]); + } + } diff --git a/app/Services/Search/GroupDocument.php b/app/Services/Search/GroupDocument.php index 9148feab..b5144664 100644 --- a/app/Services/Search/GroupDocument.php +++ b/app/Services/Search/GroupDocument.php @@ -9,6 +9,7 @@ namespace App\Services\Search; use App\Models\ImGroup as GroupModel; use App\Models\User as UserModel; +use App\Repos\User as UserRepo; use Phalcon\Di\Injectable; class GroupDocument extends Injectable @@ -42,11 +43,7 @@ class GroupDocument extends Injectable $owner = '{}'; if ($group->owner_id > 0) { - $record = UserModel::findFirst($group->owner_id); - $owner = kg_json_encode([ - 'id' => $record->id, - 'name' => $record->name, - ]); + $owner = $this->handleUser($group->owner_id); } $group->avatar = GroupModel::getAvatarPath($group->avatar); @@ -62,4 +59,19 @@ class GroupDocument extends Injectable ]; } + protected function handleUser($id) + { + $userRepo = new UserRepo(); + + $user = $userRepo->findById($id); + + $user->avatar = UserModel::getAvatarPath($user->avatar); + + return kg_json_encode([ + 'id' => $user->id, + 'name' => $user->name, + 'avatar' => $user->avatar, + ]); + } + } diff --git a/app/Services/Search/QuestionDocument.php b/app/Services/Search/QuestionDocument.php index 01c2768a..3c8a5e2a 100644 --- a/app/Services/Search/QuestionDocument.php +++ b/app/Services/Search/QuestionDocument.php @@ -8,6 +8,7 @@ namespace App\Services\Search; use App\Models\Question as QuestionModel; +use App\Models\User as UserModel; use App\Repos\Answer as AnswerRepo; use App\Repos\Category as CategoryRepo; use App\Repos\User as UserRepo; @@ -93,11 +94,11 @@ class QuestionDocument extends Injectable 'answer_count' => $question->answer_count, 'comment_count' => $question->comment_count, 'favorite_count' => $question->favorite_count, + 'accept_answer' => $acceptAnswer, + 'last_answer' => $lastAnswer, + 'last_replier' => $lastReplier, 'category' => $category, 'owner' => $owner, - 'last_replier' => $lastReplier, - 'last_answer' => $lastAnswer, - 'accept_answer' => $acceptAnswer, ]; } @@ -107,9 +108,12 @@ class QuestionDocument extends Injectable $user = $userRepo->findById($id); + $user->avatar = UserModel::getAvatarPath($user->avatar); + return kg_json_encode([ 'id' => $user->id, 'name' => $user->name, + 'avatar' => $user->avatar, ]); } diff --git a/app/Validators/Answer.php b/app/Validators/Answer.php index 85c54348..739ec2d9 100644 --- a/app/Validators/Answer.php +++ b/app/Validators/Answer.php @@ -56,7 +56,7 @@ class Answer extends Validator public function checkContent($content) { - $value = $this->filter->sanitize($content, ['trim', 'string']); + $value = $this->filter->sanitize($content, ['trim']); $length = kg_strlen($value); diff --git a/app/Validators/PointGift.php b/app/Validators/PointGift.php index 6a672b01..2a296437 100644 --- a/app/Validators/PointGift.php +++ b/app/Validators/PointGift.php @@ -84,7 +84,7 @@ class PointGift extends Validator public function checkDetails($details) { - $value = $this->filter->sanitize($details, ['trim', 'string']); + $value = $this->filter->sanitize($details, ['trim']); $length = kg_strlen($value); diff --git a/config/xs.course.default.ini b/config/xs.course.default.ini index b292d9b4..14c048e5 100644 --- a/config/xs.course.default.ini +++ b/config/xs.course.default.ini @@ -61,6 +61,9 @@ tokenizer = full [attrs] type = string +[tags] +type = string + [category] type = string diff --git a/db/migrations/20210825111618.php b/db/migrations/20210825111618.php new file mode 100644 index 00000000..5b337651 --- /dev/null +++ b/db/migrations/20210825111618.php @@ -0,0 +1,27 @@ +alterUploadTable(); + } + + protected function alterUploadTable() + { + $table = $this->table('kg_upload'); + + $table->removeIndexByName('md5')->save(); + + $table->addIndex('md5')->save(); + } + +}