BaseVideoController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\BaseProductCategory;
  4. use App\Admin\Repositories\BaseVideo;
  5. use App\Admin\Repositories\BaseVideoCategory;
  6. use App\Libraries\CommonHelper;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Dcat\Admin\Layout\Content;
  12. class BaseVideoController extends AdminController
  13. {
  14. public function title()
  15. {
  16. return admin_trans( 'admin.video_list');
  17. }
  18. /**
  19. * page index
  20. */
  21. public function index(Content $content)
  22. {
  23. return $content
  24. ->header($this->title())
  25. ->description('')
  26. ->breadcrumb(['text'=>'list','url'=>''])
  27. ->body($this->grid());
  28. }
  29. /**
  30. * Make a grid builder.
  31. *
  32. * @return Grid
  33. */
  34. protected function grid()
  35. {
  36. return Grid::make(BaseVideo::with(['baseVideoCategory']), function (Grid $grid) {
  37. //默认分页条数
  38. $grid->paginate(config('admin.per_page'));
  39. $grid->column('id')->display(function () {
  40. return $this->_index+1;
  41. });
  42. $grid->column('title')->width('25%');
  43. $grid->column('base_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,100);
  48. });
  49. $grid->column('order');
  50. $grid->column('enabled')->switch();
  51. $grid->column('created_at')->sortable();
  52. $grid->column('updated_at')->sortable();
  53. // 筛选
  54. $grid->filter(function (Grid\Filter $filter) {
  55. $filter->panel();
  56. $filter->expand();
  57. $filter->equal('sku')->width(2);
  58. $filter->like('title')->width(2);
  59. $filter->equal('category_id',admin_trans_label('category'))->select(BaseVideoCategory::selectOptions())->width(2);
  60. $filter->equal('enabled', admin_trans_label('enabled'))->select(admin_trans_array(config('dictionary.enabled')))->width(2);
  61. });
  62. //排序
  63. $grid->model()->orderBy("order",'desc')->orderBy('id','desc');
  64. //按钮
  65. });
  66. }
  67. /**
  68. * Make a show builder.
  69. *
  70. * @param mixed $id
  71. *
  72. * @return Show
  73. */
  74. protected function detail($id)
  75. {
  76. return Show::make($id, BaseVideo::with(['baseVideoCategory']), function (Show $show) {
  77. $show->field('title');
  78. $show->field('base_video_category.name',admin_trans_field('category_name'));
  79. $show->field('cover_image')->as(function ($image) {
  80. // 开始生成 HTML
  81. $dataImages = [$image];
  82. return CommonHelper::displayImage($dataImages,150);
  83. })->unescape();
  84. $show->html(function () {
  85. $content = $this->video_url;
  86. return view('admin::show.field', [
  87. 'wrapped'=>true,
  88. 'escape'=>false,
  89. 'width'=>['label' => '2','field'=>'8'],
  90. 'label'=>admin_trans_label('video_url'),
  91. 'content'=>$content
  92. ]);
  93. });
  94. $show->field('video_url',admin_trans_label('video_player'))->as(function ($value) {
  95. $html = '
  96. <iframe width="560" height="315" src="'.$value.'"
  97. title="YouTube video player"
  98. frameborder="0"
  99. allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  100. allowfullscreen>
  101. </iframe>';
  102. return $html;
  103. })->unescape();
  104. $show->field('remark')->unescape();
  105. $show->field('order');
  106. $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
  107. $show->field('created_at');
  108. $show->field('updated_at');
  109. });
  110. }
  111. /**
  112. * Make a form builder.
  113. *
  114. * @return Form
  115. */
  116. protected function form()
  117. {
  118. return Form::make(new BaseVideo(), function (Form $form) {
  119. //$form->display('id');
  120. $help = admin_trans_label('video_help');
  121. $inputFormat = admin_trans_label('input_format');
  122. $form->text('title')->required();
  123. $form->select('category_id', admin_trans_field('category_name'))
  124. ->options(BaseVideoCategory::selectOptions())
  125. ->required();
  126. $form->image("cover_image")
  127. ->retainable()//禁止删OSS图
  128. ->autoUpload()
  129. ->uniqueName()
  130. ->accept(config('admin.upload.oss_image.accept'))
  131. ->maxSize(config('admin.upload.oss_image.max_size'))
  132. ->dir(config("admin.upload.directory.image").'/video/'.date("Ymd"));//
  133. $form->text("video_url")
  134. ->required()
  135. ->help('<a href="/help/youtube_url/index.html" target="_blank">'.$help. ' </a>, '.$inputFormat.' : https://www.youtube.com/embed/xxxxxxxx');
  136. $form->editor('remark');
  137. $form->number('order')
  138. ->default(0)
  139. ->rules('numeric');
  140. $form->switch('enabled')->default(1);
  141. $form->saving(function (Form $form) {
  142. //替换youtube URL
  143. if (strpos($form->video_url, '<iframe') !== false) {
  144. // 使用正则表达式提取 src 属性的值
  145. if (preg_match('/<iframe[^>]+src="([^"]+)"/', $form->video_url, $matches)) {
  146. $src = $matches[1];
  147. $form->video_url = $src;
  148. }
  149. }
  150. });
  151. });
  152. }
  153. }