From ded8f523e94d732b5e17bbd10cbc9e4c07378378 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Fri, 3 Dec 2021 17:24:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BC=9A=E5=91=98=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Actions/Grid/DisableUser.php | 44 ++++++++++++++++ app/Admin/Actions/Grid/EnableUser.php | 67 ++++++++++++++++++++++++ app/Admin/Actions/Grid/SkuVerify.php | 4 +- app/Admin/Controllers/UserController.php | 26 ++++++--- app/Admin/Forms/DisableUser.php | 51 ++++++++++++++++++ app/Models/User.php | 33 ++++++++++++ resources/lang/zh_CN/admin_message.php | 2 + resources/lang/zh_CN/user.php | 6 +++ 8 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 app/Admin/Actions/Grid/DisableUser.php create mode 100644 app/Admin/Actions/Grid/EnableUser.php create mode 100644 app/Admin/Forms/DisableUser.php diff --git a/app/Admin/Actions/Grid/DisableUser.php b/app/Admin/Actions/Grid/DisableUser.php new file mode 100644 index 00000000..b0f28012 --- /dev/null +++ b/app/Admin/Actions/Grid/DisableUser.php @@ -0,0 +1,44 @@ +'; + + public function title() + { + if ($this->title) { + return $this->title.' '.__('admin_message.actions.grid.disable_user'); + } + + return __('admin_message.actions.grid.disable_user'); + } + + /** + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return $user->can('dcat.admin.users.disable'); + } + + public function render() + { + $form = DisableUserForm::make()->payload(['id'=>$this->getKey()]); + return Modal::make() + ->lg() + ->title($this->title()) + ->body($form) + ->button($this->title()); + } +} diff --git a/app/Admin/Actions/Grid/EnableUser.php b/app/Admin/Actions/Grid/EnableUser.php new file mode 100644 index 00000000..59183dfd --- /dev/null +++ b/app/Admin/Actions/Grid/EnableUser.php @@ -0,0 +1,67 @@ +'; + + public function title() + { + if ($this->title) { + return $this->title.' '.__('admin_message.actions.grid.enable_user'); + } + + return __('admin_message.actions.grid.enable_user'); + } + + /** + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return $user->can('dcat.admin.users.enable'); + } + + /** + * Handle the action request. + * + * @param Request $request + * + * @return Response + */ + public function handle(Request $request) + { + $user = User::findOrFail($this->getKey()); + try { + DB::beginTransaction(); + $user->enable(); + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + return $this->response()->error('操作失败,'.$th->getMessage())->refresh(); + } + + return $this->response()->success('操作成功')->refresh(); + } + + /** + * @return string|array|void + */ + public function confirm() + { + return ['确认启用当前用户?']; + } +} diff --git a/app/Admin/Actions/Grid/SkuVerify.php b/app/Admin/Actions/Grid/SkuVerify.php index d36b3a4e..2dab0d58 100644 --- a/app/Admin/Actions/Grid/SkuVerify.php +++ b/app/Admin/Actions/Grid/SkuVerify.php @@ -37,8 +37,8 @@ class SkuVerify extends RowAction $form = SkuVerifyForm::make()->payload(['id'=>$this->getKey()]); return Modal::make() ->lg() - ->title($this->title) + ->title($this->title()) ->body($form) - ->button($this->title); + ->button($this->title()); } } diff --git a/app/Admin/Controllers/UserController.php b/app/Admin/Controllers/UserController.php index d5c5c764..bf158add 100644 --- a/app/Admin/Controllers/UserController.php +++ b/app/Admin/Controllers/UserController.php @@ -2,6 +2,8 @@ namespace App\Admin\Controllers; +use App\Admin\Actions\Grid\DisableUser; +use App\Admin\Actions\Grid\EnableUser; use App\Admin\Repositories\User; use Dcat\Admin\Admin; use Dcat\Admin\Form; @@ -25,17 +27,18 @@ class UserController extends AdminController $grid->column('phone'); // $grid->column('username'); // $grid->column('password'); - $grid->column('userVip.vip.name'); + $grid->column('userVip.vip.name')->label(); // $grid->column('email'); $grid->column('last_login_ip'); $grid->column('last_login_at')->sortable(); $grid->column('register_ip'); // $grid->column('remember_token'); - $grid->column('status')->using([1=>'正常', -1=>'禁用']) - ->dot([ - 1 => 'primary', - -1 => 'danger', - ]); + $grid->column('status')->using([-1=>'冻结', 1=>'正常', 2=>'禁用']) + ->dot([ + -1 => '#b3b9bf', + 1 => 'primary', + 2 => '#b3b9bf', + ]); $grid->column('created_at'); /** 操作 **/ @@ -47,6 +50,17 @@ class UserController extends AdminController //删除以及自定义操作 $grid->actions(function (Grid\Displayers\Actions $actions) { $actions->disableView(Admin::user()->cannot('dcat.admin.users.show')); + + if ($actions->row->status == 1) { + if (Admin::user()->can('dcat.admin.users.disable')) { + $actions->append(new DisableUser()); + } + } + if (in_array($actions->row->status, [-1, 2])) { + if (Admin::user()->can('dcat.admin.users.enable')) { + $actions->append(new EnableUser()); + } + } }); $grid->filter(function (Grid\Filter $filter) { diff --git a/app/Admin/Forms/DisableUser.php b/app/Admin/Forms/DisableUser.php new file mode 100644 index 00000000..ccfb3caa --- /dev/null +++ b/app/Admin/Forms/DisableUser.php @@ -0,0 +1,51 @@ +payload['id'] ?? 0; + $user = User::findOrFail($id); + try { + DB::beginTransaction(); + $user->disable($input['status_remark']); + 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() + { + $id = $this->payload['id'] ?? 0; + + $this->text('status_remark')->required(); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 60cab1a2..e8c31953 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -80,6 +80,39 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac return $this->hasOne(UserVip::class, 'user_id'); } + /** + * 禁用用户 + * + * @param string|null $status_remarks + * @return void + */ + public function disable(?string $status_remark = null) + { + $res = null; + if ($this->status == 1) {//只操作正常用户 + $this->status = 2; + $this->status_remark = $status_remark; + $res = $this->save(); + } + + return $res; + } + + /** + * 启用用户 + * + * @return void + */ + public function enable() + { + $res = null; + if ($this->status != 1) {// + $this->status = 1; + $res = $this->save(); + } + return $res; + } + /** * 设置此用户的密码 * diff --git a/resources/lang/zh_CN/admin_message.php b/resources/lang/zh_CN/admin_message.php index e24fbb04..69760e50 100644 --- a/resources/lang/zh_CN/admin_message.php +++ b/resources/lang/zh_CN/admin_message.php @@ -69,6 +69,8 @@ return [ 'release_up'=>'申请上架', 'sku_sync_spu'=>'同步主商品', 'sku_verify'=>'商品审核', + 'disable_user'=>'禁用用户', + 'enable_user'=>'启用用户', ], ], ]; diff --git a/resources/lang/zh_CN/user.php b/resources/lang/zh_CN/user.php index 4f22a12a..931fabbf 100644 --- a/resources/lang/zh_CN/user.php +++ b/resources/lang/zh_CN/user.php @@ -16,6 +16,12 @@ return [ 'last_login_at' => '最近登录时间', 'register_ip' => '注册IP', 'created_at' => '注册时间', + 'userVip'=>[ + 'vip' => [ + 'name' => '等级', + ], + ], + 'status_remark'=>'备注', ], 'options' => [ ],