2020-11-02 22:45:37 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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-11-07 22:57:10 +08:00
|
|
|
function container()
|
|
|
|
{
|
|
|
|
return ApplicationContext::getContainer();
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redis 客户端实例
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function redis()
|
|
|
|
{
|
|
|
|
return container()->get(Redis::class);
|
2020-11-02 22:45:37 +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()
|
|
|
|
{
|
|
|
|
return container()->get(ServerFactory::class)->getServer()->getServer();
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
2020-11-07 22:57:10 +08:00
|
|
|
|
2020-11-02 22:45:37 +08:00
|
|
|
/**
|
|
|
|
* websocket frame 实例
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function frame()
|
|
|
|
{
|
|
|
|
return container()->get(Frame::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* websocket 实例
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function websocket()
|
|
|
|
{
|
|
|
|
return container()->get(WebSocketServer::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 缓存实例 简单的缓存
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function cache()
|
|
|
|
{
|
|
|
|
return container()->get(Psr\SimpleCache\CacheInterface::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 控制台日志
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function stdout_log()
|
|
|
|
{
|
|
|
|
return container()->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')
|
|
|
|
{
|
|
|
|
return container()->get(LoggerFactory::class)->get($name);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* http 请求实例
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function request()
|
|
|
|
{
|
|
|
|
return container()->get(ServerRequestInterface::class);
|
2020-11-02 22:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 请求响应
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function response()
|
|
|
|
{
|
|
|
|
return container()->get(ResponseInterface::class);
|
2020-11-04 16:47:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取加密后的密码字符
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @return bool|false|null|string
|
|
|
|
*/
|
2020-11-07 22:57:10 +08:00
|
|
|
function create_password(string $password)
|
|
|
|
{
|
2020-11-04 16:47:17 +08:00
|
|
|
return password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
}
|
2020-11-05 17:40:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 从HTML文本中提取所有图片
|
|
|
|
* @param $content
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_html_images($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 $day1
|
|
|
|
* @param $day2
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
function diff_date($day1, $day2)
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
{
|
2020-11-21 19:53:01 +08:00
|
|
|
return config('domain.img_url') . $path;
|
2020-11-07 22:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 随机生成图片名
|
|
|
|
*
|
|
|
|
* @param string $ext 图片后缀名
|
|
|
|
* @param int $width 图片宽度
|
|
|
|
* @param int $height 图片高度
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function create_image_name(string $ext, int $width, int $height)
|
|
|
|
{
|
2020-11-29 14:44:11 +08:00
|
|
|
return uniqid() . Str::random(16) . uniqid() . '_' . $width . 'x' . $height . '.' . $ext;
|
2020-11-08 17:10:05 +08:00
|
|
|
}
|
2020-11-09 22:59:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 替换文本中的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, $field, $sort = SORT_DESC)
|
|
|
|
{
|
|
|
|
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或正整数
|
|
|
|
*
|
|
|
|
* @param string $int 验证字符串
|
|
|
|
* @param bool $isZero 判断是否可为0
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function check_int($int, $isZero = false)
|
|
|
|
{
|
|
|
|
$reg = $isZero ? '/^[+]{0,1}(\d+)$/' : '/^[1-9]\d*$/';
|
|
|
|
return is_numeric($int) && preg_match($reg, $int);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse_ids($ids)
|
|
|
|
{
|
|
|
|
return array_unique(explode(',', trim($ids)));
|
|
|
|
}
|