123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace app\admin\controller\security;
- use Throwable;
- use app\common\controller\Backend;
- use app\admin\model\DataRecycle as DataRecycleModel;
- class DataRecycle extends Backend
- {
- /**
- * @var object
- * @phpstan-var DataRecycleModel
- */
- protected object $model;
- // 排除字段
- protected string|array $preExcludeFields = ['update_time', 'create_time'];
- protected string|array $quickSearchField = 'name';
- public function initialize(): void
- {
- parent::initialize();
- $this->model = new DataRecycleModel();
- }
- /**
- * 添加
- * @throws Throwable
- */
- public function add(): void
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- $data = $this->excludeFields($data);
- $data['controller_as'] = str_ireplace('.php', '', $data['controller'] ?? '');
- $data['controller_as'] = strtolower(str_ireplace(['\\', '.'], '/', $data['controller_as']));
- $result = false;
- $this->model->startTrans();
- try {
- // 模型验证
- if ($this->modelValidate) {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- if (class_exists($validate)) {
- $validate = new $validate;
- if ($this->modelSceneValidate) $validate->scene('add');
- $validate->check($data);
- }
- }
- $result = $this->model->save($data);
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Added successfully'));
- } else {
- $this->error(__('No rows were added'));
- }
- }
- // 放在add方法内,就不需要额外添加权限节点了
- $this->success('', [
- 'tables' => $this->getTableList(),
- 'controllers' => $this->getControllerList(),
- ]);
- }
- /**
- * 编辑
- * @param int|string|null $id
- * @throws Throwable
- */
- public function edit(int|string $id = null): void
- {
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('Record not found'));
- }
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- $data = $this->excludeFields($data);
- $data['controller_as'] = str_ireplace('.php', '', $data['controller'] ?? '');
- $data['controller_as'] = strtolower(str_ireplace(['\\', '.'], '/', $data['controller_as']));
- $result = false;
- $this->model->startTrans();
- try {
- // 模型验证
- if ($this->modelValidate) {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- if (class_exists($validate)) {
- $validate = new $validate;
- if ($this->modelSceneValidate) $validate->scene('edit');
- $validate->check($data);
- }
- }
- $result = $row->save($data);
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Update successful'));
- } else {
- $this->error(__('No rows updated'));
- }
- }
- $this->success('', [
- 'row' => $row
- ]);
- }
- protected function getControllerList(): array
- {
- $outExcludeController = [
- 'Addon.php',
- 'Ajax.php',
- 'Module.php',
- 'Terminal.php',
- 'Dashboard.php',
- 'Index.php',
- 'routine/AdminInfo.php',
- 'user/MoneyLog.php',
- 'user/ScoreLog.php',
- ];
- $outControllers = [];
- $controllers = get_controller_list();
- foreach ($controllers as $key => $controller) {
- if (!in_array($controller, $outExcludeController)) {
- $outControllers[$key] = $controller;
- }
- }
- return $outControllers;
- }
- protected function getTableList(): array
- {
- $tablePrefix = config('database.connections.mysql.prefix');
- $outExcludeTable = [
- // 功能表
- 'area',
- 'token',
- 'captcha',
- 'admin_group_access',
- // 无删除功能
- 'user_money_log',
- 'user_score_log',
- ];
- $outTables = [];
- $tables = get_table_list();
- $pattern = '/^' . $tablePrefix . '/i';
- foreach ($tables as $table => $tableComment) {
- $table = preg_replace($pattern, '', $table);
- if (!in_array($table, $outExcludeTable)) {
- $outTables[$table] = $tableComment;
- }
- }
- return $outTables;
- }
- }
|