diff --git a/app/Caches/CourseRelatedList.php b/app/Caches/CourseRelatedList.php index 9c04c5fb..67f80c90 100644 --- a/app/Caches/CourseRelatedList.php +++ b/app/Caches/CourseRelatedList.php @@ -47,8 +47,10 @@ class CourseRelatedList extends Cache 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, - 'market_price' => (float)$course->market_price, - 'vip_price' => (float)$course->vip_price, + 'summary' => $course->summary, + '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/PackageCourseList.php b/app/Caches/PackageCourseList.php index c78f7b6a..05860e42 100644 --- a/app/Caches/PackageCourseList.php +++ b/app/Caches/PackageCourseList.php @@ -46,8 +46,14 @@ class PackageCourseList extends Cache 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, + 'summary' => $course->summary, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, + 'rating' => $course->rating, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, ]; } diff --git a/app/Caches/SlideList.php b/app/Caches/SlideList.php index 9acaf2ea..e951757a 100644 --- a/app/Caches/SlideList.php +++ b/app/Caches/SlideList.php @@ -3,7 +3,7 @@ namespace App\Caches; use App\Models\Slide as SlideModel; -use Phalcon\Mvc\Model\Resultset; +use App\Repos\Slide as SlideRepo; class SlideList extends Cache { @@ -22,14 +22,9 @@ class SlideList extends Cache public function getContent($id = null) { - /** - * @var Resultset|SlideModel[] $slides - */ - $slides = SlideModel::query() - ->columns(['id', 'title', 'cover', 'summary', 'target', 'content']) - ->where('published = 1 AND deleted = 0') - ->orderBy('priority ASC') - ->execute(); + $slideRepo = new SlideRepo(); + + $slides = $slideRepo->findTopSlides(); if ($slides->count() == 0) { return []; @@ -51,7 +46,6 @@ class SlideList extends Cache 'id' => $slide->id, 'title' => $slide->title, 'cover' => $slide->cover, - 'summary' => $slide->summary, 'target' => $slide->target, 'content' => $slide->content, ]; diff --git a/app/Caches/TopicCourseList.php b/app/Caches/TopicCourseList.php index b09328b4..f02ffbbc 100644 --- a/app/Caches/TopicCourseList.php +++ b/app/Caches/TopicCourseList.php @@ -46,8 +46,14 @@ class TopicCourseList extends Cache 'id' => $course->id, 'title' => $course->title, 'cover' => $course->cover, + 'summary' => $course->summary, 'market_price' => $course->market_price, 'vip_price' => $course->vip_price, + 'rating' => $course->rating, + 'model' => $course->model, + 'level' => $course->level, + 'user_count' => $course->user_count, + 'lesson_count' => $course->lesson_count, ]; } diff --git a/app/Http/Admin/Services/User.php b/app/Http/Admin/Services/User.php index 9dcf0ea6..764bae04 100644 --- a/app/Http/Admin/Services/User.php +++ b/app/Http/Admin/Services/User.php @@ -125,7 +125,7 @@ class User extends Service $data['vip'] = $validator->checkVipStatus($post['vip']); } - if (isset($post['vip_expiry_time'])) { + if (!empty($post['vip_expiry_time'])) { $data['vip_expiry_time'] = $validator->checkVipExpiryTime($post['vip_expiry_time']); if ($data['vip_expiry_time'] < time()) { $data['vip'] = 0; @@ -136,7 +136,7 @@ class User extends Service $data['locked'] = $validator->checkLockStatus($post['locked']); } - if (isset($post['lock_expiry_time'])) { + if (!empty($post['lock_expiry_time'])) { $data['lock_expiry_time'] = $validator->checkLockExpiryTime($post['lock_expiry_time']); if ($data['lock_expiry_time'] < time()) { $data['locked'] = 0; diff --git a/app/Http/Admin/Views/course/edit_basic.volt b/app/Http/Admin/Views/course/edit_basic.volt index 4109537a..49cbe2de 100644 --- a/app/Http/Admin/Views/course/edit_basic.volt +++ b/app/Http/Admin/Views/course/edit_basic.volt @@ -11,11 +11,11 @@
{% if course.cover %} - + {% else %} {% endif %} - +
编辑 diff --git a/app/Http/Admin/Views/slide/edit.volt b/app/Http/Admin/Views/slide/edit.volt index bdefcd69..67a6e390 100644 --- a/app/Http/Admin/Views/slide/edit.volt +++ b/app/Http/Admin/Views/slide/edit.volt @@ -18,11 +18,11 @@
{% if slide.cover %} - + {% else %} {% endif %} - +
编辑 diff --git a/app/Http/Admin/Views/user/edit.volt b/app/Http/Admin/Views/user/edit.volt index 34883579..3995d45d 100644 --- a/app/Http/Admin/Views/user/edit.volt +++ b/app/Http/Admin/Views/user/edit.volt @@ -28,8 +28,8 @@
- - + +
@@ -151,7 +151,7 @@ form.on('radio(vip)', function (data) { var block = $('#vip-expiry-block'); - if (data.value == 1) { + if (data.value === '1') { block.show(); } else { block.hide(); @@ -165,7 +165,7 @@ form.on('radio(locked)', function (data) { var block = $('#lock-expiry-block'); - if (data.value == 1) { + if (data.value === '1') { block.show(); } else { block.hide(); diff --git a/app/Http/Admin/Views/user/list.volt b/app/Http/Admin/Views/user/list.volt index 33649dc8..4835c3c3 100644 --- a/app/Http/Admin/Views/user/list.volt +++ b/app/Http/Admin/Views/user/list.volt @@ -1,33 +1,39 @@ -{%- macro gender_info(value) %} - {% if value == 1 %} - - {% elseif value == 2 %} - - {% elseif value == 3 %} - +{%- macro location_info(value) %} + {% if value %} + {{ value }} + {% else %} + N/A {% endif %} {%- endmacro %} -{%- macro role_info(user) %} - {% if user.edu_role.id > 0 %} - {{ user.edu_role.name }} +{%- macro gender_info(value) %} + {% if value == 'male' %} + 男 + {% elseif value == 'female' %} + 女 + {% elseif value == 'none' %} + 密 {% endif %} - {% if user.admin_role.id > 0 %} - {{ user.admin_role.name }} +{%- endmacro %} + +{%- macro edu_role_info(user) %} + {% if user.edu_role.id %} + {{ user.edu_role.name }} + {% endif %} +{%- endmacro %} + +{%- macro admin_role_info(user) %} + {% if user.admin_role.id %} + {{ user.admin_role.name }} {% endif %} {%- endmacro %} {%- macro status_info(user) %} - {% if user.locked == 0 %} - 正常 - {% else %} - 锁定 - {% endif %} -{%- endmacro %} - -{%- macro vip_info(user) %} {% if user.vip == 1 %} - vip + 会员 + {% endif %} + {% if user.locked == 1 %} + 锁定 {% endif %} {%- endmacro %} @@ -55,6 +61,7 @@ + @@ -63,8 +70,9 @@ 昵称 地区 性别 - 角色 - 状态 + 教学角色 + 后台角色 + 注册时间 操作 @@ -72,11 +80,12 @@ {% for item in pager.items %} {{ item.id }} - {{ item.name }}{{ vip_info(item) }} - {% if item.location %} {{ item.location }} {% else %} N/A {% endif %} + {{ item.name }}{{ status_info(item) }} + {{ location_info(item.location) }} {{ gender_info(item.gender) }} - {{ role_info(item) }} - {{ status_info(item) }} + {{ edu_role_info(item) }} + {{ admin_role_info(item) }} + {{ date('Y-m-d H:i',item.create_time) }}
diff --git a/app/Http/Admin/Views/user/search.volt b/app/Http/Admin/Views/user/search.volt index 8bc38c0d..64cc3fd1 100644 --- a/app/Http/Admin/Views/user/search.volt +++ b/app/Http/Admin/Views/user/search.volt @@ -21,8 +21,8 @@
- - + +
diff --git a/app/Library/Helper.php b/app/Library/Helper.php index 7c693427..91d66139 100644 --- a/app/Library/Helper.php +++ b/app/Library/Helper.php @@ -3,6 +3,7 @@ use App\Services\Storage as StorageService; use Koogua\Ip2Region\Searcher as Ip2RegionSearcher; use Phalcon\Di; +use Phalcon\Text; /** * 获取字符长度 @@ -171,7 +172,13 @@ function kg_ci_base_url() */ function kg_ci_img_url($path, $width = 0, $height = 0) { - if (!$path) return ''; + if (empty($path)) { + return ''; + } + + if (Text::startsWith($path, 'http')) { + return $path; + } $storage = new StorageService(); diff --git a/app/Models/Category.php b/app/Models/Category.php index d994ab83..4402e719 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Caches\MaxCategoryId as MaxCategoryIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Category extends Model @@ -118,4 +119,11 @@ class Category extends Model $this->update_time = time(); } + public function afterCreate() + { + $cache = new MaxCategoryIdCache(); + + $cache->rebuild(); + } + } diff --git a/app/Models/Chapter.php b/app/Models/Chapter.php index 79401f5a..9a750f47 100644 --- a/app/Models/Chapter.php +++ b/app/Models/Chapter.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Caches\MaxChapterId as MaxChapterIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Chapter extends Model @@ -223,13 +224,6 @@ class Chapter extends Model } } - public function afterFetch() - { - if (!empty($this->attrs)) { - $this->attrs = json_decode($this->attrs, true); - } - } - public function afterCreate() { if ($this->parent_id > 0) { @@ -256,6 +250,17 @@ class Chapter extends Model break; } } + + $cache = new MaxChapterIdCache(); + + $cache->rebuild(); + } + + public function afterFetch() + { + if (!empty($this->attrs)) { + $this->attrs = json_decode($this->attrs, true); + } } } diff --git a/app/Models/Course.php b/app/Models/Course.php index cf1252a0..f8ada85c 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -2,8 +2,10 @@ namespace App\Models; +use App\Caches\MaxCourseId as MaxCourseIdCache; use App\Services\CourseIndexSyncer; use Phalcon\Mvc\Model\Behavior\SoftDelete; +use Phalcon\Text; class Course extends Model { @@ -261,6 +263,10 @@ class Course extends Model break; } + if (Text::startsWith($this->cover, 'http')) { + $this->cover = self::getCoverPath($this->cover); + } + if (!empty($attrs)) { $this->attrs = kg_json_encode($attrs); } @@ -270,6 +276,10 @@ class Course extends Model { $this->update_time = time(); + if (Text::startsWith($this->cover, 'http')) { + $this->cover = self::getCoverPath($this->cover); + } + if (is_array($this->attrs) && !empty($this->attrs)) { $this->attrs = kg_json_encode($this->attrs); } @@ -278,6 +288,10 @@ class Course extends Model public function afterCreate() { $this->rebuildIndex(); + + $cache = new MaxCourseIdCache(); + + $cache->rebuild(); } public function afterUpdate() @@ -292,6 +306,10 @@ class Course extends Model $this->rating = (float)$this->rating; $this->score = (float)$this->score; + if (!Text::startsWith($this->cover, 'http')) { + $this->cover = kg_ci_img_url($this->cover); + } + if (!empty($this->attrs)) { $this->attrs = json_decode($this->attrs, true); } @@ -304,6 +322,15 @@ class Course extends Model $syncer->addItem($this->id); } + public static function getCoverPath($url) + { + if (Text::startsWith($url, 'http')) { + return parse_url($url, PHP_URL_PATH); + } + + return $url; + } + public static function modelTypes() { return [ diff --git a/app/Models/Help.php b/app/Models/Help.php index f5fd640d..b535b8cd 100644 --- a/app/Models/Help.php +++ b/app/Models/Help.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Caches\MaxHelpId as MaxHelpIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Help extends Model @@ -90,4 +91,11 @@ class Help extends Model $this->update_time = time(); } + public function afterCreate() + { + $cache = new MaxHelpIdCache(); + + $cache->rebuild(); + } + } diff --git a/app/Models/Package.php b/app/Models/Package.php index f20904fe..62a7824e 100644 --- a/app/Models/Package.php +++ b/app/Models/Package.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Caches\MaxPackageId as MaxPackageIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Package extends Model @@ -104,6 +105,13 @@ class Package extends Model $this->update_time = time(); } + public function afterCreate() + { + $cache = new MaxPackageIdCache(); + + $cache->rebuild(); + } + public function afterFetch() { $this->market_price = (float)$this->market_price; diff --git a/app/Models/Page.php b/app/Models/Page.php index 9c87e1cf..9bf00e1b 100644 --- a/app/Models/Page.php +++ b/app/Models/Page.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Caches\MaxPageId as MaxPageIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Page extends Model @@ -76,4 +77,11 @@ class Page extends Model $this->update_time = time(); } + public function afterCreate() + { + $cache = new MaxPageIdCache(); + + $cache->rebuild(); + } + } diff --git a/app/Models/Slide.php b/app/Models/Slide.php index 8260dd9f..4f46fc20 100644 --- a/app/Models/Slide.php +++ b/app/Models/Slide.php @@ -3,6 +3,7 @@ namespace App\Models; use Phalcon\Mvc\Model\Behavior\SoftDelete; +use Phalcon\Text; class Slide extends Model { @@ -111,11 +112,35 @@ class Slide extends Model public function beforeCreate() { $this->create_time = time(); + + if (Text::startsWith($this->cover, 'http')) { + $this->cover = self::getCoverPath($this->cover); + } } public function beforeUpdate() { $this->update_time = time(); + + if (Text::startsWith($this->cover, 'http')) { + $this->cover = self::getCoverPath($this->cover); + } + } + + public function afterFetch() + { + if (!Text::startsWith($this->cover, 'http')) { + $this->cover = kg_ci_img_url($this->cover); + } + } + + public static function getCoverPath($url) + { + if (Text::startsWith($url, 'http')) { + return parse_url($url, PHP_URL_PATH); + } + + return $url; } public static function targetTypes() diff --git a/app/Models/Topic.php b/app/Models/Topic.php index dab754b2..b5ea5077 100644 --- a/app/Models/Topic.php +++ b/app/Models/Topic.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Caches\MaxTopicId as MaxTopicIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Topic extends Model @@ -15,18 +16,11 @@ class Topic extends Model public $id; /** - * 名称 + * 标题 * * @var string */ - public $name; - - /** - * 别名 - * - * @var string - */ - public $alias; + public $title; /** * 简介 @@ -97,4 +91,11 @@ class Topic extends Model $this->update_time = time(); } + public function afterCreate() + { + $cache = new MaxTopicIdCache(); + + $cache->rebuild(); + } + } diff --git a/app/Models/User.php b/app/Models/User.php index 0fc01b24..1b83e22b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Caches\MaxUserId as MaxUserIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; +use Phalcon\Text; class User extends Model { @@ -11,15 +12,15 @@ class User extends Model /** * 性别类型 */ - const GENDER_MALE = 1; // 男 - const GENDER_FEMALE = 2; // 女 - const GENDER_NONE = 3; // 保密 + const GENDER_MALE = 'male'; // 男 + const GENDER_FEMALE = 'female'; // 女 + const GENDER_NONE = 'none'; // 保密 /** * 教学角色 */ - const EDU_ROLE_STUDENT = 1; // 学员 - const EDU_ROLE_TEACHER = 2; // 讲师 + const EDU_ROLE_STUDENT = 'student'; // 学员 + const EDU_ROLE_TEACHER = 'teacher'; // 讲师 /** * 主键编号 @@ -66,7 +67,7 @@ class User extends Model /** * 性别 * - * @var int + * @var string */ public $gender; @@ -167,11 +168,19 @@ class User extends Model public function beforeCreate() { $this->create_time = time(); + + if (Text::startsWith($this->avatar, 'http')) { + $this->avatar = self::getAvatarPath($this->avatar); + } } public function beforeUpdate() { $this->update_time = time(); + + if (Text::startsWith($this->avatar, 'http')) { + $this->avatar = self::getAvatarPath($this->avatar); + } } public function afterCreate() @@ -181,6 +190,22 @@ class User extends Model $cache->rebuild(); } + public function afterFetch() + { + if (!Text::startsWith($this->avatar, 'http')) { + $this->avatar = kg_ci_img_url($this->avatar); + } + } + + public static function getAvatarPath($url) + { + if (Text::startsWith($url, 'http')) { + return parse_url($url, PHP_URL_PATH); + } + + return $url; + } + public static function genderTypes() { return [ diff --git a/app/Services/Frontend/Course/ConsultList.php b/app/Services/Frontend/Course/ConsultList.php index 1217fc34..0e900899 100644 --- a/app/Services/Frontend/Course/ConsultList.php +++ b/app/Services/Frontend/Course/ConsultList.php @@ -74,8 +74,8 @@ class ConsultList extends Service $user = $users[$consult['user_id']] ?? []; $me = [ - 'agreed' => $votes[$consult['id']]['agreed'] ?? false, - 'opposed' => $votes[$consult['id']]['opposed'] ?? false, + 'agreed' => $votes[$consult['id']]['agreed'] ?? 0, + 'opposed' => $votes[$consult['id']]['opposed'] ?? 0, ]; $items[] = [ @@ -116,8 +116,8 @@ class ConsultList extends Service foreach ($votes as $vote) { $result[$vote->consult_id] = [ - 'agreed' => $vote->type == ConsultVoteModel::TYPE_AGREE, - 'opposed' => $vote->type == ConsultVoteModel::TYPE_OPPOSE, + 'agreed' => $vote->type == ConsultVoteModel::TYPE_AGREE ? 1 : 0, + 'opposed' => $vote->type == ConsultVoteModel::TYPE_OPPOSE ? 1 : 0, ]; } diff --git a/app/Services/Frontend/Course/CourseFavorite.php b/app/Services/Frontend/Course/CourseFavorite.php index bcdb38a0..d3e6f81e 100644 --- a/app/Services/Frontend/Course/CourseFavorite.php +++ b/app/Services/Frontend/Course/CourseFavorite.php @@ -14,7 +14,7 @@ class CourseFavorite extends Service use CourseTrait; - public function saveFavorite($id) + public function favorite($id) { $course = $this->checkCourse($id); @@ -41,20 +41,15 @@ class CourseFavorite extends Service } else { - if ($favorite->deleted == 0) { - - $favorite->deleted = 1; - - $course->favorite_count -= 1; - - } else { + if ($favorite->deleted == 1) { $favorite->deleted = 0; $course->favorite_count += 1; + + $favorite->update(); } - $favorite->update(); } $course->update(); @@ -62,6 +57,28 @@ class CourseFavorite extends Service $this->incrUserDailyFavoriteCount($user); } + public function unfavorite($id) + { + $course = $this->checkCourse($id); + + $user = $this->getLoginUser(); + + $favoriteRepo = new CourseFavoriteRepo(); + + $favorite = $favoriteRepo->findCourseFavorite($course->id, $user->id); + + if (!$favorite) return; + + if ($favorite->deleted == 0) { + + $favorite->deleted = 1; + + $course->favorite_count -= 1; + } + + $favorite->update(); + } + protected function incrUserDailyFavoriteCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrFavoriteCount', $this, $user); diff --git a/app/Services/Frontend/Course/CourseInfo.php b/app/Services/Frontend/Course/CourseInfo.php index 50ea3113..a8d6cda7 100644 --- a/app/Services/Frontend/Course/CourseInfo.php +++ b/app/Services/Frontend/Course/CourseInfo.php @@ -33,7 +33,7 @@ class CourseInfo extends Service $result = [ 'id' => $course->id, 'title' => $course->title, - 'cover' => kg_ci_img_url($course->cover), + 'cover' => $course->cover, 'summary' => $course->summary, 'details' => $course->details, 'keywords' => $course->keywords, diff --git a/app/Services/Frontend/Course/CourseList.php b/app/Services/Frontend/Course/CourseList.php index 4d69cb1e..84b80259 100644 --- a/app/Services/Frontend/Course/CourseList.php +++ b/app/Services/Frontend/Course/CourseList.php @@ -68,8 +68,6 @@ class CourseList extends Service 'level' => $course['level'], 'user_count' => $course['user_count'], 'lesson_count' => $course['lesson_count'], - 'review_count' => $course['review_count'], - 'favorite_count' => $course['favorite_count'], ]; } diff --git a/app/Services/Frontend/Course/CourseRelated.php b/app/Services/Frontend/Course/CourseRelated.php index 71f1109b..a9e87a56 100644 --- a/app/Services/Frontend/Course/CourseRelated.php +++ b/app/Services/Frontend/Course/CourseRelated.php @@ -15,9 +15,11 @@ class CourseRelated extends Service { $course = $this->checkCourse($id); - $listCache = new CourseRelatedListCache(); + $cache = new CourseRelatedListCache(); - return $listCache->get($course->id); + $result = $cache->get($course->id); + + return $result ?: []; } } diff --git a/app/Services/Frontend/Course/PackageList.php b/app/Services/Frontend/Course/PackageList.php index 0d43216e..98497772 100644 --- a/app/Services/Frontend/Course/PackageList.php +++ b/app/Services/Frontend/Course/PackageList.php @@ -2,11 +2,11 @@ namespace App\Services\Frontend\Course; -use App\Models\Package as PackageModel; -use App\Repos\Course as CourseRepo; -use App\Repos\Package as PackageRepo; +use App\Caches\CoursePackageList as CoursePackageListCache; +use App\Caches\PackageCourseList as PackageCourseListCache; use App\Services\Frontend\CourseTrait; use App\Services\Frontend\Service; +use Yansongda\Supports\Collection; class PackageList extends Service { @@ -15,77 +15,33 @@ class PackageList extends Service public function getPackages($id) { - $course = $this->checkCourse($id); + $course = $this->checkCourseCache($id); - $courseRepo = new CourseRepo(); + $cache = new CoursePackageListCache(); - $packages = $courseRepo->findPackages($course->id); + /** + * @var Collection $packages + */ + $packages = $cache->get($course->id); if ($packages->count() == 0) { return []; } - return $this->handlePackages($packages); - } - - /** - * @param PackageModel[] $packages - * @return array - */ - protected function handlePackages($packages) - { - $result = []; - - foreach ($packages as $package) { - - $courses = $this->getPackageCourses($package->id); - - $result[] = [ - 'id' => $package->id, - 'title' => $package->title, - 'market_price' => (float)$package->market_price, - 'vip_price' => (float)$package->vip_price, - 'courses' => $courses, - ]; - } - - return $result; - } - - protected function getPackageCourses($packageId) - { - $packageRepo = new PackageRepo(); - - $courses = $packageRepo->findCourses($packageId); - - if ($courses->count() == 0) { - return []; - } + $cache = new PackageCourseListCache(); $result = []; - $baseUrl = kg_ci_base_url(); + foreach ($packages->toArray() as $package) { - foreach ($courses as $course) { + /** + * @var Collection $courses + */ + $courses = $cache->get($package['id']); - $course->cover = $baseUrl . $course->cover; + $package['courses'] = $courses->count() > 0 ? $courses->toArray() : []; - $result[] = [ - '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'], - 'score' => (float)$course['score'], - 'model' => $course->model, - 'level' => $course->level, - 'user_count' => $course->user_count, - 'lesson_count' => $course->lesson_count, - 'review_count' => $course->review_count, - 'favorite_count' => $course->favorite_count, - ]; + $result[] = $package; } return $result; diff --git a/app/Services/Frontend/Course/ReviewList.php b/app/Services/Frontend/Course/ReviewList.php index e735e389..4ae0171f 100644 --- a/app/Services/Frontend/Course/ReviewList.php +++ b/app/Services/Frontend/Course/ReviewList.php @@ -73,8 +73,8 @@ class ReviewList extends Service $user = $users[$review['user_id']] ?? []; $me = [ - 'agreed' => $votes[$review['id']]['agreed'] ?? false, - 'opposed' => $votes[$review['id']]['opposed'] ?? false, + 'agreed' => $votes[$review['id']]['agreed'] ?? 0, + 'opposed' => $votes[$review['id']]['opposed'] ?? 0, ]; $items[] = [ @@ -112,8 +112,8 @@ class ReviewList extends Service foreach ($votes as $vote) { $result[$vote->review_id] = [ - 'agreed' => $vote->type == ReviewVoteModel::TYPE_AGREE, - 'opposed' => $vote->type == ReviewVoteModel::TYPE_OPPOSE, + 'agreed' => $vote->type == ReviewVoteModel::TYPE_AGREE ? 1 : 0, + 'opposed' => $vote->type == ReviewVoteModel::TYPE_OPPOSE ? 1 : 0, ]; } diff --git a/app/Services/Frontend/Course/TeacherList.php b/app/Services/Frontend/Course/TeacherList.php index c737245b..57639423 100644 --- a/app/Services/Frontend/Course/TeacherList.php +++ b/app/Services/Frontend/Course/TeacherList.php @@ -15,9 +15,11 @@ class TeacherList extends Service { $course = $this->checkCourse($id); - $listCache = new CourseTeacherListCache(); + $cache = new CourseTeacherListCache(); - return $listCache->get($course->id); + $result = $cache->get($course->id); + + return $result ?: []; } } diff --git a/app/Services/Frontend/Package/CourseList.php b/app/Services/Frontend/Package/CourseList.php new file mode 100644 index 00000000..3ff43f13 --- /dev/null +++ b/app/Services/Frontend/Package/CourseList.php @@ -0,0 +1,12 @@ +checkPackageCache($id); + + return $this->handlePackage($package); + } + + protected function handlePackage(PackageModel $package) + { + return [ + 'id' => $package->id, + 'title' => $package->title, + 'summary' => $package->summary, + 'market_price' => $package->market_price, + 'vip_price' => $package->vip_price, + 'course_count' => $package->course_count, + ]; + } + +} diff --git a/app/Services/Frontend/PackageTrait.php b/app/Services/Frontend/PackageTrait.php new file mode 100644 index 00000000..8f23dea6 --- /dev/null +++ b/app/Services/Frontend/PackageTrait.php @@ -0,0 +1,24 @@ +checkPackage($id); + } + + public function checkPackageCache($id) + { + $validator = new PackageValidator(); + + return $validator->checkPackageCache($id); + } + +} diff --git a/app/Services/Frontend/Topic/CourseList.php b/app/Services/Frontend/Topic/CourseList.php new file mode 100644 index 00000000..a9a28742 --- /dev/null +++ b/app/Services/Frontend/Topic/CourseList.php @@ -0,0 +1,25 @@ +checkTopicCache($id); + + $cache = new TopicCourseListCache(); + + $result = $cache->get($topic->id); + + return $result ?: []; + } + +} diff --git a/app/Services/Frontend/Topic/TopicInfo.php b/app/Services/Frontend/Topic/TopicInfo.php new file mode 100644 index 00000000..a18f728f --- /dev/null +++ b/app/Services/Frontend/Topic/TopicInfo.php @@ -0,0 +1,31 @@ +checkTopicCache($id); + + return $this->handleTopic($topic); + } + + protected function handleTopic(TopicModel $topic) + { + return [ + 'id' => $topic->id, + 'title' => $topic->title, + 'summary' => $topic->about, + 'course_count' => $topic->course_count, + ]; + } + +} diff --git a/app/Services/Frontend/TopicTrait.php b/app/Services/Frontend/TopicTrait.php new file mode 100644 index 00000000..44144953 --- /dev/null +++ b/app/Services/Frontend/TopicTrait.php @@ -0,0 +1,24 @@ +checkTopic($id); + } + + public function checkTopicCache($id) + { + $validator = new TopicValidator(); + + return $validator->checkTopicCache($id); + } + +} diff --git a/app/Validators/Account.php b/app/Validators/Account.php index 96c8c9dc..7d4c2d56 100644 --- a/app/Validators/Account.php +++ b/app/Validators/Account.php @@ -13,6 +13,19 @@ use App\Repos\User as UserRepo; class Account extends Validator { + public function checkAccount($id) + { + $accountRepo = new AccountRepo(); + + $account = $accountRepo->findById($id); + + if (!$account) { + throw new BadRequestException('account.not_found'); + } + + return $account; + } + public function checkPhone($phone) { if (!CommonValidator::phone($phone)) { diff --git a/app/Validators/Category.php b/app/Validators/Category.php index 2b3f996a..73fdfed7 100644 --- a/app/Validators/Category.php +++ b/app/Validators/Category.php @@ -5,11 +5,17 @@ namespace App\Validators; use App\Caches\Category as CategoryCache; use App\Caches\MaxCategoryId as MaxCategoryIdCache; use App\Exceptions\BadRequest as BadRequestException; +use App\Models\Category as CategoryModel; use App\Repos\Category as CategoryRepo; class Category extends Validator { + /** + * @param int $id + * @return CategoryModel + * @throws BadRequestException + */ public function checkCategoryCache($id) { $id = intval($id); diff --git a/app/Validators/Chapter.php b/app/Validators/Chapter.php index 845814e4..5a79a038 100644 --- a/app/Validators/Chapter.php +++ b/app/Validators/Chapter.php @@ -5,6 +5,7 @@ namespace App\Validators; use App\Caches\Chapter as ChapterCache; use App\Caches\MaxChapterId as MaxChapterIdCache; use App\Exceptions\BadRequest as BadRequestException; +use App\Models\Chapter as ChapterModel; use App\Models\Course as CourseModel; use App\Repos\Chapter as ChapterRepo; use App\Repos\Course as CourseRepo; @@ -12,6 +13,11 @@ use App\Repos\Course as CourseRepo; class Chapter extends Validator { + /** + * @param int $id + * @return ChapterModel + * @throws BadRequestException + */ public function checkChapterCache($id) { $id = intval($id); diff --git a/app/Validators/Course.php b/app/Validators/Course.php index d7b1eeb5..bf109222 100644 --- a/app/Validators/Course.php +++ b/app/Validators/Course.php @@ -12,6 +12,11 @@ use App\Repos\Course as CourseRepo; class Course extends Validator { + /** + * @param int $id + * @return CourseModel + * @throws BadRequestException + */ public function checkCourseCache($id) { $id = intval($id); @@ -81,7 +86,7 @@ class Course extends Validator throw new BadRequestException('course.invalid_cover'); } - return parse_url($value, PHP_URL_PATH); + return $value; } public function checkTitle($title) diff --git a/app/Validators/Help.php b/app/Validators/Help.php index 624302ec..ddeab14b 100644 --- a/app/Validators/Help.php +++ b/app/Validators/Help.php @@ -5,11 +5,17 @@ namespace App\Validators; use App\Caches\Help as HelpCache; use App\Caches\MaxHelpId as MaxHelpIdCache; use App\Exceptions\BadRequest as BadRequestException; +use App\Models\Help as HelpModel; use App\Repos\Help as HelpRepo; class Help extends Validator { + /** + * @param int $id + * @return HelpModel + * @throws BadRequestException + */ public function checkHelpCache($id) { $id = intval($id); diff --git a/app/Validators/Package.php b/app/Validators/Package.php index 85f3ffb0..0a979e80 100644 --- a/app/Validators/Package.php +++ b/app/Validators/Package.php @@ -5,11 +5,17 @@ namespace App\Validators; use App\Caches\MaxPackageId as MaxPackageIdCache; use App\Caches\Package as PackageCache; use App\Exceptions\BadRequest as BadRequestException; +use App\Models\Package as PackageModel; use App\Repos\Package as PackageRepo; class Package extends Validator { + /** + * @param int $id + * @return PackageModel + * @throws BadRequestException + */ public function checkPackageCache($id) { $id = intval($id); diff --git a/app/Validators/Page.php b/app/Validators/Page.php index 0283e5d5..8d54a12f 100644 --- a/app/Validators/Page.php +++ b/app/Validators/Page.php @@ -5,11 +5,17 @@ namespace App\Validators; use App\Caches\MaxPageId as MaxPageIdCache; use App\Caches\Page as PageCache; use App\Exceptions\BadRequest as BadRequestException; +use App\Models\Page as PageModel; use App\Repos\Page as PageRepo; class Page extends Validator { + /** + * @param int $id + * @return PageModel + * @throws BadRequestException + */ public function checkPageCache($id) { $id = intval($id); diff --git a/app/Validators/Slide.php b/app/Validators/Slide.php index 2d1ef007..66aa24ed 100644 --- a/app/Validators/Slide.php +++ b/app/Validators/Slide.php @@ -63,7 +63,7 @@ class Slide extends Validator throw new BadRequestException('slide.invalid_cover'); } - return parse_url($value, PHP_URL_PATH); + return $value; } public function checkTarget($target) diff --git a/app/Validators/Topic.php b/app/Validators/Topic.php index 51d6eb55..e5231c6f 100644 --- a/app/Validators/Topic.php +++ b/app/Validators/Topic.php @@ -5,11 +5,17 @@ namespace App\Validators; use App\Caches\MaxTopicId as MaxTopicIdCache; use App\Caches\Topic as TopicCache; use App\Exceptions\BadRequest as BadRequestException; +use App\Models\Topic as TopicModel; use App\Repos\Topic as TopicRepo; class Topic extends Validator { + /** + * @param int $id + * @return TopicModel + * @throws BadRequestException + */ public function checkTopicCache($id) { $id = intval($id); diff --git a/app/Validators/User.php b/app/Validators/User.php index 8b0f12c9..f503b92d 100644 --- a/app/Validators/User.php +++ b/app/Validators/User.php @@ -14,6 +14,11 @@ use App\Services\Auth\Admin as AdminAuth; class User extends Validator { + /** + * @param int $id + * @return UserModel + * @throws BadRequestException + */ public function checkUserCache($id) { $id = intval($id);