UserSecretPasswordController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Module\User\AdminControllers;
  3. use App\Module\User\AdminControllers\Helper\FilterHelper;
  4. use App\Module\User\AdminControllers\Helper\FormHelper;
  5. use App\Module\User\AdminControllers\Helper\GridHelper;
  6. use App\Module\User\AdminControllers\Helper\ShowHelper;
  7. use App\Module\User\Enums\SECRET_PASSWORD_STATUS;
  8. use App\Module\User\Models\UserSecretPassword;
  9. use App\Module\User\Repositorys\UserSecretPasswordRepository;
  10. use App\Module\User\Services\UserService;
  11. use Dcat\Admin\Form;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Show;
  14. use Illuminate\Support\Facades\Hash;
  15. use Spatie\RouteAttributes\Attributes\Resource;
  16. use UCore\DcatAdmin\AdminController;
  17. /**
  18. * 用户安全密码管理控制器
  19. */
  20. #[Resource('user-secret-passwords', names: 'dcat.admin.user-secret-passwords')]
  21. class UserSecretPasswordController extends AdminController
  22. {
  23. /**
  24. * 页面标题
  25. *
  26. * @var string
  27. */
  28. protected $title = '用户安全密码管理';
  29. /**
  30. * 用户服务
  31. *
  32. * @var UserService
  33. */
  34. protected $service;
  35. /**
  36. * 构造函数
  37. */
  38. public function __construct()
  39. {
  40. $this->service = new UserService();
  41. }
  42. /**
  43. * 列表页面
  44. *
  45. * @return Grid
  46. */
  47. protected function grid()
  48. {
  49. return Grid::make(new UserSecretPasswordRepository(), function (Grid $grid) {
  50. $helper = new GridHelper($grid, $this);
  51. $grid->column('id', 'ID')->sortable();
  52. $grid->column('user_id', '用户ID')->sortable();
  53. $grid->column('status', '状态')->using([
  54. SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
  55. SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
  56. SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
  57. ])->label([
  58. SECRET_PASSWORD_STATUS::BIND->value => 'success',
  59. SECRET_PASSWORD_STATUS::UNBIND->value => 'danger',
  60. SECRET_PASSWORD_STATUS::WAIT_CHECK->value => 'warning',
  61. ]);
  62. $grid->column('secret_password', '安全密码')->display(function () {
  63. return '******';
  64. });
  65. $grid->column('last_check_at', '最后验证时间');
  66. $helper->columnCreatedAt();
  67. $helper->columnUpdatedAt();
  68. // 筛选器
  69. $grid->filter(function (Grid\Filter $filter) {
  70. $helper = new FilterHelper($filter, $this);
  71. $filter->equal('id', 'ID');
  72. $filter->equal('user_id', '用户ID');
  73. $filter->equal('status', '状态')->select([
  74. SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
  75. SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
  76. SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
  77. ]);
  78. $helper->betweenCreatedAt();
  79. $filter->between('last_check_at', '最后验证时间')->datetime();
  80. });
  81. });
  82. }
  83. /**
  84. * 详情页面
  85. *
  86. * @param mixed $id
  87. * @return Show
  88. */
  89. protected function detail($id)
  90. {
  91. return Show::make($id, new UserSecretPasswordRepository(), function (Show $show) {
  92. $helper = new ShowHelper($show, $this);
  93. $show->field('id', 'ID');
  94. $show->field('user_id', '用户ID');
  95. $show->field('status', '状态')->as(function ($value) {
  96. $statusMap = [
  97. SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
  98. SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
  99. SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
  100. ];
  101. return $statusMap[$value] ?? '未知';
  102. });
  103. $show->field('secret_password', '安全密码')->as(function () {
  104. return '******';
  105. });
  106. $show->field('last_check_at', '最后验证时间');
  107. // 显示关联的用户信息
  108. $show->relation('user', '用户信息', function ($model) {
  109. $show = Show::make($model, new \App\Module\User\Models\User());
  110. $helper = new ShowHelper($show, $this);
  111. $helper->fieldUserId();
  112. $helper->fieldUsername();
  113. $helper->fieldStatus();
  114. return $show;
  115. });
  116. });
  117. }
  118. /**
  119. * 表单页面
  120. *
  121. * @return Form
  122. */
  123. protected function form()
  124. {
  125. return Form::make(new UserSecretPasswordRepository(), function (Form $form) {
  126. $helper = new FormHelper($form, $this);
  127. $form->display('id', 'ID');
  128. $form->number('user_id', '用户ID')
  129. ->required()
  130. ->min(1)
  131. ->help('用户ID,必须是有效的用户');
  132. $form->radio('status', '状态')
  133. ->options([
  134. SECRET_PASSWORD_STATUS::BIND->value => '已绑定',
  135. SECRET_PASSWORD_STATUS::UNBIND->value => '未绑定',
  136. SECRET_PASSWORD_STATUS::WAIT_CHECK->value => '等待验证',
  137. ])
  138. ->default(SECRET_PASSWORD_STATUS::UNBIND->value);
  139. $form->password('secret_password', '安全密码')
  140. ->help('不修改请留空')
  141. ->saving(function ($value) {
  142. if ($value) {
  143. return Hash::make($value);
  144. }
  145. });
  146. $form->datetime('last_check_at', '最后验证时间');
  147. $form->display('created_at', '创建时间');
  148. $form->display('updated_at', '更新时间');
  149. // 保存前回调
  150. $form->saving(function (Form $form) {
  151. // 如果是新建记录,需要检查用户ID是否存在
  152. if ($form->isCreating()) {
  153. $userId = $form->user_id;
  154. $user = \App\Module\User\Models\User::find($userId);
  155. if (!$user) {
  156. return $form->response()->error('用户ID不存在');
  157. }
  158. // 如果是新建记录,密码必填
  159. if (!$form->secret_password) {
  160. return $form->response()->error('安全密码不能为空');
  161. }
  162. }
  163. });
  164. });
  165. }
  166. }