From d31cffd23e06e502798902ab1cdbc6bb9d8ae3fe Mon Sep 17 00:00:00 2001 From: liapples Date: Fri, 13 Aug 2021 16:46:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9C=B0=E6=8E=A5=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/Controllers/GuideController.php | 136 ++++++++++++++++++++++ app/Admin/Extensions/Grid/AuditGuide.php | 56 +++++++++ app/Admin/Repositories/Guide.php | 16 +++ app/Models/Guide.php | 23 ++++ resources/lang/zh_CN/guide.php | 18 +++ 5 files changed, 249 insertions(+) create mode 100644 app/Admin/Controllers/GuideController.php create mode 100644 app/Admin/Extensions/Grid/AuditGuide.php create mode 100644 app/Admin/Repositories/Guide.php create mode 100644 app/Models/Guide.php create mode 100644 resources/lang/zh_CN/guide.php diff --git a/app/Admin/Controllers/GuideController.php b/app/Admin/Controllers/GuideController.php new file mode 100644 index 0000000..ec94a43 --- /dev/null +++ b/app/Admin/Controllers/GuideController.php @@ -0,0 +1,136 @@ +disableDeleteButton(); + + //如果是审核页面,多加where条件判断 + if (strpos(Route::current()->uri, 'audit')) { + $grid->model()->where('status', UserStatus::UNAUDITED); + } + $grid->column('id')->sortable(); + $grid->column('account'); + $grid->column('guide_name'); + $grid->column('photo')->image(60, 60); + $grid->column('license_pic')->image(60, 60); + $grid->column('contact_phone'); + $grid->column('created_at')->display(fn($v) => $v); + $grid->column('updated_at')->display(fn($v) => $v); + + $grid->column('status', '状态') + ->if(fn() => $this->status == UserStatus::UNAUDITED) + ->display('') + ->then(function ($column) { + $column->append((new AuditGuide(null, 1))->setKey($this->id))->append(' '); + $column->append((new AuditGuide(null, 2))->setKey($this->id)); + }) + ->else() + ->using(UserStatus::array()) + ->dot([ + UserStatus::NORMAL => 'success', + UserStatus::UNAUDITED => '', + UserStatus::REFUSE => 'danger', + UserStatus::DISABLED => 'warning', + ], 'primary'); + + $grid->filter(function (Grid\Filter $filter) { + $filter->panel(); + + $filter->equal('id')->width(2); + $filter->like('guide_name')->width(3); + $filter->equal('status')->select(UserStatus::array())->width(2); + }); + }); + } + + /** + * Make a show builder. + * + * @param mixed $id + * + * @return Show + */ + protected function detail($id) + { + return Show::make($id, new Guide(), function (Show $show) { + $show->disableDeleteButton(); + + $show->field('id'); + $show->field('account'); + $show->field('guide_name'); + $show->field('status')->using(UserStatus::array()); + $show->field('photo')->image(); + $show->field('license_pic')->image(); + $show->field('contact_phone'); + $show->field('created_at')->as(fn($v) => date('Y-m-d H:i:s', $v)); + $show->field('updated_at')->as(fn($v) => date('Y-m-d H:i:s', $v)); + }); + } + + /** + * Make a form builder. + * + * @return Form + */ + protected function form() + { + return Form::make(new Guide(), function (Form $form) { + $form->disableDeleteButton(); + + $form->display('id'); + //新增 + if ($form->isCreating()) { + $form->text('account')->required(); + $form->text('password')->required(); + } + //编辑 + else if ($form->isEditing()) { + $form->display('account'); + $form->text('password')->customFormat(fn() => ''); + } + $form->select('status')->options(UserStatus::array())->default(UserStatus::NORMAL); + $form->text('guide_name'); + $form->image('photo'); + $form->image('license_pic'); + $form->text('contact_phone'); + })->saving(function (Form $form) { + //判断账号是否唯一 + if ($form->isCreating()) { + if ($form->repository()->model()->where('account', $form->account)->exists()) { + return $form->response()->error($form->account . ' 的账号已经存在'); + } + } + + //不允许编辑的字段 + if ($form->isEditing()) { + $form->ignore(['id', 'account', 'created_at', 'updated_at', 'deleted_at']); + } + + //过滤null字段 + foreach ($form->input() as $k => $v) { + if (is_null($v)) { + $form->$k = ''; + } + } + }); + } +} diff --git a/app/Admin/Extensions/Grid/AuditGuide.php b/app/Admin/Extensions/Grid/AuditGuide.php new file mode 100644 index 0000000..8e9d19e --- /dev/null +++ b/app/Admin/Extensions/Grid/AuditGuide.php @@ -0,0 +1,56 @@ +action = $action; //$action:1=通过;2=拒绝 + $this->title = $action == 1 ? '通过' : '拒绝'; + } + + protected function html() + { + $class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger'; + $this->appendHtmlAttribute('class', $class); + $this->defaultHtmlAttribute('href', 'javascript:;'); + + return "formatHtmlAttributes()}>{$this->title}"; + } + + public function handle(Request $request) + { + try { + $user = Guide::find($this->getKey()); + $user->status = $request->action == 1 ? UserStatus::NORMAL : UserStatus::REFUSE; + $user->save(); + + return $this->response()->success("审核成功")->refresh(); + } catch (\Exception $e) { + return $this->response()->error($e->getMessage()); + } + } + + public function confirm() + { + return ['确定要'.$this->title.'该用户吗?', '']; + } + + public function parameters() + { + return ['action' => $this->action]; + } +} diff --git a/app/Admin/Repositories/Guide.php b/app/Admin/Repositories/Guide.php new file mode 100644 index 0000000..b097ab9 --- /dev/null +++ b/app/Admin/Repositories/Guide.php @@ -0,0 +1,16 @@ +attributes['password']) || //新增时 + $this->attributes['password'] != $value && !empty($this->attributes['password']) //编辑时 + ) { + $this->attributes['password'] = $this->passMd5($value); + } + } + } +} diff --git a/resources/lang/zh_CN/guide.php b/resources/lang/zh_CN/guide.php new file mode 100644 index 0000000..a2763fc --- /dev/null +++ b/resources/lang/zh_CN/guide.php @@ -0,0 +1,18 @@ + [ + 'Guide' => '地接', + 'guide' => '地接', + ], + 'fields' => [ + 'account' => '账号', + 'password' => '密码', + 'guide_name' => '地接名称', + 'photo' => '形象照', + 'license_pic' => '地接资格证件', + 'contact_phone' => '联系电话', + 'status' => '状态', + ], + 'options' => [ + ], +];