AdminMakeVideoController.php 5.66 KB
<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\PoemTable;
use App\Admin\Renderable\PoemTable2;
use App\Admin\Renderable\TemplateTable;
use App\Jobs\AdminMakeImmerse;
use App\Admin\Repositories\AdminMakeVideo;
use App\Models\AdminMakeVideo as AdminVideo;
use App\Models\OnePoem;
use App\Models\OnePoem2;
use App\Models\VideoTemp;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Storage;

class AdminMakeVideoController extends AdminController
{
    protected $title = '制作历史记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdminMakeVideo(), function (Grid $grid) {
            // 设置自定义视图
            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->column('id')->sortable();
            $grid->column('type','类型')->using([1 => '视频', 2 => '音频']);

            $grid->column('video_url')->display(function ($url){
                if ($url == '' || $url == null) return '';
                $path = Storage::disk('public')->url($url);
                return "<a target='_blank' href='". $path ."'>查看</a>";
            });

            $grid->column('images_url')->display(function ($url){
                if ($url == '' || $url == null) return '';
                $path = Storage::disk('public')->url($url);
                return "<a target='_blank' href='". $path ."'>查看</a>";
            });

            $grid->column('feel','有感');
            $grid->column('weather','天气');
            $grid->column('huangli','黄历');
            $grid->column('location','此地');
            $grid->column('poem_id');
            $grid->column('temp_id');
            $grid->column('thumbnail');
            $grid->column('thumbnail_url')->image();

            $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 AdminMakeVideo(), function (Show $show) {
            $show->field('id');
            $show->field('poem_id');
            $show->field('type');
            $show->field('video_url');
            $show->field('images_url');
            $show->field('bg_music');
            $show->field('bgm_url');
            $show->field('feel');
            $show->field('temp_id');
            $show->field('thumbnail');
            $show->field('thumbnail_url');
            $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('temp_id','选择模板')
                ->title('模板选择')
                ->from(TemplateTable::make())
                ->model(VideoTemp::class,'id','title');

            $form->radio('type')
                ->options([1=>'视频', 2=>'图文音频'])
                ->when(1,function (Form $form){
                    $form->file('video_url','上传视频')
//                        ->accept('mp4,mov')
//                        ->chunked()
                        ->autoUpload()
                        ->uniqueName()
                        ->maxSize('128000')
                        ->addElementClass('video_url');
                })
                ->when(2,function (Form $form){
                    $form->image('images_url','上传图片')
                        ->uniqueName()
                        ->addElementClass('images_url');
                })
                ->default(1);

            $form->selectTable('poem_id','选择一言')
                ->title('一言诗词库')
                ->from(PoemTable2::make())
                ->model(OnePoem2::class,'id','title');
            $form->textarea('feel','有感');
            $form->text('weather','天气');
            $form->text('huangli','黄历')->default('宜');

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

            $form->text('location','此地')->default("/")->placeholder('暂时先手动输入此地');

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

            $form->saved(function (Form $form, $result) {
                $model = $form->repository()->model();
                AdminMakeImmerse::dispatch($model);
            });
        });
    }

    public function destroy($id)
    {
        $admin = AdminVideo::query()->find($id);

        if(Storage::disk("public")->exists($admin->video_url))
            Storage::disk('public')->delete($admin->video_url);

        if(Storage::disk("public")->exists($admin->images_url))
            Storage::disk('public')->delete($admin->images_url);

        if(Storage::disk("public")->exists($admin->thumbnail_url))
            Storage::disk('public')->delete($admin->thumbnail_url);

        $admin->delete();
//
        return $this->form()->response()->refresh()->success('删除成功');
    }
}