VideoShowController.php 4.21 KB
<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\PoemTable;
use App\Admin\Renderable\TemplateTable;
use App\Admin\Repositories\VideoShow;
use App\Models\AdminMakeVideo;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class VideoShowController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new VideoShow(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('video_url');
            $grid->column('video_size');
            $grid->column('video_time');
            $grid->column('is_horizontal');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new VideoShow(), function (Show $show) {
            $show->field('id');
            $show->field('video_url');
            $show->field('video_size');
            $show->field('video_time');
            $show->field('is_horizontal');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdminMakeVideo(), function (Form $form) {
            $form->display('id');

            $form->selectTable('poem_id','选择一言')
                ->title('一言诗词库')
                ->from(PoemTable::make());

            $form->radio('type')
                ->options([1=>'视频', 2=>'图文音频'])
                ->when(1,function (Form $form){
                    $form->file('video_url','上传视频')
                        ->accept('mp4')
                        ->autoUpload()
                        ->uniqueName()
                        ->maxSize('102400')
                        ->addElementClass('video_url');
                })
                ->when(2,function (Form $form){
                    $form->multipleImage('images_url','上传图片')
                        ->limit(5)
                        ->uniqueName()
                        ->addElementClass('images_url');
                })
                ->default(1);
            $form->radio('bg_music','背景音')
                ->options(['无', '有'])
                ->when(1,function (Form $form){
                    $form->file('bgm_url','上传背景音')
                        ->accept('mp3,aac,wav')
                        ->autoUpload()
                        ->uniqueName()
                        ->addElementClass('bg_music');
                })
                ->default(0);

            $form->textarea('feel','有感');

            $form->selectTable('temp_id','选择模板')
                ->title('模板选择')
                ->from(TemplateTable::make());

            $form->radio('thumbnail','封面')
                ->options([1=>'手动上传', 2=>'自动截屏'])
                ->when(1,function (Form $form){
                    $form->multipleImage('thumbnail_url','上传图片')
                        ->limit(5)
                        ->uniqueName();
//                        ->addElementClass('bg_img_url');
                })
                ->when(2,function (Form $form){
                    $form->html('');
                })
                ->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function store()
    {
        $all = request()->all();

        if (isset($all['upload_column']))  return $this->form()->store();

        try{
            $video = AdminMakeVideo::query()->create($all);

            // todo 添加至队列

        }catch (\Exception $exception){
            return $this->form()->response()->error($exception->getMessage());
        }

        return $this->form()->response()->refresh()->success(trans('admin.save_succeeded'));
    }
}