SiteAlbumController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\SiteAlbum;
  4. use App\Admin\Repositories\SiteAlbumFolder;
  5. use App\Libraries\CommonHelper;
  6. use App\Models\SiteAlbumFolder as SiteAlbumFolderModel;
  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. use Dcat\Admin\Admin;
  13. use App\Admin\Repositories\NullRepository;
  14. use Dcat\Admin\Traits\HasUploadedFile;
  15. use function Symfony\Component\Translation\t;
  16. class SiteAlbumController extends AdminController
  17. {
  18. use HasUploadedFile;
  19. public function title()
  20. {
  21. return admin_trans( 'admin.album');
  22. }
  23. /**
  24. * page index
  25. */
  26. public function index(Content $content)
  27. {
  28. //记录folder_id
  29. $folderId = isset($_GET['folder_id']) ? intval($_GET['folder_id']) : 0;
  30. //保存临时变量
  31. setTempValue('folderId', $folderId);
  32. $html = $content
  33. ->header(admin_trans( 'admin.album'))
  34. ->body($this->indexForm());
  35. $html = $html->render();
  36. //把post请求改为get请求
  37. $html = preg_replace('/<form(.*?)method="post"(.*?)>/s', '<form$1method="get"$2>', $html, 1);
  38. //删除第一个formID对应的JS代码
  39. preg_match('/<form[^>]*id="([^"]*)"[^>]*>/', $html, $matches);
  40. if (isset($matches[1])) {
  41. $formId = $matches[1]; // 获取 id 的值
  42. // echo "找到的 form id: " . $formId . "\n";
  43. // 2. 根据 id 值,删除对应的 JavaScript 代码
  44. $pattern = '/\$\(\'#' . preg_quote($formId, '/') . '\'\)\.form\(\{.*?\}\);/s';
  45. $html = preg_replace($pattern, '', $html);
  46. }
  47. return $html;
  48. }
  49. protected function indexForm()
  50. {
  51. return Form::make(new NullRepository(), function (Form $form) {
  52. $form->action('/site-album');
  53. $folderModel = new SiteAlbumFolderModel();
  54. $folderTree = $folderModel->allNodes();
  55. $form->block(2, function (Form\BlockForm $form) use ($folderTree) {
  56. $type = [
  57. 'default' => [
  58. 'icon' => true,
  59. ],
  60. ];
  61. $plugins = ['types'];
  62. $form->tree()
  63. ->setTitleColumn('title')
  64. ->nodes($folderTree)
  65. ->type($type)
  66. ->plugins($plugins)
  67. ->width(12,0);
  68. });
  69. $form->block(10, function (Form\BlockForm $form) {
  70. $form->html($this->grid())->width(12);
  71. });
  72. //加入JS
  73. Admin::script(
  74. <<<JS
  75. setTimeout(() => {
  76. // 获取所有具有 class="jstree-anchor" 的 <a> 元素
  77. const anchors = document.querySelectorAll('a.jstree-anchor');
  78. anchors.forEach(anchor => {
  79. // 提取 id 中的数字部分
  80. const id = anchor.id.split('_')[0];
  81. // 动态生成跳转链接
  82. const href = `/prime-control/site-album?folder_id=`+id;
  83. // 绑定点击事件
  84. anchor.addEventListener('click', function(event) {
  85. event.preventDefault(); // 阻止默认的链接跳转行为
  86. window.location.href = href; // 跳转到目标页面
  87. });
  88. });
  89. }, 200);
  90. const firstCheckbox = document.querySelector('.vs-checkbox-primary');
  91. // 如果找到元素,则隐藏它
  92. if (firstCheckbox) {
  93. firstCheckbox.style.display = 'none';
  94. }
  95. JS
  96. );
  97. });
  98. }
  99. protected function grid()
  100. {
  101. return Grid::make(new SiteAlbum(), function (Grid $grid) {
  102. //默认分页条数
  103. $grid->paginate(config('admin.per_page'));
  104. $grid->column('id')->sortable();
  105. $grid->column('cover')->display(function ($images) {
  106. $images = json_decode($images);
  107. // 限制最多显示2个缩略图
  108. $dataImages = array_slice($images, 0, 1);
  109. return CommonHelper::displayImage($dataImages,80);
  110. });
  111. $grid->column('model');
  112. $grid->column('enabled')->using(admin_trans_array(config('dictionary.enabled'))) ->label([
  113. 0 => 'danger',
  114. 1 => 'success',
  115. ]);
  116. $grid->column('missing_content')->display(function ($missing_content) {
  117. $missing_content = [];
  118. if ($this->cover == '[]') {$missing_content[] = '主图';}
  119. if ($this->en_detail == '[]') {$missing_content[] = '英文详情';}
  120. if ($this->cn_detail == '[]') {$missing_content[] = '中文详情';}
  121. if ($this->video == '[]') {$missing_content[] = '视频';}
  122. if ($this->poster == '[]') {$missing_content[] = '海报';}
  123. if ($this->cert == '[]') {$missing_content[] = '证书';}
  124. if ($this->pdf == '[]') {$missing_content[] = 'PDF';}
  125. return implode(' / ', $missing_content);
  126. });
  127. // 筛选
  128. $grid->filter(function (Grid\Filter $filter) {
  129. $filter->panel();
  130. $filter->expand();
  131. $filter->like('model')->width(2);
  132. $filter->equal('folder_id',admin_trans_label('folder'))->select(SiteAlbumFolderModel::selectOptions())->width(3);
  133. });
  134. $grid->disableViewButton();
  135. $grid->disablePerPages();
  136. $grid->disableRefreshButton();
  137. $grid->model()->orderBy('order', 'asc')->orderBy('id', 'desc');
  138. //弹窗大小
  139. $grid->setDialogFormDimensions('830px','670px');
  140. });
  141. }
  142. protected function form()
  143. {
  144. $thisObj = $this;
  145. return Form::make(new SiteAlbum(), function (Form $form) use ($thisObj) {
  146. $form->width(9, 1);
  147. $form->disableViewButton();
  148. $form->disableViewCheck();
  149. $form->saving(function (Form $form) use ($thisObj) {
  150. //处理video
  151. $videos = $form->input('video');
  152. if ($videos) {
  153. foreach ($videos as $key => $value) {
  154. if (empty($value['cover']) && $value['_remove_'] != 1) {
  155. //自动生成封面
  156. $result = $thisObj->autoGenerateCover($value['video_src']);
  157. if ($result['status']) {
  158. $videos[$key]['cover'] = $result['path'];
  159. } else {
  160. return $form->response()->error($result['msg']);
  161. }
  162. }
  163. }
  164. } else {
  165. $videos = [];
  166. }
  167. $form->input('video', $videos);
  168. //处理pdf
  169. $pdfs = $form->input('pdf');
  170. $pdfs = empty($pdfs) ? [] : $pdfs;
  171. $form->input('pdf', $pdfs);
  172. });
  173. $form->tab(admin_trans_label('basic_info'), function (Form $form) {
  174. $folderId = getTempValue('folderId');
  175. $form->select('folder_id')->options(SiteAlbumFolder::selectOptions())->default($folderId)->required();
  176. $form->text('model')->required();
  177. $form->switch('enabled')->default(1);
  178. })->tab(admin_trans_label('cover'), function (Form $form) {
  179. $form->multipleImage('cover')
  180. ->retainable()//禁止删OSS图
  181. ->sortable() // 可拖动排序
  182. ->removable() // 可移除图片
  183. ->autoUpload() // 自动上传
  184. ->uniqueName()
  185. ->limit(config('admin.upload.oss_image.limit'))
  186. ->accept(config('admin.upload.oss_image.accept'))
  187. ->maxSize(config('admin.upload.oss_image.max_size'))
  188. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  189. ->saving(function ($images) use ($form) {
  190. return json_encode($images);
  191. });
  192. })->tab(admin_trans_label('en_detail'), function (Form $form) {
  193. $form->multipleImage('en_detail')
  194. ->retainable()//禁止删OSS图
  195. ->sortable() // 可拖动排序
  196. ->removable() // 可移除图片
  197. ->autoUpload() // 自动上传
  198. ->uniqueName()
  199. ->limit(config('admin.upload.oss_image.limit'))
  200. ->accept(config('admin.upload.oss_image.accept'))
  201. ->maxSize(config('admin.upload.oss_image.max_size'))
  202. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  203. ->saving(function ($images) use ($form) {
  204. return json_encode($images);
  205. });
  206. })->tab(admin_trans_label('cn_detail'), function (Form $form) {
  207. $form->multipleImage('cn_detail')
  208. ->retainable()//禁止删OSS图
  209. ->sortable() // 可拖动排序
  210. ->removable() // 可移除图片
  211. ->autoUpload() // 自动上传
  212. ->uniqueName()
  213. ->limit(config('admin.upload.oss_image.limit'))
  214. ->accept(config('admin.upload.oss_image.accept'))
  215. ->maxSize(config('admin.upload.oss_image.max_size'))
  216. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  217. ->saving(function ($images) use ($form) {
  218. return json_encode($images);
  219. });
  220. })->tab(admin_trans_label('video'), function (Form $form) {
  221. $count = 0;
  222. $form->hasMany('video', function (Form\NestedForm $form) use (&$count) {
  223. $videos = $form->model()->video;
  224. $imgArray = "";
  225. if ($videos) {
  226. $videos = json_decode($videos,true);
  227. foreach ($videos as $key => $value) {
  228. if ($value['cover'] && $key == $count-1) {
  229. $imgArray = [$value['cover']];
  230. }
  231. }
  232. }
  233. $imgHtml = CommonHelper::displayImage($imgArray);
  234. $form->html($imgHtml,admin_trans_label('image_preview'));
  235. $count++;
  236. $form->text('cover',admin_trans_label('video_cover'))->placeholder('为空则自动生成');
  237. $form->tradFile('video_src')
  238. ->retainable()//禁止删OSS图
  239. ->removable() // 可移除图片
  240. ->autoUpload() // 自动上传
  241. ->uniqueName()
  242. ->accept(config('admin.upload.oss_video.accept'))
  243. ->maxSize(config('admin.upload.oss_video.max_size'))
  244. ->dir(config("admin.upload.directory.video").'/uploads/'.date("Ymd"))
  245. ->chunkSize(1024)
  246. ->required();
  247. })->useTable()
  248. ->customFormat(function ($data) {return json_decode($data,true);})
  249. ->setView('admin.form_custom.hasmanytable')
  250. ->saving(function ($input) {
  251. $data = [];
  252. foreach ($input as $value) {
  253. if ($value['_remove_'] != 1){
  254. $data[] = ['cover'=>$value['cover'],'video_src'=>$value['video_src']];
  255. }
  256. }
  257. return json_encode($data);
  258. });
  259. })->tab(admin_trans_label('poster'), function (Form $form) {
  260. $form->multipleImage('poster')
  261. ->retainable()//禁止删OSS图
  262. ->sortable() // 可拖动排序
  263. ->removable() // 可移除图片
  264. ->autoUpload() // 自动上传
  265. ->uniqueName()
  266. ->limit(config('admin.upload.oss_image.limit'))
  267. ->accept(config('admin.upload.oss_image.accept'))
  268. ->maxSize(config('admin.upload.oss_image.max_size'))
  269. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  270. ->saving(function ($images) use ($form) {
  271. return json_encode($images);
  272. });
  273. })->tab(admin_trans_label('cert'), function (Form $form) {
  274. $form->multipleImage('cert')
  275. ->retainable()//禁止删OSS图
  276. ->sortable() // 可拖动排序
  277. ->removable() // 可移除图片
  278. ->autoUpload() // 自动上传
  279. ->uniqueName()
  280. ->limit(config('admin.upload.oss_image.limit'))
  281. ->accept(config('admin.upload.oss_image.accept'))
  282. ->maxSize(config('admin.upload.oss_image.max_size'))
  283. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  284. ->saving(function ($images) use ($form) {
  285. return json_encode($images);
  286. });
  287. })->tab(admin_trans_label('pdf'), function (Form $form) {
  288. $form->hasMany('pdf', function ($form) {
  289. $form->text('pdf_title')->required();
  290. $form->tradFile('pdf_src')
  291. ->retainable()//禁止删OSS图
  292. ->removable() // 可移除图片
  293. ->autoUpload() // 自动上传
  294. ->uniqueName()
  295. ->accept(config('admin.upload.oss_pdf.accept'))
  296. ->maxSize(config('admin.upload.oss_pdf.max_size'))
  297. ->dir(config("admin.upload.directory.pdf").'/uploads/'.date("Ymd"))
  298. ->chunkSize(1024)
  299. ->required();
  300. })->useTable()
  301. ->customFormat(function ($data) {
  302. return json_decode($data,true);
  303. })
  304. ->setView('admin.form_custom.hasmanytable')
  305. ->saving(function ($input) {
  306. $data = [];
  307. foreach ($input as $value) {
  308. if ($value['_remove_'] != 1){
  309. $data[] = ['pdf_title'=>$value['pdf_title'],'pdf_src'=>$value['pdf_src']];
  310. }
  311. }
  312. return json_encode($data);
  313. });
  314. });
  315. $folderId = getTempValue('folderId');
  316. if ($folderId >0) {
  317. Admin::script(
  318. <<<JS
  319. const featherIcon = document.querySelector('i.feather.icon-list');
  320. if (featherIcon) {
  321. // 找到 <i> 的上级 <a> 元素
  322. const parentLink = featherIcon.closest('a');
  323. if (parentLink && parentLink.href) {
  324. // 在 href 后追加 ?folder_id=3
  325. parentLink.href = parentLink.href + '?folder_id={$folderId}';
  326. }
  327. }
  328. JS
  329. );
  330. }
  331. });
  332. }
  333. /*
  334. * 自动生成视频封面
  335. */
  336. private function autoGenerateCover($videoSrc)
  337. {
  338. $cover = $videoSrc.'?x-oss-process=video/snapshot,t_2000,f_jpg,h_500,m_fast';
  339. //TODO 上传到OSS
  340. $cover = CommonHelper::ossUrl($cover);
  341. $path = $this->upload($cover,'.jpg');
  342. return $path;
  343. }
  344. private function upload($file,$imgType='.jpg')
  345. {
  346. $disk = $this->disk('oss');
  347. $newName = uniqueCode("video_cover_").$imgType;
  348. $dir = config("admin.upload.directory.image").'/uploads/'.date("Ymd").'/'.$newName;
  349. $contents = file_get_contents($file);
  350. if (!$contents) {
  351. return ['status'=>false,'msg'=>'图片上传失败,请检查PHP配置'];
  352. }
  353. $disk->put($dir, $contents);
  354. return ['status'=>true,'path'=>$dir];
  355. }
  356. }