DistAdminDistributorController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. $show->field('seo_title');
  107. $show->field('seo_keywords');
  108. $show->field('seo_description');
  109. // 按钮
  110. $show->disableDeleteButton();
  111. });
  112. }
  113. /**
  114. * Make a form builder.
  115. *
  116. * @return Form
  117. */
  118. protected function form()
  119. {
  120. return Form::make(new DistAdminDistributor(), function (Form $form) {
  121. $form->text('company_name')->width(9,3)->required();
  122. $form->text('company_address',)->width(9,3);
  123. $form->text('site_name')->width(9,3)->required();
  124. $form->radio('domain_type')
  125. ->width(9,3)
  126. ->when(0, function (Form $form) {
  127. $form->text('secondary_domain')->width(9,3)->help('Please enter the domain name, such as demo.'.env('TOP_DOMAIN'));
  128. })
  129. ->when(1, function (Form $form) {
  130. $form->text('custom_domain')->width(9,3)->help('Please enter the domain name, such as www.example.com,and bind the domain name to the IP address '.env('DIST_SITE_IP'));
  131. })
  132. ->default(0)
  133. ->options([0=>'second-level domain',1=>'custom domain'])
  134. ->required();
  135. $form->select('appearance_id',admin_trans_field('appearance'))->width(9,3)->options(DistAppearance::selectOptions())->required();
  136. $form->text('country')->width(9,3)->required();
  137. $form->text('contact_number')->width(9,3);
  138. $form->text('service_hotline')->width(9,3);
  139. $form->text('whats_app')->width(9,3);
  140. $form->text('facebook')->width(9,3);
  141. $form->text('instagram')->width(9,3);
  142. $form->text('youtube')->width(9,3);
  143. $form->text('linkedin')->width(9,3);
  144. $form->text('tiktok')->width(9,3);
  145. $form->textarea('remark')->width(9,3);
  146. $form->switch('enabled')->width(9,3)->default(1);
  147. $form->text('seo_title')->width(9,3)->required();
  148. $form->text('seo_keywords')->width(9,3);
  149. $form->textarea('seo_description')->width(9,3);
  150. //保存前回调
  151. $form->saving(function (Form $form) {
  152. if (!$form->isCreating()) {
  153. //如果appearance_id有变化,则更新模版与变量
  154. if ($form->model()->appearance_id != $form->input('appearance_id')) {
  155. $id = $form->model()->id;
  156. DistAppearance::switchTheme($form->input('appearance_id'), $id);
  157. }
  158. }
  159. });
  160. //保存后回调
  161. $form->saved(function (Form $form, $result) {
  162. if ($form->isCreating()) {
  163. //创建后创建默认分类
  164. $newId = $form->getKey();
  165. $distProductCategory = new DistProductCategory();
  166. $categoryRow = $distProductCategory->create([
  167. 'parent_id' => 0,
  168. 'name' => 'Default Category',
  169. 'order' => 0,
  170. 'enabled' => 1,
  171. 'dist_id'=>$newId,
  172. ]);
  173. //slug = id
  174. $categoryRow->slug = $categoryRow->id;
  175. $categoryRow->save();
  176. //更新模版与变量
  177. DistAppearance::switchTheme($form->input('appearance_id'), $newId);
  178. }
  179. });
  180. });
  181. }
  182. }