DistAdminUserController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Renderable\DistDistributorTable;
  4. use App\Admin\Repositories\DistAdminUser;
  5. use App\Models\DistAdminDistributor;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Layout\Content;
  11. class DistAdminUserController extends AdminController
  12. {
  13. /**
  14. * page index
  15. */
  16. public function index(Content $content)
  17. {
  18. return $content
  19. ->header(admin_trans( 'admin.users_management'))
  20. ->description('')
  21. ->breadcrumb(['text'=>'list','url'=>''])
  22. ->body($this->grid());
  23. }
  24. /**
  25. * Make a grid builder.
  26. *
  27. * @return Grid
  28. */
  29. protected function grid()
  30. {
  31. return Grid::make(DistAdminUser::with(['distributor']), function (Grid $grid) {
  32. //指定表格视图,去掉右上角的操作按钮
  33. $grid->view('admin.grid.table');
  34. //设置列表
  35. $grid->column('id')->sortable();
  36. $grid->column('username');
  37. $grid->column('name');
  38. $grid->column('distributor.company_name',admin_trans_label('distributor_company_name'));
  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',)->width(2);
  49. $filter->like('distributor.company_name', admin_trans_label('company_name'))->width(2);
  50. $filter->equal('enabled', )->select(admin_trans_array(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');
  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', admin_trans_label('distributor_company_name'));
  83. $show->width(6)->field('distributor.company_address',admin_trans_label('company_address'));
  84. $show->width(6)->field('distributor.site_name',admin_trans_label('site_name'));
  85. // $show->width(6)->field('distributor.level_domain',admin_trans_label('level_domain'));
  86. $show->width(6)->field('distributor.country',admin_trans_label('country'));
  87. $show->width(6)->field('distributor.contact_number',admin_trans_label('contact_number'));
  88. $show->width(6)->field('distributor.service_hotline',admin_trans_label('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',admin_trans_label('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',)->required();
  115. $form->select('language')->options(config('dictionary.languages'))->required();
  116. $form->selectTable('dist_id', admin_trans_label('distributor'))
  117. ->title('distId')
  118. ->from(DistDistributorTable::make())
  119. ->model(DistAdminDistributor::class, 'id', 'company_name');;
  120. $form->switch('enabled')->default(1);
  121. //保存前回调
  122. $form->saving(function (Form $form) {
  123. //判断用户名是否重复
  124. $count = DistAdminUser::findCountByUsername($form->getKey(), $form->username);
  125. if ($count > 0) {
  126. return $form->response()->error('Username already exists');
  127. }
  128. if($form->isCreating() && ($form->password === null || $form->password === '')) {
  129. return $form->response()->error('Password cannot be empty');
  130. }
  131. //密码加密
  132. if ($form->password) {
  133. $form->password = bcrypt($form->password);
  134. } else {
  135. $form->deleteInput('password');
  136. }
  137. });
  138. //保存后回调
  139. $form->saved(function (Form $form, $result) {
  140. if ($form->isCreating()) {
  141. //添加角色
  142. $newId = $form->getKey();
  143. if (! $newId) {
  144. return $form->response()->error('Failed to save data');
  145. }
  146. DistAdminUser::addRoleUser($newId, config('dictionary.dist_role_id'));//分销商角色ID 2
  147. }
  148. });
  149. });
  150. }
  151. }