DistAdminUserController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Renderable\DistDistributorTable;
  4. use App\Admin\Repositories\DistAdminUser;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. use Dcat\Admin\Layout\Content;
  10. class DistAdminUserController extends AdminController
  11. {
  12. /**
  13. * page index
  14. */
  15. public function index(Content $content)
  16. {
  17. return $content
  18. ->header('Users Management')
  19. ->description('Users Management')
  20. ->breadcrumb(['text'=>'list','url'=>''])
  21. ->body($this->grid());
  22. }
  23. /**
  24. * Make a grid builder.
  25. *
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. return Grid::make(DistAdminUser::with(['distributor']), function (Grid $grid) {
  31. //指定表格视图,去掉右上角的操作按钮
  32. $grid->view('admin.grid.table');
  33. //设置列表
  34. $grid->column('id')->sortable();
  35. $grid->column('username');
  36. $grid->column('name');
  37. $grid->column('distributor.company_name','Company Name');
  38. $grid->column('distributor.level_domain','Level Domain');
  39. $grid->column('language')->using(config('dictionary.languages'));
  40. $grid->column('enabled')->switch();
  41. $grid->column('created_at');
  42. $grid->column('updated_at')->sortable();
  43. //设置过滤器
  44. $grid->filter(function (Grid\Filter $filter) {
  45. $filter->panel();
  46. $filter->expand();
  47. $filter->equal('username')->width(2);
  48. $filter->equal('name','name')->width(2);
  49. $filter->like('distributor.company_name', 'Company Name')->width(2);
  50. $filter->equal('enabled', 'enabled')->select(config('dictionary.enabled'))->width(2);
  51. });
  52. //排序
  53. $grid->model()->orderBy("id",'desc');
  54. //按钮
  55. $grid->showQuickEditButton();
  56. $grid->enableDialogCreate();
  57. $grid->disableEditButton();
  58. $grid->disableDeleteButton();
  59. $grid->disableBatchDelete();
  60. });
  61. }
  62. /**
  63. * Make a show builder.
  64. *
  65. * @param mixed $id
  66. *
  67. * @return Show
  68. */
  69. protected function detail($id)
  70. {
  71. return Show::make($id, DistAdminUser::with(['distributor']), function (Show $show) {
  72. $show->row(function ($show) {
  73. $show->width(6)->field('id');
  74. $show->width(6)->field('username');
  75. $show->width(6)->field('name','name');
  76. $show->width(6)->field('language')->using(config('dictionary.languages'));
  77. $show->width(6)->field('enabled')->using(config('dictionary.enabled'));
  78. $show->width(6)->field('created_at');
  79. $show->width(6)->field('updated_at');
  80. });
  81. $show->row(function ($show) {
  82. $show->width(6)->field('distributor.company_name', 'Company Name');
  83. $show->width(6)->field('distributor.company_address','Company Address');
  84. $show->width(6)->field('distributor.site_name','Site Name');
  85. $show->width(6)->field('distributor.level_domain','Level Domain');
  86. $show->width(6)->field('distributor.country','Country');
  87. $show->width(6)->field('distributor.contact_number','Contact Number');
  88. $show->width(6)->field('distributor.service_hotline','Service Hotline');
  89. $show->width(6)->field('distributor.whats_app','whatsApp');
  90. $show->width(6)->field('distributor.facebook','Facebook');
  91. $show->width(6)->field('distributor.instagram','Instagram');
  92. $show->width(6)->field('distributor.youtube','Youtube');
  93. $show->width(6)->field('distributor.linkedin','Linkedin');
  94. $show->width(6)->field('distributor.tiktok','Tiktok');
  95. $show->width(6)->field('distributor.remark','Remark');
  96. });
  97. // 按钮
  98. $show->disableDeleteButton();
  99. });
  100. }
  101. /**
  102. * Make a form builder.
  103. *
  104. * @return Form
  105. */
  106. protected function form()
  107. {
  108. return Form::make(DistAdminUser::with(['distributor']), function (Form $form) {
  109. $form->display('id');
  110. $form->text('username')->required();
  111. $form->password('password')->customFormat(function ($v) {
  112. return "";
  113. });
  114. $form->text('name','name')->required();
  115. $form->select('language')->options(config('dictionary.languages'))->required();
  116. $form->switch('enabled')->default(1);
  117. $form->selectTable('dist_id', 'Select Distributor')
  118. ->title('distId')
  119. ->from(DistDistributorTable::make());
  120. //保存前回调
  121. $form->saving(function (Form $form) {
  122. //判断用户名是否重复
  123. $count = DistAdminUser::findCountByUsername($form->getKey(), $form->username);
  124. if ($count > 0) {
  125. return $form->response()->error('Username already exists');
  126. }
  127. if($form->isCreating() && ($form->password === null || $form->password === '')) {
  128. return $form->response()->error('Password cannot be empty');
  129. }
  130. //密码加密
  131. if ($form->password) {
  132. $form->password = bcrypt($form->password);
  133. } else {
  134. $form->deleteInput('password');
  135. }
  136. });
  137. //保存后回调
  138. $form->saved(function (Form $form, $result) {
  139. if ($form->isCreating()) {
  140. //添加角色
  141. $newId = $form->getKey();
  142. if (! $newId) {
  143. return $form->response()->error('Failed to save data');
  144. }
  145. DistAdminUser::addRoleUser($newId, config('dictionary.dist_role_id'));//分销商角色ID 2
  146. }
  147. });
  148. });
  149. }
  150. }