diff --git a/app/Caches/MaxPointGiftId.php b/app/Caches/MaxPointGiftId.php deleted file mode 100644 index f68994e0..00000000 --- a/app/Caches/MaxPointGiftId.php +++ /dev/null @@ -1,34 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return 'max_point_gift_id'; - } - - public function getContent($id = null) - { - $gift = PointGiftModel::findFirst(['order' => 'id DESC']); - - return $gift->id ?? 0; - } - -} diff --git a/app/Caches/PointGift.php b/app/Caches/PointGift.php deleted file mode 100644 index 07f5afa0..00000000 --- a/app/Caches/PointGift.php +++ /dev/null @@ -1,36 +0,0 @@ -lifetime; - } - - public function getKey($id = null) - { - return "point_gift:{$id}"; - } - - public function getContent($id = null) - { - $giftRepo = new PointGiftRepo(); - - $gift = $giftRepo->findById($id); - - return $gift ?: null; - } - -} diff --git a/app/Http/Admin/Services/PointGift.php b/app/Http/Admin/Services/PointGift.php index 545092c8..d92502bb 100644 --- a/app/Http/Admin/Services/PointGift.php +++ b/app/Http/Admin/Services/PointGift.php @@ -7,7 +7,6 @@ namespace App\Http\Admin\Services; -use App\Caches\PointGift as PointGiftCache; use App\Library\Paginator\Query as PagerQuery; use App\Models\PointGift as PointGiftModel; use App\Repos\Course as CourseRepo; @@ -116,8 +115,6 @@ class PointGift extends Service break; } - $this->rebuildPointGiftCache($gift); - return $gift; } @@ -165,8 +162,6 @@ class PointGift extends Service $gift->update($data); - $this->rebuildPointGiftCache($gift); - return $gift; } @@ -178,8 +173,6 @@ class PointGift extends Service $gift->update(); - $this->rebuildPointGiftCache($gift); - return $gift; } @@ -191,8 +184,6 @@ class PointGift extends Service $gift->update(); - $this->rebuildPointGiftCache($gift); - return $gift; } @@ -203,13 +194,6 @@ class PointGift extends Service return $validator->checkPointGift($id); } - protected function rebuildPointGiftCache(PointGiftModel $gift) - { - $cache = new PointGiftCache(); - - $cache->rebuild($gift->id); - } - protected function createCoursePointGift($post) { $validator = new PointGiftValidator(); diff --git a/app/Http/Home/Controllers/PointGiftController.php b/app/Http/Home/Controllers/PointGiftController.php index 1b7a8c0d..5b2c4b16 100644 --- a/app/Http/Home/Controllers/PointGiftController.php +++ b/app/Http/Home/Controllers/PointGiftController.php @@ -87,7 +87,7 @@ class PointGiftController extends Controller $hotGifts = $this->getHotGifts(); $userBalance = $this->getUserBalance(); - $this->seo->prependTitle(['积分兑换', $gift['name']]); + $this->seo->prependTitle(['积分商城', $gift['name']]); $this->view->pick('point/gift/show'); $this->view->setVar('gift', $gift); diff --git a/app/Http/Home/Views/point/gift/list.volt b/app/Http/Home/Views/point/gift/list.volt index 6db04fc4..e6ca49e4 100644 --- a/app/Http/Home/Views/point/gift/list.volt +++ b/app/Http/Home/Views/point/gift/list.volt @@ -6,7 +6,7 @@
@@ -17,4 +17,4 @@ {{ js_include('home/js/point.gift.list.js') }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/app/Http/Home/Views/point/gift/show.volt b/app/Http/Home/Views/point/gift/show.volt index ae94d2e9..48d84652 100644 --- a/app/Http/Home/Views/point/gift/show.volt +++ b/app/Http/Home/Views/point/gift/show.volt @@ -10,7 +10,7 @@ @@ -55,7 +55,23 @@
物品详情
-
{{ gift.details }}
+
+ {% if gift.type == 1 %} + {% set course_url = url({'for':'home.course.show','id':gift.attrs.id}) %} +

+ {{ gift.name }} + 查看 +

+ {% elseif gift.type == 3 %} + {% set vip_url = url({'for':'home.vip.index'}) %} +

+ {{ gift.name }} + 查看 +

+ {% else %} + {{ gift.details }} + {% endif %} +
@@ -124,4 +140,4 @@ {{ js_include('home/js/point.gift.show.js') }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/app/Models/PointGift.php b/app/Models/PointGift.php index a93077cd..3278e1f3 100644 --- a/app/Models/PointGift.php +++ b/app/Models/PointGift.php @@ -7,7 +7,6 @@ namespace App\Models; -use App\Caches\MaxPointGiftId as MaxPointGiftIdCache; use Phalcon\Mvc\Model\Behavior\SoftDelete; use Phalcon\Text; @@ -206,13 +205,6 @@ class PointGift extends Model } } - public function afterCreate() - { - $cache = new MaxPointGiftIdCache(); - - $cache->rebuild(); - } - public function afterFetch() { if (!Text::startsWith($this->cover, 'http')) { diff --git a/app/Services/Logic/ContentTrait.php b/app/Services/Logic/ContentTrait.php new file mode 100644 index 00000000..1ff67c78 --- /dev/null +++ b/app/Services/Logic/ContentTrait.php @@ -0,0 +1,29 @@ +handleCosImageStyle($content); + } + + protected function handleCosImageStyle($content) + { + $style = '!content_800'; + + $pattern = '/src="(.*?)\/img\/content\/(.*?)"/'; + + $replacement = 'src="$1/img/content/$2' . $style . '"'; + + return preg_replace($pattern, $replacement, $content); + } + +} diff --git a/app/Services/Logic/Point/GiftInfo.php b/app/Services/Logic/Point/GiftInfo.php index 1be4eef6..aa2a4769 100644 --- a/app/Services/Logic/Point/GiftInfo.php +++ b/app/Services/Logic/Point/GiftInfo.php @@ -7,33 +7,44 @@ namespace App\Services\Logic\Point; -use App\Models\PointGift; +use App\Models\PointGift as PointGiftModel; +use App\Models\User as UserModel; use App\Repos\User as UserRepo; +use App\Services\Logic\ContentTrait; use App\Services\Logic\CourseTrait; use App\Services\Logic\PointGiftTrait; use App\Services\Logic\Service as LogicService; +use App\Services\Logic\VipTrait; class GiftInfo extends LogicService { use CourseTrait; + use ContentTrait; use PointGiftTrait; + use VipTrait; public function handle($id) { $gift = $this->checkPointGift($id); - if ($gift->type == PointGift::TYPE_COURSE) { - $gift = $this->getCourseGift($gift); + $user = $this->getCurrentUser(); + + if ($gift->type == PointGiftModel::TYPE_COURSE) { + $gift = $this->handleCoursePointGift($gift); + } elseif ($gift->type == PointGiftModel::TYPE_VIP) { + $gift = $this->handleVipPointGift($gift); } - $meInfo = $this->handleMeInfo($gift); + $details = $this->handleContent($gift->details); + + $meInfo = $this->handleMeInfo($gift, $user); return [ 'id' => $gift->id, 'name' => $gift->name, 'cover' => $gift->cover, - 'details' => $gift->details, + 'details' => $details, 'attrs' => $gift->attrs, 'type' => $gift->type, 'stock' => $gift->stock, @@ -48,43 +59,53 @@ class GiftInfo extends LogicService ]; } - protected function getCourseGift(PointGift $gift) + protected function handleCoursePointGift(PointGiftModel $gift) { - $courseId = $gift->attrs['id'] ?? 0; + $id = $gift->attrs['id'] ?? 0; - $course = $this->checkCourse($courseId); + if ($id == 0) return $gift; + + $course = $this->checkCourse($id); $gift->name = $course->title; $gift->cover = $course->cover; $gift->details = $course->details; - $gift->attrs = [ - 'id' => $course->id, - 'title' => $course->title, - 'price' => $course->market_price, - ]; return $gift; } - protected function handleMeInfo(PointGift $gift) + protected function handleVipPointGift(PointGiftModel $gift) + { + $id = $gift->attrs['id'] ?? 0; + + if ($id == 0) return $gift; + + $vip = $this->checkVip($id); + + $gift->name = sprintf('会员服务(%d个月)', $vip->expiry); + $gift->cover = $vip->cover; + + return $gift; + } + + protected function handleMeInfo(PointGiftModel $gift, UserModel $user) { $me = [ 'allow_redeem' => 0, 'logged' => 0, ]; - $user = $this->getLoginUser(true); + if ($user->id > 0) { - if ($user->id == 0) return $me; + $me['logged'] = 1; - $me['logged'] = 1; + $userRepo = new UserRepo(); - $userRepo = new UserRepo(); + $balance = $userRepo->findUserBalance($user->id); - $balance = $userRepo->findUserBalance($user->id); - - if ($gift->stock > 0 && $balance->point > $gift->point) { - $me['allow_redeem'] = 1; + if ($gift->stock > 0 && $balance->point > $gift->point) { + $me['allow_redeem'] = 1; + } } return $me; diff --git a/app/Services/Logic/PointGiftTrait.php b/app/Services/Logic/PointGiftTrait.php index c81a577e..4390457f 100644 --- a/app/Services/Logic/PointGiftTrait.php +++ b/app/Services/Logic/PointGiftTrait.php @@ -19,11 +19,4 @@ trait PointGiftTrait return $validator->checkPointGift($id); } - public function checkPointGiftCache($id) - { - $validator = new PointGiftValidator(); - - return $validator->checkPointGiftCache($id); - } - } diff --git a/app/Services/Logic/VipTrait.php b/app/Services/Logic/VipTrait.php new file mode 100644 index 00000000..76c42883 --- /dev/null +++ b/app/Services/Logic/VipTrait.php @@ -0,0 +1,22 @@ +checkVip($id); + } + +} diff --git a/app/Validators/PointGift.php b/app/Validators/PointGift.php index 99d7de0f..0f0c7995 100644 --- a/app/Validators/PointGift.php +++ b/app/Validators/PointGift.php @@ -7,8 +7,6 @@ namespace App\Validators; -use App\Caches\MaxPointGiftId as MaxPointGiftIdCache; -use App\Caches\PointGift as PointGiftCache; use App\Exceptions\BadRequest as BadRequestException; use App\Library\Validators\Common as CommonValidator; use App\Models\PointGift as PointGiftModel; @@ -18,30 +16,8 @@ use App\Services\EditorStorage as EditorStorageService; class PointGift extends Validator { - /** - * @param int $id - * @return PointGiftModel - * @throws BadRequestException - */ - public function checkPointGiftCache($id) - { - $this->checkId($id); - - $giftCache = new PointGiftCache(); - - $gift = $giftCache->get($id); - - if (!$gift) { - throw new BadRequestException('point_gift.not_found'); - } - - return $gift; - } - public function checkPointGift($id) { - $this->checkId($id); - $giftRepo = new PointGiftRepo(); $gift = $giftRepo->findById($id); @@ -53,19 +29,6 @@ class PointGift extends Validator return $gift; } - public function checkId($id) - { - $id = intval($id); - - $maxGiftIdCache = new MaxPointGiftIdCache(); - - $maxId = $maxGiftIdCache->get(); - - if ($id < 1 || $id > $maxId) { - throw new BadRequestException('point_gift.not_found'); - } - } - public function checkName($name) { $value = $this->filter->sanitize($name, ['trim', 'string']);