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 2025-01-04 21:34:37 +08:00
parent 9d8ed0f863
commit 09723a9d34
12 changed files with 116 additions and 166 deletions

View File

@ -1,34 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Models\PointGift as PointGiftModel;
class MaxPointGiftId extends Cache
{
protected $lifetime = 365 * 86400;
public function getLifetime()
{
return $this->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;
}
}

View File

@ -1,36 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Caches;
use App\Repos\PointGift as PointGiftRepo;
class PointGift extends Cache
{
protected $lifetime = 86400;
public function getLifetime()
{
return $this->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;
}
}

View File

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

View File

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

View File

@ -6,7 +6,7 @@
<div class="layui-breadcrumb breadcrumb">
<a href="/">首页</a>
<a><cite>积分兑换</cite></a>
<a><cite>积分商城</cite></a>
</div>
<div id="gift-list" data-url="{{ pager_url }}"></div>
@ -17,4 +17,4 @@
{{ js_include('home/js/point.gift.list.js') }}
{% endblock %}
{% endblock %}

View File

@ -10,7 +10,7 @@
<div class="breadcrumb">
<span class="layui-breadcrumb">
<a href="/">首页</a>
<a href="{{ gift_list_url }}">积分兑换</a>
<a href="{{ gift_list_url }}">积分商城</a>
<a><cite>{{ gift.name }}</cite></a>
</span>
</div>
@ -55,7 +55,23 @@
<div class="layui-card">
<div class="layui-card-header">物品详情</div>
<div class="layui-card-body">
<div class="gift-details ke-content kg-zoom">{{ gift.details }}</div>
<div class="gift-details ke-content kg-zoom">
{% if gift.type == 1 %}
{% set course_url = url({'for':'home.course.show','id':gift.attrs.id}) %}
<p class="item">
<a href="{{ course_url }}">{{ gift.name }}</a>
<span class="layui-badge">查看</span>
</p>
{% elseif gift.type == 3 %}
{% set vip_url = url({'for':'home.vip.index'}) %}
<p class="item">
<a href="{{ vip_url }}">{{ gift.name }}</a>
<span class="layui-badge">查看</span>
</p>
{% else %}
{{ gift.details }}
{% endif %}
</div>
</div>
</div>
</div>
@ -124,4 +140,4 @@
{{ js_include('home/js/point.gift.show.js') }}
{% endblock %}
{% endblock %}

View File

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

View File

@ -0,0 +1,29 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Services\Logic;
trait ContentTrait
{
public function handleContent($content)
{
return $this->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);
}
}

View File

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

View File

@ -19,11 +19,4 @@ trait PointGiftTrait
return $validator->checkPointGift($id);
}
public function checkPointGiftCache($id)
{
$validator = new PointGiftValidator();
return $validator->checkPointGiftCache($id);
}
}

View File

@ -0,0 +1,22 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Services\Logic;
use App\Validators\Vip as VipValidator;
trait VipTrait
{
public function checkVip($id)
{
$validator = new VipValidator();
return $validator->checkVip($id);
}
}

View File

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