1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-04 16:01:35 +08:00

整理代码

This commit is contained in:
xiaochong0302 2020-05-12 16:06:33 +08:00
parent 1280283818
commit 30a2b6aee5
18 changed files with 298 additions and 274 deletions

View File

@ -34,7 +34,7 @@ class CloseTradeTask extends Task
*
* @param TradeModel $trade
*/
protected function handleAlipayTrade($trade)
protected function handleAlipayTrade(TradeModel $trade)
{
$allowClosed = true;
@ -66,7 +66,7 @@ class CloseTradeTask extends Task
*
* @param TradeModel $trade
*/
protected function handleWxpayTrade($trade)
protected function handleWxpayTrade(TradeModel $trade)
{
$allowClosed = true;

View File

@ -46,25 +46,43 @@ class SyncChapterCounterTask extends Task
return;
}
$counterCache = new ChapterCounterCache();
$chapterCache = new ChapterCache();
$chapterCounterCache = new ChapterCounterCache();
$hour = date('H');
foreach ($chapters as $chapter) {
$counter = $chapterCounterCache->get($chapter->id);
if ($hour % 3 == 0) {
if ($counter) {
$chapter->user_count = $counter['user_count'];
$chapter->lesson_count = $counter['lesson_count'];
$chapter->comment_count = $counter['comment_count'];
$chapter->agree_count = $counter['agree_count'];
$chapter->oppose_count = $counter['oppose_count'];
$chapter->user_count = $chapterRepo->countUsers($chapter->id);
$chapter->lesson_count = $chapterRepo->countLessons($chapter->id);
$chapter->comment_count = $chapterRepo->countComments($chapter->id);
$chapter->agree_count = $chapterRepo->countAgrees($chapter->id);
$chapter->oppose_count = $chapterRepo->countOpposes($chapter->id);
$chapter->update();
$counterCache->rebuild($chapter->id);
$chapterCache->rebuild($chapter->id);
} else {
$counter = $counterCache->get($chapter->id);
if ($counter) {
$chapter->user_count = $counter['user_count'];
$chapter->lesson_count = $counter['lesson_count'];
$chapter->comment_count = $counter['comment_count'];
$chapter->agree_count = $counter['agree_count'];
$chapter->oppose_count = $counter['oppose_count'];
$chapter->update();
$chapterCache->rebuild($chapter->id);
}
}
}

View File

@ -45,19 +45,34 @@ class SyncCommentCounterTask extends Task
return;
}
$cache = new CommentCounterCache();
$counterCache = new CommentCounterCache();
$hour = date('H');
foreach ($comments as $comment) {
$counter = $cache->get($comment->id);
if ($hour % 3 == 0) {
if ($counter) {
$comment->reply_count = $counter['reply_count'];
$comment->agree_count = $counter['agree_count'];
$comment->oppose_count = $counter['oppose_count'];
$comment->reply_count = $commentRepo->countReplies($comment->id);
$comment->agree_count = $commentRepo->countAgrees($comment->id);
$comment->oppose_count = $commentRepo->countOpposes($comment->id);
$comment->update();
$counterCache->rebuild($comment->id);
} else {
$counter = $counterCache->get($comment->id);
if ($counter) {
$comment->reply_count = $counter['reply_count'];
$comment->agree_count = $counter['agree_count'];
$comment->oppose_count = $counter['oppose_count'];
$comment->update();
}
}
}

View File

@ -45,18 +45,32 @@ class SyncConsultCounterTask extends Task
return;
}
$cache = new ConsultCounterCache();
$counterCache = new ConsultCounterCache();
$hour = date('H');
foreach ($consults as $consult) {
$counter = $cache->get($consult->id);
if ($hour % 3 == 0) {
if ($counter) {
$consult->agree_count = $counter['agree_count'];
$consult->oppose_count = $counter['oppose_count'];
$consult->agree_count = $consultRepo->countAgrees($consult->id);
$consult->oppose_count = $consultRepo->countOpposes($consult->id);
$consult->update();
$counterCache->rebuild($consult->id);
} else {
$counter = $counterCache->get($consult->id);
if ($counter) {
$consult->agree_count = $counter['agree_count'];
$consult->oppose_count = $counter['oppose_count'];
$consult->update();
}
}
}

View File

@ -46,26 +46,43 @@ class SyncCourseCounterTask extends Task
return;
}
$courseCounterCache = new CourseCounterCache();
$counterCache = new CourseCounterCache();
$courseCache = new CourseCache();
$hour = date('H');
foreach ($courses as $course) {
$counter = $courseCounterCache->get($course->id);
if ($hour % 3 == 0) {
if ($counter) {
$course->user_count = $counter['user_count'];
$course->lesson_count = $counter['lesson_count'];
$course->comment_count = $counter['comment_count'];
$course->consult_count = $counter['consult_count'];
$course->review_count = $counter['review_count'];
$course->favorite_count = $counter['favorite_count'];
$course->user_count = $courseRepo->countUsers($course->id);
$course->lesson_count = $courseRepo->countLessons($course->id);
$course->comment_count = $courseRepo->countComments($course->id);
$course->consult_count = $courseRepo->countConsults($course->id);
$course->review_count = $courseRepo->countReviews($course->id);
$course->favorite_count = $courseRepo->countFavorites($course->id);
$course->update();
$counterCache->rebuild($course->id);
$courseCache->rebuild($course->id);
} else {
$counter = $counterCache->get($course->id);
if ($counter) {
$course->user_count = $counter['user_count'];
$course->lesson_count = $counter['lesson_count'];
$course->comment_count = $counter['comment_count'];
$course->consult_count = $counter['consult_count'];
$course->review_count = $counter['review_count'];
$course->favorite_count = $counter['favorite_count'];
$course->update();
}
}
}

View File

@ -45,18 +45,32 @@ class SyncReviewCounterTask extends Task
return;
}
$cache = new ReviewCounterCache();
$counterCache = new ReviewCounterCache();
$hour = date('H');
foreach ($reviews as $review) {
$counter = $cache->get($review->id);
if ($hour % 3 == 0) {
if ($counter) {
$review->agree_count = $counter['agree_count'];
$review->oppose_count = $counter['oppose_count'];
$review->agree_count = $reviewRepo->countAgrees($review->id);
$review->oppose_count = $reviewRepo->countOpposes($review->id);
$review->update();
$counterCache->rebuild($review->id);
} else {
$counter = $counterCache->get($review->id);
if ($counter) {
$review->agree_count = $counter['agree_count'];
$review->oppose_count = $counter['oppose_count'];
$review->update();
}
}
}

View File

@ -30,7 +30,7 @@
<div style="text-align: center">
{% if order.status == 'pending' %}
<button class="kg-close layui-btn layui-bg-green" data-url="{{ url({'for':'admin.order.close','id':item.id}) }}">关闭订单</button>
<button class="kg-close layui-btn layui-bg-green" data-url="{{ url({'for':'admin.order.close','id':order.id}) }}">关闭订单</button>
{% endif %}
<button class="kg-back layui-btn layui-bg-gray">返回上页</button>
</div>

View File

@ -53,42 +53,4 @@
</tbody>
</table>
{{ partial('partials/pager') }}
<script>
layui.use(['jquery', 'form', 'layer'], function () {
var $ = layui.jquery;
var form = layui.form;
var layer = layui.layer;
form.on('switch(switch-published)', function (data) {
var pageId = $(this).attr('page-id');
var checked = $(this).is(':checked');
var published = checked ? 1 : 0;
var tips = published === 1 ? '确定要发布页面?' : '确定要下架页面?';
layer.confirm(tips, function () {
$.ajax({
type: 'POST',
url: '/admin/page/' + pageId + '/update',
data: {published: published},
success: function (res) {
layer.msg(res.msg, {icon: 1});
},
error: function (xhr) {
var json = JSON.parse(xhr.responseText);
layer.msg(json.msg, {icon: 2});
data.elem.checked = !checked;
form.render();
}
});
}, function () {
data.elem.checked = !checked;
form.render();
});
});
});
</script>
{{ partial('partials/pager') }}

View File

@ -39,7 +39,7 @@
uploader.on('media_progress', function (info) {
if (!isNaN(info.percent)) {
var percent = parseInt(100 * info.percent);
var percent = Math.ceil(100 * info.percent);
element.progress('upload-progress', percent + '%');
}
});

View File

@ -33,8 +33,6 @@
layui.use(['jquery'], function () {
var $ = layui.jquery;
var loopTime = 0;
var tradeSn = $('input[name=trade_sn]').val();
var interval = setInterval(function () {
$.ajax({
@ -54,21 +52,6 @@
clearInterval(interval);
}
});
loopTime += 5;
if (loopTime >= 300) {
$.ajax({
type: 'POST',
url: '/admin/test/alipay/cancel',
data: {trade_sn: tradeSn},
success: function (res) {
},
error: function (xhr) {
}
});
$('#error-tips').removeClass('layui-hide');
$('#qrcode').addClass('layui-hide');
clearInterval(interval);
}
}, 5000);
});

View File

@ -33,8 +33,6 @@
layui.use(['jquery'], function () {
var $ = layui.jquery;
var loopTime = 0;
var tradeSn = $('input[name=trade_sn]').val();
var interval = setInterval(function () {
$.ajax({
@ -54,21 +52,6 @@
clearInterval(interval);
}
});
loopTime += 5;
if (loopTime >= 300) {
$.ajax({
type: 'POST',
url: '/admin/test/wxpay/cancel',
data: {trade_sn: tradeSn},
success: function (res) {
},
error: function (xhr) {
}
});
$('#error-tips').removeClass('layui-hide');
$('#qrcode').addClass('layui-hide');
clearInterval(interval);
}
}, 5000);
});

View File

@ -27,10 +27,10 @@
<div style="text-align: center">
{% if trade.status == 'pending' %}
<button class="kg-close layui-btn layui-bg-green" trade-id="{{ trade.id }}">关闭交易</button>
<button class="kg-close layui-btn layui-bg-green" data-url="{{ url('for':'admin.trade.close','id':trade.id}) }}">关闭交易</button>
{% endif %}
{% if trade.status == 'finished' %}
<button class="kg-refund layui-btn layui-bg-green" trade-id="{{ trade.id }}">申请退款</button>
<button class="kg-refund layui-btn layui-bg-green" data-url="{{ url('for':'admin.trade.refund','id':trade.id}) }}">申请退款</button>
{% endif %}
<button class="kg-back layui-btn layui-bg-gray">返回上页</button>
</div>
@ -110,12 +110,12 @@
var $ = layui.jquery;
$('.kg-close').on('click', function () {
var tradeId = $(this).attr('trade-id');
var url = $(this).attr('data-url');
var tips = '确定要关闭交易吗?';
layer.confirm(tips, function () {
$.ajax({
type: 'POST',
url: '/admin/trade/' + tradeId + '/close',
url: url,
finished: function (res) {
layer.msg(res.msg, {icon: 1});
setTimeout(function () {
@ -133,12 +133,12 @@
});
$('.kg-refund').on('click', function () {
var tradeId = $(this).attr('trade-id');
var url = $(this).attr('data-url');
var tips = '确定要申请退款吗?';
layer.confirm(tips, function () {
$.ajax({
type: 'POST',
url: '/admin/trade/' + tradeId + '/refund',
url: url,
success: function (res) {
layer.msg(res.msg, {icon: 1});
setTimeout(function () {

View File

@ -2,8 +2,6 @@
namespace App\Library\Http;
use App\Exceptions\BadRequest;
class Request extends \Phalcon\Http\Request
{
@ -43,13 +41,13 @@ class Request extends \Phalcon\Http\Request
{
$contentType = $this->getContentType();
if (stripos($contentType, 'form')) {
return parent::getPost($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive);
} elseif (stripos($contentType, 'json')) {
return $this->getPut($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive);
if (stripos($contentType, 'json')) {
$data = $this->getPut($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive);
} else {
throw new BadRequest('sys.invalid_content_type');
$data = parent::getPost($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive);
}
return $data;
}
}

View File

@ -6,33 +6,23 @@ use App\Models\Refund as RefundModel;
use App\Models\Trade as TradeModel;
use App\Repos\Trade as TradeRepo;
use App\Services\Pay as PayService;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use Yansongda\Pay\Gateways\Alipay as AlipayGateway;
use Symfony\Component\HttpFoundation\Response;
use Yansongda\Pay\Log;
use Yansongda\Pay\Pay;
use Yansongda\Supports\Collection;
class Alipay extends PayService
{
/**
* @var array
* @var \Yansongda\Pay\Gateways\Alipay
*/
protected $settings;
protected $gateway;
public function __construct()
public function __construct($gateway = null)
{
$this->settings = $this->getSectionSettings('pay.alipay');
}
$gateway = $gateway instanceof AlipayGateway ? $gateway : new AlipayGateway();
public function setReturnUrl($returnUrl)
{
$this->settings['return_url'] = $returnUrl;
}
public function setNotifyUrl($notifyUrl)
{
$this->settings['notify_url'] = $notifyUrl;
$this->gateway = $gateway->getInstance();
}
/**
@ -43,11 +33,9 @@ class Alipay extends PayService
*/
public function scan(TradeModel $trade)
{
$gateway = $this->getGateway();
try {
$response = $gateway->scan([
$response = $this->gateway->scan([
'out_trade_no' => $trade->sn,
'total_amount' => $trade->amount,
'subject' => $trade->subject,
@ -72,15 +60,13 @@ class Alipay extends PayService
* 移动端支付
*
* @param TradeModel $trade
* @return HttpResponse|bool
* @return Response|bool
*/
public function wap(TradeModel $trade)
{
$gateway = $this->getGateway();
try {
return $gateway->wap([
return $this->gateway->wap([
'out_trade_no' => $trade->sn,
'total_amount' => $trade->amount,
'subject' => $trade->subject,
@ -101,15 +87,13 @@ class Alipay extends PayService
/**
* 异步通知
* @return HttpResponse|bool
* @return Response|bool
*/
public function notify()
{
$gateway = $this->getGateway();
try {
$data = $gateway->verify();
$data = $this->gateway->verify();
Log::debug('Alipay Verify Data', $data->all());
@ -127,17 +111,11 @@ class Alipay extends PayService
return false;
}
if ($data->app_id != $this->settings['app_id']) {
return false;
}
$tradeRepo = new TradeRepo();
$trade = $tradeRepo->findBySn($data->out_trade_no);
if (!$trade) {
return false;
}
if (!$trade) return false;
if ($data->total_amount != $trade->amount) {
return false;
@ -151,7 +129,7 @@ class Alipay extends PayService
$this->eventsManager->fire('pay:afterPay', $this, $trade);
return $gateway->success();
return $this->gateway->success();
}
/**
@ -163,13 +141,11 @@ class Alipay extends PayService
*/
public function find($outTradeNo, $type = 'wap')
{
$gateway = $this->getGateway();
try {
$order = ['out_trade_no' => $outTradeNo];
$result = $gateway->find($order, $type);
$result = $this->gateway->find($order, $type);
} catch (\Exception $e) {
@ -192,11 +168,9 @@ class Alipay extends PayService
*/
public function close($outTradeNo)
{
$gateway = $this->getGateway();
try {
$response = $gateway->close(['out_trade_no' => $outTradeNo]);
$response = $this->gateway->close(['out_trade_no' => $outTradeNo]);
$result = $response->code == '10000';
@ -221,11 +195,9 @@ class Alipay extends PayService
*/
public function cancel($outTradeNo)
{
$gateway = $this->getGateway();
try {
$response = $gateway->cancel(['out_trade_no' => $outTradeNo]);
$response = $this->gateway->cancel(['out_trade_no' => $outTradeNo]);
$result = $response->code == '10000';
@ -250,15 +222,13 @@ class Alipay extends PayService
*/
public function refund(RefundModel $refund)
{
$gateway = $this->getGateway();
try {
$tradeRepo = new TradeRepo();
$trade = $tradeRepo->findById($refund->trade_id);
$response = $gateway->refund([
$response = $this->gateway->refund([
'out_trade_no' => $trade->sn,
'out_request_no' => $refund->sn,
'refund_amount' => $refund->amount,
@ -279,36 +249,4 @@ class Alipay extends PayService
return $result;
}
/**
* 获取 Gateway
*
* @return AlipayGateway
*/
public function getGateway()
{
$config = $this->getDI()->get('config');
$level = $config->env == ENV_DEV ? 'debug' : 'info';
$payConfig = [
'app_id' => $this->settings['app_id'],
'ali_public_key' => $this->settings['public_key'],
'private_key' => $this->settings['private_key'],
'return_url' => $this->settings['return_url'],
'notify_url' => $this->settings['notify_url'],
'log' => [
'file' => log_path('alipay.log'),
'level' => $level,
'type' => 'daily',
'max_file' => 30,
],
];
if ($config->env == ENV_DEV) {
$payConfig['mode'] = 'dev';
}
return Pay::alipay($payConfig);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace App\Services\Pay;
use App\Services\Service;
use Yansongda\Pay\Gateways\Alipay;
use Yansongda\Pay\Pay;
class AlipayGateway extends Service
{
/**
* @var array
*/
protected $settings;
public function __construct($options = [])
{
$defaults = $this->getSectionSettings('pay.alipay');
$this->settings = array_merge($defaults, $options);
}
public function getSettings()
{
return $this->settings;
}
public function setReturnUrl($returnUrl)
{
$this->settings['return_url'] = $returnUrl;
}
public function setNotifyUrl($notifyUrl)
{
$this->settings['notify_url'] = $notifyUrl;
}
/**
* @return Alipay
*/
public function getInstance()
{
$config = $this->getDI()->get('config');
$level = $config->env == ENV_DEV ? 'debug' : 'info';
$payConfig = [
'app_id' => $this->settings['app_id'],
'ali_public_key' => $this->settings['public_key'],
'private_key' => $this->settings['private_key'],
'return_url' => $this->settings['return_url'],
'notify_url' => $this->settings['notify_url'],
'log' => [
'file' => log_path('alipay.log'),
'level' => $level,
'type' => 'daily',
'max_file' => 30,
],
];
if ($config->env == ENV_DEV) {
$payConfig['mode'] = 'dev';
}
return Pay::alipay($payConfig);
}
}

View File

@ -6,28 +6,24 @@ use App\Models\Refund as RefundModel;
use App\Models\Trade as TradeModel;
use App\Repos\Trade as TradeRepo;
use App\Services\Pay as PayService;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use Yansongda\Pay\Gateways\Wechat as WechatGateway;
use Symfony\Component\HttpFoundation\Response;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Log;
use Yansongda\Pay\Pay;
use Yansongda\Supports\Collection;
class Wxpay extends PayService
{
/**
* @var array
* @var Wechat
*/
protected $settings;
protected $gateway;
public function __construct()
public function __construct($gateway = null)
{
$this->settings = $this->getSectionSettings('pay.wxpay');
}
$gateway = $gateway instanceof WxpayGateway ? $gateway : new WxpayGateway();
public function setNotifyUrl($notifyUrl)
{
$this->settings['notify_url'] = $notifyUrl;
$this->gateway = $gateway->getInstance();
}
/**
@ -38,11 +34,9 @@ class Wxpay extends PayService
*/
public function scan(TradeModel $trade)
{
$gateway = $this->getGateway();
try {
$response = $gateway->scan([
$response = $this->gateway->scan([
'out_trade_no' => $trade->sn,
'total_fee' => 100 * $trade->amount,
'body' => $trade->subject,
@ -67,15 +61,13 @@ class Wxpay extends PayService
* 移动端支付
*
* @param TradeModel $trade
* @return HttpResponse|bool
* @return Response|bool
*/
public function wap(TradeModel $trade)
{
$gateway = $this->getGateway();
try {
return $gateway->wap([
return $this->gateway->wap([
'out_trade_no' => $trade->sn,
'total_fee' => 100 * $trade->amount,
'body' => $trade->subject,
@ -97,15 +89,13 @@ class Wxpay extends PayService
/**
* 异步通知
*
* @return HttpResponse|bool
* @return Response|bool
*/
public function notify()
{
$gateway = $this->getGateway();
try {
$data = $gateway->verify();
$data = $this->gateway->verify();
Log::debug('Wxpay Verify Data', $data->all());
@ -123,10 +113,6 @@ class Wxpay extends PayService
return false;
}
if ($data->mch_id != $this->settings['mch_id']) {
return false;
}
$tradeRepo = new TradeRepo();
$trade = $tradeRepo->findBySn($data->out_trade_no);
@ -145,7 +131,7 @@ class Wxpay extends PayService
$this->eventsManager->fire('pay:afterPay', $this, $trade);
return $gateway->success();
return $this->gateway->success();
}
/**
@ -157,13 +143,11 @@ class Wxpay extends PayService
*/
public function find($outTradeNo, $type = 'wap')
{
$gateway = $this->getGateway();
try {
$order = ['out_trade_no' => $outTradeNo];
$result = $gateway->find($order, $type);
$result = $this->gateway->find($order, $type);
} catch (\Exception $e) {
@ -186,11 +170,9 @@ class Wxpay extends PayService
*/
public function close($outTradeNo)
{
$gateway = $this->getGateway();
try {
$response = $gateway->close(['out_trade_no' => $outTradeNo]);
$response = $this->gateway->close(['out_trade_no' => $outTradeNo]);
$result = $response->result_code == 'SUCCESS';
@ -226,15 +208,13 @@ class Wxpay extends PayService
*/
public function refund(RefundModel $refund)
{
$gateway = $this->getGateway();
try {
$tradeRepo = new TradeRepo();
$trade = $tradeRepo->findById($refund->trade_id);
$response = $gateway->refund([
$response = $this->gateway->refund([
'out_trade_no' => $trade->sn,
'out_refund_no' => $refund->sn,
'total_fee' => 100 * $trade->amount,
@ -256,35 +236,4 @@ class Wxpay extends PayService
return $result;
}
/**
* 获取 Gateway
*
* @return WechatGateway
*/
public function getGateway()
{
$config = $this->getDI()->get('config');
$level = $config->env == ENV_DEV ? 'debug' : 'info';
$payConfig = [
'app_id' => $this->settings['app_id'],
'mch_id' => $this->settings['mch_id'],
'key' => $this->settings['key'],
'notify_url' => $this->settings['notify_url'],
'log' => [
'file' => log_path('wxpay.log'),
'level' => $level,
'type' => 'daily',
'max_file' => 30,
],
];
if ($config->env == ENV_DEV) {
$payConfig['mode'] = 'dev';
}
return Pay::wechat($payConfig);
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Services\Pay;
use App\Services\Service as Service;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Pay\Pay;
class WxpayGateway extends Service
{
/**
* @var array
*/
protected $settings;
public function __construct($options = [])
{
$defaults = $this->getSectionSettings('pay.wxpay');
$this->settings = array_merge($defaults, $options);
}
public function getSettings()
{
return $this->settings;
}
public function setNotifyUrl($notifyUrl)
{
$this->settings['notify_url'] = $notifyUrl;
}
/**
* @return Wechat
*/
public function getInstance()
{
$config = $this->getDI()->get('config');
$level = $config->env == ENV_DEV ? 'debug' : 'info';
$payConfig = [
'app_id' => $this->settings['app_id'],
'mch_id' => $this->settings['mch_id'],
'key' => $this->settings['key'],
'notify_url' => $this->settings['notify_url'],
'log' => [
'file' => log_path('wxpay.log'),
'level' => $level,
'type' => 'daily',
'max_file' => 30,
],
];
if ($config->env == ENV_DEV) {
$payConfig['mode'] = 'dev';
}
return Pay::wechat($payConfig);
}
}

View File

@ -16,6 +16,7 @@ layui.use(['jquery', 'form', 'element', 'layer', 'dropdown'], function () {
var csrfTokenValue = $('meta[name="csrf-token-value"]').attr('content');
xhr.setRequestHeader('X-Csrf-Token-Key', csrfTokenKey);
xhr.setRequestHeader('X-Csrf-Token-Value', csrfTokenValue);
xhr.setRequestHeader('Content-Type', 'application/json');
}
});