| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // | Original reference: https://gitee.com/karson/fastadmin
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 后台控制模块
- // +----------------------------------------------------------------------
- namespace app\common\controller;
- use app\admin\library\Auth;
- use think\facade\Config;
- use think\facade\Event;
- use think\facade\Session;
- use think\Model;
- //定义是后台
- define('IN_ADMIN', true);
- class Backend extends BaseController
- {
- //无需登录的方法,同时也就不需要鉴权了
- protected $noNeedLogin = [];
- //无需鉴权的方法,但需要登录
- protected $noNeedRight = [];
- //当前登录账号信息
- //public $_userinfo;
- //Multi方法可批量修改的字段
- protected $multiFields = 'status,listorder';
- /**
- * 权限控制类
- * @var Auth
- */
- protected $auth = null;
- //模型对象
- protected $modelClass = null;
- //快速搜索时执行查找的字段
- protected $searchFields = 'id';
- //Selectpage可显示的字段
- protected $selectpageFields = '*';
- //是否是关联查询
- protected $relationSearch = false;
- //前台提交过来,需要排除的字段数据
- protected $excludeFields = "";
- /**
- * 是否开启数据限制
- * 支持auth/personal
- * 表示按权限判断/仅限个人
- * 默认为禁用,若启用请务必保证表中存在admin_id字段
- */
- protected $dataLimit = false;
- //数据限制字段
- protected $dataLimitField = 'admin_id';
- //数据限制开启时自动填充限制字段值
- protected $dataLimitFieldAutoFill = true;
- //是否开启Validate验证
- protected $modelValidate = false;
- //是否开启模型场景验证
- protected $modelSceneValidate = false;
- //表查询参数预处理规则(用于 buildTableParames)
- protected $tableParamsFilters = ['trim', 'strip_tags'];
- /*
- * 引入后台控制器的traits
- */
- use \app\admin\library\traits\Backend;
- //初始化
- protected function initialize()
- {
- parent::initialize();
- $this->auth = Auth::instance();
- $modulename = app()->http->getName();
- $controller = preg_replace_callback('/\.[A-Z]/', function ($d) {
- return strtolower($d[0]);
- }, $this->request->controller());
- $controllername = parse_name($controller);
- $actionname = strtolower($this->request->action());
- $path = $controllername . '/' . $actionname;
- // 定义是否Dialog请求
- !defined('IS_DIALOG') && define('IS_DIALOG', $this->request->param("dialog") ? true : false);
- // 设置当前请求的URI
- $this->auth->setRequestUri($path);
- // 检测是否需要验证登录
- if (!$this->auth->match($this->noNeedLogin)) {
- if (!$this->auth->isLogin()) {
- Event::trigger('admin_nologin', $this);
- $url = Session::get('referer');
- $url = $url ?: $this->request->url();
- if (in_array($this->request->pathinfo(), ['', '/', 'index/index'])) {
- $this->redirect((string) url('index/login'), 302, ['referer' => $url]);
- exit;
- }
- $this->error('请先登陆', (string) url('index/login', ['url' => $url]));
- }
- // 是否是超级管理员
- define('IS_ROOT', $this->auth->isAdministrator());
- $forbiddenIps = config::get('site.admin_forbid_ip');
- if ($forbiddenIps) {
- $ip = $this->request->ip();
- // 检查IP地址访问
- $arr = explode(',', $forbiddenIps);
- foreach ($arr as $val) {
- //是否是IP段
- if (strpos($val, '*')) {
- if (str_contains($ip, str_replace('.*', '', $val))) {
- $this->error('403:你在IP禁止段内,禁止访问!');
- }
- } else {
- //不是IP段,用绝对匹配
- if ($ip == $val) {
- $this->error('403:IP地址绝对匹配,禁止访问!');
- }
- }
- }
- }
- if (!IS_ROOT && !$this->auth->match($this->noNeedRight)) {
- //检测访问权限
- if (!$this->auth->check($path)) {
- Event::trigger('admin_nopermission', $this);
- $this->error('未授权访问!');
- }
- }
- }
- $site = Config::get("site");
- // 上传信息配置后
- $upload = array_merge(\app\common\model\Config::upload(), ...Event::trigger("upload_config_init"));
- $config = [
- 'site' => array_intersect_key($site, array_flip(['name', 'indexurl', 'cdnurl', 'version'])),
- 'upload' => $upload,
- 'modulename' => $modulename,
- 'controllername' => $controllername,
- 'actionname' => $actionname,
- 'jsname' => 'backend/' . str_replace('.', '/', $controllername) . (in_array($actionname, ['index', '_empty']) ? '' : '/' . $actionname),
- 'moduleurl' => rtrim((string) url("/", [], false), '/'),
- 'referer' => Session::get("referer")
- ];
- $controllerJsName = 'backend/' . str_replace('.', '/', $controllername);
- $actionJsPath = root_path() . 'public/assets/js/' . $config['jsname'] . '.js';
- $controllerJsPath = root_path() . 'public/assets/js/' . $controllerJsName . '.js';
- if (!in_array($actionname, ['index', '_empty']) && !is_file($actionJsPath) && is_file($controllerJsPath)) {
- $config['jsname'] = $controllerJsName;
- }
- $config = array_merge($config, Config::get("view.tpl_replace_string"), ...Event::trigger("config_init"));
- Config::set(array_merge(Config::get('upload'), $upload), 'upload');
- //渲染站点配置
- $this->assign('site', $site);
- //渲染配置信息
- $this->assign('config', $config);
- //渲染权限对象
- $this->assign('auth', $this->auth);
- //渲染管理员对象
- $this->assign('admin', Session::get('admin'));
- $this->view->filter(function ($content) {
- return Event::trigger("admin_view_filter", $content, true) ?: $content;
- });
- }
- /**
- * 渲染配置信息
- * @param mixed $name 键名或数组
- * @param mixed $value 值
- */
- protected function assignconfig($name, $value = '')
- {
- $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
- }
- /**
- * 构建请求参数
- * @param mixed $searchfields 快速查询的字段
- * @param array $excludeFields 忽略构建搜索的字段
- * @param boolean $relationSearch 是否关联查询
- * @return array
- */
- protected function buildTableParames($searchfields = null, $excludeFields = [], $relationSearch = null)
- {
- //设置过滤方法
- $this->request->filter($this->tableParamsFilters);
- $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
- $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
- $search = $this->request->get("search", '');
- $filter = $this->request->get("filter", '');
- $op = $this->request->get("op", '');
- $sort = $this->request->get("sort", !empty($this->modelClass) && $this->modelClass->getPk() ? $this->modelClass->getPk() : 'id');
- $order = $this->request->get("order", "DESC");
- $limit = $this->request->get("limit/d", 999999);
- $page = $this->request->get("page/d", 1);
- //新增自动计算分页偏移
- $offset = $page ? ($page - 1) * $limit : 0;
- if ($this->request->has("offset")) {
- $offset = $this->request->get("offset/d", 0);
- }
- $this->request->withGet([config::get('paginate.var_page') => $page]);
- $filter = (array) json_decode($filter, true);
- $op = (array) json_decode($op, true);
- $filter = $filter ? $filter : [];
- $where = [];
- $alias = [];
- $bind = [];
- $name = '';
- $aliasName = '';
- if (!empty($this->modelClass) && $relationSearch) {
- $name = $this->modelClass->getTable();
- $alias[$name] = parse_name(basename(str_replace('\\', '/', get_class($this->modelClass))));
- $aliasName = $alias[$name] . '.';
- }
- $sortArr = explode(',', $sort);
- foreach ($sortArr as $index => &$item) {
- $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
- }
- unset($item);
- $sort = implode(',', $sortArr);
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
- }
- if ($search) {
- $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
- foreach ($searcharr as $k => &$v) {
- $v = stripos($v, ".") === false ? $aliasName . $v : $v;
- }
- unset($v);
- $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
- }
- $index = 0;
- foreach ($filter as $k => $v) {
- if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
- continue;
- }
- $sym = $op[$k] ?? '=';
- if (stripos($k, ".") === false) {
- $k = $aliasName . $k;
- }
- $v = !is_array($v) ? trim($v) : $v;
- $sym = strtoupper($op[$k] ?? $sym);
- //null和空字符串特殊处理
- if (!is_array($v)) {
- if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
- $sym = strtoupper($v);
- }
- if (in_array($v, ['""', "''"])) {
- $v = '';
- $sym = '=';
- }
- }
- $sym = $this->getMappedOperator($sym);
- switch ($sym) {
- case '=':
- case '<>':
- $where[] = [$k, $sym, (string) $v];
- break;
- case 'LIKE':
- case 'NOT LIKE':
- case 'LIKE %...%':
- case 'NOT LIKE %...%':
- $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
- break;
- case '>':
- case '>=':
- case '<':
- case '<=':
- $where[] = [$k, $sym, intval($v)];
- break;
- case 'FINDIN':
- case 'FINDINSET':
- case 'FIND_IN_SET':
- $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
- $findArr = array_values($v);
- foreach ($findArr as $idx => $item) {
- $bindName = "item_" . $index . "_" . $idx;
- $bind[$bindName] = $item;
- $where[] = ["FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)", [$bindName => $item]];
- }
- break;
- case 'IN':
- case 'IN(...)':
- case 'NOT IN':
- case 'NOT IN(...)':
- $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
- break;
- case 'BETWEEN':
- case 'NOT BETWEEN':
- $arr = array_slice(explode(',', $v), 0, 2);
- if (stripos($v, ',') === false || !array_filter($arr)) {
- continue 2;
- }
- //当出现一边为空时改变操作符
- if ($arr[0] === '') {
- $sym = $sym == 'BETWEEN' ? '<=' : '>';
- $arr = $arr[1];
- } elseif ($arr[1] === '') {
- $sym = $sym == 'BETWEEN' ? '>=' : '<';
- $arr = $arr[0];
- }
- $where[] = [$k, $sym, $arr];
- break;
- case 'RANGE':
- case 'NOT RANGE':
- $v = str_replace(' - ', ',', $v);
- $arr = array_slice(explode(',', $v), 0, 2);
- if (stripos($v, ',') === false || !array_filter($arr)) {
- continue 2;
- }
- //当出现一边为空时改变操作符
- if ($arr[0] === '') {
- $sym = $sym == 'RANGE' ? '<=' : '>';
- $arr = $arr[1];
- } elseif ($arr[1] === '') {
- $sym = $sym == 'RANGE' ? '>=' : '<';
- $arr = $arr[0];
- }
- $tableArr = explode('.', $k);
- if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->modelClass)) {
- //修复关联模型下时间无法搜索的BUG
- $relation = parse_name($tableArr[0], 1, false);
- $alias[$this->modelClass->$relation()->getTable()] = $tableArr[0];
- }
- $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
- break;
- case 'NULL':
- case 'IS NULL':
- case 'NOT NULL':
- case 'IS NOT NULL':
- $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
- break;
- default:
- break;
- }
- $index++;
- }
- if (!empty($this->modelClass)) {
- $this->modelClass->alias($alias);
- }
- $where = function ($query) use ($where, $alias, $bind) {
- $query->alias($alias);
- foreach ($where as $v) {
- if (is_array($v)) {
- call_user_func_array([$query, 'where'], $v);
- } else {
- $query->where($v);
- }
- }
- };
- return [$page, $limit, $where, $sort, $order, $offset, $alias, $bind];
- }
- /**
- * 解析操作符
- */
- protected function getMappedOperator($operator)
- {
- $operatorMap = [
- 'EQ' => '=',
- 'NEQ' => '<>',
- 'GT' => '>',
- 'EGT' => '>=',
- 'LT' => '<',
- 'ELT' => '<=',
- ];
- return $operatorMap[$operator] ?? $operator;
- }
- /**
- * 获取数据限制的管理员ID
- * 禁用数据限制时返回的是null
- * @return mixed
- */
- protected function getDataLimitAdminIds()
- {
- if (!$this->dataLimit) {
- return null;
- }
- if ($this->auth->isAdministrator()) {
- return null;
- }
- $adminIds = [];
- if (in_array($this->dataLimit, ['auth', 'personal'])) {
- $adminIds = $this->dataLimit == 'auth' ? $this->auth->getChildrenAdminIds(true) : [$this->auth->id];
- }
- return $adminIds;
- }
- /**
- *
- * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
- * 这里示例了所有的参数,所以比较复杂,实现上自己实现只需简单的几行即可
- *
- */
- protected function selectpage()
- {
- //设置过滤方法
- $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
- //搜索关键词,客户端输入以空格分开,这里接收为数组
- $word = (array) $this->request->request("q_word/a");
- //当前页
- $page = $this->request->request("pageNumber", 0);
- //分页大小
- $pagesize = $this->request->request("pageSize");
- //搜索条件
- $andor = $this->request->request("andOr", "and", "strtoupper");
- //排序方式
- $orderby = (array) $this->request->request("orderBy/a");
- //显示的字段
- $field = $this->request->request("showField");
- //主键
- $primarykey = $this->request->request("keyField");
- //主键值
- $primaryvalue = $this->request->request("keyValue");
- //搜索字段
- $searchfield = (array) $this->request->request("searchField/a");
- //自定义搜索条件
- $custom = (array) $this->request->request("custom/a");
- //是否返回树形结构
- $istree = $this->request->request("isTree", 0);
- $ishtml = $this->request->request("isHtml", 0);
- if ($istree) {
- $word = [];
- $pagesize = 99999;
- }
- $order = [];
- foreach ($orderby as $k => $v) {
- $order[$v[0]] = $v[1];
- }
- $field = $field ?: 'name';
- //如果有primaryvalue,说明当前是初始化传值
- if ($primaryvalue !== null) {
- $where = [$primarykey => explode(',', $primaryvalue)];
- $pagesize = 99999;
- } else {
- $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
- $logic = $andor == 'AND' ? '&' : '|';
- $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
- $searchfield = str_replace(',', $logic, $searchfield);
- $word = array_filter(array_unique($word));
- if (count($word) == 1) {
- $query->where($searchfield, "like", "%" . reset($word) . "%");
- } else {
- $query->where(function ($query) use ($word, $searchfield) {
- foreach ($word as $item) {
- $query->whereOr(function ($query) use ($item, $searchfield) {
- $query->where($searchfield, "like", "%{$item}%");
- });
- }
- });
- }
- if ($custom && is_array($custom)) {
- foreach ($custom as $k => $v) {
- if (is_array($v) && 2 == count($v)) {
- $query->where($k, trim($v[0]), $v[1]);
- } else {
- $query->where($k, '=', $v);
- }
- }
- }
- };
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $this->modelClass = $this->modelClass->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = [];
- $total = $this->modelClass->where($where)->count();
- if ($total > 0) {
- $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
- //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
- if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
- $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
- //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
- $primaryvalue = array_map(function ($value) {
- return '\'' . $value . '\'';
- }, $primaryvalue);
- $primaryvalue = implode(',', $primaryvalue);
- $this->modelClass->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
- } else {
- $this->modelClass->order($order);
- }
- $this->modelClass->removeOption('where');
- if (is_array($adminIds)) {
- $this->modelClass->where($this->dataLimitField, 'in', $adminIds);
- }
- $datalist = $this->modelClass->where($where)
- ->page($page, $pagesize)
- ->select();
- foreach ($datalist as $item) {
- unset($item['password'], $item['salt']);
- if ($this->selectpageFields == '*') {
- $result = [
- $primarykey => $item[$primarykey] ?? '',
- $field => $item[$field] ?? '',
- ];
- } else {
- $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array) $item), array_flip($fields));
- }
- $result['pid'] = $item['pid'] ?? ($item['parentid'] ?? 0);
- //安全处理字符
- $result = array_map(function ($value) {
- return $value === null ? '' : htmlentities((string) $value);
- }, $result);
- $list[] = $result;
- }
- if ($istree && !$primaryvalue) {
- $tree = \util\Tree::instance();
- $tree->init($list, 'pid');
- $list = $tree->getTreeList($tree->getTreeArray(0), $field);
- if (!$ishtml) {
- foreach ($list as &$item) {
- $item = str_replace(' ', ' ', $item);
- }
- unset($item);
- }
- }
- }
- //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
- return json(['data' => $list, 'count' => $total]);
- }
- //刷新Token
- protected function token()
- {
- $check = $this->request->checkToken('__token__');
- // 刷新token
- $token = $this->request->buildToken();
- if ($this->request->isAjax()) {
- header('__token__: ' . $token);
- }
- if (false === $check) {
- $this->error('令牌错误!', '', ['__token__' => $token]);
- }
- }
- }
|