DistAdminUserController.php 6.2 KB

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