1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 20:06:09 +08:00

增强缓存

This commit is contained in:
xiaochong0302 2020-04-22 21:30:41 +08:00
parent a51189636e
commit 1d8af72d29
44 changed files with 483 additions and 161 deletions

View File

@ -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,

View File

@ -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,
];
}

View File

@ -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,
];

View File

@ -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,
];
}

View File

@ -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;

View File

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

View File

@ -28,8 +28,8 @@
<div class="layui-form-item">
<label class="layui-form-label">教学角色</label>
<div class="layui-input-block">
<input type="radio" name="edu_role" value="1" title="学员" {% if user.edu_role == 1 %}checked{% endif %}>
<input type="radio" name="edu_role" value="2" title="讲师" {% if user.edu_role == 2 %}checked{% endif %}>
<input type="radio" name="edu_role" value="student" title="学员" {% if user.edu_role == 'student' %}checked{% endif %}>
<input type="radio" name="edu_role" value="teacher" title="讲师" {% if user.edu_role == 'teacher' %}checked{% endif %}>
</div>
</div>
@ -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();

View File

@ -1,33 +1,39 @@
{%- macro gender_info(value) %}
{% if value == 1 %}
<span class="layui-badge layui-bg-green">男</span>
{% elseif value == 2 %}
<span class="layui-badge layui-bg-blue">女</span>
{% elseif value == 3 %}
<span class="layui-badge layui-bg-black">密</span>
{%- macro location_info(value) %}
{% if value %}
{{ value }}
{% else %}
N/A
{% endif %}
{%- endmacro %}
{%- macro role_info(user) %}
{% if user.edu_role.id > 0 %}
<span class="layui-badge layui-bg-green">{{ user.edu_role.name }}</span>
{%- macro gender_info(value) %}
{% if value == 'male' %}
{% elseif value == 'female' %}
{% elseif value == 'none' %}
{% endif %}
{% if user.admin_role.id > 0 %}
<span class="layui-badge layui-bg-blue">{{ user.admin_role.name }}</span>
{%- 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 %}
<span class="layui-badge layui-bg-green">正常</span>
{% else %}
<span class="layui-badge" title="期限:{{ date('Y-m-d H:i',user.lock_expiry_time) }}">锁定</span>
{% endif %}
{%- endmacro %}
{%- macro vip_info(user) %}
{% if user.vip == 1 %}
<span class="layui-badge layui-bg-orange" title="期限:{{ date('Y-m-d H:i',user.vip_expiry_time) }}">vip</span>
<span class="layui-badge layui-bg-orange" title="期限:{{ date('Y-m-d H:i',user.vip_expiry_time) }}">会员</span>
{% endif %}
{% if user.locked == 1 %}
<span class="layui-badge" title="期限:{{ date('Y-m-d H:i',user.lock_expiry_time) }}">锁定</span>
{% endif %}
{%- endmacro %}
@ -55,6 +61,7 @@
<col>
<col>
<col>
<col>
<col width="12%">
</colgroup>
<thead>
@ -63,8 +70,9 @@
<th>昵称</th>
<th>地区</th>
<th>性别</th>
<th>角色</th>
<th>状态</th>
<th>教学角色</th>
<th>后台角色</th>
<th>注册时间</th>
<th>操作</th>
</tr>
</thead>
@ -72,11 +80,12 @@
{% for item in pager.items %}
<tr>
<td>{{ item.id }}</td>
<td><span title="{{ item.about }}">{{ item.name }}</span>{{ vip_info(item) }}</td>
<td>{% if item.location %} {{ item.location }} {% else %} N/A {% endif %}</td>
<td><span title="{{ item.about }}">{{ item.name }}</span>{{ status_info(item) }}</td>
<td>{{ location_info(item.location) }}</td>
<td>{{ gender_info(item.gender) }}</td>
<td>{{ role_info(item) }}</td>
<td>{{ status_info(item) }}</td>
<td>{{ edu_role_info(item) }}</td>
<td>{{ admin_role_info(item) }}</td>
<td>{{ date('Y-m-d H:i',item.create_time) }}</td>
<td align="center">
<div class="layui-dropdown">
<button class="layui-btn layui-btn-sm">操作 <span class="layui-icon layui-icon-triangle-d"></span></button>

View File

@ -21,8 +21,8 @@
<div class="layui-form-item">
<label class="layui-form-label">教学角色</label>
<div class="layui-input-block">
<input type="radio" name="edu_role" value="1" title="学员">
<input type="radio" name="edu_role" value="2" title="讲师">
<input type="radio" name="edu_role" value="student" title="学员">
<input type="radio" name="edu_role" value="teacher" title="讲师">
</div>
</div>

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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 [

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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()

View File

@ -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();
}
}

View File

@ -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 [

View File

@ -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,
];
}

View File

@ -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);

View File

@ -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,

View File

@ -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'],
];
}

View File

@ -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 ?: [];
}
}

View File

@ -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;

View File

@ -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,
];
}

View File

@ -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 ?: [];
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace App\Services\Frontend\Package;
use App\Services\Frontend\PackageTrait;
use App\Services\Frontend\Service;
class CourseList extends Service
{
use PackageTrait;
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Services\Frontend\Package;
use App\Models\Package as PackageModel;
use App\Services\Frontend\PackageTrait;
use App\Services\Frontend\Service;
class PackageInfo extends Service
{
use PackageTrait;
public function getPackage($id)
{
$package = $this->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,
];
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Services\Frontend;
use App\Validators\Package as PackageValidator;
trait PackageTrait
{
public function checkPackage($id)
{
$validator = new PackageValidator();
return $validator->checkPackage($id);
}
public function checkPackageCache($id)
{
$validator = new PackageValidator();
return $validator->checkPackageCache($id);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Services\Frontend\Topic;
use App\Caches\TopicCourseList as TopicCourseListCache;
use App\Services\Frontend\Service;
use App\Services\Frontend\TopicTrait;
class CourseList extends Service
{
use TopicTrait;
public function getCourses($id)
{
$topic = $this->checkTopicCache($id);
$cache = new TopicCourseListCache();
$result = $cache->get($topic->id);
return $result ?: [];
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Services\Frontend\Topic;
use App\Models\Topic as TopicModel;
use App\Services\Frontend\Service;
use App\Services\Frontend\TopicTrait;
class TopicInfo extends Service
{
use TopicTrait;
public function getTopic($id)
{
$topic = $this->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,
];
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Services\Frontend;
use App\Validators\Topic as TopicValidator;
trait TopicTrait
{
public function checkTopic($id)
{
$validator = new TopicValidator();
return $validator->checkTopic($id);
}
public function checkTopicCache($id)
{
$validator = new TopicValidator();
return $validator->checkTopicCache($id);
}
}

View File

@ -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)) {

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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);