2020-11-02 22:45:37 +08:00
|
|
|
<?php
|
2020-12-02 17:15:32 +08:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Common function method
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-11-02 22:45:37 +08:00
|
|
|
use Hyperf\Contract\StdoutLoggerInterface;
|
|
|
|
use Hyperf\HttpServer\Contract\ResponseInterface;
|
|
|
|
use Hyperf\Logger\LoggerFactory;
|
|
|
|
use Hyperf\Server\ServerFactory;
|
|
|
|
use Hyperf\Utils\ApplicationContext;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Swoole\Websocket\Frame;
|
|
|
|
use Swoole\WebSocket\Server as WebSocketServer;
|
2020-11-07 22:57:10 +08:00
|
|
|
use Hyperf\Utils\Str;
|
2020-11-08 17:10:05 +08:00
|
|
|
use Hyperf\Redis\Redis;
|
2020-11-02 22:45:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 容器实例
|
2020-12-02 17:15:32 +08:00
|
|
|
*
|
|
|
|
* @return \Psr\Container\ContainerInterface
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2021-07-23 21:34:29 +08:00
|
|
|
function di()
|
2020-11-07 22:57:10 +08:00
|
|
|
{
|
|
|
|
return ApplicationContext::getContainer();
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redis 客户端实例
|
2020-12-02 17:15:32 +08:00
|
|
|
*
|
|
|
|
* @return Redis|mixed
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function redis()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(Redis::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-03 16:22:55 +08:00
|
|
|
* Server 实例 基于 Swoole Server
|
2020-11-29 14:44:11 +08:00
|
|
|
*
|
|
|
|
* @return \Swoole\Coroutine\Server|\Swoole\Server
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function server()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(ServerFactory::class)->getServer()->getServer();
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-03 16:22:55 +08:00
|
|
|
* WebSocket frame 实例
|
|
|
|
*
|
|
|
|
* @return mixed|Frame
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function frame()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(Frame::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-03 16:22:55 +08:00
|
|
|
* WebSocketServer 实例
|
|
|
|
*
|
|
|
|
* @return mixed|WebSocketServer
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function websocket()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(WebSocketServer::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 缓存实例 简单的缓存
|
2020-12-03 16:22:55 +08:00
|
|
|
*
|
|
|
|
* @return mixed|\Psr\SimpleCache\CacheInterface
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function cache()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(Psr\SimpleCache\CacheInterface::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
2021-07-10 10:55:25 +08:00
|
|
|
/**
|
|
|
|
* Dispatch an event and call the listeners.
|
|
|
|
*/
|
|
|
|
function event()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(\Psr\EventDispatcher\EventDispatcherInterface::class);
|
2021-07-10 10:55:25 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 22:45:37 +08:00
|
|
|
/**
|
|
|
|
* 控制台日志
|
2020-12-03 16:22:55 +08:00
|
|
|
*
|
|
|
|
* @return StdoutLoggerInterface|mixed
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function stdout_log()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(StdoutLoggerInterface::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 文件日志
|
2020-11-29 14:44:11 +08:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return \Psr\Log\LoggerInterface
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function logger(string $name = 'APP')
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(LoggerFactory::class)->get($name);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-02 17:15:32 +08:00
|
|
|
* Http 请求实例
|
|
|
|
*
|
|
|
|
* @return mixed|ServerRequestInterface
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function request()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(ServerRequestInterface::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 请求响应
|
2020-12-02 17:15:32 +08:00
|
|
|
*
|
|
|
|
* @return ResponseInterface|mixed
|
2020-11-02 22:45:37 +08:00
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function response()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(ResponseInterface::class);
|
2020-11-04 16:47:17 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 11:31:36 +08:00
|
|
|
|
|
|
|
function email()
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(\App\Support\Mail::class);
|
2021-05-24 11:31:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-05 17:40:51 +08:00
|
|
|
/**
|
|
|
|
* 从HTML文本中提取所有图片
|
2020-12-03 11:57:46 +08:00
|
|
|
*
|
|
|
|
* @param string $content HTML文本
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2021-05-24 11:31:36 +08:00
|
|
|
function get_html_images(string $content)
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取两个日期相差多少天
|
|
|
|
*
|
2020-12-03 11:57:46 +08:00
|
|
|
* @param string $day1 日期1
|
|
|
|
* @param string $day2 日期2
|
2020-11-05 17:40:51 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
2021-05-24 11:31:36 +08:00
|
|
|
function diff_date(string $day1, string $day2)
|
2020-11-05 17:40:51 +08:00
|
|
|
{
|
|
|
|
$second1 = strtotime($day1);
|
|
|
|
$second2 = strtotime($day2);
|
|
|
|
|
|
|
|
if ($second1 < $second2) {
|
|
|
|
[$second1, $second2] = [$second2, $second1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ceil(($second1 - $second2) / 86400);
|
|
|
|
}
|
2020-11-07 22:57:10 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取媒体文件url
|
|
|
|
*
|
|
|
|
* @param string $path 文件相对路径
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_media_url(string $path)
|
|
|
|
{
|
2021-04-22 16:14:34 +08:00
|
|
|
return sprintf('%s/%s', rtrim(config('domain.img_url'), '/'), ltrim($path, '/'));
|
2020-11-07 22:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 随机生成图片名
|
|
|
|
*
|
2021-06-30 19:27:49 +08:00
|
|
|
* @param string $ext 图片后缀名
|
|
|
|
* @param array $filesize 图片文件大小信息
|
2020-11-07 22:57:10 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2021-06-30 19:27:49 +08:00
|
|
|
function create_image_name(string $ext, array $filesize)
|
2020-11-07 22:57:10 +08:00
|
|
|
{
|
2021-06-30 19:27:49 +08:00
|
|
|
return uniqid() . Str::random() . '_' . $filesize[0] . 'x' . $filesize[1] . '.' . $ext;
|
2020-11-08 17:10:05 +08:00
|
|
|
}
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 替换文本中的url 为 a标签
|
|
|
|
*
|
2020-12-03 11:57:46 +08:00
|
|
|
* @param string $str 字符串
|
2020-11-09 22:59:25 +08:00
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 二维数组排序
|
2020-12-02 17:15:32 +08:00
|
|
|
*
|
2021-04-22 16:14:34 +08:00
|
|
|
* @param array $array 数组
|
2020-11-09 22:59:25 +08:00
|
|
|
* @param string $field 排序字段
|
2021-04-22 16:14:34 +08:00
|
|
|
* @param int $sort 排序方式
|
2020-11-09 22:59:25 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2021-05-24 11:31:36 +08:00
|
|
|
function arraysSort(array $array, string $field, $sort = SORT_DESC)
|
2020-11-09 22:59:25 +08:00
|
|
|
{
|
|
|
|
array_multisort(array_column($array, $field), $sort, $array);
|
|
|
|
return $array;
|
2020-11-21 19:53:01 +08:00
|
|
|
}
|
2020-11-29 17:39:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断0或正整数
|
|
|
|
*
|
2021-05-24 11:31:36 +08:00
|
|
|
* @param string|int $value 验证字符串
|
|
|
|
* @param bool $isZero 判断是否可为0
|
2020-11-29 17:39:24 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-05-24 11:31:36 +08:00
|
|
|
function check_int($value, $isZero = false)
|
2020-11-29 17:39:24 +08:00
|
|
|
{
|
|
|
|
$reg = $isZero ? '/^[+]{0,1}(\d+)$/' : '/^[1-9]\d*$/';
|
2021-05-24 11:31:36 +08:00
|
|
|
return is_numeric($value) && preg_match($reg, $value);
|
2020-11-29 17:39:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-02 17:15:32 +08:00
|
|
|
/**
|
|
|
|
* 解析英文逗号',' 拼接的 ID 字符串
|
|
|
|
*
|
2021-05-24 11:31:36 +08:00
|
|
|
* @param string|int $ids 字符串(例如; "1,2,3,4,5,6")
|
2020-12-02 17:15:32 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-11-29 17:39:24 +08:00
|
|
|
function parse_ids($ids)
|
|
|
|
{
|
|
|
|
return array_unique(explode(',', trim($ids)));
|
|
|
|
}
|
2021-05-13 18:01:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 推送消息至 RabbitMQ 队列
|
|
|
|
*
|
2021-07-10 10:55:25 +08:00
|
|
|
* @param \Hyperf\Amqp\Message\ProducerMessage $message
|
|
|
|
* @param bool $confirm
|
|
|
|
* @param int $timeout
|
|
|
|
* @return mixed
|
2021-05-13 18:01:34 +08:00
|
|
|
*/
|
2021-07-10 10:55:25 +08:00
|
|
|
function push_amqp(\Hyperf\Amqp\Message\ProducerMessage $message, bool $confirm = false, int $timeout = 5)
|
2021-05-13 18:01:34 +08:00
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
return di()->get(\Hyperf\Amqp\Producer::class)->produce($message, $confirm, $timeout);
|
2021-05-13 18:01:34 +08:00
|
|
|
}
|
2021-05-24 11:31:36 +08:00
|
|
|
|
2021-07-07 19:43:09 +08:00
|
|
|
/**
|
|
|
|
* 推送消息到 Redis 订阅中
|
|
|
|
*
|
|
|
|
* @param string $chan
|
|
|
|
* @param string|array $message
|
|
|
|
*/
|
|
|
|
function push_redis_subscribe(string $chan, $message)
|
|
|
|
{
|
|
|
|
redis()->publish($chan, is_string($message) ? $message : json_encode($message));
|
|
|
|
}
|
|
|
|
|
2021-06-30 19:27:49 +08:00
|
|
|
/**
|
|
|
|
* 生成随机文件名
|
|
|
|
*
|
|
|
|
* @param string $ext 文件后缀名
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function create_random_filename(string $ext)
|
|
|
|
{
|
|
|
|
$ext = $ext ? '.' . $ext : '';
|
|
|
|
return Str::random(10) . uniqid() . $ext;
|
|
|
|
}
|