AccountController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $grid->export()
  35. ->titles([
  36. 'user_name' => '用户名',
  37. 'type_text' => '用户类型',
  38. 'account' => '账号',
  39. 'status_text' => '状态',
  40. 'remark' => '备注',
  41. 'created_at' => '添加时间',
  42. ])->rows(function ($rows){
  43. foreach ($rows as &$row) {
  44. $row['type_text']= config('global.user_type')[$row['type']];
  45. $row['status_text'] = config('global.user_status')[$row['status']];
  46. }
  47. return $rows;
  48. })
  49. ->xlsx()
  50. ->disableExportSelectedRow();
  51. });
  52. }
  53. /**
  54. * Make a show builder.
  55. *
  56. * @param mixed $id
  57. *
  58. * @return Show
  59. */
  60. protected function detail($id)
  61. {
  62. return Show::make($id, new Account(), function (Show $show) {
  63. $show->field('id');
  64. $show->field('user_name');
  65. $show->field('account');
  66. $show->field('password');
  67. $show->field('type');
  68. $show->field('remark');
  69. $show->field('status');
  70. $show->field('created_at');
  71. $show->field('updated_at');
  72. });
  73. }
  74. /**
  75. * Make a form builder.
  76. *
  77. * @return Form
  78. */
  79. protected function form()
  80. {
  81. return Form::make(new Account(), function (Form $form) {
  82. $form->display('id');
  83. $form->text('user_name')->required();
  84. $form->mobile('account')
  85. ->placeholder('手机号')
  86. ->required()
  87. ->rules(function ($form) {
  88. // 如果不是编辑状态,则添加字段唯一验证
  89. if (!$form->model()->id) {
  90. return 'unique:accounts';
  91. }
  92. });
  93. $form->password('password')
  94. ->placeholder('默认123456');
  95. $form->radio('type')
  96. ->options(config('global.user_type'))
  97. ->default(1);
  98. $form->radio('status')
  99. ->options(config('global.user_status'))
  100. ->default(1);
  101. $form->textarea('remark');
  102. // $form->display('created_at');
  103. //$form->display('updated_at');
  104. $form->saving(function (Form $form) {
  105. // 如果不是编辑状态 不填写密码默认123456
  106. if (!$form->model()->id) {
  107. if(!$form->password){
  108. $form->password = \Hash::make(123456);
  109. }else{
  110. $form->password = \Hash::make($form->password);
  111. }
  112. }else{
  113. if ($form->password && $form->model()->password != $form->password) {
  114. $form->password = \Hash::make($form->password);
  115. }
  116. if (! $form->password) {
  117. $form->deleteInput('password');
  118. }
  119. }
  120. });
  121. $form->disableViewButton();
  122. $form->disableDeleteButton();
  123. $form->disableListButton();
  124. $form->disableEditingCheck();
  125. $form->disableViewCheck();
  126. $form->disableCreatingCheck();
  127. });
  128. }
  129. }