海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Extensions\Grid\AuditGuide;
  4. use App\Admin\Repositories\Guide;
  5. use App\Common\UserStatus;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Route;
  12. class GuideController extends AdminController
  13. {
  14. /**
  15. * Make a grid builder.
  16. *
  17. * @return Grid
  18. */
  19. protected function grid()
  20. {
  21. return Grid::make(new Guide(), function (Grid $grid) {
  22. $grid->disableDeleteButton();
  23. //如果是审核页面,多加where条件判断
  24. if (strpos(Route::current()->uri, 'audit')) {
  25. $grid->model()->where('status', UserStatus::UNAUDITED);
  26. }
  27. $grid->column('id')->sortable();
  28. $grid->column('username');
  29. $grid->column('name');
  30. $grid->column('photo')->image('', 60, 60);
  31. $grid->column('license_pic')->image('', 60, 60);
  32. $grid->column('contact_phone');
  33. $grid->column('rate')->editable()->help('分成百分比,如10%,则输入10');
  34. $grid->column('created_at');
  35. $grid->column('status', '状态')
  36. ->if(fn() => $this->status == UserStatus::UNAUDITED)
  37. ->display('')
  38. ->then(function ($column) {
  39. $column->append((new AuditGuide(null, 1))->setKey($this->id))->append('&nbsp;');
  40. $column->append((new AuditGuide(null, 2))->setKey($this->id));
  41. })
  42. ->else()
  43. ->using(UserStatus::array())
  44. ->dot([
  45. UserStatus::NORMAL => 'success',
  46. UserStatus::UNAUDITED => '',
  47. UserStatus::REFUSE => 'danger',
  48. UserStatus::DISABLED => 'warning',
  49. ], 'primary');
  50. $grid->filter(function (Grid\Filter $filter) {
  51. $filter->panel();
  52. $filter->equal('id')->width(2);
  53. $filter->like('name')->width(3);
  54. $filter->equal('status')->select(UserStatus::array())->width(2);
  55. });
  56. });
  57. }
  58. /**
  59. * Make a show builder.
  60. *
  61. * @param mixed $id
  62. *
  63. * @return Show
  64. */
  65. protected function detail($id)
  66. {
  67. return Show::make($id, new Guide(), function (Show $show) {
  68. $show->disableDeleteButton();
  69. $show->field('id');
  70. $show->field('username');
  71. $show->field('name');
  72. $show->field('status')->using(UserStatus::array());
  73. $show->field('photo')->image('', 80, 80);
  74. $show->field('license_pic')->image('', 80, 80);
  75. $show->field('contact_phone');
  76. $show->field('rate');
  77. $show->field('created_at');
  78. $show->field('updated_at');
  79. });
  80. }
  81. /**
  82. * Make a form builder.
  83. *
  84. * @return Form
  85. */
  86. protected function form()
  87. {
  88. return Form::make(new Guide(), function (Form $form) {
  89. $form->disableDeleteButton();
  90. $form->display('id');
  91. //新增
  92. if ($form->isCreating()) {
  93. $form->text('username')->required();
  94. $form->text('password')->required();
  95. }
  96. //编辑
  97. else if ($form->isEditing()) {
  98. $form->display('username');
  99. $form->text('password')->customFormat(fn() => '');
  100. }
  101. $form->select('status')->options(UserStatus::array())->default(UserStatus::NORMAL);
  102. $form->text('name');
  103. $form->image('photo')->removable(false)->uniqueName();
  104. $form->image('license_pic')->removable(false)->uniqueName();
  105. $form->text('contact_phone');
  106. $form->number('rate')->min(0)->max(100)->help('分成百分比,如10%,则输入10');
  107. })->saving(function (Form $form) {
  108. //判断账号是否唯一
  109. if ($form->isCreating()) {
  110. if ($form->repository()->model()->where('username', $form->username)->exists()) {
  111. return $form->response()->error($form->username . ' 的账号已经存在');
  112. }
  113. }
  114. //抽成比例
  115. if ($form->rate < 0 || $form->rate > 100) {
  116. return $form->response()->error('抽成比例在 0 ~ 100 之间');
  117. }
  118. //不允许编辑的字段
  119. if ($form->isEditing()) {
  120. $form->ignore(['id', 'username', 'created_at', 'updated_at', 'deleted_at']);
  121. }
  122. //过滤null字段
  123. foreach ($form->input() as $k => $v) {
  124. if (is_null($v)) {
  125. $form->$k = '';
  126. }
  127. }
  128. })->saved(function (Form $form) {
  129. //插入权限表
  130. if ($form->status == UserStatus::NORMAL) {
  131. DB::table(config('admin-guide.database.role_users_table'))
  132. ->insertOrIgnore(['role_id' => 2, 'user_id' => $form->getKey()]);
  133. }
  134. });
  135. }
  136. }