优化代码
parent
9a89816b67
commit
900b5fefdb
|
@ -41,3 +41,6 @@ MAIL_PASSWORD=xxxx
|
|||
MAIL_FROM_ADDRESS=xxxx@163.com
|
||||
MAIL_FROM_NAME="xxxxx"
|
||||
MAIL_ENCRYPTION=ssl
|
||||
|
||||
# ---- 开启 SQL 日志 ----
|
||||
SQL_QUERY_LOG=true
|
||||
|
|
|
@ -37,8 +37,7 @@ class TestCommand extends HyperfCommand
|
|||
{
|
||||
$repository = di()->get(ExampleRepository::class);
|
||||
|
||||
$repository->case4();
|
||||
|
||||
// $repository->case4();
|
||||
//$api = config('juhe_api.ip');
|
||||
//$options = [];
|
||||
//$client = di()->get(ClientFactory::class)->create($options);
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
namespace App\Controller\Api\V1;
|
||||
|
||||
use App\Helpers\DateHelper;
|
||||
use App\Helpers\StringHelper;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
|
@ -273,7 +275,7 @@ class ArticleController extends CController
|
|||
'title' => $params['title'],
|
||||
'abstract' => mb_substr(strip_tags($params['content']), 0, 200),
|
||||
'class_id' => $params['class_id'],
|
||||
'image' => get_html_images($params['content']),
|
||||
'image' => StringHelper::getHtmlImage($params['content']),
|
||||
'md_content' => htmlspecialchars($params['md_content']),
|
||||
'content' => htmlspecialchars($params['content'])
|
||||
]);
|
||||
|
@ -540,8 +542,7 @@ class ArticleController extends CController
|
|||
if ($rows) {
|
||||
$getDay = function ($delete_at) {
|
||||
$last_time = strtotime('+30 days', strtotime($delete_at));
|
||||
|
||||
return (time() > $last_time) ? 0 : diff_date(date('Y-m-d', $last_time), date('Y-m-d'));
|
||||
return (time() > $last_time) ? 0 : DateHelper::diff(date('Y-m-d', $last_time), date('Y-m-d'));
|
||||
};
|
||||
|
||||
array_walk($rows, function (&$item) use ($getDay) {
|
||||
|
|
|
@ -11,7 +11,7 @@ class ArrayHelper
|
|||
* @param array $items
|
||||
* @return bool
|
||||
*/
|
||||
public static function isRelationArray(array $items): bool
|
||||
public static function isAssociativeArray(array $items): bool
|
||||
{
|
||||
$i = 0;
|
||||
foreach (array_keys($items) as $value) {
|
||||
|
@ -22,4 +22,18 @@ class ArrayHelper
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维数组排序
|
||||
*
|
||||
* @param array $array 数组
|
||||
* @param string $field 排序字段
|
||||
* @param int $sort 排序方式
|
||||
* @return array
|
||||
*/
|
||||
public static function sort(array $array, string $field, $sort = SORT_DESC): array
|
||||
{
|
||||
array_multisort(array_column($array, $field), $sort, $array);
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
/**
|
||||
* 时间处理助手
|
||||
*
|
||||
* @package App\Helpers
|
||||
*/
|
||||
class DateHelper
|
||||
{
|
||||
/**
|
||||
* 获取两个日期相差多少天
|
||||
*
|
||||
* @param string $day1 日期1
|
||||
* @param string $day2 日期2
|
||||
* @return int
|
||||
*/
|
||||
public static function diff(string $day1, string $day2): int
|
||||
{
|
||||
$second1 = strtotime($day1);
|
||||
$second2 = strtotime($day2);
|
||||
|
||||
if ($second1 < $second2) {
|
||||
[$second1, $second2] = [$second2, $second1];
|
||||
}
|
||||
|
||||
return intval(ceil(($second1 - $second2) / 86400));
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This is my open source code, please do not use it for commercial applications.
|
||||
* For the full copyright and license information,
|
||||
|
|
|
@ -20,5 +20,38 @@ namespace App\Helpers;
|
|||
*/
|
||||
class StringHelper
|
||||
{
|
||||
/**
|
||||
* 替换文本中的url 为 a标签
|
||||
*
|
||||
* @param string $str 字符串
|
||||
* @return null|string|string[]
|
||||
*/
|
||||
public static function formatUrlLink(string $str)
|
||||
{
|
||||
$re = '@((https|http)?://([-\w\.]+)+(:\d+)?(/([\w/_\-.#%]*(\?\S+)?)?)?)@';
|
||||
return preg_replace_callback($re, function ($matches) {
|
||||
return sprintf('<a href="%s" target="_blank">%s</a>', trim($matches[0], '"'), $matches[0]);
|
||||
}, $str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从HTML文本中提取所有图片
|
||||
*
|
||||
* @param string $content HTML文本
|
||||
* @return array
|
||||
*/
|
||||
public static function getHtmlImage(string $content): array
|
||||
{
|
||||
$pattern = "/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/";
|
||||
preg_match_all($pattern, htmlspecialchars_decode($content), $match);
|
||||
$data = [];
|
||||
if (!empty($match[1])) {
|
||||
foreach ($match[1] as $img) {
|
||||
if (!empty($img)) $data[] = $img;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,15 @@ class DbQueryExecutedListener implements ListenerInterface
|
|||
}
|
||||
}
|
||||
|
||||
if (env('SQL_QUERY_LOG', false)) {
|
||||
$this->logger->info(sprintf('[%s] %s', $event->time, $sql));
|
||||
return;
|
||||
}
|
||||
|
||||
// SQL 监控,通知管理员(大于两秒)
|
||||
if ($event->time >= 2000) {
|
||||
$this->logger->info(sprintf('[%s] %s', $event->time, $sql));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -392,7 +392,7 @@ trait RepositoryTrait
|
|||
}
|
||||
|
||||
// 判断是否是关联数组
|
||||
if (ArrayHelper::isRelationArray($items)) {
|
||||
if (ArrayHelper::isAssociativeArray($items)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
100
app/helper.php
100
app/helper.php
|
@ -80,10 +80,12 @@ function cache()
|
|||
|
||||
/**
|
||||
* Dispatch an event and call the listeners.
|
||||
*
|
||||
* @return mixed|\Psr\EventDispatcher\EventDispatcherInterface
|
||||
*/
|
||||
function event()
|
||||
{
|
||||
return di()->get(\Psr\EventDispatcher\EventDispatcherInterface::class);
|
||||
return di()->get(Psr\EventDispatcher\EventDispatcherInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,51 +129,14 @@ function response()
|
|||
return di()->get(ResponseInterface::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取邮件助手
|
||||
*
|
||||
* @return \App\Support\Mail|mixed
|
||||
*/
|
||||
function email()
|
||||
{
|
||||
return di()->get(\App\Support\Mail::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从HTML文本中提取所有图片
|
||||
*
|
||||
* @param string $content HTML文本
|
||||
* @return array
|
||||
*/
|
||||
function get_html_images(string $content)
|
||||
{
|
||||
$pattern = "/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/";
|
||||
preg_match_all($pattern, htmlspecialchars_decode($content), $match);
|
||||
$data = [];
|
||||
if (!empty($match[1])) {
|
||||
foreach ($match[1] as $img) {
|
||||
if (!empty($img)) $data[] = $img;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两个日期相差多少天
|
||||
*
|
||||
* @param string $day1 日期1
|
||||
* @param string $day2 日期2
|
||||
* @return float|int
|
||||
*/
|
||||
function diff_date(string $day1, string $day2)
|
||||
{
|
||||
$second1 = strtotime($day1);
|
||||
$second2 = strtotime($day2);
|
||||
|
||||
if ($second1 < $second2) {
|
||||
[$second1, $second2] = [$second2, $second1];
|
||||
}
|
||||
|
||||
return ceil(($second1 - $second2) / 86400);
|
||||
return di()->get(App\Support\Mail::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -180,7 +145,7 @@ function diff_date(string $day1, string $day2)
|
|||
* @param string $path 文件相对路径
|
||||
* @return string
|
||||
*/
|
||||
function get_media_url(string $path)
|
||||
function get_media_url(string $path): string
|
||||
{
|
||||
return sprintf('%s/%s', rtrim(config('domain.img_url'), '/'), ltrim($path, '/'));
|
||||
}
|
||||
|
@ -197,34 +162,6 @@ function create_image_name(string $ext, array $filesize): string
|
|||
return uniqid() . Str::random() . '_' . $filesize[0] . 'x' . $filesize[1] . '.' . $ext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换文本中的url 为 a标签
|
||||
*
|
||||
* @param string $str 字符串
|
||||
* @return null|string|string[]
|
||||
*/
|
||||
function replace_url_link(string $str)
|
||||
{
|
||||
$re = '@((https|http)?://([-\w\.]+)+(:\d+)?(/([\w/_\-.#%]*(\?\S+)?)?)?)@';
|
||||
return preg_replace_callback($re, function ($matches) {
|
||||
return sprintf('<a href="%s" target="_blank">%s</a>', trim($matches[0], '"'), $matches[0]);
|
||||
}, $str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 二维数组排序
|
||||
*
|
||||
* @param array $array 数组
|
||||
* @param string $field 排序字段
|
||||
* @param int $sort 排序方式
|
||||
* @return array
|
||||
*/
|
||||
function arraysSort(array $array, string $field, $sort = SORT_DESC)
|
||||
{
|
||||
array_multisort(array_column($array, $field), $sort, $array);
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断0或正整数
|
||||
*
|
||||
|
@ -232,7 +169,7 @@ function arraysSort(array $array, string $field, $sort = SORT_DESC)
|
|||
* @param bool $isZero 判断是否可为0
|
||||
* @return bool
|
||||
*/
|
||||
function check_int($value, $isZero = false)
|
||||
function check_int($value, $isZero = false): bool
|
||||
{
|
||||
$reg = $isZero ? '/^[+]{0,1}(\d+)$/' : '/^[1-9]\d*$/';
|
||||
return is_numeric($value) && preg_match($reg, $value);
|
||||
|
@ -244,24 +181,11 @@ function check_int($value, $isZero = false)
|
|||
* @param string|int $ids 字符串(例如; "1,2,3,4,5,6")
|
||||
* @return array
|
||||
*/
|
||||
function parse_ids($ids)
|
||||
function parse_ids($ids): array
|
||||
{
|
||||
return array_unique(explode(',', trim($ids)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送消息至 RabbitMQ 队列
|
||||
*
|
||||
* @param \Hyperf\Amqp\Message\ProducerMessage $message
|
||||
* @param bool $confirm
|
||||
* @param int $timeout
|
||||
* @return mixed
|
||||
*/
|
||||
function push_amqp(\Hyperf\Amqp\Message\ProducerMessage $message, bool $confirm = false, int $timeout = 5)
|
||||
{
|
||||
return di()->get(\Hyperf\Amqp\Producer::class)->produce($message, $confirm, $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送消息到 Redis 订阅中
|
||||
*
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
|
@ -12,9 +13,9 @@ declare(strict_types=1);
|
|||
return [
|
||||
'default' => [
|
||||
'handler' => [
|
||||
'class' => Monolog\Handler\StreamHandler::class,
|
||||
'class' => Monolog\Handler\RotatingFileHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'filename' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Monolog\Logger::DEBUG,
|
||||
],
|
||||
],
|
||||
|
|
|
@ -11,8 +11,8 @@ class ArrayHelperTest extends HttpTestCase
|
|||
{
|
||||
public function testIsRelationArray()
|
||||
{
|
||||
$this->assertTrue(!ArrayHelper::isRelationArray([1, 2, 3, 4, 5]), '判断是否是关联数组失败1!');
|
||||
$this->assertTrue(ArrayHelper::isRelationArray([1, 6 => 2, 3, 4, 5]), '判断是否是关联数组失败2!');
|
||||
$this->assertTrue(ArrayHelper::isRelationArray(['k1' => 'test', 'k2' => 'test2']), '判断是否是关联数组失败3!');
|
||||
$this->assertTrue(!ArrayHelper::isAssociativeArray([1, 2, 3, 4, 5]), '判断是否是关联数组失败1!');
|
||||
$this->assertTrue(ArrayHelper::isAssociativeArray([1, 6 => 2, 3, 4, 5]), '判断是否是关联数组失败2!');
|
||||
$this->assertTrue(ArrayHelper::isAssociativeArray(['k1' => 'test', 'k2' => 'test2']), '判断是否是关联数组失败3!');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue