From 7a6fd399cb76d78b5613f32f054ffbd53c9af1f4 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Tue, 4 Jan 2022 11:36:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=AD=89=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Actions/Grid/UserEditAgent.php | 38 +++++++++++ app/Admin/Controllers/UserController.php | 8 ++- app/Admin/Forms/UserEditAgent.php | 87 ++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 app/Admin/Actions/Grid/UserEditAgent.php create mode 100644 app/Admin/Forms/UserEditAgent.php diff --git a/app/Admin/Actions/Grid/UserEditAgent.php b/app/Admin/Actions/Grid/UserEditAgent.php new file mode 100644 index 00000000..49ad04d7 --- /dev/null +++ b/app/Admin/Actions/Grid/UserEditAgent.php @@ -0,0 +1,38 @@ +title) { + return $this->title; + } + return ' 修改等级  '; + } + + /** + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return $user->can('dcat.admin.users.edit_agent'); + } + + public function render() + { + $form = UserEditAgentForm::make()->payload(['id'=>$this->getKey()]); + return Modal::make() + ->lg() + ->title($this->title()) + ->body($form) + ->button($this->title()); + } +} diff --git a/app/Admin/Controllers/UserController.php b/app/Admin/Controllers/UserController.php index 5dc9c21a..dfe48459 100644 --- a/app/Admin/Controllers/UserController.php +++ b/app/Admin/Controllers/UserController.php @@ -5,6 +5,7 @@ namespace App\Admin\Controllers; use App\Admin\Actions\Grid\DisableUser; use App\Admin\Actions\Grid\EnableUser; use App\Admin\Actions\Grid\Frozen; +use App\Admin\Actions\Grid\UserEditAgent; use App\Admin\Actions\Show\UserDisableBonus; use App\Admin\Actions\Show\UserEditBank; use App\Admin\Actions\Show\UserEditPhone; @@ -102,10 +103,15 @@ class UserController extends AdminController } } - // todo-冻结资产 + // 冻结资产 if (Admin::user()->can('dcat.admin.users.frozen')) { $actions->append(new Frozen()); } + + //调整等级 + if (Admin::user()->can('dcat.admin.users.edit_agent')) { + $actions->append(new UserEditAgent()); + } }); $grid->filter(function (Grid\Filter $filter) { diff --git a/app/Admin/Forms/UserEditAgent.php b/app/Admin/Forms/UserEditAgent.php new file mode 100644 index 00000000..3e15d539 --- /dev/null +++ b/app/Admin/Forms/UserEditAgent.php @@ -0,0 +1,87 @@ +can('dcat.admin.users.edit_agent'); + } + + /** + * Handle the form request. + * + * @param array $input + * + * @return mixed + */ + public function handle(array $input) + { + $id = $this->payload['id'] ?? 0; + $user = User::findOrFail($id); + // dd($user->userInfo->agent_level, $input['agent_level'], $user->userInfo->agent_level >= $input['agent_level']); + if ($user->userInfo->agent_level >= $input['agent_level']) { + throw new BizException('请选择大于当前的等级'); + } + try { + DB::beginTransaction(); + //执行自己升级 + $user->userInfo->update([ + 'agent_level'=>$input['agent_level'], + ]); + //给自己的上级依次升级 + if (count($pids = $user->userInfo->parent_ids) > 0) { + $ancestors = UserInfo::whereIn('user_id', $pids)->latest('depth')->get(); + + foreach ($ancestors as $ancestor) { + $ancestor->attemptUpgradeAgentLevel(); + } + } + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + return $this->response()->error('操作失败:'.$th->getMessage()); + } + + return $this->response() + ->success(__('admin.update_succeeded')) + ->refresh(); + } + + /** + * Build a form here. + */ + public function form() + { + // dd(UserInfo::$agentLevelTexts); + $this->select('agent_level', '管理级别')->options(UserInfo::$agentLevelTexts)->required(); + } + + public function default() + { + $id = $this->payload['id'] ?? 0; + $user = User::with('userInfo')->findOrFail($id); + return [ + 'agent_level' => $user->userInfo->agent_level, + ]; + } +}