DataRecycle.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\admin\controller\security;
  3. use app\common\controller\Backend;
  4. use app\admin\model\DataRecycle as DataRecycleModel;
  5. use think\db\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. use Exception;
  9. class DataRecycle extends Backend
  10. {
  11. protected $model = null;
  12. // 排除字段
  13. protected $preExcludeFields = ['updatetime', 'createtime'];
  14. protected $quickSearchField = 'name';
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. $this->model = new DataRecycleModel();
  19. }
  20. /**
  21. * 添加
  22. */
  23. public function add()
  24. {
  25. if ($this->request->isPost()) {
  26. $data = $this->request->post();
  27. if (!$data) {
  28. $this->error(__('Parameter %s can not be empty', ['']));
  29. }
  30. $data = $this->excludeFields($data);
  31. $data['controller_as'] = str_ireplace('.php', '', $data['controller'] ?? '');
  32. $data['controller_as'] = strtolower(str_ireplace(['\\', '.'], '/', $data['controller_as']));
  33. $result = false;
  34. Db::startTrans();
  35. try {
  36. // 模型验证
  37. if ($this->modelValidate) {
  38. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  39. if (class_exists($validate)) {
  40. $validate = new $validate;
  41. if ($this->modelSceneValidate) $validate->scene('add');
  42. $validate->check($data);
  43. }
  44. }
  45. $result = $this->model->save($data);
  46. Db::commit();
  47. } catch (ValidateException|Exception|PDOException $e) {
  48. Db::rollback();
  49. $this->error($e->getMessage());
  50. }
  51. if ($result !== false) {
  52. $this->success(__('Added successfully'));
  53. } else {
  54. $this->error(__('No rows were added'));
  55. }
  56. }
  57. // 放在add方法内,就不需要额外添加权限节点了
  58. $this->success('', [
  59. 'tables' => $this->getTableList(),
  60. 'controllers' => $this->getControllerList(),
  61. ]);
  62. }
  63. /**
  64. * 编辑
  65. * @param null $id
  66. */
  67. public function edit($id = null)
  68. {
  69. $row = $this->model->find($id);
  70. if (!$row) {
  71. $this->error(__('Record not found'));
  72. }
  73. if ($this->request->isPost()) {
  74. $data = $this->request->post();
  75. if (!$data) {
  76. $this->error(__('Parameter %s can not be empty', ['']));
  77. }
  78. $data = $this->excludeFields($data);
  79. $data['controller_as'] = str_ireplace('.php', '', $data['controller'] ?? '');
  80. $data['controller_as'] = strtolower(str_ireplace(['\\', '.'], '/', $data['controller_as']));
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. // 模型验证
  85. if ($this->modelValidate) {
  86. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  87. if (class_exists($validate)) {
  88. $validate = new $validate;
  89. if ($this->modelSceneValidate) $validate->scene('edit');
  90. $validate->check($data);
  91. }
  92. }
  93. $result = $row->save($data);
  94. Db::commit();
  95. } catch (ValidateException|Exception|PDOException $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result !== false) {
  100. $this->success(__('Update successful'));
  101. } else {
  102. $this->error(__('No rows updated'));
  103. }
  104. }
  105. $this->success('', [
  106. 'row' => $row
  107. ]);
  108. }
  109. protected function getControllerList()
  110. {
  111. $outExcludeController = [
  112. 'Addon.php',
  113. 'Ajax.php',
  114. 'Module.php',
  115. 'Terminal.php',
  116. 'Dashboard.php',
  117. 'Index.php',
  118. 'routine/AdminInfo.php',
  119. 'user/MoneyLog.php',
  120. 'user/ScoreLog.php',
  121. ];
  122. $outControllers = [];
  123. $controllers = get_controller_list();
  124. foreach ($controllers as $key => $controller) {
  125. if (!in_array($controller, $outExcludeController)) {
  126. $outControllers[$key] = $controller;
  127. }
  128. }
  129. return $outControllers;
  130. }
  131. protected function getTableList()
  132. {
  133. $tablePrefix = config('database.connections.mysql.prefix');
  134. $outExcludeTable = [
  135. // 功能表
  136. 'area',
  137. 'token',
  138. 'captcha',
  139. 'admin_group_access',
  140. // 无删除功能
  141. 'user_money_log',
  142. 'user_score_log',
  143. ];
  144. $outTables = [];
  145. $tables = get_table_list();
  146. $pattern = '/^' . $tablePrefix . '/i';
  147. foreach ($tables as $table => $tableComment) {
  148. $table = preg_replace($pattern, '', $table);
  149. if (!in_array($table, $outExcludeTable)) {
  150. $outTables[$table] = $tableComment;
  151. }
  152. }
  153. return $outTables;
  154. }
  155. }