BaseVideoController.php 6.0 KB

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