From a51189636e90e07ac4da9ad72b25ce236da36f45 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Tue, 21 Apr 2020 19:23:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Caches/Chapter.php | 2 +- app/Caches/ChapterCounter.php | 2 ++ app/Caches/Course.php | 8 ++--- app/Caches/CourseCategoryList.php | 2 +- app/Caches/CourseChapterList.php | 2 +- app/Caches/CourseCounter.php | 2 ++ app/Caches/CoursePackageList.php | 2 +- app/Caches/CourseRelatedList.php | 11 +------ app/Caches/CourseTeacherList.php | 7 +---- app/Caches/CourseUser.php | 8 ++--- app/Caches/Help.php | 31 +++++++++++++++++++ app/Caches/MaxHelpId.php | 29 +++++++++++++++++ app/Caches/MaxPageId.php | 29 +++++++++++++++++ app/Caches/Page.php | 31 +++++++++++++++++++ app/Http/Admin/Services/Category.php | 19 ++++++++---- app/Http/Admin/Services/Chapter.php | 17 ++++++++-- app/Http/Admin/Services/Course.php | 19 ++++++++++++ app/Http/Admin/Services/Help.php | 21 +++++++++++++ app/Http/Admin/Services/Nav.php | 4 --- app/Http/Admin/Services/Package.php | 5 +++ app/Http/Admin/Services/Page.php | 21 +++++++++++++ app/Http/Admin/Services/Topic.php | 5 +++ app/Http/Admin/Views/course/edit_basic.volt | 4 +-- app/Http/Admin/Views/slide/edit.volt | 4 +-- app/Models/Course.php | 4 --- app/Models/Help.php | 14 +++++++++ app/Models/Role.php | 2 +- app/Models/Slide.php | 7 ----- app/Models/Task.php | 2 +- app/Models/User.php | 5 +-- app/Services/Frontend/UserTrait.php | 7 +++++ app/Services/Mailer/Verify.php | 6 ++-- app/Services/Smser/Verify.php | 6 ++-- .../{VerifyCode.php => Verification.php} | 2 +- app/Validators/Category.php | 28 +++++++++++++++++ app/Validators/Chapter.php | 8 ++--- app/Validators/Help.php | 28 +++++++++++++++++ app/Validators/Package.php | 28 +++++++++++++++++ app/Validators/Page.php | 28 +++++++++++++++++ app/Validators/Security.php | 16 +++++----- app/Validators/Topic.php | 28 +++++++++++++++++ 41 files changed, 422 insertions(+), 82 deletions(-) create mode 100644 app/Caches/Help.php create mode 100644 app/Caches/MaxHelpId.php create mode 100644 app/Caches/MaxPageId.php create mode 100644 app/Caches/Page.php rename app/Services/{VerifyCode.php => Verification.php} (97%) diff --git a/app/Caches/Chapter.php b/app/Caches/Chapter.php index 2b8cb436..b280704d 100644 --- a/app/Caches/Chapter.php +++ b/app/Caches/Chapter.php @@ -7,7 +7,7 @@ use App\Repos\Chapter as ChapterRepo; class Chapter extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/ChapterCounter.php b/app/Caches/ChapterCounter.php index 3d928275..a07b39a7 100644 --- a/app/Caches/ChapterCounter.php +++ b/app/Caches/ChapterCounter.php @@ -25,6 +25,8 @@ class ChapterCounter extends Counter $chapter = $chapterRepo->findById($id); + if (!$chapter) return null; + return [ 'user_count' => $chapter->user_count, 'lesson_count' => $chapter->lesson_count, diff --git a/app/Caches/Course.php b/app/Caches/Course.php index b29c555f..6071c305 100644 --- a/app/Caches/Course.php +++ b/app/Caches/Course.php @@ -7,7 +7,7 @@ use App\Repos\Course as CourseRepo; class Course extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -25,11 +25,7 @@ class Course extends Cache $course = $courseRepo->findById($id); - if (!$course) return null; - - $course->cover = kg_ci_img_url($course->cover); - - return $course; + return $course ?: null; } } diff --git a/app/Caches/CourseCategoryList.php b/app/Caches/CourseCategoryList.php index fbaf2a0e..65ac5080 100644 --- a/app/Caches/CourseCategoryList.php +++ b/app/Caches/CourseCategoryList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CourseCategoryList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/CourseChapterList.php b/app/Caches/CourseChapterList.php index 21203677..74f1082d 100644 --- a/app/Caches/CourseChapterList.php +++ b/app/Caches/CourseChapterList.php @@ -9,7 +9,7 @@ use Phalcon\Mvc\Model\Resultset; class CourseChapterList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/CourseCounter.php b/app/Caches/CourseCounter.php index 8270ff3f..793cfc18 100644 --- a/app/Caches/CourseCounter.php +++ b/app/Caches/CourseCounter.php @@ -25,6 +25,8 @@ class CourseCounter extends Counter $course = $courseRepo->findById($id); + if (!$course) return null; + return [ 'user_count' => $course->user_count, 'lesson_count' => $course->lesson_count, diff --git a/app/Caches/CoursePackageList.php b/app/Caches/CoursePackageList.php index f3574e5f..3462825d 100644 --- a/app/Caches/CoursePackageList.php +++ b/app/Caches/CoursePackageList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CoursePackageList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { diff --git a/app/Caches/CourseRelatedList.php b/app/Caches/CourseRelatedList.php index dd85a83c..9c04c5fb 100644 --- a/app/Caches/CourseRelatedList.php +++ b/app/Caches/CourseRelatedList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CourseRelatedList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -41,27 +41,18 @@ class CourseRelatedList extends Cache { $result = []; - $baseUrl = kg_ci_base_url(); - foreach ($courses as $course) { - $course->cover = $baseUrl . $course->cover; - $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, ]; } diff --git a/app/Caches/CourseTeacherList.php b/app/Caches/CourseTeacherList.php index 3302a866..051160cf 100644 --- a/app/Caches/CourseTeacherList.php +++ b/app/Caches/CourseTeacherList.php @@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo; class CourseTeacherList extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -41,12 +41,7 @@ class CourseTeacherList extends Cache { $result = []; - $baseUrl = kg_ci_base_url(); - foreach ($users as $user) { - - $user->avatar = $baseUrl . $user->avatar; - $result[] = [ 'id' => $user->id, 'name' => $user->name, diff --git a/app/Caches/CourseUser.php b/app/Caches/CourseUser.php index 1fedc9b4..433a9425 100644 --- a/app/Caches/CourseUser.php +++ b/app/Caches/CourseUser.php @@ -7,7 +7,7 @@ use App\Repos\CourseUser as CourseUserRepo; class CourseUser extends Cache { - protected $lifetime = 7 * 86400; + protected $lifetime = 1 * 86400; public function getLifetime() { @@ -33,11 +33,7 @@ class CourseUser extends Cache $courseUser = $courseUserRepo->findCourseUser($courseId, $userId); - if (!$courseUser) { - return new \stdClass(); - } - - return $courseUser; + return $courseUser ?: null; } } diff --git a/app/Caches/Help.php b/app/Caches/Help.php new file mode 100644 index 00000000..ecea52c5 --- /dev/null +++ b/app/Caches/Help.php @@ -0,0 +1,31 @@ +lifetime; + } + + public function getKey($id = null) + { + return "help:{$id}"; + } + + public function getContent($id = null) + { + $helpRepo = new HelpRepo(); + + $help = $helpRepo->findById($id); + + return $help ?: null; + } + +} diff --git a/app/Caches/MaxHelpId.php b/app/Caches/MaxHelpId.php new file mode 100644 index 00000000..ae43642d --- /dev/null +++ b/app/Caches/MaxHelpId.php @@ -0,0 +1,29 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'max_help_id'; + } + + public function getContent($id = null) + { + $help = HelpModel::findFirst(['order' => 'id DESC']); + + return $help->id ?? 0; + } + +} diff --git a/app/Caches/MaxPageId.php b/app/Caches/MaxPageId.php new file mode 100644 index 00000000..cfed90f3 --- /dev/null +++ b/app/Caches/MaxPageId.php @@ -0,0 +1,29 @@ +lifetime; + } + + public function getKey($id = null) + { + return 'max_page_id'; + } + + public function getContent($id = null) + { + $page = PageModel::findFirst(['order' => 'id DESC']); + + return $page->id ?? 0; + } + +} diff --git a/app/Caches/Page.php b/app/Caches/Page.php new file mode 100644 index 00000000..2c9c31de --- /dev/null +++ b/app/Caches/Page.php @@ -0,0 +1,31 @@ +lifetime; + } + + public function getKey($id = null) + { + return "page:{$id}"; + } + + public function getContent($id = null) + { + $pageRepo = new PageRepo(); + + $page = $pageRepo->findById($id); + + return $page ?: null; + } + +} diff --git a/app/Http/Admin/Services/Category.php b/app/Http/Admin/Services/Category.php index faae26c5..193e2874 100644 --- a/app/Http/Admin/Services/Category.php +++ b/app/Http/Admin/Services/Category.php @@ -5,6 +5,7 @@ namespace App\Http\Admin\Services; use App\Caches\Category as CategoryCache; use App\Caches\CategoryList as CategoryListCache; use App\Caches\CategoryTreeList as CategoryTreeListCache; +use App\Caches\MaxCategoryId as MaxCategoryIdCache; use App\Models\Category as CategoryModel; use App\Repos\Category as CategoryRepo; use App\Validators\Category as CategoryValidator; @@ -172,23 +173,29 @@ class Category extends Service } $childCount = $categoryRepo->countChildCategories($category->id); + $category->child_count = $childCount; + $category->update(); } protected function rebuildCategoryCache(CategoryModel $category) { - $itemCache = new CategoryCache(); + $cache = new CategoryCache(); - $itemCache->rebuild($category->id); + $cache->rebuild($category->id); - $listCache = new CategoryListCache(); + $cache = new CategoryListCache(); - $listCache->rebuild(); + $cache->rebuild(); - $treeListCache = new CategoryTreeListCache(); + $cache = new CategoryTreeListCache(); - $treeListCache->rebuild(); + $cache->rebuild(); + + $cache = new MaxCategoryIdCache(); + + $cache->rebuild(); } protected function enableChildCategories($parentId) diff --git a/app/Http/Admin/Services/Chapter.php b/app/Http/Admin/Services/Chapter.php index 98cd2406..673fba5c 100644 --- a/app/Http/Admin/Services/Chapter.php +++ b/app/Http/Admin/Services/Chapter.php @@ -2,7 +2,9 @@ namespace App\Http\Admin\Services; +use App\Caches\Chapter as ChapterCache; use App\Caches\CourseChapterList as CourseChapterListCache; +use App\Caches\MaxChapterId as MaxChapterIdCache; use App\Models\Chapter as ChapterModel; use App\Models\Course as CourseModel; use App\Repos\Chapter as ChapterRepo; @@ -38,7 +40,9 @@ class Chapter extends Service $data = []; - $data['course_id'] = $validator->checkCourseId($post['course_id']); + $course = $validator->checkCourse($post['course_id']); + + $data['course_id'] = $course->id; $data['title'] = $validator->checkTitle($post['title']); $data['summary'] = $validator->checkSummary($post['summary']); $data['free'] = $validator->checkFreeStatus($post['free']); @@ -46,7 +50,8 @@ class Chapter extends Service $chapterRepo = new ChapterRepo(); if (isset($post['parent_id'])) { - $data['parent_id'] = $validator->checkParentId($post['parent_id']); + $parent = $validator->checkParent($post['parent_id']); + $data['parent_id'] = $parent->id; $data['priority'] = $chapterRepo->maxLessonPriority($post['parent_id']); } else { $data['priority'] = $chapterRepo->maxChapterPriority($post['course_id']); @@ -175,9 +180,17 @@ class Chapter extends Service protected function rebuildChapterCache(ChapterModel $chapter) { + $cache = new ChapterCache(); + + $cache->rebuild($chapter->id); + $cache = new CourseChapterListCache(); $cache->rebuild($chapter->course_id); + + $cache = new MaxChapterIdCache(); + + $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Services/Course.php b/app/Http/Admin/Services/Course.php index 12b7c970..8881c642 100644 --- a/app/Http/Admin/Services/Course.php +++ b/app/Http/Admin/Services/Course.php @@ -3,9 +3,11 @@ namespace App\Http\Admin\Services; use App\Builders\CourseList as CourseListBuilder; +use App\Caches\Course as CourseCache; use App\Caches\CourseCategoryList as CourseCategoryListCache; use App\Caches\CourseRelatedList as CourseRelatedListCache; use App\Caches\CourseTeacherList as CourseTeacherListCache; +use App\Caches\MaxCourseId as MaxCourseIdCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\Course as CourseModel; use App\Models\CourseCategory as CourseCategoryModel; @@ -67,6 +69,8 @@ class Course extends Service $course->create($data); + $this->rebuildCourseCache($course); + return $course; } @@ -148,6 +152,8 @@ class Course extends Service $course->update(); + $this->rebuildCourseCache($course); + return $course; } @@ -159,6 +165,8 @@ class Course extends Service $course->update(); + $this->rebuildCourseCache($course); + return $course; } @@ -310,6 +318,17 @@ class Course extends Service return $validator->checkCourse($id); } + protected function rebuildCourseCache(CourseModel $course) + { + $cache = new CourseCache(); + + $cache->rebuild($course->id); + + $cache = new MaxCourseIdCache(); + + $cache->rebuild(); + } + protected function saveTeachers(CourseModel $course, $teacherIds) { $courseRepo = new CourseRepo(); diff --git a/app/Http/Admin/Services/Help.php b/app/Http/Admin/Services/Help.php index 2ed88cdf..1ee61d43 100644 --- a/app/Http/Admin/Services/Help.php +++ b/app/Http/Admin/Services/Help.php @@ -2,6 +2,8 @@ namespace App\Http\Admin\Services; +use App\Caches\Help as HelpCache; +use App\Caches\MaxHelpId as MaxHelpIdCache; use App\Models\Help as HelpModel; use App\Repos\Help as HelpRepo; use App\Validators\Help as HelpValidator; @@ -42,6 +44,8 @@ class Help extends Service $help->create($data); + $this->rebuildHelpCache($help); + return $help; } @@ -73,6 +77,8 @@ class Help extends Service $help->update($data); + $this->rebuildHelpCache($help); + return $help; } @@ -84,6 +90,8 @@ class Help extends Service $help->update(); + $this->rebuildHelpCache($help); + return $help; } @@ -95,9 +103,22 @@ class Help extends Service $help->update(); + $this->rebuildHelpCache($help); + return $help; } + protected function rebuildHelpCache(HelpModel $help) + { + $cache = new HelpCache(); + + $cache->rebuild($help->id); + + $cache = new MaxHelpIdCache(); + + $cache->rebuild(); + } + protected function findOrFail($id) { $validator = new HelpValidator(); diff --git a/app/Http/Admin/Services/Nav.php b/app/Http/Admin/Services/Nav.php index bfe3634a..8fcd451d 100644 --- a/app/Http/Admin/Services/Nav.php +++ b/app/Http/Admin/Services/Nav.php @@ -91,7 +91,6 @@ class Nav extends Service $nav->update(); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; @@ -141,7 +140,6 @@ class Nav extends Service $nav->update($data); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; @@ -160,7 +158,6 @@ class Nav extends Service $nav->update(); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; @@ -175,7 +172,6 @@ class Nav extends Service $nav->update(); $this->updateNavStats($nav); - $this->rebuildNavCache(); return $nav; diff --git a/app/Http/Admin/Services/Package.php b/app/Http/Admin/Services/Package.php index e510fc7c..ac34aa96 100644 --- a/app/Http/Admin/Services/Package.php +++ b/app/Http/Admin/Services/Package.php @@ -2,6 +2,7 @@ namespace App\Http\Admin\Services; +use App\Caches\MaxPackageId as MaxPackageIdCache; use App\Caches\Package as PackageCache; use App\Caches\PackageCourseList as PackageCourseListCache; use App\Library\Paginator\Query as PagerQuery; @@ -239,6 +240,10 @@ class Package extends Service $cache = new PackageCourseListCache(); $cache->rebuild($package->id); + + $cache = new MaxPackageIdCache(); + + $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Services/Page.php b/app/Http/Admin/Services/Page.php index 434ff177..9e53a344 100644 --- a/app/Http/Admin/Services/Page.php +++ b/app/Http/Admin/Services/Page.php @@ -2,6 +2,8 @@ namespace App\Http\Admin\Services; +use App\Caches\MaxPageId as MaxPageIdCache; +use App\Caches\Page as PageCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\Page as PageModel; use App\Repos\Page as PageRepo; @@ -48,6 +50,8 @@ class Page extends Service $page->create($data); + $this->rebuildPageCache($page); + return $page; } @@ -75,6 +79,8 @@ class Page extends Service $page->update($data); + $this->rebuildPageCache($page); + return $page; } @@ -86,6 +92,8 @@ class Page extends Service $page->update(); + $this->rebuildPageCache($page); + return $page; } @@ -97,9 +105,22 @@ class Page extends Service $page->update(); + $this->rebuildPageCache($page); + return $page; } + protected function rebuildPageCache(PageModel $help) + { + $cache = new PageCache(); + + $cache->rebuild($help->id); + + $cache = new MaxPageIdCache(); + + $cache->rebuild(); + } + protected function findOrFail($id) { $validator = new PageValidator(); diff --git a/app/Http/Admin/Services/Topic.php b/app/Http/Admin/Services/Topic.php index ac8289bf..bd4719fc 100644 --- a/app/Http/Admin/Services/Topic.php +++ b/app/Http/Admin/Services/Topic.php @@ -2,6 +2,7 @@ namespace App\Http\Admin\Services; +use App\Caches\MaxTopicId as MaxTopicIdCache; use App\Caches\Topic as TopicCache; use App\Caches\TopicCourseList as TopicCourseListCache; use App\Library\Paginator\Query as PagerQuery; @@ -198,6 +199,10 @@ class Topic extends Service $cache = new TopicCourseListCache(); $cache->rebuild($topic->id); + + $cache = new MaxTopicIdCache(); + + $cache->rebuild(); } protected function findOrFail($id) diff --git a/app/Http/Admin/Views/course/edit_basic.volt b/app/Http/Admin/Views/course/edit_basic.volt index 49cbe2de..4109537a 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 67a6e390..bdefcd69 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/Models/Course.php b/app/Models/Course.php index 8a814af0..cf1252a0 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -292,10 +292,6 @@ class Course extends Model $this->rating = (float)$this->rating; $this->score = (float)$this->score; - if (!empty($this->cover)) { - $this->cover = kg_ci_img_url($this->cover); - } - if (!empty($this->attrs)) { $this->attrs = json_decode($this->attrs, true); } diff --git a/app/Models/Help.php b/app/Models/Help.php index 567caa16..f5fd640d 100644 --- a/app/Models/Help.php +++ b/app/Models/Help.php @@ -2,6 +2,8 @@ namespace App\Models; +use Phalcon\Mvc\Model\Behavior\SoftDelete; + class Help extends Model { @@ -66,6 +68,18 @@ class Help extends Model return 'kg_help'; } + public function initialize() + { + parent::initialize(); + + $this->addBehavior( + new SoftDelete([ + 'field' => 'deleted', + 'value' => 1, + ]) + ); + } + public function beforeCreate() { $this->create_time = time(); diff --git a/app/Models/Role.php b/app/Models/Role.php index 6bc30885..e85e89fa 100644 --- a/app/Models/Role.php +++ b/app/Models/Role.php @@ -122,7 +122,7 @@ class Role extends Model public function afterFetch() { if (!empty($this->routes)) { - $this->routes = json_decode($this->routes); + $this->routes = json_decode($this->routes, true); } } diff --git a/app/Models/Slide.php b/app/Models/Slide.php index 439c6e03..8260dd9f 100644 --- a/app/Models/Slide.php +++ b/app/Models/Slide.php @@ -118,13 +118,6 @@ class Slide extends Model $this->update_time = time(); } - public function afterFetch() - { - if (!empty($this->cover)) { - $this->cover = kg_ci_img_url($this->cover); - } - } - public static function targetTypes() { return [ diff --git a/app/Models/Task.php b/app/Models/Task.php index 9f89d694..3b69dbc2 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -111,7 +111,7 @@ class Task extends Model { $this->update_time = time(); - if (!empty($this->item_info)) { + if (is_array($this->item_info) && !empty($this->item_info)) { $this->item_info = kg_json_encode($this->item_info); } } diff --git a/app/Models/User.php b/app/Models/User.php index cd370919..0fc01b24 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -176,8 +176,9 @@ class User extends Model public function afterCreate() { - $maxUserIdCache = new MaxUserIdCache(); - $maxUserIdCache->rebuild(); + $cache = new MaxUserIdCache(); + + $cache->rebuild(); } public static function genderTypes() diff --git a/app/Services/Frontend/UserTrait.php b/app/Services/Frontend/UserTrait.php index b75c79b1..03d3fa4a 100644 --- a/app/Services/Frontend/UserTrait.php +++ b/app/Services/Frontend/UserTrait.php @@ -14,4 +14,11 @@ trait UserTrait return $validator->checkUser($id); } + public function checkUserCache($id) + { + $validator = new UserValidator(); + + return $validator->checkUserCache($id); + } + } diff --git a/app/Services/Mailer/Verify.php b/app/Services/Mailer/Verify.php index 1d04c723..8a4e863d 100644 --- a/app/Services/Mailer/Verify.php +++ b/app/Services/Mailer/Verify.php @@ -3,7 +3,7 @@ namespace App\Services\Mailer; use App\Services\Mailer; -use App\Services\VerifyCode; +use App\Services\Verification; class Verify extends Mailer { @@ -18,11 +18,11 @@ class Verify extends Mailer $message = $this->manager->createMessage(); - $verifyCode = new VerifyCode(); + $verification = new Verification(); $minutes = 5; - $code = $verifyCode->getSmsCode($email, 60 * $minutes); + $code = $verification->getSmsCode($email, 60 * $minutes); $subject = '邮件验证码'; diff --git a/app/Services/Smser/Verify.php b/app/Services/Smser/Verify.php index dc4c5f01..89d915f2 100644 --- a/app/Services/Smser/Verify.php +++ b/app/Services/Smser/Verify.php @@ -3,7 +3,7 @@ namespace App\Services\Smser; use App\Services\Smser; -use App\Services\VerifyCode; +use App\Services\Verification; class Verify extends Smser { @@ -16,11 +16,11 @@ class Verify extends Smser */ public function handle($phone) { - $verifyCode = new VerifyCode(); + $verification = new Verification(); $minutes = 5; - $code = $verifyCode->getSmsCode($phone, 60 * $minutes); + $code = $verification->getSmsCode($phone, 60 * $minutes); $templateId = $this->getTemplateId($this->templateCode); diff --git a/app/Services/VerifyCode.php b/app/Services/Verification.php similarity index 97% rename from app/Services/VerifyCode.php rename to app/Services/Verification.php index e32b1fed..b06e6016 100644 --- a/app/Services/VerifyCode.php +++ b/app/Services/Verification.php @@ -7,7 +7,7 @@ use App\Services\Mailer\Verify as VerifyMailer; use App\Services\Smser\Verify as VerifySmser; use Phalcon\Text; -class VerifyCode extends Service +class Verification extends Service { /** diff --git a/app/Validators/Category.php b/app/Validators/Category.php index 6d761b66..2b3f996a 100644 --- a/app/Validators/Category.php +++ b/app/Validators/Category.php @@ -2,12 +2,40 @@ namespace App\Validators; +use App\Caches\Category as CategoryCache; +use App\Caches\MaxCategoryId as MaxCategoryIdCache; use App\Exceptions\BadRequest as BadRequestException; use App\Repos\Category as CategoryRepo; class Category extends Validator { + public function checkCategoryCache($id) + { + $id = intval($id); + + $maxCategoryIdCache = new MaxCategoryIdCache(); + + $maxCategoryId = $maxCategoryIdCache->get(); + + /** + * 防止缓存穿透 + */ + if ($id < 1 || $id > $maxCategoryId) { + throw new BadRequestException('category.not_found'); + } + + $categoryCache = new CategoryCache(); + + $category = $categoryCache->get($id); + + if (!$category) { + throw new BadRequestException('category.not_found'); + } + + return $category; + } + public function checkCategory($id) { $categoryRepo = new CategoryRepo(); diff --git a/app/Validators/Chapter.php b/app/Validators/Chapter.php index a4d23898..845814e4 100644 --- a/app/Validators/Chapter.php +++ b/app/Validators/Chapter.php @@ -51,7 +51,7 @@ class Chapter extends Validator return $chapter; } - public function checkCourseId($courseId) + public function checkCourse($courseId) { $courseRepo = new CourseRepo(); @@ -61,10 +61,10 @@ class Chapter extends Validator throw new BadRequestException('chapter.invalid_course_id'); } - return $course->id; + return $course; } - public function checkParentId($parentId) + public function checkParent($parentId) { $chapterRepo = new ChapterRepo(); @@ -74,7 +74,7 @@ class Chapter extends Validator throw new BadRequestException('chapter.invalid_parent_id'); } - return $chapter->id; + return $chapter; } public function checkTitle($title) diff --git a/app/Validators/Help.php b/app/Validators/Help.php index 4eb40bf4..624302ec 100644 --- a/app/Validators/Help.php +++ b/app/Validators/Help.php @@ -2,12 +2,40 @@ namespace App\Validators; +use App\Caches\Help as HelpCache; +use App\Caches\MaxHelpId as MaxHelpIdCache; use App\Exceptions\BadRequest as BadRequestException; use App\Repos\Help as HelpRepo; class Help extends Validator { + public function checkHelpCache($id) + { + $id = intval($id); + + $maxHelpIdCache = new MaxHelpIdCache(); + + $maxHelpId = $maxHelpIdCache->get(); + + /** + * 防止缓存穿透 + */ + if ($id < 1 || $id > $maxHelpId) { + throw new BadRequestException('help.not_found'); + } + + $helpCache = new HelpCache(); + + $help = $helpCache->get($id); + + if (!$help) { + throw new BadRequestException('help.not_found'); + } + + return $help; + } + public function checkHelp($id) { $helpRepo = new HelpRepo(); diff --git a/app/Validators/Package.php b/app/Validators/Package.php index 4acca161..85f3ffb0 100644 --- a/app/Validators/Package.php +++ b/app/Validators/Package.php @@ -2,12 +2,40 @@ namespace App\Validators; +use App\Caches\MaxPackageId as MaxPackageIdCache; +use App\Caches\Package as PackageCache; use App\Exceptions\BadRequest as BadRequestException; use App\Repos\Package as PackageRepo; class Package extends Validator { + public function checkPackageCache($id) + { + $id = intval($id); + + $maxPackageIdCache = new MaxPackageIdCache(); + + $maxPackageId = $maxPackageIdCache->get(); + + /** + * 防止缓存穿透 + */ + if ($id < 1 || $id > $maxPackageId) { + throw new BadRequestException('package.not_found'); + } + + $packageCache = new PackageCache(); + + $package = $packageCache->get($id); + + if (!$package) { + throw new BadRequestException('package.not_found'); + } + + return $package; + } + public function checkPackage($id) { $packageRepo = new PackageRepo(); diff --git a/app/Validators/Page.php b/app/Validators/Page.php index 3ff150c5..0283e5d5 100644 --- a/app/Validators/Page.php +++ b/app/Validators/Page.php @@ -2,12 +2,40 @@ namespace App\Validators; +use App\Caches\MaxPageId as MaxPageIdCache; +use App\Caches\Page as PageCache; use App\Exceptions\BadRequest as BadRequestException; use App\Repos\Page as PageRepo; class Page extends Validator { + public function checkPageCache($id) + { + $id = intval($id); + + $maxPageIdCache = new MaxPageIdCache(); + + $maxPageId = $maxPageIdCache->get(); + + /** + * 防止缓存穿透 + */ + if ($id < 1 || $id > $maxPageId) { + throw new BadRequestException('page.not_found'); + } + + $pageCache = new PageCache(); + + $page = $pageCache->get($id); + + if (!$page) { + throw new BadRequestException('page.not_found'); + } + + return $page; + } + public function checkPage($id) { $pageRepo = new PageRepo(); diff --git a/app/Validators/Security.php b/app/Validators/Security.php index aea0f860..2e7a5fbb 100644 --- a/app/Validators/Security.php +++ b/app/Validators/Security.php @@ -7,7 +7,7 @@ use App\Exceptions\ServiceUnavailable as ServiceUnavailableException; use App\Library\Validator\Common as CommonValidator; use App\Services\Captcha as CaptchaService; use App\Services\Throttle as ThrottleService; -use App\Services\VerifyCode as VerifyCodeService; +use App\Services\Verification as VerificationService; class Security extends Validator { @@ -37,9 +37,9 @@ class Security extends Validator public function checkRateLimit() { - $throttleService = new ThrottleService(); + $service = new ThrottleService(); - $result = $throttleService->checkRateLimit(); + $result = $service->checkRateLimit(); if (!$result) { throw new ServiceUnavailableException('security.too_many_requests'); @@ -48,14 +48,14 @@ class Security extends Validator public function checkVerifyCode($key, $code) { - $verifyCodeService = new VerifyCodeService(); + $service = new VerificationService(); $result = false; if (CommonValidator::email($key)) { - $result = $verifyCodeService->checkMailCode($key, $code); + $result = $service->checkMailCode($key, $code); } elseif (CommonValidator::phone($key)) { - $result = $verifyCodeService->checkSmsCode($key, $code); + $result = $service->checkSmsCode($key, $code); } if (!$result) { @@ -65,9 +65,9 @@ class Security extends Validator public function checkCaptchaCode($ticket, $rand) { - $captchaService = new CaptchaService(); + $service = new CaptchaService(); - $result = $captchaService->verify($ticket, $rand); + $result = $service->verify($ticket, $rand); if (!$result) { throw new BadRequestException('security.invalid_captcha_code'); diff --git a/app/Validators/Topic.php b/app/Validators/Topic.php index 80f11ece..51d6eb55 100644 --- a/app/Validators/Topic.php +++ b/app/Validators/Topic.php @@ -2,12 +2,40 @@ namespace App\Validators; +use App\Caches\MaxTopicId as MaxTopicIdCache; +use App\Caches\Topic as TopicCache; use App\Exceptions\BadRequest as BadRequestException; use App\Repos\Topic as TopicRepo; class Topic extends Validator { + public function checkTopicCache($id) + { + $id = intval($id); + + $maxTopicIdCache = new MaxTopicIdCache(); + + $maxTopicId = $maxTopicIdCache->get(); + + /** + * 防止缓存穿透 + */ + if ($id < 1 || $id > $maxTopicId) { + throw new BadRequestException('topic.not_found'); + } + + $topicCache = new TopicCache(); + + $topic = $topicCache->get($id); + + if (!$topic) { + throw new BadRequestException('topic.not_found'); + } + + return $topic; + } + public function checkTopic($id) { $topicRepo = new TopicRepo();