*/
function de($data = [])
{
echo "";
if (empty($data) || is_bool($data)) {
var_dump($data);
exit();
}
if (empty($data)) exit;
print_r($data);
exit;
}
}
if (!function_exists('dp')) {
/**
* 调试打印函数,执行后后续代码程序将终止
* @param mixed $data 需要打印的数据
* @author fuyelk
*/
function dp($data = [])
{
echo "";
if (empty($data) || is_bool($data)) {
var_dump($data);
} else {
print_r($data);
}
}
}
if (!function_exists('dt')) {
/**
* 日志
* @param array $data 需要打印的数据
* @param string $name 数据说明
* @return string 日志唯一标识
* @author fuyelk
*/
function dt($data = [], $name = '')
{
$log_id = uniqid();
trace("########### [$log_id] 日志开始 ############");
trace($name);
trace($data);
trace("########### [$log_id] 日志结束 ###########");
return $log_id;
}
}
if (!function_exists('dta')) {
/**
* 实时追加型日志
* @param mixed $log 日志内容
* @param string $name 数据说明
* @return string 日志唯一标识
* @author fuyelk
*/
function dta($log, $name = '')
{
if (!think\Env::get('app.dta', true)) {
return '';
}
$debug = debug_backtrace();
$log = is_object($log) ? (array)$log : $log;
$log_id = uniqid();
$trace_root = dirname(__DIR__) . '/runtime/log/' . date('Ym');
$file = $trace_root . '/' . date('d') . '_a.log';
if (!is_dir(dirname($file))) mkdir(dirname($file), 0755, true);
$msg = "============ [$log_id] 日志开始 ============" . PHP_EOL;;
$msg .= '[ file ] ' . $debug[0]['file'] . ':' . $debug[0]['line'] . PHP_EOL;
$msg .= '[ time ] ' . date('Y-m-d H:i:s') . PHP_EOL;
$msg .= '[ name ] ' . $name . PHP_EOL;
$msg .= '[ data ] ' . (is_array($log) ? var_export($log, true) : $log) . PHP_EOL;
$fp = @fopen($file, 'a');
fwrite($fp, $msg);
fclose($fp);
return date('Ymd') . $log_id;
}
}
if (!function_exists('get_log')) {
/**
* 获取日志
* @param string $log 日志ID
* @return bool|string
* @author fuyelk
* @date 2021/4/21 13:18
*/
function get_log($log)
{
if (empty($log)) return false;
$logFile = dirname(__DIR__) . '/runtime/log/' . mb_substr($log, 0, 6) . '/' . mb_substr($log, 6, 2) . '_a.log';
if (!is_file($logFile)) return false;
$content = file_get_contents($logFile);
$logName = mb_substr($log, 8);
$tag = "============ [$logName] 日志开始 ============";
$startIndex = mb_strpos($content, $tag);
$content = mb_substr($content, $startIndex);
$endIndex = mb_strpos($content, '============ [', 10);
if (false === $endIndex) return $content;
return mb_substr($content, 0, $endIndex);
}
}
if (!function_exists('add_submenu')) {
/**
* Fastadmin 菜单节点添加增删改查
* @param string $menu 节点名
* @param int $pid 父节点ID
* @return int|string
* @author fuyelk
* @date 2021/06/28 14:32
*/
function add_submenu(string $menu, int $pid)
{
$time = time();
return db('auth_rule')->insertAll([
[
'pid' => $pid,
'name' => $menu . '/index',
'title' => 'View',
'icon' => 'fa fa-circle-o',
'status' => 'normal',
'createtime' => $time,
'updatetime' => $time,
],
[
'pid' => $pid,
'name' => $menu . '/add',
'title' => 'Add',
'icon' => 'fa fa-circle-o',
'status' => 'normal',
'createtime' => $time,
'updatetime' => $time,
],
[
'pid' => $pid,
'name' => $menu . '/del',
'title' => 'Del',
'icon' => 'fa fa-circle-o',
'status' => 'normal',
'createtime' => $time,
'updatetime' => $time,
],
[
'pid' => $pid,
'name' => $menu . '/edit',
'title' => 'Edit',
'icon' => 'fa fa-circle-o',
'status' => 'normal',
'createtime' => $time,
'updatetime' => $time,
],
[
'pid' => $pid,
'name' => $menu . '/multi',
'title' => 'Multi',
'icon' => 'fa fa-circle-o',
'status' => 'normal',
'createtime' => $time,
'updatetime' => $time,
],
]);
}
}
if (!function_exists('byte_conversion')) {
/**
* 格式化字节单位
* @param int $size 字节大小
* @param int $decimals 小数点位数
* @param string $delimiter 分隔符
* @return string
*/
function byte_conversion($size = 0, $decimals = 3, $delimiter = ' ')
{
if ($size > pow(1024, 6)) {
$size /= pow(1024, 5);
return number_format($size, $decimals) . $delimiter . 'PB';
}
$units = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB');
for ($i = 0; $size >= 1024 && $i < 6; $i++) {
$size /= 1024;
}
return number_format($size, $decimals) . $delimiter . $units[$i];
}
}
if (!function_exists('create_tips_icon')) {
/**
* 创建角标
* @description 应用在a标签中:{:create_tips_icon(1)}
* @param string|number $tips 角标
* @param string $color 颜色
* @return string
* @author fuyelk
* @date 2021/07/14 16:39
*/
function create_tips_icon($tips, $color = 'yellow')
{
if (empty($tips)) {
return '';
}
return " {$tips}";
}
}