DataRecycle.php 5.4 KB

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