AccountController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Grid\ResetPassword;
  4. use App\Models\Account;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. class AccountController extends AdminController
  10. {
  11. /**
  12. * Make a grid builder.
  13. *
  14. * @return Grid
  15. */
  16. protected function grid()
  17. {
  18. return Grid::make(new Account(), function (Grid $grid) {
  19. $grid->column('id')->sortable();
  20. $grid->column('user_name');
  21. $grid->column('type')
  22. ->using(config('global.user_type'))
  23. ->label(['info','primary','success']);;
  24. $grid->column('account');
  25. $grid->column('status')
  26. ->using(config('global.user_status'))
  27. ->label(['danger','success'])->switch();
  28. $grid->column('remark')->editable();
  29. $grid->column('created_at');
  30. $grid->actions(function (Grid\Displayers\Actions $actions) {
  31. $actions->append(new ResetPassword(Account::class, $actions->row->id));
  32. });
  33. $grid->disableViewButton();
  34. });
  35. }
  36. /**
  37. * Make a show builder.
  38. *
  39. * @param mixed $id
  40. *
  41. * @return Show
  42. */
  43. protected function detail($id)
  44. {
  45. return Show::make($id, new Account(), function (Show $show) {
  46. $show->field('id');
  47. $show->field('user_name');
  48. $show->field('account');
  49. $show->field('password');
  50. $show->field('type');
  51. $show->field('remark');
  52. $show->field('status');
  53. $show->field('created_at');
  54. $show->field('updated_at');
  55. });
  56. }
  57. /**
  58. * Make a form builder.
  59. *
  60. * @return Form
  61. */
  62. protected function form()
  63. {
  64. return Form::make(new Account(), function (Form $form) {
  65. $form->display('id');
  66. $form->text('user_name')->required();
  67. $form->mobile('account')
  68. ->placeholder('手机号')
  69. ->required()
  70. ->rules(function ($form) {
  71. // 如果不是编辑状态,则添加字段唯一验证
  72. if (!$form->model()->id) {
  73. return 'unique:accounts';
  74. }
  75. });
  76. $form->password('password')
  77. ->placeholder('默认123456');
  78. $form->radio('type')
  79. ->options(config('global.user_type'))
  80. ->default(1);
  81. $form->radio('status')
  82. ->options(config('global.user_status'))
  83. ->default(1);
  84. $form->textarea('remark');
  85. $form->display('created_at');
  86. //$form->display('updated_at');
  87. $form->saving(function (Form $form) {
  88. // 如果不是编辑状态 不填写密码默认123456
  89. if (!$form->model()->id) {
  90. if(!$form->password){
  91. $form->password = \Hash::make(123456);
  92. }else{
  93. $form->password = \Hash::make($form->password);
  94. }
  95. }else{
  96. if ($form->password && $form->model()->password != $form->password) {
  97. $form->password = \Hash::make($form->password);
  98. }
  99. if (! $form->password) {
  100. $form->deleteInput('password');
  101. }
  102. }
  103. });
  104. $form->disableViewButton();
  105. $form->disableDeleteButton();
  106. $form->disableListButton();
  107. $form->disableEditingCheck();
  108. $form->disableViewCheck();
  109. $form->disableCreatingCheck();
  110. });
  111. }
  112. }