DistAdminUserController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\DistAdminUser;
  4. use App\Models\DistProductCategory;
  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. use Dcat\Admin\Admin;
  11. class DistAdminUserController extends AdminController
  12. {
  13. /**
  14. * page index
  15. */
  16. public function index(Content $content)
  17. {
  18. return $content
  19. ->header('Distribution Management')
  20. ->description('Distribution Management')
  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(new DistAdminUser(), function (Grid $grid) {
  32. //指定表格视图,去掉右上角的操作按钮
  33. $grid->view('admin.grid.table');
  34. //设置列表
  35. $grid->column('id')->sortable();
  36. $grid->column('username');
  37. $grid->column('name','Company Name');
  38. $grid->column('language')->using(config('dictionary.languages'));
  39. $grid->column('enabled')->switch();
  40. $grid->column('created_at');
  41. $grid->column('updated_at')->sortable();
  42. $grid->filter(function (Grid\Filter $filter) {
  43. $filter->equal('username');
  44. $filter->equal('name','company name');
  45. });
  46. //排序
  47. $grid->model()->orderBy("id",'desc');
  48. //按钮
  49. $grid->showQuickEditButton();
  50. $grid->enableDialogCreate();
  51. $grid->disableEditButton();
  52. $grid->disableDeleteButton();
  53. $grid->disableBatchDelete();
  54. });
  55. }
  56. /**
  57. * Make a show builder.
  58. *
  59. * @param mixed $id
  60. *
  61. * @return Show
  62. */
  63. protected function detail($id)
  64. {
  65. return Show::make($id, DistAdminUser::with(['info']), function (Show $show) {
  66. $show->row(function (Show\Row $show) {
  67. $show->width(6)->field('id');
  68. $show->width(6)->field('username');
  69. $show->width(6)->field('info.site_name','Site Name');
  70. $show->width(6)->field('name','Company Name');
  71. $show->width(6)->field('info.country','Country');
  72. $show->width(6)->field('info.contact_number','Contact Number');
  73. $show->width(6)->field('info.service_hotline','Service Hotline');
  74. $show->width(6)->field('info.whats_app','whatsApp');
  75. $show->width(6)->field('info.facebook','Facebook');
  76. $show->width(6)->field('info.instagram','Instagram');
  77. $show->width(6)->field('info.youtube','Youtube');
  78. $show->width(6)->field('info.linkedin','Linkedin');
  79. $show->width(6)->field('info.tiktok','Tiktok');
  80. $show->width(6)->field('info.second_level_domain','Second Level Domain');
  81. $show->width(6)->field('language')->using(config('dictionary.languages'));
  82. $show->width(6)->field('enabled')->using(config('dictionary.enabled'));
  83. $show->width(6)->field('created_at');
  84. $show->width(6)->field('updated_at');
  85. $show->width(6)->field('info.company_address','Company Address');
  86. $show->width(6)->field('info.remark','Remark');
  87. });
  88. });
  89. }
  90. /**
  91. * Make a form builder.
  92. *
  93. * @return Form
  94. */
  95. protected function form()
  96. {
  97. return Form::make(DistAdminUser::with(['info']), function (Form $form) {
  98. $form->display('id');
  99. $form->text('username')->required();
  100. $form->password('password')->customFormat(function ($v) {
  101. return "";
  102. });
  103. $form->text('name','Company Name')->required();
  104. $form->text('info.site_name','Site Name')->required();
  105. $form->text('info.second_level_domain','Second Level Domain')->required();
  106. $form->select('language')->options(config('dictionary.languages'))->required();
  107. $form->switch('enabled')->default(1);
  108. $form->fieldset('Contact Information', function (Form $form) {
  109. $form->text('info.country','Country');
  110. $form->text('info.contact_number','Contact Number');
  111. $form->text('info.service_hotline','Service Hotline');
  112. $form->text('info.whats_app','WhatsApp');
  113. $form->text('info.facebook','Facebook');
  114. $form->text('info.instagram','Instagram');
  115. $form->text('info.youtube','Youtube');
  116. $form->text('info.linkedin','Linkedin');
  117. $form->text('info.tiktok','Tiktok');
  118. $form->textarea('info.company_address','Company Address');
  119. $form->textarea('info.remark','Remark');
  120. });
  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. $distProductCategory = new DistProductCategory();
  149. $distProductCategory->create([
  150. 'parent_id' => 0,
  151. 'name' => 'Default Category',
  152. 'order' => 0,
  153. 'enabled' => 1,
  154. 'dist_user_id'=>$newId,
  155. ]);
  156. }
  157. });
  158. });
  159. }
  160. }