diff --git a/application/admin/Controller.php b/application/admin/Controller.php index a7bd627..3cac7d2 100644 --- a/application/admin/Controller.php +++ b/application/admin/Controller.php @@ -288,6 +288,7 @@ protected function filterId($filterData, $error = '该记录不能执行此操 * $map['_order_by'] 可强制设置排序字段(field asc|desc[,filed2 asc|desc...]或者false) * $map['_paginate'] 是否开启分页,传入false可以关闭分页 * $map['_model'] 可强制指定模型 + * $map['_func'] 匿名函数,可以给模型设置属性,比如关联,alias,function ($model) {$model->alias('table')->join(...)} * * @param Model|Db $model 数据对象 * @param array $map 过滤条件 @@ -300,13 +301,17 @@ protected function filterId($filterData, $error = '该记录不能执行此操 protected function datalist($model, $map, $field = null, $sortBy = '', $asc = false, $return = false, $paginate = true) { // 私有字段,指定特殊条件,查询时要删除 - $protectField = ['_table', '_relation', '_field', '_order_by', '_paginate', '_model']; + $protectField = ['_table', '_relation', '_field', '_order_by', '_paginate', '_model', '_func']; // 通过过滤器指定模型 if (isset($map['_model'])) { $model = $map['_model']; } + if (isset($map['_func']) && ($map['_func'] instanceof \Closure)) { + call_user_func_array($map['_func'], [$model]); + } + // 排序字段 默认为主键名 $order = $this->request->param('_order') ?: (empty($sortBy) ? $model->getPk() : $sortBy); diff --git a/application/admin/common.php b/application/admin/common.php index 9f06eac..6be2423 100644 --- a/application/admin/common.php +++ b/application/admin/common.php @@ -240,11 +240,13 @@ function reset_by_key($arr, $key) /** * 节点遍历 - * @param $list + * + * @param $list * @param string $pk * @param string $pid * @param string $child - * @param int $root + * @param int $root + * * @return array */ function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) @@ -255,10 +257,16 @@ function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root // 创建基于主键的数组引用 $refer = []; foreach ($list as $key => $data) { + if ($data instanceof \think\Model) { + $list[$key] = $data->toArray(); + } $refer[$data[$pk]] =& $list[$key]; } foreach ($list as $key => $data) { // 判断是否存在parent + if (!isset($list[$key][$child])) { + $list[$key][$child] = []; + } $parentId = $data[$pid]; if ($root == $parentId) { $tree[] =& $list[$key]; @@ -418,3 +426,79 @@ function format_bytes($size, $delimiter = '') return round($size, 2) . $delimiter . $units[$i]; } + +/** + * 生成一定长度的UUID + * + * @param int $length + * + * @return string + */ +function get_uuid($length = 16) +{ + mt_srand((double)microtime()*10000); + $uuid = sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); + $str = base64_encode($uuid); + return substr($str, mt_rand(0, strlen($str) - $length), $length); +} + +/** + * 根据模型名称获取模型 + * + * @param $modelName + * + * @return \think\Model|\think\db\Query + */ +function get_model($modelName) +{ + if (false !== strpos($modelName, '\\')) { + // 指定模型类 + $db = new $modelName; + } else { + try { + $db = \think\Loader::model($modelName); + } catch (\think\exception\ClassNotFoundException $e) { + $db = \think\Db::name($modelName); + } + } + + return $db; +} + +/** + * 验证规则扩展 + */ +\think\Validate::extend([ + // 验证字段是否在模型中存在 + 'checkExist' => function($value, $rule, $data, $field) { + if (is_string($rule)) { + $rule = explode(',', $rule); + } + $db = get_model($rule[0]); + $key = isset($rule[1]) ? $rule[1] : $field; + + if (strpos($key, '^')) { + // 支持多个字段验证 + $fields = explode('^', $key); + foreach ($fields as $key) { + $map[$key] = $data[$key]; + } + } elseif (strpos($key, '=')) { + parse_str($key, $map); + } else { + $map[$key] = $data[$field]; + } + + $pk = strval(isset($rule[3]) ? $rule[3] : $db->getPk()); + if (isset($rule[2])) { + $map[$pk] = ['neq', $rule[2]]; + } elseif (isset($data[$pk])) { + $map[$pk] = ['neq', $data[$pk]]; + } + + if ($db->where($map)->field($pk)->find()) { + return true; + } + return false; + } +]); diff --git a/application/admin/controller/AdminUser.php b/application/admin/controller/AdminUser.php index eb91064..865163e 100644 --- a/application/admin/controller/AdminUser.php +++ b/application/admin/controller/AdminUser.php @@ -35,13 +35,13 @@ protected function filter(&$map) $map['realname'] = ["like", "%" . $this->request->param('realname') . "%"]; } if ($this->request->param('account')) { - $map['realname'] = ["like", "%" . $this->request->param('account') . "%"]; + $map['account'] = ["like", "%" . $this->request->param('account') . "%"]; } if ($this->request->param('email')) { - $map['realname'] = ["like", "%" . $this->request->param('email') . "%"]; + $map['email'] = ["like", "%" . $this->request->param('email') . "%"]; } if ($this->request->param('mobile')) { - $map['realname'] = ["like", "%" . $this->request->param('mobile') . "%"]; + $map['mobile'] = ["like", "%" . $this->request->param('mobile') . "%"]; } } diff --git a/application/admin/controller/LoginLog.php b/application/admin/controller/LoginLog.php index 26655dc..b4a9755 100644 --- a/application/admin/controller/LoginLog.php +++ b/application/admin/controller/LoginLog.php @@ -17,6 +17,8 @@ \think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT); use app\admin\Controller; +use app\common\model\LoginLog as ModelLoginLog; +use app\common\model\AdminUser as ModelAdminUser; class LoginLog extends Controller { @@ -42,7 +44,9 @@ protected function filter(&$map) // 设置属性 $map['_table'] = "login_log"; - $map['_relation'] = "user"; $map['_order_by'] = "login_log.id desc"; + $map['_func'] = function (ModelLoginLog $model) use ($map) { + $model->alias($map['_table'])->join(ModelAdminUser::getTable() . ' user', 'login_log.uid = user.id'); + }; } -} \ No newline at end of file +} diff --git a/application/admin/controller/NodeMap.php b/application/admin/controller/NodeMap.php index 4125ee1..ccaa915 100644 --- a/application/admin/controller/NodeMap.php +++ b/application/admin/controller/NodeMap.php @@ -31,7 +31,7 @@ class NodeMap extends Controller protected function filter(&$map) { if ($this->request->param("map")) { - $map['map'] = ["like", "%" . $this->request->param("map") . "%"]; + $map['action|module|controller'] = ["like", "%" . $this->request->param("map") . "%"]; } if ($this->request->param("comment")) { $map['comment'] = ["like", "%" . $this->request->param("comment") . "%"]; diff --git a/application/admin/view/admin_group/index.html b/application/admin/view/admin_group/index.html index b41508c..1075bb7 100755 --- a/application/admin/view/admin_group/index.html +++ b/application/admin/view/admin_group/index.html @@ -33,7 +33,7 @@
{$page}
{/block} -{block name='script'}} +{block name='script'} diff --git a/application/admin/view/index/index.html b/application/admin/view/index/index.html index a71a3e9..16e1000 100755 --- a/application/admin/view/index/index.html +++ b/application/admin/view/index/index.html @@ -44,7 +44,7 @@
  • 退出
  • -
  • 1
  • +
  • 1