mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
149 lines
5.5 KiB
PHP
149 lines
5.5 KiB
PHP
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | Think.Admin
|
||
// +----------------------------------------------------------------------
|
||
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
|
||
// +----------------------------------------------------------------------
|
||
// | 官方网站: http://think.ctolog.com
|
||
// +----------------------------------------------------------------------
|
||
// | 开源协议 ( https://mit-license.org )
|
||
// +----------------------------------------------------------------------
|
||
// | github开源项目:https://github.com/zoujingli/Think.Admin
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace controller;
|
||
|
||
use service\DataService;
|
||
use think\Controller;
|
||
use think\db\Query;
|
||
use think\Db;
|
||
|
||
/**
|
||
* 后台权限基础控制器
|
||
* Class BasicAdmin
|
||
* @package controller
|
||
*/
|
||
class BasicAdmin extends Controller {
|
||
|
||
/**
|
||
* 页面标题
|
||
* @var string
|
||
*/
|
||
public $title;
|
||
|
||
/**
|
||
* 默认操作数据表
|
||
* @var string
|
||
*/
|
||
public $table;
|
||
|
||
/**
|
||
* 默认检查用户登录状态
|
||
* @var bool
|
||
*/
|
||
public $checkLogin = true;
|
||
|
||
/**
|
||
* 默认检查节点访问权限
|
||
* @var bool
|
||
*/
|
||
public $checkAuth = true;
|
||
|
||
/**
|
||
* 表单默认操作
|
||
* @param Query $dbQuery 数据库查询对象
|
||
* @param string $tplFile 显示模板名字
|
||
* @param string $pkField 更新主键规则
|
||
* @param array $where 查询规则
|
||
* @param array $data 扩展数据
|
||
* @return array|string
|
||
*/
|
||
protected function _form($dbQuery = null, $tplFile = '', $pkField = null, $where = [], $data = []) {
|
||
$db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
|
||
$pk = is_null($pkField) ? ($db->getPk() ? $db->getPk() : 'id') : $pkField;
|
||
$pkValue = $this->request->request($pk, isset($where[$pk]) ? $where[$pk] : (isset($data[$pk]) ? $data[$pk] : null));
|
||
// POST请求, 数据自动存库
|
||
if ($this->request->isPost()) {
|
||
$data = array_merge($this->request->post(), $data);
|
||
if (false !== $this->_callback('_form_filter', $data)) {
|
||
$result = DataService::save($db, $data, $pk, $where);
|
||
(false !== $this->_callback('_form_result', $result)) && ($result !== false ? $this->success('恭喜, 数据保存成功!', '') : $this->error('数据保存失败, 请稍候再试!'));
|
||
}
|
||
return $result;
|
||
}
|
||
// GET请求, 获取并显示表单页面
|
||
$vo = ($pkValue !== null) ? array_merge((array) $db->where($pk, $pkValue)->where($where)->find(), $data) : $data;
|
||
if (false !== $this->_callback('_form_filter', $vo)) {
|
||
return $this->fetch($tplFile, ['title' => $this->title, 'vo' => $vo]);
|
||
}
|
||
return $vo;
|
||
}
|
||
|
||
/**
|
||
* 列表集成处理方法
|
||
* @param Query $db 数据库查询对象
|
||
* @param bool $is_page 是启用分页
|
||
* @param bool $is_display 是否直接输出显示
|
||
* @param bool $total 总记录数
|
||
* @return array|string
|
||
*/
|
||
protected function _list($db = null, $is_page = true, $is_display = true, $total = false) {
|
||
if (is_null($db)) {
|
||
$db = Db::name($this->table);
|
||
} elseif (is_string($db)) {
|
||
$db = Db::name($db);
|
||
}
|
||
# 列表排序默认处理
|
||
if ($this->request->isPost() && $this->request->post('action') === 'resort') {
|
||
$data = $this->request->post();
|
||
unset($data['action']);
|
||
foreach ($data as $key => &$value) {
|
||
if (false === $db->where('id', intval(ltrim($key, '_')))->update(['sort' => $value])) {
|
||
$this->error('列表排序失败,请稍候再试!');
|
||
}
|
||
}
|
||
$this->success('列表排序成功,正在刷新列表!', '');
|
||
}
|
||
# 列表显示
|
||
$result = array();
|
||
# 默认排序
|
||
$options = $db->getOptions();
|
||
if (empty($options['order'])) {
|
||
$fields = $db->getTableFields(['table' => $db->getTable()]);
|
||
in_array('sort', $fields) && $db->order('sort asc');
|
||
}
|
||
if ($is_page) {
|
||
$row_page = $this->request->get('rows', cookie('rows'), 'intval');
|
||
cookie('rows', $row_page >= 10 ? $row_page : 20);
|
||
$page = $db->paginate($row_page, $total, ['query' => $this->request->get()]);
|
||
$result['list'] = $page->all();
|
||
$result['page'] = preg_replace(['|href="(.*?)"|', '|pagination|'], ['data-open="$1" href="javascript:void(0);"', 'pagination pull-right'], $page->render());
|
||
} else {
|
||
$result['list'] = $db->select();
|
||
}
|
||
if ($this->_callback('_data_filter', $result['list']) === false) {
|
||
return $result;
|
||
}
|
||
!empty($this->title) && $this->assign('title', $this->title);
|
||
$is_display && exit($this->fetch('', $result));
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 当前对象回调成员方法
|
||
* @param string $method
|
||
* @param array $data
|
||
* @return bool
|
||
*/
|
||
protected function _callback($method, &$data) {
|
||
foreach ([$method, "_" . $this->request->action() . "{$method}"] as $_method) {
|
||
if (method_exists($this, $_method) && false === $this->$_method($data)) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
}
|