123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- /**
- * 大屏操作
- */
- class Operation extends Backend
- {
- /**
- * Operation模型对象
- * @var object
- * @phpstan-var \app\admin\model\Operation
- */
- protected object $model;
- protected array|string $preExcludeFields = ['id', 'update_time', 'create_time'];
- protected string|array $quickSearchField = ['id'];
- public function initialize(): void
- {
- parent::initialize();
- $this->model = new \app\admin\model\Operation;
- }
- /**
- * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
- */
- /**
- * 编辑
- * @throws Throwable
- */
- public function edit(): void
- {
- $pk = $this->model->getPk();
- $id = $this->request->param($pk);
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('Record not found'));
- }
- $dataLimitAdminIds = $this->getDataLimitAdminIds();
- if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
- $this->error(__('You have no permission'));
- }
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- if( $data['switch'] == 0 && !isset($data['types'])){
- $this->error('请确保至少有一种协议正在使用中!');
- }
- $data = $this->excludeFields($data);
- $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');
- $data[$pk] = $row[$pk];
- $validate->check($data);
- }
- }
- if( $data['switch'] == 1 && !isset($data['types'])){
- $this->model->where('switch',1)->update(['switch' => 0]);
- }
- $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
- ]);
- }
- }
|