1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-22 11:41:27 +08:00

增强缓存

This commit is contained in:
xiaochong0302 2020-04-21 19:23:20 +08:00
parent 6215802ee9
commit a51189636e
41 changed files with 422 additions and 82 deletions

View File

@ -7,7 +7,7 @@ use App\Repos\Chapter as ChapterRepo;
class Chapter extends Cache class Chapter extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {

View File

@ -25,6 +25,8 @@ class ChapterCounter extends Counter
$chapter = $chapterRepo->findById($id); $chapter = $chapterRepo->findById($id);
if (!$chapter) return null;
return [ return [
'user_count' => $chapter->user_count, 'user_count' => $chapter->user_count,
'lesson_count' => $chapter->lesson_count, 'lesson_count' => $chapter->lesson_count,

View File

@ -7,7 +7,7 @@ use App\Repos\Course as CourseRepo;
class Course extends Cache class Course extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {
@ -25,11 +25,7 @@ class Course extends Cache
$course = $courseRepo->findById($id); $course = $courseRepo->findById($id);
if (!$course) return null; return $course ?: null;
$course->cover = kg_ci_img_url($course->cover);
return $course;
} }
} }

View File

@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo;
class CourseCategoryList extends Cache class CourseCategoryList extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {

View File

@ -9,7 +9,7 @@ use Phalcon\Mvc\Model\Resultset;
class CourseChapterList extends Cache class CourseChapterList extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {

View File

@ -25,6 +25,8 @@ class CourseCounter extends Counter
$course = $courseRepo->findById($id); $course = $courseRepo->findById($id);
if (!$course) return null;
return [ return [
'user_count' => $course->user_count, 'user_count' => $course->user_count,
'lesson_count' => $course->lesson_count, 'lesson_count' => $course->lesson_count,

View File

@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo;
class CoursePackageList extends Cache class CoursePackageList extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {

View File

@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo;
class CourseRelatedList extends Cache class CourseRelatedList extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {
@ -41,27 +41,18 @@ class CourseRelatedList extends Cache
{ {
$result = []; $result = [];
$baseUrl = kg_ci_base_url();
foreach ($courses as $course) { foreach ($courses as $course) {
$course->cover = $baseUrl . $course->cover;
$result[] = [ $result[] = [
'id' => $course->id, 'id' => $course->id,
'title' => $course->title, 'title' => $course->title,
'cover' => $course->cover, 'cover' => $course->cover,
'summary' => $course->summary,
'market_price' => (float)$course->market_price, 'market_price' => (float)$course->market_price,
'vip_price' => (float)$course->vip_price, 'vip_price' => (float)$course->vip_price,
'rating' => (float)$course['rating'],
'score' => (float)$course['score'],
'model' => $course->model, 'model' => $course->model,
'level' => $course->level, 'level' => $course->level,
'user_count' => $course->user_count, 'user_count' => $course->user_count,
'lesson_count' => $course->lesson_count, 'lesson_count' => $course->lesson_count,
'review_count' => $course->review_count,
'favorite_count' => $course->favorite_count,
]; ];
} }

View File

@ -8,7 +8,7 @@ use App\Repos\Course as CourseRepo;
class CourseTeacherList extends Cache class CourseTeacherList extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {
@ -41,12 +41,7 @@ class CourseTeacherList extends Cache
{ {
$result = []; $result = [];
$baseUrl = kg_ci_base_url();
foreach ($users as $user) { foreach ($users as $user) {
$user->avatar = $baseUrl . $user->avatar;
$result[] = [ $result[] = [
'id' => $user->id, 'id' => $user->id,
'name' => $user->name, 'name' => $user->name,

View File

@ -7,7 +7,7 @@ use App\Repos\CourseUser as CourseUserRepo;
class CourseUser extends Cache class CourseUser extends Cache
{ {
protected $lifetime = 7 * 86400; protected $lifetime = 1 * 86400;
public function getLifetime() public function getLifetime()
{ {
@ -33,11 +33,7 @@ class CourseUser extends Cache
$courseUser = $courseUserRepo->findCourseUser($courseId, $userId); $courseUser = $courseUserRepo->findCourseUser($courseId, $userId);
if (!$courseUser) { return $courseUser ?: null;
return new \stdClass();
}
return $courseUser;
} }
} }

31
app/Caches/Help.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace App\Caches;
use App\Repos\Help as HelpRepo;
class Help extends Cache
{
protected $lifetime = 7 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return "help:{$id}";
}
public function getContent($id = null)
{
$helpRepo = new HelpRepo();
$help = $helpRepo->findById($id);
return $help ?: null;
}
}

29
app/Caches/MaxHelpId.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace App\Caches;
use App\Models\Help as HelpModel;
class MaxHelpId extends Cache
{
protected $lifetime = 365 * 86400;
public function getLifetime()
{
return $this->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;
}
}

29
app/Caches/MaxPageId.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace App\Caches;
use App\Models\Page as PageModel;
class MaxPageId extends Cache
{
protected $lifetime = 365 * 86400;
public function getLifetime()
{
return $this->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;
}
}

31
app/Caches/Page.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace App\Caches;
use App\Repos\Page as PageRepo;
class Page extends Cache
{
protected $lifetime = 7 * 86400;
public function getLifetime()
{
return $this->lifetime;
}
public function getKey($id = null)
{
return "page:{$id}";
}
public function getContent($id = null)
{
$pageRepo = new PageRepo();
$page = $pageRepo->findById($id);
return $page ?: null;
}
}

View File

@ -5,6 +5,7 @@ namespace App\Http\Admin\Services;
use App\Caches\Category as CategoryCache; use App\Caches\Category as CategoryCache;
use App\Caches\CategoryList as CategoryListCache; use App\Caches\CategoryList as CategoryListCache;
use App\Caches\CategoryTreeList as CategoryTreeListCache; use App\Caches\CategoryTreeList as CategoryTreeListCache;
use App\Caches\MaxCategoryId as MaxCategoryIdCache;
use App\Models\Category as CategoryModel; use App\Models\Category as CategoryModel;
use App\Repos\Category as CategoryRepo; use App\Repos\Category as CategoryRepo;
use App\Validators\Category as CategoryValidator; use App\Validators\Category as CategoryValidator;
@ -172,23 +173,29 @@ class Category extends Service
} }
$childCount = $categoryRepo->countChildCategories($category->id); $childCount = $categoryRepo->countChildCategories($category->id);
$category->child_count = $childCount; $category->child_count = $childCount;
$category->update(); $category->update();
} }
protected function rebuildCategoryCache(CategoryModel $category) 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) protected function enableChildCategories($parentId)

View File

@ -2,7 +2,9 @@
namespace App\Http\Admin\Services; namespace App\Http\Admin\Services;
use App\Caches\Chapter as ChapterCache;
use App\Caches\CourseChapterList as CourseChapterListCache; use App\Caches\CourseChapterList as CourseChapterListCache;
use App\Caches\MaxChapterId as MaxChapterIdCache;
use App\Models\Chapter as ChapterModel; use App\Models\Chapter as ChapterModel;
use App\Models\Course as CourseModel; use App\Models\Course as CourseModel;
use App\Repos\Chapter as ChapterRepo; use App\Repos\Chapter as ChapterRepo;
@ -38,7 +40,9 @@ class Chapter extends Service
$data = []; $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['title'] = $validator->checkTitle($post['title']);
$data['summary'] = $validator->checkSummary($post['summary']); $data['summary'] = $validator->checkSummary($post['summary']);
$data['free'] = $validator->checkFreeStatus($post['free']); $data['free'] = $validator->checkFreeStatus($post['free']);
@ -46,7 +50,8 @@ class Chapter extends Service
$chapterRepo = new ChapterRepo(); $chapterRepo = new ChapterRepo();
if (isset($post['parent_id'])) { 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']); $data['priority'] = $chapterRepo->maxLessonPriority($post['parent_id']);
} else { } else {
$data['priority'] = $chapterRepo->maxChapterPriority($post['course_id']); $data['priority'] = $chapterRepo->maxChapterPriority($post['course_id']);
@ -175,9 +180,17 @@ class Chapter extends Service
protected function rebuildChapterCache(ChapterModel $chapter) protected function rebuildChapterCache(ChapterModel $chapter)
{ {
$cache = new ChapterCache();
$cache->rebuild($chapter->id);
$cache = new CourseChapterListCache(); $cache = new CourseChapterListCache();
$cache->rebuild($chapter->course_id); $cache->rebuild($chapter->course_id);
$cache = new MaxChapterIdCache();
$cache->rebuild();
} }
protected function findOrFail($id) protected function findOrFail($id)

View File

@ -3,9 +3,11 @@
namespace App\Http\Admin\Services; namespace App\Http\Admin\Services;
use App\Builders\CourseList as CourseListBuilder; use App\Builders\CourseList as CourseListBuilder;
use App\Caches\Course as CourseCache;
use App\Caches\CourseCategoryList as CourseCategoryListCache; use App\Caches\CourseCategoryList as CourseCategoryListCache;
use App\Caches\CourseRelatedList as CourseRelatedListCache; use App\Caches\CourseRelatedList as CourseRelatedListCache;
use App\Caches\CourseTeacherList as CourseTeacherListCache; use App\Caches\CourseTeacherList as CourseTeacherListCache;
use App\Caches\MaxCourseId as MaxCourseIdCache;
use App\Library\Paginator\Query as PagerQuery; use App\Library\Paginator\Query as PagerQuery;
use App\Models\Course as CourseModel; use App\Models\Course as CourseModel;
use App\Models\CourseCategory as CourseCategoryModel; use App\Models\CourseCategory as CourseCategoryModel;
@ -67,6 +69,8 @@ class Course extends Service
$course->create($data); $course->create($data);
$this->rebuildCourseCache($course);
return $course; return $course;
} }
@ -148,6 +152,8 @@ class Course extends Service
$course->update(); $course->update();
$this->rebuildCourseCache($course);
return $course; return $course;
} }
@ -159,6 +165,8 @@ class Course extends Service
$course->update(); $course->update();
$this->rebuildCourseCache($course);
return $course; return $course;
} }
@ -310,6 +318,17 @@ class Course extends Service
return $validator->checkCourse($id); 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) protected function saveTeachers(CourseModel $course, $teacherIds)
{ {
$courseRepo = new CourseRepo(); $courseRepo = new CourseRepo();

View File

@ -2,6 +2,8 @@
namespace App\Http\Admin\Services; 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\Models\Help as HelpModel;
use App\Repos\Help as HelpRepo; use App\Repos\Help as HelpRepo;
use App\Validators\Help as HelpValidator; use App\Validators\Help as HelpValidator;
@ -42,6 +44,8 @@ class Help extends Service
$help->create($data); $help->create($data);
$this->rebuildHelpCache($help);
return $help; return $help;
} }
@ -73,6 +77,8 @@ class Help extends Service
$help->update($data); $help->update($data);
$this->rebuildHelpCache($help);
return $help; return $help;
} }
@ -84,6 +90,8 @@ class Help extends Service
$help->update(); $help->update();
$this->rebuildHelpCache($help);
return $help; return $help;
} }
@ -95,9 +103,22 @@ class Help extends Service
$help->update(); $help->update();
$this->rebuildHelpCache($help);
return $help; return $help;
} }
protected function rebuildHelpCache(HelpModel $help)
{
$cache = new HelpCache();
$cache->rebuild($help->id);
$cache = new MaxHelpIdCache();
$cache->rebuild();
}
protected function findOrFail($id) protected function findOrFail($id)
{ {
$validator = new HelpValidator(); $validator = new HelpValidator();

View File

@ -91,7 +91,6 @@ class Nav extends Service
$nav->update(); $nav->update();
$this->updateNavStats($nav); $this->updateNavStats($nav);
$this->rebuildNavCache(); $this->rebuildNavCache();
return $nav; return $nav;
@ -141,7 +140,6 @@ class Nav extends Service
$nav->update($data); $nav->update($data);
$this->updateNavStats($nav); $this->updateNavStats($nav);
$this->rebuildNavCache(); $this->rebuildNavCache();
return $nav; return $nav;
@ -160,7 +158,6 @@ class Nav extends Service
$nav->update(); $nav->update();
$this->updateNavStats($nav); $this->updateNavStats($nav);
$this->rebuildNavCache(); $this->rebuildNavCache();
return $nav; return $nav;
@ -175,7 +172,6 @@ class Nav extends Service
$nav->update(); $nav->update();
$this->updateNavStats($nav); $this->updateNavStats($nav);
$this->rebuildNavCache(); $this->rebuildNavCache();
return $nav; return $nav;

View File

@ -2,6 +2,7 @@
namespace App\Http\Admin\Services; namespace App\Http\Admin\Services;
use App\Caches\MaxPackageId as MaxPackageIdCache;
use App\Caches\Package as PackageCache; use App\Caches\Package as PackageCache;
use App\Caches\PackageCourseList as PackageCourseListCache; use App\Caches\PackageCourseList as PackageCourseListCache;
use App\Library\Paginator\Query as PagerQuery; use App\Library\Paginator\Query as PagerQuery;
@ -239,6 +240,10 @@ class Package extends Service
$cache = new PackageCourseListCache(); $cache = new PackageCourseListCache();
$cache->rebuild($package->id); $cache->rebuild($package->id);
$cache = new MaxPackageIdCache();
$cache->rebuild();
} }
protected function findOrFail($id) protected function findOrFail($id)

View File

@ -2,6 +2,8 @@
namespace App\Http\Admin\Services; 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\Library\Paginator\Query as PagerQuery;
use App\Models\Page as PageModel; use App\Models\Page as PageModel;
use App\Repos\Page as PageRepo; use App\Repos\Page as PageRepo;
@ -48,6 +50,8 @@ class Page extends Service
$page->create($data); $page->create($data);
$this->rebuildPageCache($page);
return $page; return $page;
} }
@ -75,6 +79,8 @@ class Page extends Service
$page->update($data); $page->update($data);
$this->rebuildPageCache($page);
return $page; return $page;
} }
@ -86,6 +92,8 @@ class Page extends Service
$page->update(); $page->update();
$this->rebuildPageCache($page);
return $page; return $page;
} }
@ -97,9 +105,22 @@ class Page extends Service
$page->update(); $page->update();
$this->rebuildPageCache($page);
return $page; return $page;
} }
protected function rebuildPageCache(PageModel $help)
{
$cache = new PageCache();
$cache->rebuild($help->id);
$cache = new MaxPageIdCache();
$cache->rebuild();
}
protected function findOrFail($id) protected function findOrFail($id)
{ {
$validator = new PageValidator(); $validator = new PageValidator();

View File

@ -2,6 +2,7 @@
namespace App\Http\Admin\Services; namespace App\Http\Admin\Services;
use App\Caches\MaxTopicId as MaxTopicIdCache;
use App\Caches\Topic as TopicCache; use App\Caches\Topic as TopicCache;
use App\Caches\TopicCourseList as TopicCourseListCache; use App\Caches\TopicCourseList as TopicCourseListCache;
use App\Library\Paginator\Query as PagerQuery; use App\Library\Paginator\Query as PagerQuery;
@ -198,6 +199,10 @@ class Topic extends Service
$cache = new TopicCourseListCache(); $cache = new TopicCourseListCache();
$cache->rebuild($topic->id); $cache->rebuild($topic->id);
$cache = new MaxTopicIdCache();
$cache->rebuild();
} }
protected function findOrFail($id) protected function findOrFail($id)

View File

@ -11,11 +11,11 @@
<label class="layui-form-label">封面</label> <label class="layui-form-label">封面</label>
<div class="layui-input-inline"> <div class="layui-input-inline">
{% if course.cover %} {% if course.cover %}
<img id="cover-img" class="kg-cover" src="{{ course.cover }}"> <img id="cover-img" class="kg-cover" src="{{ ci_image(course.cover) }}">
{% else %} {% else %}
<img id="cover-img" class="kg-cover" src="{{ image('admin/img/default_cover.png') }}"> <img id="cover-img" class="kg-cover" src="{{ image('admin/img/default_cover.png') }}">
{% endif %} {% endif %}
<input type="hidden" name="cover" value="{{ course.cover }}"> <input type="hidden" name="cover" value="{{ ci_image(course.cover) }}">
</div> </div>
<div class="layui-input-inline" style="padding-top:35px;"> <div class="layui-input-inline" style="padding-top:35px;">
<a href="javascript:" class="layui-btn layui-btn-sm" id="choose-cover">编辑</a> <a href="javascript:" class="layui-btn layui-btn-sm" id="choose-cover">编辑</a>

View File

@ -18,11 +18,11 @@
<label class="layui-form-label">封面</label> <label class="layui-form-label">封面</label>
<div class="layui-input-inline"> <div class="layui-input-inline">
{% if slide.cover %} {% if slide.cover %}
<img id="cover-img" class="kg-cover" src="{{ slide.cover }}"> <img id="cover-img" class="kg-cover" src="{{ ci_image(slide.cover) }}">
{% else %} {% else %}
<img id="cover-img" class="kg-cover" src="{{ image('admin/img/default_cover.png') }}"> <img id="cover-img" class="kg-cover" src="{{ image('admin/img/default_cover.png') }}">
{% endif %} {% endif %}
<input type="hidden" name="cover" value="{{ slide.cover }}"> <input type="hidden" name="cover" value="{{ ci_image(slide.cover) }}">
</div> </div>
<div class="layui-input-inline" style="padding-top:35px;"> <div class="layui-input-inline" style="padding-top:35px;">
<a href="javascript:" class="layui-btn layui-btn-sm" id="choose-cover">编辑</a> <a href="javascript:" class="layui-btn layui-btn-sm" id="choose-cover">编辑</a>

View File

@ -292,10 +292,6 @@ class Course extends Model
$this->rating = (float)$this->rating; $this->rating = (float)$this->rating;
$this->score = (float)$this->score; $this->score = (float)$this->score;
if (!empty($this->cover)) {
$this->cover = kg_ci_img_url($this->cover);
}
if (!empty($this->attrs)) { if (!empty($this->attrs)) {
$this->attrs = json_decode($this->attrs, true); $this->attrs = json_decode($this->attrs, true);
} }

View File

@ -2,6 +2,8 @@
namespace App\Models; namespace App\Models;
use Phalcon\Mvc\Model\Behavior\SoftDelete;
class Help extends Model class Help extends Model
{ {
@ -66,6 +68,18 @@ class Help extends Model
return 'kg_help'; return 'kg_help';
} }
public function initialize()
{
parent::initialize();
$this->addBehavior(
new SoftDelete([
'field' => 'deleted',
'value' => 1,
])
);
}
public function beforeCreate() public function beforeCreate()
{ {
$this->create_time = time(); $this->create_time = time();

View File

@ -122,7 +122,7 @@ class Role extends Model
public function afterFetch() public function afterFetch()
{ {
if (!empty($this->routes)) { if (!empty($this->routes)) {
$this->routes = json_decode($this->routes); $this->routes = json_decode($this->routes, true);
} }
} }

View File

@ -118,13 +118,6 @@ class Slide extends Model
$this->update_time = time(); $this->update_time = time();
} }
public function afterFetch()
{
if (!empty($this->cover)) {
$this->cover = kg_ci_img_url($this->cover);
}
}
public static function targetTypes() public static function targetTypes()
{ {
return [ return [

View File

@ -111,7 +111,7 @@ class Task extends Model
{ {
$this->update_time = time(); $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); $this->item_info = kg_json_encode($this->item_info);
} }
} }

View File

@ -176,8 +176,9 @@ class User extends Model
public function afterCreate() public function afterCreate()
{ {
$maxUserIdCache = new MaxUserIdCache(); $cache = new MaxUserIdCache();
$maxUserIdCache->rebuild();
$cache->rebuild();
} }
public static function genderTypes() public static function genderTypes()

View File

@ -14,4 +14,11 @@ trait UserTrait
return $validator->checkUser($id); return $validator->checkUser($id);
} }
public function checkUserCache($id)
{
$validator = new UserValidator();
return $validator->checkUserCache($id);
}
} }

View File

@ -3,7 +3,7 @@
namespace App\Services\Mailer; namespace App\Services\Mailer;
use App\Services\Mailer; use App\Services\Mailer;
use App\Services\VerifyCode; use App\Services\Verification;
class Verify extends Mailer class Verify extends Mailer
{ {
@ -18,11 +18,11 @@ class Verify extends Mailer
$message = $this->manager->createMessage(); $message = $this->manager->createMessage();
$verifyCode = new VerifyCode(); $verification = new Verification();
$minutes = 5; $minutes = 5;
$code = $verifyCode->getSmsCode($email, 60 * $minutes); $code = $verification->getSmsCode($email, 60 * $minutes);
$subject = '邮件验证码'; $subject = '邮件验证码';

View File

@ -3,7 +3,7 @@
namespace App\Services\Smser; namespace App\Services\Smser;
use App\Services\Smser; use App\Services\Smser;
use App\Services\VerifyCode; use App\Services\Verification;
class Verify extends Smser class Verify extends Smser
{ {
@ -16,11 +16,11 @@ class Verify extends Smser
*/ */
public function handle($phone) public function handle($phone)
{ {
$verifyCode = new VerifyCode(); $verification = new Verification();
$minutes = 5; $minutes = 5;
$code = $verifyCode->getSmsCode($phone, 60 * $minutes); $code = $verification->getSmsCode($phone, 60 * $minutes);
$templateId = $this->getTemplateId($this->templateCode); $templateId = $this->getTemplateId($this->templateCode);

View File

@ -7,7 +7,7 @@ use App\Services\Mailer\Verify as VerifyMailer;
use App\Services\Smser\Verify as VerifySmser; use App\Services\Smser\Verify as VerifySmser;
use Phalcon\Text; use Phalcon\Text;
class VerifyCode extends Service class Verification extends Service
{ {
/** /**

View File

@ -2,12 +2,40 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\Category as CategoryCache;
use App\Caches\MaxCategoryId as MaxCategoryIdCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Repos\Category as CategoryRepo; use App\Repos\Category as CategoryRepo;
class Category extends Validator 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) public function checkCategory($id)
{ {
$categoryRepo = new CategoryRepo(); $categoryRepo = new CategoryRepo();

View File

@ -51,7 +51,7 @@ class Chapter extends Validator
return $chapter; return $chapter;
} }
public function checkCourseId($courseId) public function checkCourse($courseId)
{ {
$courseRepo = new CourseRepo(); $courseRepo = new CourseRepo();
@ -61,10 +61,10 @@ class Chapter extends Validator
throw new BadRequestException('chapter.invalid_course_id'); throw new BadRequestException('chapter.invalid_course_id');
} }
return $course->id; return $course;
} }
public function checkParentId($parentId) public function checkParent($parentId)
{ {
$chapterRepo = new ChapterRepo(); $chapterRepo = new ChapterRepo();
@ -74,7 +74,7 @@ class Chapter extends Validator
throw new BadRequestException('chapter.invalid_parent_id'); throw new BadRequestException('chapter.invalid_parent_id');
} }
return $chapter->id; return $chapter;
} }
public function checkTitle($title) public function checkTitle($title)

View File

@ -2,12 +2,40 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\Help as HelpCache;
use App\Caches\MaxHelpId as MaxHelpIdCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Repos\Help as HelpRepo; use App\Repos\Help as HelpRepo;
class Help extends Validator 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) public function checkHelp($id)
{ {
$helpRepo = new HelpRepo(); $helpRepo = new HelpRepo();

View File

@ -2,12 +2,40 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\MaxPackageId as MaxPackageIdCache;
use App\Caches\Package as PackageCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Repos\Package as PackageRepo; use App\Repos\Package as PackageRepo;
class Package extends Validator 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) public function checkPackage($id)
{ {
$packageRepo = new PackageRepo(); $packageRepo = new PackageRepo();

View File

@ -2,12 +2,40 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\MaxPageId as MaxPageIdCache;
use App\Caches\Page as PageCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Repos\Page as PageRepo; use App\Repos\Page as PageRepo;
class Page extends Validator 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) public function checkPage($id)
{ {
$pageRepo = new PageRepo(); $pageRepo = new PageRepo();

View File

@ -7,7 +7,7 @@ use App\Exceptions\ServiceUnavailable as ServiceUnavailableException;
use App\Library\Validator\Common as CommonValidator; use App\Library\Validator\Common as CommonValidator;
use App\Services\Captcha as CaptchaService; use App\Services\Captcha as CaptchaService;
use App\Services\Throttle as ThrottleService; use App\Services\Throttle as ThrottleService;
use App\Services\VerifyCode as VerifyCodeService; use App\Services\Verification as VerificationService;
class Security extends Validator class Security extends Validator
{ {
@ -37,9 +37,9 @@ class Security extends Validator
public function checkRateLimit() public function checkRateLimit()
{ {
$throttleService = new ThrottleService(); $service = new ThrottleService();
$result = $throttleService->checkRateLimit(); $result = $service->checkRateLimit();
if (!$result) { if (!$result) {
throw new ServiceUnavailableException('security.too_many_requests'); throw new ServiceUnavailableException('security.too_many_requests');
@ -48,14 +48,14 @@ class Security extends Validator
public function checkVerifyCode($key, $code) public function checkVerifyCode($key, $code)
{ {
$verifyCodeService = new VerifyCodeService(); $service = new VerificationService();
$result = false; $result = false;
if (CommonValidator::email($key)) { if (CommonValidator::email($key)) {
$result = $verifyCodeService->checkMailCode($key, $code); $result = $service->checkMailCode($key, $code);
} elseif (CommonValidator::phone($key)) { } elseif (CommonValidator::phone($key)) {
$result = $verifyCodeService->checkSmsCode($key, $code); $result = $service->checkSmsCode($key, $code);
} }
if (!$result) { if (!$result) {
@ -65,9 +65,9 @@ class Security extends Validator
public function checkCaptchaCode($ticket, $rand) public function checkCaptchaCode($ticket, $rand)
{ {
$captchaService = new CaptchaService(); $service = new CaptchaService();
$result = $captchaService->verify($ticket, $rand); $result = $service->verify($ticket, $rand);
if (!$result) { if (!$result) {
throw new BadRequestException('security.invalid_captcha_code'); throw new BadRequestException('security.invalid_captcha_code');

View File

@ -2,12 +2,40 @@
namespace App\Validators; namespace App\Validators;
use App\Caches\MaxTopicId as MaxTopicIdCache;
use App\Caches\Topic as TopicCache;
use App\Exceptions\BadRequest as BadRequestException; use App\Exceptions\BadRequest as BadRequestException;
use App\Repos\Topic as TopicRepo; use App\Repos\Topic as TopicRepo;
class Topic extends Validator 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) public function checkTopic($id)
{ {
$topicRepo = new TopicRepo(); $topicRepo = new TopicRepo();