DistVideoController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Distributor\Repositories\DistVideo;
  4. use App\Distributor\Repositories\DistVideoCategory;
  5. use App\Libraries\CommonHelper;
  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. class DistVideoController extends AdminController
  12. {
  13. /**
  14. * page index
  15. */
  16. public function index(Content $content)
  17. {
  18. return $content
  19. ->header(admin_trans('admin.video_list'))
  20. ->description(admin_trans('admin.all'))
  21. ->breadcrumb(['text'=>admin_trans('admin.video_list'),'url'=>''])
  22. ->body($this->grid());
  23. }
  24. public function create(Content $content)
  25. {
  26. return $content
  27. ->translation($this->translation())
  28. ->title(admin_trans('admin.video'))
  29. ->description($this->description()['create'] ?? trans('admin.create'))
  30. ->body($this->form());
  31. }
  32. /**
  33. * Make a grid builder.
  34. *
  35. * @return Grid
  36. */
  37. protected function grid()
  38. {
  39. return Grid::make(DistVideo::with(['distVideoCategory']), function (Grid $grid) {
  40. $grid->model()->where('dist_id', getDistributorId());
  41. $grid->column('id')->sortable();
  42. $grid->column('title');
  43. $grid->column('dist_video_category.name',admin_trans_label('category_name'));
  44. $grid->column('cover_image')->display(function ($image) {
  45. // 开始生成 HTML
  46. $dataImages = [$image];
  47. return CommonHelper::displayImage($dataImages,150);
  48. });
  49. $grid->column('order')->orderable();
  50. $grid->column('is_pinned')->switch();
  51. $grid->column('enabled')->switch();
  52. $grid->column('created_at');
  53. $grid->column('updated_at')->sortable();
  54. // 筛选
  55. $grid->filter(function (Grid\Filter $filter) {
  56. $filter->panel();
  57. $filter->expand();
  58. $filter->equal('sku')->width(2);
  59. $filter->like('title')->width(2);
  60. $filter->equal('category_id',admin_trans_label('category'))->select(DistVideoCategory::selectOptions())->width(2);
  61. $filter->equal('enabled', admin_trans_label('enabled'))->select(array_map('admin_trans_label', config('dictionary.enabled')))->width(2);
  62. });
  63. //排序
  64. $grid->model()->orderBy("is_pinned",'desc')->orderBy("order",'desc');
  65. //按钮
  66. });
  67. }
  68. /**
  69. * Make a show builder.
  70. *
  71. * @param mixed $id
  72. *
  73. * @return Show
  74. */
  75. protected function detail($id)
  76. {
  77. return Show::make($id, DistVideo::with(['distVideoCategory']), function (Show $show) {
  78. // 比较 dist_id 和 getDistributorId(),如果不相同则返回 404
  79. if ($show->model()->dist_id !== getDistributorId()) {
  80. abort(404);
  81. }
  82. $show->field('id');
  83. $show->field('title');
  84. $show->field('dist_video_category.name',admin_trans_label('category_name'));
  85. $show->field('cover_image')->as(function ($image) {
  86. // 开始生成 HTML
  87. $dataImages = [$image];
  88. return CommonHelper::displayImage($dataImages,150);
  89. })->unescape();
  90. $show->html(function () {
  91. $content = $this->video_url;
  92. return view('admin::show.field', [
  93. 'wrapped'=>true,
  94. 'escape'=>false,
  95. 'width'=>['label' => '2','field'=>'8'],
  96. 'label'=>'Video Url',
  97. 'content'=>$content
  98. ]);
  99. });
  100. $show->field('video_url','Video player')->as(function ($value) {
  101. $html = '
  102. <iframe width="560" height="315" src="'.$value.'"
  103. title="YouTube video player"
  104. frameborder="0"
  105. allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  106. allowfullscreen>
  107. </iframe>';
  108. return $html;
  109. })->unescape();
  110. $show->field('remark')->unescape();
  111. $show->field('enabled')->using(config('dictionary.enabled'));
  112. $show->field('created_at');
  113. $show->field('updated_at');
  114. });
  115. }
  116. /**
  117. * Make a form builder.
  118. *
  119. * @return Form
  120. */
  121. protected function form()
  122. {
  123. return Form::make(new DistVideo(), function (Form $form) {
  124. $form->display('id');
  125. $form->text('title')->required();
  126. $form->select('category_id', admin_trans_label('category_name'))
  127. ->options(DistVideoCategory::selectOptions())
  128. ->required();
  129. $form->image("cover_image", admin_trans_label('cover_image'))
  130. ->autoUpload()
  131. ->uniqueName()
  132. ->accept(config('admin.upload.oss_image.accept'))
  133. ->maxSize(config('admin.upload.oss_image.max_size'))
  134. ->dir('images/video/'.date("Ymd"));//
  135. $form->url("video_url", admin_trans_label('video_url'))->required();
  136. $form->editor('remark');
  137. $form->switch('is_pinned')->default(0);
  138. $form->switch('enabled')->default(1);
  139. $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存
  140. $form->saving(function (Form $form) {
  141. $form->dist_id =getDistributorId();//保存时直接写死dist_id
  142. if (!$form->isCreating()) {
  143. // 验证主键 ID 的 dist_id 是否与 session 的 dist_id 一致
  144. $currentDistProduct = DistVideo::findById($form->getKey());
  145. if ($currentDistProduct && $currentDistProduct->dist_id !== $form->dist_id) {
  146. throw new \Exception('Unable to modify the product because the distributor ID does not match.');
  147. }
  148. }
  149. });
  150. });
  151. }
  152. }