DistSiteController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Grid\InitAppearance;
  4. use App\Admin\Repositories\DistAdminDistributor;
  5. use App\Admin\Repositories\DistAppearance;
  6. use App\Libraries\CommonHelper;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Layout\Content;
  11. use Dcat\Admin\Show;
  12. use Illuminate\Support\Facades\DB;
  13. class DistSiteController extends AdminController
  14. {
  15. /**
  16. * page index
  17. */
  18. public function index(Content $content)
  19. {
  20. return $content
  21. ->header(admin_trans( 'admin.site_management'))
  22. ->description('')
  23. ->breadcrumb(['text'=>'list','url'=>''])
  24. ->body($this->grid());
  25. }
  26. /**
  27. * Make a grid builder.
  28. *
  29. * @return Grid
  30. */
  31. protected function grid()
  32. {
  33. return Grid::make(DistAdminDistributor::with(['appearance']), function (Grid $grid) {
  34. //默认分页条数
  35. $grid->paginate(config('admin.per_page'));
  36. //指定视图,去掉删除按钮
  37. $grid->view('admin.grid.table');
  38. $grid->column('id')->sortable();
  39. $grid->column('client_code',admin_trans_label('distributor_code'))->width('15%');
  40. $grid->column('site_name');
  41. $grid->column('appearance.title' ,admin_trans_field('appearance'));
  42. $grid->column('domain_type')->using(admin_trans_array(config('dictionary.domain_type')));
  43. $grid->column('secondary_domain');
  44. $grid->column('custom_domain');
  45. $grid->column('updated_at')->sortable();
  46. // 过滤
  47. $grid->filter(function (Grid\Filter $filter) {
  48. $filter->panel();
  49. $filter->expand();
  50. $filter->equal('dist_code',admin_trans_label('client_code') )->width(2);
  51. $filter->equal('site_name' )->width(2);
  52. });
  53. // 排序
  54. $grid->model()->where('enabled',1)->orderBy("id",'desc');
  55. //按钮
  56. $grid->enableDialogCreate();
  57. $grid->disableDeleteButton();
  58. $grid->disableCreateButton();
  59. //批量操作
  60. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  61. $batch->disableDelete();
  62. });
  63. });
  64. }
  65. /**
  66. * Make a show builder.
  67. *
  68. * @param mixed $id
  69. *
  70. * @return Show
  71. */
  72. protected function detail($id)
  73. {
  74. return Show::make($id, DistAdminDistributor::with(['appearance']), function (Show $show) {
  75. $show->field('domain_type',admin_trans_label('domain_type'))->using(admin_trans_array(config('dictionary.domain_type')));
  76. $show->field('secondary_domain');
  77. $show->field('custom_domain');
  78. $show->divider();
  79. $show->field('site_name');
  80. $show->field('appearance.title' ,admin_trans_field('appearance'));
  81. $show->field('copy_right');
  82. $show->field('logo')->as(function ($image) {
  83. // 开始生成 HTML
  84. $dataImages = [$image];
  85. return CommonHelper::displayImage($dataImages,100);
  86. })->unescape();
  87. $show->field('seo_title');
  88. $show->field('seo_keywords');
  89. $show->field('seo_description');
  90. $show->divider();
  91. $show->field('company_name');
  92. $show->field('company_address');
  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('copy_right');
  103. $show->field('statistics_js');
  104. $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
  105. $show->field('created_at');
  106. $show->field('updated_at');
  107. // 按钮
  108. $show->disableDeleteButton();
  109. });
  110. }
  111. /**
  112. * Make a form builder.
  113. *
  114. * @return Form
  115. */
  116. protected function form()
  117. {
  118. return Form::make(new DistAdminDistributor(), function (Form $form) {
  119. $form->column(6, function (Form $form) {
  120. $form->radio('domain_type')->width(9,3)
  121. ->options([0=>admin_trans_field('secondary_domain'),1=>admin_trans_field('custom_domain')])
  122. ->default(0)
  123. ->required();
  124. $form->text('secondary_domain')->width(9,3)
  125. ->required()
  126. ->help('Please enter the domain name, such as demo.'.env('TOP_DOMAIN'));
  127. $form->text('custom_domain')->width(9,3)->help('Please enter the domain name, such as www.example.com');
  128. $form->text('site_name')->width(9,3)->required();
  129. $form->text('seo_title')->width(9,3)->required();
  130. $form->text('seo_keywords')->width(9,3);
  131. $form->textarea('seo_description')->rows(2)->width(9,3);
  132. $form->select('appearance_id',admin_trans_field('appearance'))->width(9,3)->options(DistAppearance::selectOptions())->required();
  133. $form->cutImage("logo")
  134. ->aspectRatio(130/50)// 设置裁剪框比例
  135. ->retainable()//禁止删OSS图
  136. ->autoUpload()
  137. ->uniqueName()
  138. ->accept(config('distributor.upload.oss_image.accept'))
  139. ->maxSize(config('distributor.upload.oss_image.max_size'))
  140. ->dir(config("distributor.upload.directory.image").'/logo')
  141. ->width(4,3);
  142. });
  143. $form->column(6, function (Form $form) {
  144. $form->text('company_name')->width(9,3);
  145. $form->text('company_address',)->width(9,3);
  146. $form->text('service_hotline')->width(9,3);
  147. $form->text('dist_email',admin_trans_label('email'))->width(9,3);
  148. $form->text('whats_app')->width(9,3);
  149. $form->text('facebook')->width(9,3);
  150. $form->text('instagram')->width(9,3);
  151. $form->text('youtube')->width(9,3);
  152. $form->text('linkedin')->width(9,3);
  153. $form->text('tiktok')->width(9,3);
  154. $form->text('copy_right')->width(9,3);
  155. $form->textarea('statistics_js')->width(9,3);
  156. });
  157. //保存前回调
  158. $form->saving(function (Form $form) {
  159. if (!$form->isCreating()) {
  160. //如果appearance_id有变化,则更新模版与变量
  161. if ($form->model()->appearance_id != $form->input('appearance_id')) {
  162. $id = $form->model()->id;
  163. //更新模版与变量
  164. DistAppearance::switchTheme($form->input('appearance_id'), $id);
  165. }
  166. }
  167. });
  168. //
  169. //保存后回调
  170. $form->saved(function (Form $form, $result) {
  171. $newId = $form->getKey();
  172. if ($form->isCreating()) {
  173. //创建后创建默认分类
  174. $appearanceId = $form->input('appearance_id');
  175. //更新模版与变量
  176. DistAppearance::switchTheme($appearanceId, $newId);
  177. }
  178. //更新缓存
  179. DistAdminDistributor::clearCache($newId);
  180. });
  181. $form->disableDeleteButton();
  182. });
  183. }
  184. }