From 900b5fefdbda3688ce84898d848bb2598ab09f43 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Sat, 4 Sep 2021 19:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 3 + app/Command/TestCommand.php | 3 +- app/Controller/Api/V1/ArticleController.php | 7 +- app/Helpers/ArrayHelper.php | 16 +++- app/Helpers/DateHelper.php | 31 ++++++ app/Helpers/HashHelper.php | 1 + app/Helpers/StringHelper.php | 33 +++++++ app/Listener/DbQueryExecutedListener.php | 10 +- app/Traits/RepositoryTrait.php | 2 +- app/helper.php | 100 +++----------------- config/autoload/logger.php | 15 +-- test/Cases/Helper/ArrayHelperTest.php | 6 +- 12 files changed, 121 insertions(+), 106 deletions(-) create mode 100644 app/Helpers/DateHelper.php diff --git a/.env.example b/.env.example index 43d9853..cfeefa0 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/app/Command/TestCommand.php b/app/Command/TestCommand.php index 119a46e..212bd6a 100644 --- a/app/Command/TestCommand.php +++ b/app/Command/TestCommand.php @@ -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); diff --git a/app/Controller/Api/V1/ArticleController.php b/app/Controller/Api/V1/ArticleController.php index a822d95..1511729 100644 --- a/app/Controller/Api/V1/ArticleController.php +++ b/app/Controller/Api/V1/ArticleController.php @@ -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) { diff --git a/app/Helpers/ArrayHelper.php b/app/Helpers/ArrayHelper.php index c84d7d1..4f79565 100644 --- a/app/Helpers/ArrayHelper.php +++ b/app/Helpers/ArrayHelper.php @@ -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; + } } diff --git a/app/Helpers/DateHelper.php b/app/Helpers/DateHelper.php new file mode 100644 index 0000000..d07adcd --- /dev/null +++ b/app/Helpers/DateHelper.php @@ -0,0 +1,31 @@ +%s', trim($matches[0], '"'), $matches[0]); + }, $str); + } + /** + * 从HTML文本中提取所有图片 + * + * @param string $content HTML文本 + * @return array + */ + public static function getHtmlImage(string $content): array + { + $pattern = "//"; + 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; + } } diff --git a/app/Listener/DbQueryExecutedListener.php b/app/Listener/DbQueryExecutedListener.php index f5d39f0..c5c0fd6 100644 --- a/app/Listener/DbQueryExecutedListener.php +++ b/app/Listener/DbQueryExecutedListener.php @@ -56,7 +56,15 @@ class DbQueryExecutedListener implements ListenerInterface } } - $this->logger->info(sprintf('[%s] %s', $event->time, $sql)); + 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)); + } } } } diff --git a/app/Traits/RepositoryTrait.php b/app/Traits/RepositoryTrait.php index 493c578..07685eb 100644 --- a/app/Traits/RepositoryTrait.php +++ b/app/Traits/RepositoryTrait.php @@ -392,7 +392,7 @@ trait RepositoryTrait } // 判断是否是关联数组 - if (ArrayHelper::isRelationArray($items)) { + if (ArrayHelper::isAssociativeArray($items)) { return false; } diff --git a/app/helper.php b/app/helper.php index 0f8ba2f..8e5ce4a 100644 --- a/app/helper.php +++ b/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 = "//"; - 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('%s', 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 订阅中 * diff --git a/config/autoload/logger.php b/config/autoload/logger.php index ee4691f..da40a7a 100644 --- a/config/autoload/logger.php +++ b/config/autoload/logger.php @@ -1,6 +1,7 @@ [ - 'handler' => [ - 'class' => Monolog\Handler\StreamHandler::class, + 'handler' => [ + 'class' => Monolog\Handler\RotatingFileHandler::class, 'constructor' => [ - 'stream' => BASE_PATH . '/runtime/logs/hyperf.log', - 'level' => Monolog\Logger::DEBUG, + 'filename' => BASE_PATH . '/runtime/logs/hyperf.log', + 'level' => Monolog\Logger::DEBUG, ], ], 'formatter' => [ - 'class' => Monolog\Formatter\LineFormatter::class, + 'class' => Monolog\Formatter\LineFormatter::class, 'constructor' => [ - 'format' => null, - 'dateFormat' => 'Y-m-d H:i:s', + 'format' => null, + 'dateFormat' => 'Y-m-d H:i:s', 'allowInlineLineBreaks' => true, ], ], diff --git a/test/Cases/Helper/ArrayHelperTest.php b/test/Cases/Helper/ArrayHelperTest.php index 10765a3..fc91cb4 100644 --- a/test/Cases/Helper/ArrayHelperTest.php +++ b/test/Cases/Helper/ArrayHelperTest.php @@ -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!'); } }