DataRecycle.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. 'controllers' => $this->getControllerList(),
  62. ]);
  63. }
  64. /**
  65. * 编辑
  66. * @param int|string|null $id
  67. * @throws Throwable
  68. */
  69. public function edit(int|string $id = null): void
  70. {
  71. $row = $this->model->find($id);
  72. if (!$row) {
  73. $this->error(__('Record not found'));
  74. }
  75. if ($this->request->isPost()) {
  76. $data = $this->request->post();
  77. if (!$data) {
  78. $this->error(__('Parameter %s can not be empty', ['']));
  79. }
  80. $data = $this->excludeFields($data);
  81. $data['controller_as'] = str_ireplace('.php', '', $data['controller'] ?? '');
  82. $data['controller_as'] = strtolower(str_ireplace(['\\', '.'], '/', $data['controller_as']));
  83. $result = false;
  84. $this->model->startTrans();
  85. try {
  86. // 模型验证
  87. if ($this->modelValidate) {
  88. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  89. if (class_exists($validate)) {
  90. $validate = new $validate();
  91. if ($this->modelSceneValidate) $validate->scene('edit');
  92. $validate->check($data);
  93. }
  94. }
  95. $result = $row->save($data);
  96. $this->model->commit();
  97. } catch (Throwable $e) {
  98. $this->model->rollback();
  99. $this->error($e->getMessage());
  100. }
  101. if ($result !== false) {
  102. $this->success(__('Update successful'));
  103. } else {
  104. $this->error(__('No rows updated'));
  105. }
  106. }
  107. $this->success('', [
  108. 'row' => $row
  109. ]);
  110. }
  111. protected function getControllerList(): array
  112. {
  113. $outExcludeController = [
  114. 'Addon.php',
  115. 'Ajax.php',
  116. 'Module.php',
  117. 'Terminal.php',
  118. 'Dashboard.php',
  119. 'Index.php',
  120. 'routine/AdminInfo.php',
  121. 'user/MoneyLog.php',
  122. 'user/ScoreLog.php',
  123. ];
  124. $outControllers = [];
  125. $controllers = get_controller_list();
  126. foreach ($controllers as $key => $controller) {
  127. if (!in_array($controller, $outExcludeController)) {
  128. $outControllers[$key] = $controller;
  129. }
  130. }
  131. return $outControllers;
  132. }
  133. }