DistAdminDistributorController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\DistAdminDistributor;
  4. use App\Admin\Repositories\DistAppearance;
  5. use App\Models\DistProductCategory;
  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. use Dcat\Admin\Admin;
  12. class DistAdminDistributorController extends AdminController
  13. {
  14. /**
  15. * page index
  16. */
  17. public function index(Content $content)
  18. {
  19. return $content
  20. ->header(admin_trans( 'admin.distro_management'))
  21. ->description('')
  22. ->breadcrumb(['text'=>'list','url'=>''])
  23. ->body($this->grid());
  24. }
  25. /**
  26. * Make a grid builder.
  27. *
  28. * @return Grid
  29. */
  30. protected function grid()
  31. {
  32. return Grid::make(DistAdminDistributor::with(['appearance']), function (Grid $grid) {
  33. $grid->column('id')->sortable();
  34. $grid->column('company_name');
  35. $grid->column('site_name');
  36. $grid->column('domain_type',admin_trans_label('domain'))->display(function ($domainType) {
  37. $title = "";
  38. if ($domainType == 0) {
  39. $title = $this->secondary_domain;
  40. } else {
  41. $title = $this->custom_domain;
  42. }
  43. return "<span style='color:#586cb1'>$title</span>";
  44. });
  45. $grid->column('country',);
  46. $grid->column('appearance.title' ,admin_trans_field('appearance'));
  47. $grid->column('enabled')->switch();
  48. $grid->column('created_at');
  49. $grid->column('updated_at')->sortable();
  50. // 过滤
  51. $grid->filter(function (Grid\Filter $filter) {
  52. $filter->panel();
  53. $filter->expand();
  54. $filter->equal('company_name', )->width(2);
  55. $filter->equal('site_name')->width(2);
  56. $filter->equal('domain')->width(2);
  57. $filter->equal('enabled', )->select(admin_trans_array(config('dictionary.enabled')))->width(2);
  58. });
  59. // 排序
  60. $grid->model()->orderBy("id",'desc');
  61. //按钮
  62. $grid->showQuickEditButton();
  63. $grid->enableDialogCreate();
  64. $grid->disableEditButton();
  65. $grid->disableDeleteButton();
  66. $grid->disableBatchDelete();
  67. });
  68. }
  69. /**
  70. * Make a show builder.
  71. *
  72. * @param mixed $id
  73. *
  74. * @return Show
  75. */
  76. protected function detail($id)
  77. {
  78. return Show::make($id, DistAdminDistributor::with(['appearance']), function (Show $show) {
  79. $show->field('id');
  80. $show->field('company_name',);
  81. $show->field('company_address');
  82. $show->field('site_name');
  83. $show->field('domain_type',admin_trans_label('domain'))->as(function ($domainType) {
  84. $title = "";
  85. if ($domainType == 0) {
  86. $title = $this->secondary_domain;
  87. } else {
  88. $title = $this->custom_domain;
  89. }
  90. return "<span style='color:#586cb1'>$title</span>";
  91. })->unescape();
  92. $show->field('appearance.title' ,admin_trans_field('appearance'));
  93. $show->field('country');
  94. $show->field('contact_number');
  95. $show->field('service_hotline');
  96. $show->field('whats_app');
  97. $show->field('facebook');
  98. $show->field('instagram');
  99. $show->field('youtube');
  100. $show->field('linkedin');
  101. $show->field('tiktok');
  102. $show->field('remark');
  103. $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
  104. $show->field('created_at');
  105. $show->field('updated_at');
  106. // 按钮
  107. $show->disableDeleteButton();
  108. });
  109. }
  110. /**
  111. * Make a form builder.
  112. *
  113. * @return Form
  114. */
  115. protected function form()
  116. {
  117. return Form::make(new DistAdminDistributor(), function (Form $form) {
  118. $form->display('id');
  119. $form->text('company_name', )->required();
  120. $form->text('company_address',);
  121. $form->text('site_name')->required();
  122. $form->radio('domain_type')
  123. ->when(0, function (Form $form) {
  124. $form->text('secondary_domain')->help('Please enter the domain name, such as demo.'.env('TOP_DOMAIN'));
  125. })
  126. ->when(1, function (Form $form) {
  127. $form->text('custom_domain')->help('Please enter the domain name, such as www.example.com,and bind the domain name to the IP address '.env('DIST_SITE_IP'));
  128. })
  129. ->default(0)
  130. ->options([0=>'second-level domain',1=>'custom domain'])
  131. ->required();
  132. $form->select('appearance_id',admin_trans_field('appearance'))->options(DistAppearance::selectOptions())->required();
  133. $form->text('country')->required();
  134. $form->text('contact_number');
  135. $form->text('service_hotline');
  136. $form->text('whats_app');
  137. $form->text('facebook');
  138. $form->text('instagram');
  139. $form->text('youtube');
  140. $form->text('linkedin');
  141. $form->text('tiktok');
  142. $form->textarea('remark');
  143. $form->switch('enabled')->default(1);
  144. //保存前回调
  145. $form->saving(function (Form $form) {
  146. if (!$form->isCreating()) {
  147. //如果appearance_id有变化,则更新模版与变量
  148. if ($form->model()->appearance_id != $form->input('appearance_id')) {
  149. $id = $form->model()->id;
  150. DistAppearance::switchTheme($form->input('appearance_id'), $id);
  151. }
  152. }
  153. });
  154. //保存后回调
  155. $form->saved(function (Form $form, $result) {
  156. if ($form->isCreating()) {
  157. //创建后创建默认分类
  158. $newId = $form->getKey();
  159. $distProductCategory = new DistProductCategory();
  160. $categoryRow = $distProductCategory->create([
  161. 'parent_id' => 0,
  162. 'name' => 'Default Category',
  163. 'order' => 0,
  164. 'enabled' => 1,
  165. 'dist_id'=>$newId,
  166. ]);
  167. //slug = id
  168. $categoryRow->slug = $categoryRow->id;
  169. $categoryRow->save();
  170. //更新模版与变量
  171. DistAppearance::switchTheme($form->input('appearance_id'), $newId);
  172. }
  173. });
  174. });
  175. }
  176. }