233 lines
5.5 KiB
PHP
Raw Normal View History

2019-12-25 22:12:16 +08:00
<?php
2020-11-29 09:29:14 +08:00
declare(strict_types=1);
2019-12-26 09:03:19 +08:00
namespace catcher;
2020-09-07 08:29:12 +08:00
use catchAdmin\system\model\Config;
2020-04-29 15:05:49 +08:00
use think\facade\Db;
2019-12-26 09:03:19 +08:00
use think\helper\Str;
class Utils
{
/**
* 字符串转换成数组
*
* @time 2019年12月25日
* @param string $string
* @param string $dep
* @return array
*/
public static function stringToArrayBy(string $string, $dep = ','): array
{
if (Str::contains($string, $dep)) {
return explode($dep, trim($string, $dep));
}
return [$string];
}
2020-01-13 21:24:45 +08:00
/**
* 搜索参数
*
* @time 2020年01月13日
* @param array $params
* @param array $range
* @return array
*/
public static function filterSearchParams(array $params, array $range = []): array
{
$search = [];
// $range = array_merge(['created_at' => ['start_at', 'end_at']], $range);
if (!empty($range)) {
foreach ($range as $field => $rangeField) {
if (count($rangeField) === 1) {
$search[$field] = [$params[$rangeField[0]]];
unset($params[$rangeField[0]]);
} else {
$search[$field] = [$params[$rangeField[0]], $params[$rangeField[1]]];
unset($params[$rangeField[0]], $params[$rangeField[1]]);
}
}
}
return array_merge($search, $params);
2020-01-25 22:23:10 +08:00
}
2020-04-29 15:05:49 +08:00
/**
* 导入树形数据
*
* @time 2020年04月29日
* @param $data
* @param $table
* @param string $pid
* @param string $primaryKey
* @return void
*/
public static function importTreeData($data, $table, $pid = 'parent_id',$primaryKey = 'id')
{
foreach ($data as $value) {
if (isset($value[$primaryKey])) {
unset($value[$primaryKey]);
}
$children = $value['children'] ?? false;
if($children) {
unset($value['children']);
}
2020-09-21 16:14:27 +08:00
// 首先查询是否存在
$menu = Db::name($table)
->where('permission_name', $value['permission_name'])
->where('module', $value['module'])
->where('permission_mark', $value['permission_mark'])
->find();
if (!empty($menu)) {
$id = $menu['id'];
} else {
$id = Db::name($table)->insertGetId($value);
}
2020-04-29 15:05:49 +08:00
if ($children) {
foreach ($children as &$v) {
$v[$pid] = $id;
$v['level'] = !$value[$pid] ? $id : $value['level'] . '-' .$id;
}
2020-04-29 15:25:41 +08:00
self::importTreeData($children, $table, $pid);
2020-04-29 15:05:49 +08:00
}
}
}
2020-05-06 17:41:36 +08:00
/**
* 解析 Rule 规则
*
* @time 2020年05月06日
* @param $rule
* @return array
*/
public static function parseRule($rule)
{
[$controller, $action] = explode(Str::contains($rule, '@') ? '@' : '/', $rule);
$controller = explode('\\', $controller);
2020-06-05 15:58:16 +08:00
$controllerName = lcfirst(array_pop($controller));
2020-05-06 17:41:36 +08:00
array_pop($controller);
$module = array_pop($controller);
return [$module, $controllerName, $action];
}
2020-05-22 14:07:55 +08:00
/**
* get controller & action
*
* @time 2020年10月12日
* @param $rule
* @return false|string[]
* @throws \ReflectionException
*/
public static function isMethodNeedAuth($rule)
{
list($controller, $action) = explode(Str::contains($rule, '@') ? '@' : '/', $rule);
$docComment = (new \ReflectionClass($controller))->getMethod($action)->getDocComment();
return strpos($docComment, config('catch.permissions.method_auth_mark')) !== false;
}
2020-05-22 14:07:55 +08:00
/**
* 表前缀
*
* @time 2020年05月22日
* @return mixed
*/
2020-05-22 14:09:48 +08:00
public static function tablePrefix()
2020-05-22 14:07:55 +08:00
{
return \config('database.connections.mysql.prefix');
}
/**
* 删除表前缀
*
* @time 2020年12月01日
* @param string $table
* @return string|string[]
*/
public static function tableWithoutPrefix(string $table)
{
return str_replace(self::tablePrefix(), '', $table);
}
/**
* 添加表前缀
*
* @time 2020年12月26日
* @param string $table
* @return string
*/
public static function tableWithPrefix(string $table)
{
return Str::contains($table, self::tablePrefix()) ?
$table : self::tablePrefix() . $table;
}
/**
* 是否是超级管理员
*
* @time 2020年07月04日
* @return bool
*/
public static function isSuperAdmin()
{
return request()->user()->id == config('catch.permissions.super_admin_id');
}
2020-09-07 08:29:12 +08:00
/**
* 获取配置
*
* @time 2020年09月07日
* @param $key
* @return mixed
*/
public static function config($key)
{
return Config::where('key', $key)->value('value');
}
2020-09-08 14:10:27 +08:00
/**
* public path
*
* @param string $path
* @time 2020年09月08日
* @return string
*/
public static function publicPath($path = '')
{
return root_path($path ? 'public/'. $path : 'public');
}
2021-01-17 09:39:18 +08:00
/**
* 过滤空字符字段
*
* @time 2021年01月16日
* @param $data
* @return mixed
*/
public static function filterEmptyValue($data)
{
foreach ($data as $k => $v) {
if (!$v) {
unset($data[$k]);
}
}
return $data;
}
2019-12-26 09:03:19 +08:00
}