vd/app/helpers.php
2019-04-15 17:13:33 +08:00

286 lines
8.5 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
}
$result = $int / pow(1024, $factor);
return sprintf("%.{$decimals}f", $result) . @$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;
}
}
if (!function_exists('single_case')) {
/**
* 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
*
* @access public
* @param string $str 待转换字串
* @return string
*/
function single_case($str)
{
$arr = array('' => '0', '' => '1', '' => '2', '' => '3', '' => '4',
'' => '5', '' => '6', '' => '7', '' => '8', '' => '9',
'' => 'A', '' => 'B', '' => 'C', '' => 'D', '' => 'E',
'' => 'F', '' => 'G', '' => 'H', '' => 'I', '' => 'J',
'' => 'K', '' => 'L', '' => 'M', '' => 'N', '' => 'O',
'' => 'P', '' => 'Q', '' => 'R', '' => 'S', '' => 'T',
'' => 'U', '' => 'V', '' => 'W', '' => 'X', '' => 'Y',
'' => 'Z', '' => 'a', '' => 'b', '' => 'c', '' => 'd',
'' => 'e', '' => 'f', '' => 'g', '' => 'h', '' => 'i',
'' => 'j', '' => 'k', '' => 'l', '' => 'm', '' => 'n',
'' => 'o', '' => 'p', '' => 'q', '' => 'r', '' => 's',
'' => 't', '' => 'u', '' => 'v', '' => 'w', '' => 'x',
'' => 'y', '' => 'z',
'' => '(', '' => ')', '' => '[', '' => ']', '【' => '[',
'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
'' => '[', '' => ']', '' => '{', '' => '}', '《' => '<',
'》' => '>',
'' => '%', '' => '+', '—' => '-', '' => '-', '' => '-',
'' => ':', '。' => '.', '、' => ',', '' => '.', '、' => '.',
'' => ',', '' => '?', '' => '!', '…' => '-', '‖' => '|',
'”' => '"', '' => '`', '' => '`', '' => '|', '〃' => '"',
' ' => ' ');
return strtr($str, $arr);
}
}