| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace App\Module\User\AdminControllers;
- use App\Module\User\AdminControllers\Helper\FilterHelper;
- use App\Module\User\AdminControllers\Helper\FormHelper;
- use App\Module\User\AdminControllers\Helper\GridHelper;
- use App\Module\User\AdminControllers\Helper\ShowHelper;
- use App\Module\User\Enums\SECRET_PASSWORD_STATUS;
- use App\Module\User\Models\UserSecretPassword;
- use App\Module\User\Services\UserService;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Illuminate\Support\Facades\Hash;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- /**
- * 用户安全密码管理控制器
- */
- #[Resource('user-secret-passwords', names: 'dcat.admin.user-secret-passwords')]
- class UserSecretPasswordController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '用户安全密码管理';
-
- /**
- * 用户服务
- *
- * @var UserService
- */
- protected $service;
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->service = new UserService();
- }
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new UserSecretPassword(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
-
- $grid->column('id', 'ID')->sortable();
- $grid->column('user_id', '用户ID')->sortable();
- $grid->column('status', '状态')->using([
- SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
- SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
- SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
- ])->label([
- SECRET_PASSWORD_STATUS::BIND->value => 'success',
- SECRET_PASSWORD_STATUS::UNBIND->value => 'danger',
- SECRET_PASSWORD_STATUS::WAIT_CHECK->value => 'warning',
- ]);
- $grid->column('password', '安全密码')->display(function () {
- return '******';
- });
- $grid->column('last_check_at', '最后验证时间');
- $helper->columnCreatedAt();
- $helper->columnUpdatedAt();
-
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $helper = new FilterHelper($filter, $this);
- $filter->equal('id', 'ID');
- $filter->equal('user_id', '用户ID');
- $filter->equal('status', '状态')->select([
- SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
- SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
- SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
- ]);
- $helper->betweenCreatedAt();
- $filter->between('last_check_at', '最后验证时间')->datetime();
- });
- });
- }
- /**
- * 详情页面
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new UserSecretPassword(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
-
- $show->field('id', 'ID');
- $show->field('user_id', '用户ID');
- $show->field('status', '状态')->as(function ($value) {
- $statusMap = [
- SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
- SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
- SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
- ];
- return $statusMap[$value] ?? '未知';
- });
- $show->field('password', '安全密码')->as(function () {
- return '******';
- });
- $show->field('last_check_at', '最后验证时间');
- $helper->fieldCreatedAt();
- $helper->fieldUpdatedAt();
- $helper->fieldDeletedAt();
-
- // 显示关联的用户信息
- $show->relation('user', '用户信息', function ($model) {
- $show = Show::make($model, new \App\Module\User\Models\User());
- $helper = new ShowHelper($show, $this);
-
- $helper->fieldUserId();
- $helper->fieldUsername();
- $helper->fieldStatus();
- $helper->fieldCreatedAt();
- $helper->fieldUpdatedAt();
-
- return $show;
- });
- });
- }
- /**
- * 表单页面
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new UserSecretPassword(), function (Form $form) {
- $helper = new FormHelper($form, $this);
-
- $form->display('id', 'ID');
- $form->number('user_id', '用户ID')
- ->required()
- ->min(1)
- ->help('用户ID,必须是有效的用户');
- $form->radio('status', '状态')
- ->options([
- SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
- SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
- SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
- ])
- ->default(SECRET_PASSWORD_STATUS::UNBIND->value);
- $form->password('password', '安全密码')
- ->help('不修改请留空')
- ->saving(function ($value) {
- if ($value) {
- return Hash::make($value);
- }
- });
- $form->datetime('last_check_at', '最后验证时间');
-
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
-
- // 保存前回调
- $form->saving(function (Form $form) {
- // 如果是新建记录,需要检查用户ID是否存在
- if ($form->isCreating()) {
- $userId = $form->user_id;
- $user = \App\Module\User\Models\User::find($userId);
- if (!$user) {
- return $form->response()->error('用户ID不存在');
- }
-
- // 如果是新建记录,密码必填
- if (!$form->password) {
- return $form->response()->error('安全密码不能为空');
- }
- }
- });
- });
- }
- }
|