auth->setAllowFields($this->authAllowFields); $this->model = $this->auth->getAdmin(); } public function index() { $info = $this->auth->getInfo(); $this->success('', [ 'info' => $info ]); } public function edit($id = null) { $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', [''])); } if (isset($data['avatar']) && $data['avatar']) { $row->avatar = $data['avatar']; if ($row->save()) { $this->success('头像修改成功!'); } } // 数据验证 if ($this->modelValidate) { try { $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = new $validate; $validate->scene('info')->check($data); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } } if (isset($data['password']) && $data['password']) { $this->model->resetPassword($this->auth->id, $data['password']); } $data = $this->excludeFields($data); $result = false; Db::startTrans(); try { $result = $row->save($data); Db::commit(); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } } }