UserSecretPasswordController.php 6.5 KB

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