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, + ]; + } +}