DistAdminUserController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. $grid->filter(function (Grid\Filter $filter) {
  44. $filter->equal('username');
  45. $filter->equal('name','name');
  46. $filter->like('distributor.company_name', 'Company Name');
  47. });
  48. //排序
  49. $grid->model()->orderBy("id",'desc');
  50. //按钮
  51. $grid->showQuickEditButton();
  52. $grid->enableDialogCreate();
  53. $grid->disableEditButton();
  54. $grid->disableDeleteButton();
  55. $grid->disableBatchDelete();
  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, DistAdminUser::with(['distributor']), function (Show $show) {
  68. $show->row(function ($show) {
  69. $show->width(6)->field('id');
  70. $show->width(6)->field('username');
  71. $show->width(6)->field('name','name');
  72. $show->width(6)->field('language')->using(config('dictionary.languages'));
  73. $show->width(6)->field('enabled')->using(config('dictionary.enabled'));
  74. $show->width(6)->field('created_at');
  75. $show->width(6)->field('updated_at');
  76. });
  77. $show->row(function ($show) {
  78. $show->width(6)->field('distributor.company_name', 'Company Name');
  79. $show->width(6)->field('distributor.company_address','Company Address');
  80. $show->width(6)->field('distributor.site_name','Site Name');
  81. $show->width(6)->field('distributor.level_domain','Level Domain');
  82. $show->width(6)->field('distributor.country','Country');
  83. $show->width(6)->field('distributor.contact_number','Contact Number');
  84. $show->width(6)->field('distributor.service_hotline','Service Hotline');
  85. $show->width(6)->field('distributor.whats_app','whatsApp');
  86. $show->width(6)->field('distributor.facebook','Facebook');
  87. $show->width(6)->field('distributor.instagram','Instagram');
  88. $show->width(6)->field('distributor.youtube','Youtube');
  89. $show->width(6)->field('distributor.linkedin','Linkedin');
  90. $show->width(6)->field('distributor.tiktok','Tiktok');
  91. $show->width(6)->field('distributor.remark','Remark');
  92. });
  93. // 按钮
  94. $show->disableDeleteButton();
  95. });
  96. }
  97. /**
  98. * Make a form builder.
  99. *
  100. * @return Form
  101. */
  102. protected function form()
  103. {
  104. return Form::make(DistAdminUser::with(['distributor']), function (Form $form) {
  105. $form->display('id');
  106. $form->text('username')->required();
  107. $form->password('password')->customFormat(function ($v) {
  108. return "";
  109. });
  110. $form->text('name','name')->required();
  111. $form->select('language')->options(config('dictionary.languages'))->required();
  112. $form->switch('enabled')->default(1);
  113. $form->selectTable('dist_id', 'Select Distributor')
  114. ->title('distId')
  115. ->from(DistDistributorTable::make());
  116. //保存前回调
  117. $form->saving(function (Form $form) {
  118. //判断用户名是否重复
  119. $count = DistAdminUser::findCountByUsername($form->getKey(), $form->username);
  120. if ($count > 0) {
  121. return $form->response()->error('Username already exists');
  122. }
  123. if($form->isCreating() && ($form->password === null || $form->password === '')) {
  124. return $form->response()->error('Password cannot be empty');
  125. }
  126. //密码加密
  127. if ($form->password) {
  128. $form->password = bcrypt($form->password);
  129. } else {
  130. $form->deleteInput('password');
  131. }
  132. });
  133. //保存后回调
  134. $form->saved(function (Form $form, $result) {
  135. if ($form->isCreating()) {
  136. //添加角色
  137. $newId = $form->getKey();
  138. if (! $newId) {
  139. return $form->response()->error('Failed to save data');
  140. }
  141. DistAdminUser::addRoleUser($newId, config('dictionary.dist_role_id'));//分销商角色ID 2
  142. }
  143. });
  144. });
  145. }
  146. }