123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- namespace App\Admin\Controllers;
- use App\Admin\Repositories\SiteAlbum;
- use App\Admin\Repositories\SiteAlbumFolder;
- use App\Libraries\CommonHelper;
- use App\Models\SiteAlbumFolder as SiteAlbumFolderModel;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Admin;
- use App\Admin\Repositories\NullRepository;
- use Dcat\Admin\Traits\HasUploadedFile;
- use function Symfony\Component\Translation\t;
- class SiteAlbumController extends AdminController
- {
- use HasUploadedFile;
- public function title()
- {
- return admin_trans( 'admin.album');
- }
- /**
- * page index
- */
- public function index(Content $content)
- {
- //记录folder_id
- $folderId = isset($_GET['folder_id']) ? intval($_GET['folder_id']) : 0;
- //保存临时变量
- setTempValue('folderId', $folderId);
- $html = $content
- ->header(admin_trans( 'admin.album'))
- ->body($this->indexForm());
- $html = $html->render();
- //把post请求改为get请求
- $html = preg_replace('/<form(.*?)method="post"(.*?)>/s', '<form$1method="get"$2>', $html, 1);
- //删除第一个formID对应的JS代码
- preg_match('/<form[^>]*id="([^"]*)"[^>]*>/', $html, $matches);
- if (isset($matches[1])) {
- $formId = $matches[1]; // 获取 id 的值
- // echo "找到的 form id: " . $formId . "\n";
- // 2. 根据 id 值,删除对应的 JavaScript 代码
- $pattern = '/\$\(\'#' . preg_quote($formId, '/') . '\'\)\.form\(\{.*?\}\);/s';
- $html = preg_replace($pattern, '', $html);
- }
- return $html;
- }
- protected function indexForm()
- {
- return Form::make(new NullRepository(), function (Form $form) {
- $form->action('/site-album');
- $folderModel = new SiteAlbumFolderModel();
- $folderTree = $folderModel->allNodes();
- $form->block(2, function (Form\BlockForm $form) use ($folderTree) {
- $type = [
- 'default' => [
- 'icon' => true,
- ],
- ];
- $plugins = ['types'];
- $form->tree()
- ->setTitleColumn('title')
- ->nodes($folderTree)
- ->type($type)
- ->plugins($plugins)
- ->width(12,0);
- });
- $form->block(10, function (Form\BlockForm $form) {
- $form->html($this->grid())->width(12);
- });
- //加入JS
- Admin::script(
- <<<JS
- setTimeout(() => {
- // 获取所有具有 class="jstree-anchor" 的 <a> 元素
- const anchors = document.querySelectorAll('a.jstree-anchor');
- anchors.forEach(anchor => {
- // 提取 id 中的数字部分
- const id = anchor.id.split('_')[0];
- // 动态生成跳转链接
- const href = `/prime-control/site-album?folder_id=`+id;
- // 绑定点击事件
- anchor.addEventListener('click', function(event) {
- event.preventDefault(); // 阻止默认的链接跳转行为
- window.location.href = href; // 跳转到目标页面
- });
- });
- }, 200);
- const firstCheckbox = document.querySelector('.vs-checkbox-primary');
- // 如果找到元素,则隐藏它
- if (firstCheckbox) {
- firstCheckbox.style.display = 'none';
- }
- JS
- );
- });
- }
- protected function grid()
- {
- return Grid::make(new SiteAlbum(), function (Grid $grid) {
- //默认分页条数
- $grid->paginate(config('admin.per_page'));
- $grid->column('id')->sortable();
- $grid->column('cover')->display(function ($images) {
- $images = json_decode($images);
- // 限制最多显示2个缩略图
- $dataImages = array_slice($images, 0, 1);
- return CommonHelper::displayImage($dataImages,80);
- });
- $grid->column('model');
- $grid->column('enabled')->using(admin_trans_array(config('dictionary.enabled'))) ->label([
- 0 => 'danger',
- 1 => 'success',
- ]);
- $grid->column('missing_content')->display(function ($missing_content) {
- $missing_content = [];
- if ($this->cover == '[]') {$missing_content[] = '主图';}
- if ($this->en_detail == '[]') {$missing_content[] = '英文详情';}
- if ($this->cn_detail == '[]') {$missing_content[] = '中文详情';}
- if ($this->video == '[]') {$missing_content[] = '视频';}
- if ($this->poster == '[]') {$missing_content[] = '海报';}
- if ($this->cert == '[]') {$missing_content[] = '证书';}
- if ($this->pdf == '[]') {$missing_content[] = 'PDF';}
- return implode(' / ', $missing_content);
- });
- // 筛选
- $grid->filter(function (Grid\Filter $filter) {
- $filter->panel();
- $filter->expand();
- $filter->like('model')->width(2);
- $filter->equal('folder_id',admin_trans_label('folder'))->select(SiteAlbumFolderModel::selectOptions())->width(3);
- });
- $grid->disableViewButton();
- $grid->disablePerPages();
- $grid->disableRefreshButton();
- $grid->model()->orderBy('order', 'asc')->orderBy('id', 'desc');
- //弹窗大小
- $grid->setDialogFormDimensions('830px','670px');
- });
- }
- protected function form()
- {
- $thisObj = $this;
- return Form::make(new SiteAlbum(), function (Form $form) use ($thisObj) {
- $form->width(9, 1);
- $form->disableViewButton();
- $form->disableViewCheck();
- $form->saving(function (Form $form) use ($thisObj) {
- //处理video
- $videos = $form->input('video');
- if ($videos) {
- foreach ($videos as $key => $value) {
- if (empty($value['cover']) && $value['_remove_'] != 1) {
- //自动生成封面
- $result = $thisObj->autoGenerateCover($value['video_src']);
- if ($result['status']) {
- $videos[$key]['cover'] = $result['path'];
- } else {
- return $form->response()->error($result['msg']);
- }
- }
- }
- } else {
- $videos = [];
- }
- $form->input('video', $videos);
- //处理pdf
- $pdfs = $form->input('pdf');
- $pdfs = empty($pdfs) ? [] : $pdfs;
- $form->input('pdf', $pdfs);
- });
- $form->tab(admin_trans_label('basic_info'), function (Form $form) {
- $folderId = getTempValue('folderId');
- $form->select('folder_id')->options(SiteAlbumFolder::selectOptions())->default($folderId)->required();
- $form->text('model')->required();
- $form->switch('enabled')->default(1);
- })->tab(admin_trans_label('cover'), function (Form $form) {
- $form->multipleImage('cover')
- ->retainable()//禁止删OSS图
- ->sortable() // 可拖动排序
- ->removable() // 可移除图片
- ->autoUpload() // 自动上传
- ->uniqueName()
- ->limit(config('admin.upload.oss_image.limit'))
- ->accept(config('admin.upload.oss_image.accept'))
- ->maxSize(config('admin.upload.oss_image.max_size'))
- ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
- ->saving(function ($images) use ($form) {
- return json_encode($images);
- });
- })->tab(admin_trans_label('en_detail'), function (Form $form) {
- $form->multipleImage('en_detail')
- ->retainable()//禁止删OSS图
- ->sortable() // 可拖动排序
- ->removable() // 可移除图片
- ->autoUpload() // 自动上传
- ->uniqueName()
- ->limit(config('admin.upload.oss_image.limit'))
- ->accept(config('admin.upload.oss_image.accept'))
- ->maxSize(config('admin.upload.oss_image.max_size'))
- ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
- ->saving(function ($images) use ($form) {
- return json_encode($images);
- });
- })->tab(admin_trans_label('cn_detail'), function (Form $form) {
- $form->multipleImage('cn_detail')
- ->retainable()//禁止删OSS图
- ->sortable() // 可拖动排序
- ->removable() // 可移除图片
- ->autoUpload() // 自动上传
- ->uniqueName()
- ->limit(config('admin.upload.oss_image.limit'))
- ->accept(config('admin.upload.oss_image.accept'))
- ->maxSize(config('admin.upload.oss_image.max_size'))
- ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
- ->saving(function ($images) use ($form) {
- return json_encode($images);
- });
- })->tab(admin_trans_label('video'), function (Form $form) {
- $count = 0;
- $form->hasMany('video', function (Form\NestedForm $form) use (&$count) {
- $videos = $form->model()->video;
- $imgArray = "";
- if ($videos) {
- $videos = json_decode($videos,true);
- foreach ($videos as $key => $value) {
- if ($value['cover'] && $key == $count-1) {
- $imgArray = [$value['cover']];
- }
- }
- }
- $imgHtml = CommonHelper::displayImage($imgArray);
- $form->html($imgHtml,admin_trans_label('image_preview'));
- $count++;
- $form->text('cover',admin_trans_label('video_cover'))->placeholder('为空则自动生成');
- $form->tradFile('video_src')
- ->retainable()//禁止删OSS图
- ->removable() // 可移除图片
- ->autoUpload() // 自动上传
- ->uniqueName()
- ->accept(config('admin.upload.oss_video.accept'))
- ->maxSize(config('admin.upload.oss_video.max_size'))
- ->dir(config("admin.upload.directory.video").'/uploads/'.date("Ymd"))
- ->chunkSize(1024)
- ->required();
- })->useTable()
- ->customFormat(function ($data) {return json_decode($data,true);})
- ->setView('admin.form_custom.hasmanytable')
- ->saving(function ($input) {
- $data = [];
- foreach ($input as $value) {
- if ($value['_remove_'] != 1){
- $data[] = ['cover'=>$value['cover'],'video_src'=>$value['video_src']];
- }
- }
- return json_encode($data);
- });
- })->tab(admin_trans_label('poster'), function (Form $form) {
- $form->multipleImage('poster')
- ->retainable()//禁止删OSS图
- ->sortable() // 可拖动排序
- ->removable() // 可移除图片
- ->autoUpload() // 自动上传
- ->uniqueName()
- ->limit(config('admin.upload.oss_image.limit'))
- ->accept(config('admin.upload.oss_image.accept'))
- ->maxSize(config('admin.upload.oss_image.max_size'))
- ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
- ->saving(function ($images) use ($form) {
- return json_encode($images);
- });
- })->tab(admin_trans_label('cert'), function (Form $form) {
- $form->multipleImage('cert')
- ->retainable()//禁止删OSS图
- ->sortable() // 可拖动排序
- ->removable() // 可移除图片
- ->autoUpload() // 自动上传
- ->uniqueName()
- ->limit(config('admin.upload.oss_image.limit'))
- ->accept(config('admin.upload.oss_image.accept'))
- ->maxSize(config('admin.upload.oss_image.max_size'))
- ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
- ->saving(function ($images) use ($form) {
- return json_encode($images);
- });
- })->tab(admin_trans_label('pdf'), function (Form $form) {
- $form->hasMany('pdf', function ($form) {
- $form->text('pdf_title')->required();
- $form->tradFile('pdf_src')
- ->retainable()//禁止删OSS图
- ->removable() // 可移除图片
- ->autoUpload() // 自动上传
- ->uniqueName()
- ->accept(config('admin.upload.oss_pdf.accept'))
- ->maxSize(config('admin.upload.oss_pdf.max_size'))
- ->dir(config("admin.upload.directory.pdf").'/uploads/'.date("Ymd"))
- ->chunkSize(1024)
- ->required();
- })->useTable()
- ->customFormat(function ($data) {
- return json_decode($data,true);
- })
- ->setView('admin.form_custom.hasmanytable')
- ->saving(function ($input) {
- $data = [];
- foreach ($input as $value) {
- if ($value['_remove_'] != 1){
- $data[] = ['pdf_title'=>$value['pdf_title'],'pdf_src'=>$value['pdf_src']];
- }
- }
- return json_encode($data);
- });
- });
- $folderId = getTempValue('folderId');
- if ($folderId >0) {
- Admin::script(
- <<<JS
- const featherIcon = document.querySelector('i.feather.icon-list');
- if (featherIcon) {
- // 找到 <i> 的上级 <a> 元素
- const parentLink = featherIcon.closest('a');
- if (parentLink && parentLink.href) {
- // 在 href 后追加 ?folder_id=3
- parentLink.href = parentLink.href + '?folder_id={$folderId}';
- }
- }
- JS
- );
- }
- });
- }
- /*
- * 自动生成视频封面
- */
- private function autoGenerateCover($videoSrc)
- {
- $cover = $videoSrc.'?x-oss-process=video/snapshot,t_2000,f_jpg,h_500,m_fast';
- //TODO 上传到OSS
- $cover = CommonHelper::ossUrl($cover);
- $path = $this->upload($cover,'.jpg');
- return $path;
- }
- private function upload($file,$imgType='.jpg')
- {
- $disk = $this->disk('oss');
- $newName = uniqueCode("video_cover_").$imgType;
- $dir = config("admin.upload.directory.image").'/uploads/'.date("Ymd").'/'.$newName;
- $contents = file_get_contents($file);
- if (!$contents) {
- return ['status'=>false,'msg'=>'图片上传失败,请检查PHP配置'];
- }
- $disk->put($dir, $contents);
- return ['status'=>true,'path'=>$dir];
- }
- }
|