DistVideoController.php 5.7 KB

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