This commit is contained in:
walkor 2022-12-13 19:56:33 +08:00
parent fe6abf93f4
commit 3ee71065fb
3 changed files with 72 additions and 30 deletions

View File

@ -2,23 +2,22 @@
namespace plugin\admin\app\common;
use Throwable;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Builder;
use plugin\admin\app\model\Option;
use support\Db;
use support\exception\BusinessException;
use Throwable;
use function config;
use support\Db;
class Util
{
/**
* 密码哈希
* @param $password
* @param $algo
* @param string $algo
* @return false|string|null
*/
public static function passwordHash($password, $algo = PASSWORD_DEFAULT)
public static function passwordHash($password, string $algo = PASSWORD_DEFAULT)
{
return password_hash($password, $algo);
}
@ -29,7 +28,7 @@ class Util
* @param $hash
* @return bool
*/
public static function passwordVerify($password, $hash): bool
public static function passwordVerify(string $password, string $hash): bool
{
return password_verify($password, $hash);
}
@ -38,7 +37,7 @@ class Util
* 获取webman-admin数据库连接
* @return Connection
*/
static function db(): Connection
public static function db(): Connection
{
return Db::connection('plugin.admin.mysql');
}
@ -47,11 +46,59 @@ class Util
* 获取SchemaBuilder
* @return Builder
*/
static function schema(): Builder
public static function schema(): Builder
{
return Db::schema('plugin.admin.mysql');
}
/**
* 获取语义化时间
* @param $time
* @return false|string
*/
public static function humanDate($time)
{
$timestamp = is_numeric($time) ? $time : strtotime($time);
$dur = time() - $timestamp;
if ($dur < 0) {
return date('Y-m-d', $timestamp);
} else {
if ($dur < 60) {
return $dur . '秒前';
} else {
if ($dur < 3600) {
return floor($dur / 60) . '分钟前';
} else {
if ($dur < 86400) {
return floor($dur / 3600) . '小时前';
} else {
if ($dur < 2592000) { // 30天内
return floor($dur / 86400) . '天前';
} else {
return date('Y-m-d', $timestamp);;
}
}
}
}
}
return date('Y-m-d', $timestamp);
}
/**
* 格式化文件大小
* @param $file_size
* @return string
*/
public static function formatBytes($file_size): string
{
$size = sprintf("%u", $file_size);
if($size == 0) {
return("0 Bytes");
}
$size_name = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $size_name[$i];
}
/**
* 数据库字符串转义
* @param $var

View File

@ -306,19 +306,4 @@ class UploadController extends Crud
];
}
/**
* 格式化文件大小
* @param $file_size
* @return string
*/
protected function formatSize($file_size): string
{
$size = sprintf("%u", $file_size);
if($size == 0) {
return("0 Bytes");
}
$size_name = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $size_name[$i];
}
}

View File

@ -5,13 +5,13 @@
use app\model\User;
use plugin\admin\app\model\Admin;
use support\exception\BusinessException;
use plugin\admin\app\model\Role;
/**
* 当前管理员id
* @return integer|null
*/
function admin_id()
function admin_id(): ?int
{
return session('admin.id');
}
@ -20,7 +20,6 @@ function admin_id()
* 当前管理员
* @param null|array|string $fields
* @return array|mixed|null
* @throws BusinessException
*/
function admin($fields = null)
{
@ -41,11 +40,25 @@ function admin($fields = null)
return $admin[$fields] ?? null;
}
/**
* 是否是超级管理员
* @return bool
*/
function is_supper_admin(): bool
{
$roles = admin('roles');
if (!$roles) {
return false;
}
$rules = Role::whereIn('id', $roles)->pluck('rules');
return $rules && in_array('*', $rules->toArray());
}
/**
* 当前登录用户id
* @return integer|null
*/
function user_id()
function user_id(): ?int
{
return session('user.id');
}
@ -54,7 +67,6 @@ function user_id()
* 当前登录用户
* @param null|array|string $fields
* @return array|mixed|null
* @throws BusinessException
*/
function user($fields = null)
{
@ -79,7 +91,6 @@ function user($fields = null)
* 刷新当前管理员session
* @param bool $force
* @return void
* @throws BusinessException
*/
function refresh_admin_session(bool $force = false)
{
@ -111,7 +122,6 @@ function refresh_admin_session(bool $force = false)
* 刷新当前用户session
* @param bool $force
* @return void
* @throws BusinessException
*/
function refresh_user_session(bool $force = false)
{