DistAdminUserController.php 6.1 KB

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