BasicAdmin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace controller;
  14. use service\DataService;
  15. use think\Controller;
  16. use think\db\Query;
  17. use think\Db;
  18. /**
  19. * 后台权限基础控制器
  20. * Class BasicAdmin
  21. * @package controller
  22. */
  23. class BasicAdmin extends Controller {
  24. /**
  25. * 页面标题
  26. * @var string
  27. */
  28. public $title;
  29. /**
  30. * 默认操作数据表
  31. * @var string
  32. */
  33. public $table;
  34. /**
  35. * 默认检查用户登录状态
  36. * @var bool
  37. */
  38. public $checkLogin = true;
  39. /**
  40. * 默认检查节点访问权限
  41. * @var bool
  42. */
  43. public $checkAuth = true;
  44. /**
  45. * 表单默认操作
  46. * @param Query $dbQuery 数据库查询对象
  47. * @param string $tplFile 显示模板名字
  48. * @param string $pkField 更新主键规则
  49. * @param array $where 查询规则
  50. * @param array $data 扩展数据
  51. * @return array|string
  52. */
  53. protected function _form($dbQuery = null, $tplFile = '', $pkField = '', $where = [], $data = []) {
  54. $db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
  55. $pk = empty($pkField) ? ($db->getPk() ? $db->getPk() : 'id') : $pkField;
  56. $pkValue = $this->request->request($pk, isset($where[$pk]) ? $where[$pk] : (isset($data[$pk]) ? $data[$pk] : null));
  57. // POST请求, 数据自动存库
  58. if ($this->request->isPost()) {
  59. $data = array_merge($this->request->post(), $data);
  60. if (false !== $this->_callback('_form_filter', $data)) {
  61. $result = DataService::save($db, $data, $pk, $where);
  62. if (false === $this->_callback('_form_result', $result)) {
  63. return $result;
  64. }
  65. if ($result !== false) {
  66. $this->success('恭喜, 数据保存成功!', '');
  67. }
  68. $this->error('数据保存失败, 请稍候再试!');
  69. }
  70. }
  71. // GET请求, 获取并显示表单页面
  72. $vo = ($pkValue !== null) ? array_merge((array)$db->where($pk, $pkValue)->where($where)->find(), $data) : $data;
  73. if (false === $this->_callback('_form_filter', $vo)) {
  74. return $vo;
  75. }
  76. empty($this->title) or $this->assign('title', $this->title);
  77. return $this->fetch($tplFile, ['vo' => $vo]);
  78. }
  79. /**
  80. * 列表集成处理方法
  81. * @param Query $dbQuery 数据库查询对象
  82. * @param bool $isPage 是启用分页
  83. * @param bool $isDisplay 是否直接输出显示
  84. * @param bool $total 总记录数
  85. * @return array|string
  86. */
  87. protected function _list($dbQuery = null, $isPage = true, $isDisplay = true, $total = false) {
  88. $db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
  89. // 列表排序默认处理
  90. if ($this->request->isPost() && $this->request->post('action') === 'resort') {
  91. $data = $this->request->post();
  92. unset($data['action']);
  93. foreach ($data as $key => &$value) {
  94. if (false === $db->where('id', intval(ltrim($key, '_')))->setField('sort', $value)) {
  95. $this->error('列表排序失败, 请稍候再试');
  96. }
  97. }
  98. $this->success('列表排序成功, 正在刷新列表', '');
  99. }
  100. // 列表数据查询与显示
  101. $result = array();
  102. if (null === $db->getOptions('order')) {
  103. $fields = $db->getTableFields(['table' => $db->getTable()]);
  104. in_array('sort', $fields) && $db->order('sort asc');
  105. }
  106. if ($isPage) {
  107. $row_page = $this->request->get('rows', cookie('rows'), 'intval');
  108. cookie('rows', $row_page >= 10 ? $row_page : 20);
  109. $page = $db->paginate($row_page, $total, ['query' => $this->request->get()]);
  110. $result['list'] = $page->all();
  111. $result['page'] = preg_replace(['|href="(.*?)"|', '|pagination|'], ['data-open="$1" href="javascript:void(0);"', 'pagination pull-right'], $page->render());
  112. } else {
  113. $result['list'] = $db->select();
  114. }
  115. if (false === $this->_callback('_data_filter', $result['list']) || !$isDisplay) {
  116. return $result;
  117. }
  118. !empty($this->title) && $this->assign('title', $this->title);
  119. return $this->fetch('', $result);
  120. }
  121. /**
  122. * 当前对象回调成员方法
  123. * @param string $method
  124. * @param array|bool $data
  125. * @return bool
  126. */
  127. protected function _callback($method, &$data) {
  128. foreach ([$method, "_" . $this->request->action() . "{$method}"] as $_method) {
  129. if (method_exists($this, $_method) && false === $this->$_method($data)) {
  130. return false;
  131. }
  132. }
  133. return true;
  134. }
  135. }