247 lines
6.4 KiB
PHP
247 lines
6.4 KiB
PHP
<?php
|
||
|
||
use Carbon\Carbon;
|
||
use App\Events\FileUploadEvent;
|
||
use Dipper\Foundation\Support\Time;
|
||
use Illuminate\Support\Facades\Event;
|
||
use App\Domains\Account\Services\AccountService;
|
||
|
||
if (! function_exists('cdn')) {
|
||
/**
|
||
* 生成CDN链接, 可带时间戳防盗链
|
||
*
|
||
* @param string $disk 磁盘名称
|
||
* @param string $uri 路径, e.g. common/test/a.jpg/640
|
||
* @param bool $deadline 授权过期时间, 默认true
|
||
*
|
||
* @return string
|
||
*/
|
||
function cdn($disk, $uri, $deadline = true, $size = 640)
|
||
{
|
||
static $disks;
|
||
|
||
if (!$disks[$disk]) {
|
||
$disks[$disk] = app('filesystem')->disk($disk);
|
||
}
|
||
|
||
$disk = $disks[$disk];
|
||
|
||
if ($deadline === true) {
|
||
$deadline = Carbon::today()->addDay(3)->addHour(1)->timestamp;
|
||
} elseif ($deadline !== false) {
|
||
$deadline = Time::parse($deadline)->timestamp;
|
||
}
|
||
|
||
if ($deadline) {
|
||
$symbol = strpos($uri, '?') ? '&' : '?';
|
||
$uri = trim($uri, '/') . $symbol . 'timestamp=' . $deadline;
|
||
}
|
||
|
||
return $disk->url($uri);
|
||
}
|
||
}
|
||
|
||
if (! function_exists('account_avatar')) {
|
||
/**
|
||
* 单次获取账户头像
|
||
*
|
||
* @param int $accountId
|
||
* @param int $deadline
|
||
* @param bool $size
|
||
* @return string
|
||
*/
|
||
function account_avatar($accountId, $deadline = true, $size = 64)
|
||
{
|
||
$path = cdn('data', \App\Domains\Account\Services\AccountService::generateAvatarPath($accountId), $deadline, $size);
|
||
return $path;
|
||
}
|
||
}
|
||
|
||
if (! function_exists('save_cover')) {
|
||
/**
|
||
* 保存封面
|
||
*
|
||
* @param string $type
|
||
* @param int $id
|
||
* @param UploadedFile $file
|
||
* @return void
|
||
*/
|
||
function save_cover($type, $id, $file)
|
||
{
|
||
if ($file instanceof UploadedFile && !$file->isValid()) {
|
||
throw new \App\Exceptions\InvalidArgumentException('文件不正确或已损坏');
|
||
}
|
||
|
||
if (!in_array($file->guessExtension(), ['jpg', 'jpeg', 'gif', 'png', 'bmp'])) {
|
||
throw new \App\Exceptions\InvalidArgumentException('只支持jpg,jpeg,gif,png,bmp文件格式');
|
||
}
|
||
|
||
$filename = "/$type/$id.jpg";
|
||
|
||
Event::fire(new FileUploadEvent($file, [
|
||
'filename' => $filename,
|
||
'type' => $type,
|
||
'typeid' => $id,
|
||
'cover' => true,
|
||
], $disk = 'data'));
|
||
|
||
$url = cdn('data', $filename, time());
|
||
|
||
return $url;
|
||
}
|
||
}
|
||
|
||
if (! function_exists('get_cover')) {
|
||
/**
|
||
* 保存封面
|
||
*
|
||
* @param string $type
|
||
* @param int $id
|
||
* @param UploadedFile $file
|
||
* @return void
|
||
*/
|
||
function get_cover($type, $id, $deadline = true, $size = '640')
|
||
{
|
||
$filename = "/$type/$id.jpg";
|
||
$path = cdn('data', $filename, $deadline, $size);
|
||
return $path;
|
||
}
|
||
}
|
||
|
||
if (! function_exists('config_path')) {
|
||
/**
|
||
* Get the configuration path.
|
||
*
|
||
* @param string $path
|
||
* @return string
|
||
*/
|
||
function config_path($path = '')
|
||
{
|
||
$path = 'config' . DIRECTORY_SEPARATOR . ($path ? DIRECTORY_SEPARATOR.$path : $path);
|
||
|
||
return base_path($path);
|
||
}
|
||
}
|
||
|
||
if (! function_exists('array_merge_sum')) {
|
||
/**
|
||
* 合并数组(相同键名相加)
|
||
*
|
||
* @param array $arr1
|
||
* @param array $arr2
|
||
* @return void
|
||
*/
|
||
function array_merge_sum(...$arrays)
|
||
{
|
||
$array = [];
|
||
|
||
foreach ($arrays as $item) {
|
||
foreach ($item as $key => $value) {
|
||
$array[$key] = isset($array[$key]) ? $array[$key] + $value : $value;
|
||
}
|
||
}
|
||
|
||
return $array;
|
||
}
|
||
}
|
||
|
||
if (! function_exists('human_filesize')) {
|
||
/**
|
||
* 文件大小可读性
|
||
*
|
||
* @param int $int
|
||
* @param int $decimals
|
||
* @param array $options
|
||
* @return void
|
||
*/
|
||
function human_filesize($int, $decimals = 2, array $options = [])
|
||
{
|
||
$size = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||
$factor = floor((strlen($int) - 1) / 3);
|
||
|
||
$options = array_map('strtoupper', $options);
|
||
|
||
if (isset($options['unit']) && array_search($options['unit'], $size) !== false) {
|
||
for ($i=0; $i < array_search($options['unit'], $size); $i++) {
|
||
unset($size[$i]);
|
||
}
|
||
|
||
$size = array_values($size);
|
||
}
|
||
|
||
if (isset($options['min']) && array_search($options['min'], $size) !== false) {
|
||
$factor = $factor < array_search($options['min'], $size) ? array_search($options['min'], $size) : $factor;
|
||
}
|
||
|
||
if (isset($options['max']) && array_search($options['max'], $size) !== false) {
|
||
$factor = $factor > array_search($options['max'], $size) ? array_search($options['max'], $size) : $factor;
|
||
}
|
||
|
||
return sprintf("%.{$decimals}f", $int / pow(1024, $factor)) . @$size[$factor];
|
||
}
|
||
}
|
||
|
||
if (! function_exists('range_compare')) {
|
||
/**
|
||
* 区间比较
|
||
* 0:两区间完全相等
|
||
* 1:区间包含 区间1包含区间2
|
||
* 2:区间包含 区间2包含区间1
|
||
* 3:区间不相交 区间1在左,区间2在右
|
||
* 4:区间不相交 区间1在右,区间2在左
|
||
* 5:区间相交 区间1在左,区间2在右
|
||
* 6:区间相交 区间1在右,区间2在左
|
||
*
|
||
* 情况二:区间不相交 区间1在右,区间2在左 2
|
||
*
|
||
* @param array $array
|
||
* @param array $array
|
||
*/
|
||
function range_compare(array $array1, array $array2)
|
||
{
|
||
$array1 = array_values($array1);
|
||
$array2 = array_values($array2);
|
||
|
||
// 数组必须是两个值
|
||
if (count($array1) !== 2 || count($array2) !== 2) {
|
||
throw new \Exception('array count error.');
|
||
}
|
||
|
||
// 数组第一个值要小于第二个值
|
||
if ($array1[1] < $array1[0] || $array2[1] < $array2[0]) {
|
||
throw new \Exception('array values error.');
|
||
}
|
||
|
||
if ($array1 === $array2) {
|
||
return 0;
|
||
}
|
||
|
||
if ($array1[0] <= $array2[0] && $array1[1] >= $array2[1]) {
|
||
return 1;
|
||
}
|
||
|
||
if ($array1[0] >= $array2[0] && $array1[1] <= $array2[1]) {
|
||
return 2;
|
||
}
|
||
|
||
|
||
if ($array1[1] < $array2[0]) {
|
||
return 3;
|
||
}
|
||
|
||
if ($array2[1] < $array1[0]) {
|
||
return 4;
|
||
}
|
||
|
||
if ($array1[0] < $array2[0]) {
|
||
return 5;
|
||
}
|
||
|
||
if ($array2[0] < $array1[0]) {
|
||
return 6;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
}
|