BasicAdmin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * 页面标题
  27. * @var string
  28. */
  29. public $title;
  30. /**
  31. * 默认操作数据表
  32. * @var string
  33. */
  34. public $table;
  35. /**
  36. * 表单默认操作
  37. * @param Query $dbQuery 数据库查询对象
  38. * @param string $tplFile 显示模板名字
  39. * @param string $pkField 更新主键规则
  40. * @param array $where 查询规则
  41. * @param array $extendData 扩展数据
  42. * @return array|string
  43. */
  44. protected function _form($dbQuery = null, $tplFile = '', $pkField = '', $where = [], $extendData = [])
  45. {
  46. $db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
  47. $pk = empty($pkField) ? ($db->getPk() ? $db->getPk() : 'id') : $pkField;
  48. $pkValue = $this->request->request($pk, isset($where[$pk]) ? $where[$pk] : (isset($extendData[$pk]) ? $extendData[$pk] : null));
  49. // 非POST请求, 获取数据并显示表单页面
  50. if (!$this->request->isPost()) {
  51. $vo = ($pkValue !== null) ? array_merge((array)$db->where($pk, $pkValue)->where($where)->find(), $extendData) : $extendData;
  52. if (false !== $this->_callback('_form_filter', $vo)) {
  53. empty($this->title) || $this->assign('title', $this->title);
  54. return $this->fetch($tplFile, ['vo' => $vo]);
  55. }
  56. return $vo;
  57. }
  58. // POST请求, 数据自动存库
  59. $data = array_merge($this->request->post(), $extendData);
  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. if ($result !== false) {
  64. $this->success('恭喜, 数据保存成功!', '');
  65. }
  66. $this->error('数据保存失败, 请稍候再试!');
  67. }
  68. }
  69. }
  70. /**
  71. * 列表集成处理方法
  72. * @param Query $dbQuery 数据库查询对象
  73. * @param bool $isPage 是启用分页
  74. * @param bool $isDisplay 是否直接输出显示
  75. * @param bool $total 总记录数
  76. * @return array|string
  77. */
  78. protected function _list($dbQuery = null, $isPage = true, $isDisplay = true, $total = false)
  79. {
  80. $db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
  81. // 列表排序默认处理
  82. if ($this->request->isPost() && $this->request->post('action') === 'resort') {
  83. $data = $this->request->post();
  84. unset($data['action']);
  85. foreach ($data as $key => &$value) {
  86. if (false === $db->where('id', intval(ltrim($key, '_')))->setField('sort', $value)) {
  87. $this->error('列表排序失败, 请稍候再试');
  88. }
  89. }
  90. $this->success('列表排序成功, 正在刷新列表', '');
  91. }
  92. // 列表数据查询与显示
  93. if (null === $db->getOptions('order')) {
  94. $fields = $db->getTableFields($db->getTable());
  95. in_array('sort', $fields) && $db->order('sort asc');
  96. }
  97. $result = [];
  98. if ($isPage) {
  99. $rows = intval($this->request->get('rows', cookie('rows')));
  100. cookie('rows', $rows >= 10 ? $rows : 20);
  101. $page = $db->paginate($rows, $total, ['query' => $this->request->get()]);
  102. $result['list'] = $page->all();
  103. $pattern = ['|href="(.*?)"|', '|pagination|'];
  104. $replacement = ['data-open="$1" href="javascript:void(0);"', 'pagination pull-right'];
  105. $result['page'] = preg_replace($pattern, $replacement, $page->render());
  106. } else {
  107. $result['list'] = $db->select();
  108. }
  109. if (false !== $this->_callback('_data_filter', $result['list']) && $isDisplay) {
  110. !empty($this->title) && $this->assign('title', $this->title);
  111. return $this->fetch('', $result);
  112. }
  113. return $result;
  114. }
  115. /**
  116. * 当前对象回调成员方法
  117. * @param string $method
  118. * @param array|bool $data
  119. * @return bool
  120. */
  121. protected function _callback($method, &$data)
  122. {
  123. foreach ([$method, "_" . $this->request->action() . "{$method}"] as $_method) {
  124. if (method_exists($this, $_method) && false === $this->$_method($data)) {
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. }