SiteAlbumController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. //删除第一个formID对应的JS代码
  37. preg_match('/<form[^>]*id="([^"]*)"[^>]*>/', $html, $matches);
  38. if (isset($matches[1])) {
  39. $formId = $matches[1]; // 获取 id 的值
  40. // echo "找到的 form id: " . $formId . "\n";
  41. // 2. 根据 id 值,删除对应的 JavaScript 代码
  42. $pattern = '/\$\(\'#' . preg_quote($formId, '/') . '\'\)\.form\(\{.*?\}\);/s';
  43. $html = preg_replace($pattern, '', $html);
  44. }
  45. //把第一个form标签替换成div标签
  46. $html = preg_replace('/<form([^>]*)>(.*?)<\/form>/s', '<div$1>$2</div>', $html, 1);
  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. //清空_previous_
  96. const input = document.querySelector('input[name="_previous_"]');
  97. if (input) {
  98. // 清空其值
  99. input.value = '';
  100. }
  101. JS
  102. );
  103. });
  104. }
  105. protected function grid()
  106. {
  107. return Grid::make(new SiteAlbum(), function (Grid $grid) {
  108. //默认分页条数
  109. $grid->paginate(config('admin.per_page'));
  110. $grid->column('id')->sortable();
  111. $grid->column('cover')->display(function ($images) {
  112. $images = json_decode($images);
  113. // 限制最多显示2个缩略图
  114. $dataImages = array_slice($images, 0, 1);
  115. return CommonHelper::displayImage($dataImages,80);
  116. });
  117. $grid->column('model');
  118. $grid->column('enabled')->using(admin_trans_array(config('dictionary.enabled'))) ->label([
  119. 0 => 'danger',
  120. 1 => 'success',
  121. ]);
  122. $grid->column('missing_content')->display(function ($missing_content) {
  123. $missing_content = [];
  124. if ($this->cover == '[]') {$missing_content[] = '主图';}
  125. if ($this->en_detail == '[]') {$missing_content[] = '英文详情';}
  126. if ($this->cn_detail == '[]') {$missing_content[] = '中文详情';}
  127. if ($this->video == '[]') {$missing_content[] = '视频';}
  128. if ($this->poster == '[]') {$missing_content[] = '海报';}
  129. if ($this->cert == '[]') {$missing_content[] = '证书';}
  130. if ($this->pdf == '[]') {$missing_content[] = 'PDF';}
  131. return implode(' / ', $missing_content);
  132. });
  133. // 筛选
  134. $grid->filter(function (Grid\Filter $filter) {
  135. $filter->panel();
  136. $filter->expand();
  137. $filter->like('model')->width(2);
  138. $filter->equal('folder_id',admin_trans_label('folder'))->select(SiteAlbumFolderModel::selectOptions())->width(3);
  139. });
  140. $grid->disableViewButton();
  141. $grid->disablePerPages();
  142. $grid->disableRefreshButton();
  143. $grid->model()->orderBy('order', 'asc')->orderBy('id', 'desc');
  144. //弹窗大小
  145. $grid->setDialogFormDimensions('830px','670px');
  146. });
  147. }
  148. protected function form()
  149. {
  150. $thisObj = $this;
  151. return Form::make(new SiteAlbum(), function (Form $form) use ($thisObj) {
  152. $form->width(9, 1);
  153. $form->disableViewButton();
  154. $form->disableViewCheck();
  155. $form->saving(function (Form $form) use ($thisObj) {
  156. //处理video
  157. $videos = $form->input('video');
  158. if ($videos) {
  159. foreach ($videos as $key => $value) {
  160. if (empty($value['cover']) && $value['_remove_'] != 1) {
  161. //自动生成封面
  162. $result = $thisObj->autoGenerateCover($value['video_src']);
  163. if ($result['status']) {
  164. $videos[$key]['cover'] = $result['path'];
  165. } else {
  166. return $form->response()->error($result['msg']);
  167. }
  168. }
  169. }
  170. } else {
  171. $videos = [];
  172. }
  173. $form->input('video', $videos);
  174. //处理pdf
  175. $pdfs = $form->input('pdf');
  176. $pdfs = empty($pdfs) ? [] : $pdfs;
  177. $form->input('pdf', $pdfs);
  178. });
  179. $form->tab(admin_trans_label('basic_info'), function (Form $form) {
  180. $folderId = getTempValue('folderId');
  181. $form->select('folder_id')->options(SiteAlbumFolder::selectOptions())->default($folderId)->required();
  182. $form->text('model')->required();
  183. $form->table('parameters',admin_trans_label('attribute_name'), function (Form\NestedForm $table) {
  184. $table->text('key')->required();
  185. $table->text('value')->required();
  186. })->setView('admin.form_custom.hasmanytable')
  187. ->saving(function ($input) {
  188. return json_encode($input);
  189. });
  190. $form->switch('enabled')->default(1);
  191. })->tab(admin_trans_label('cover'), function (Form $form) {
  192. $form->multipleImage('cover')
  193. ->retainable()//禁止删OSS图
  194. ->sortable() // 可拖动排序
  195. ->removable() // 可移除图片
  196. ->autoUpload() // 自动上传
  197. ->uniqueName()
  198. ->limit(config('admin.upload.oss_image.limit'))
  199. ->accept(config('admin.upload.oss_image.accept'))
  200. ->maxSize(config('admin.upload.oss_image.max_size'))
  201. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  202. ->saving(function ($images) use ($form) {
  203. return json_encode($images);
  204. });
  205. })->tab(admin_trans_label('en_detail'), function (Form $form) {
  206. $form->multipleImage('en_detail')
  207. ->retainable()//禁止删OSS图
  208. ->sortable() // 可拖动排序
  209. ->removable() // 可移除图片
  210. ->autoUpload() // 自动上传
  211. ->uniqueName()
  212. ->limit(config('admin.upload.oss_image.limit'))
  213. ->accept(config('admin.upload.oss_image.accept'))
  214. ->maxSize(config('admin.upload.oss_image.max_size'))
  215. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  216. ->saving(function ($images) use ($form) {
  217. return json_encode($images);
  218. });
  219. })->tab(admin_trans_label('cn_detail'), function (Form $form) {
  220. $form->multipleImage('cn_detail')
  221. ->retainable()//禁止删OSS图
  222. ->sortable() // 可拖动排序
  223. ->removable() // 可移除图片
  224. ->autoUpload() // 自动上传
  225. ->uniqueName()
  226. ->limit(config('admin.upload.oss_image.limit'))
  227. ->accept(config('admin.upload.oss_image.accept'))
  228. ->maxSize(config('admin.upload.oss_image.max_size'))
  229. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  230. ->saving(function ($images) use ($form) {
  231. return json_encode($images);
  232. });
  233. })->tab(admin_trans_label('video'), function (Form $form) {
  234. $count = 0;
  235. $form->hasMany('video', function (Form\NestedForm $form) use (&$count) {
  236. $videos = $form->model()->video;
  237. $imgArray = "";
  238. if ($videos) {
  239. $videos = json_decode($videos,true);
  240. foreach ($videos as $key => $value) {
  241. if ($value['cover'] && $key == $count-1) {
  242. $imgArray = [$value['cover']];
  243. }
  244. }
  245. }
  246. $imgHtml = CommonHelper::displayImage($imgArray);
  247. $form->html($imgHtml,admin_trans_label('image_preview'));
  248. $count++;
  249. $form->text('cover',admin_trans_label('video_cover'))->placeholder('为空则自动生成');
  250. $form->tradFile('video_src')
  251. ->retainable()//禁止删OSS图
  252. ->removable() // 可移除图片
  253. ->autoUpload() // 自动上传
  254. ->uniqueName()//
  255. ->downloadable()
  256. ->accept(config('admin.upload.oss_video.accept'))
  257. ->maxSize(config('admin.upload.oss_video.max_size'))
  258. ->dir(config("admin.upload.directory.video").'/uploads/'.date("Ymd"))
  259. ->chunkSize(1024)
  260. ->required();
  261. })->useTable()
  262. ->customFormat(function ($data) {return json_decode($data,true);})
  263. ->setView('admin.form_custom.hasmanytable')
  264. ->saving(function ($input) {
  265. $data = [];
  266. foreach ($input as $value) {
  267. if ($value['_remove_'] != 1){
  268. $data[] = ['cover'=>$value['cover'],'video_src'=>$value['video_src']];
  269. }
  270. }
  271. return json_encode($data);
  272. });
  273. })->tab(admin_trans_label('poster'), function (Form $form) {
  274. $form->multipleImage('poster')
  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('cert'), function (Form $form) {
  288. $form->multipleImage('cert')
  289. ->retainable()//禁止删OSS图
  290. ->sortable() // 可拖动排序
  291. ->removable() // 可移除图片
  292. ->autoUpload() // 自动上传
  293. ->uniqueName()
  294. ->limit(config('admin.upload.oss_image.limit'))
  295. ->accept(config('admin.upload.oss_image.accept'))
  296. ->maxSize(config('admin.upload.oss_image.max_size'))
  297. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ymd"))
  298. ->saving(function ($images) use ($form) {
  299. return json_encode($images);
  300. });
  301. })->tab(admin_trans_label('pdf'), function (Form $form) {
  302. $form->hasMany('pdf', function ($form) {
  303. $form->text('pdf_title')->required();
  304. $form->tradFile('pdf_src')
  305. ->retainable()//禁止删OSS图
  306. ->removable() // 可移除图片
  307. ->autoUpload() // 自动上传
  308. ->uniqueName()
  309. ->downloadable()
  310. ->accept(config('admin.upload.oss_pdf.accept'))
  311. ->maxSize(config('admin.upload.oss_pdf.max_size'))
  312. ->dir(config("admin.upload.directory.pdf").'/uploads/'.date("Ymd"))
  313. ->chunkSize(1024)
  314. ->required();
  315. })->useTable()
  316. ->customFormat(function ($data) {
  317. return json_decode($data,true);
  318. })
  319. ->setView('admin.form_custom.hasmanytable')
  320. ->saving(function ($input) {
  321. $data = [];
  322. foreach ($input as $value) {
  323. if ($value['_remove_'] != 1){
  324. $data[] = ['pdf_title'=>$value['pdf_title'],'pdf_src'=>$value['pdf_src']];
  325. }
  326. }
  327. return json_encode($data);
  328. });
  329. });
  330. $folderId = getTempValue('folderId');
  331. if ($folderId >0) {
  332. Admin::script(
  333. <<<JS
  334. const featherIcon = document.querySelector('i.feather.icon-list');
  335. if (featherIcon) {
  336. // 找到 <i> 的上级 <a> 元素
  337. const parentLink = featherIcon.closest('a');
  338. if (parentLink && parentLink.href) {
  339. // 在 href 后追加 ?folder_id=3
  340. parentLink.href = parentLink.href + '?folder_id={$folderId}';
  341. }
  342. }
  343. JS
  344. );
  345. }
  346. });
  347. }
  348. /*
  349. * 自动生成视频封面
  350. */
  351. private function autoGenerateCover($videoSrc)
  352. {
  353. $cover = $videoSrc.'?x-oss-process=video/snapshot,t_2000,f_jpg,h_500,m_fast';
  354. //TODO 上传到OSS
  355. $cover = CommonHelper::ossUrl($cover);
  356. $path = $this->upload($cover,'.jpg');
  357. return $path;
  358. }
  359. private function upload($file,$imgType='.jpg')
  360. {
  361. $disk = $this->disk('oss');
  362. $newName = uniqueCode("video_cover_").$imgType;
  363. $dir = config("admin.upload.directory.image").'/uploads/'.date("Ymd").'/'.$newName;
  364. $contents = file_get_contents($file);
  365. if (!$contents) {
  366. return ['status'=>false,'msg'=>'图片上传失败,请检查PHP配置'];
  367. }
  368. $disk->put($dir, $contents);
  369. return ['status'=>true,'path'=>$dir];
  370. }
  371. }