SiteAlbumController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\SiteAlbum;
  4. use App\Admin\Repositories\SiteAlbumFolder;
  5. use App\Admin\Repositories\SiteAlbumLog;
  6. use App\Admin\Repositories\SitePreviewVideo;
  7. use App\Libraries\CommonHelper;
  8. use App\Models\SiteAlbumFolder as SiteAlbumFolderModel;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Dcat\Admin\Layout\Content;
  14. use Dcat\Admin\Admin;
  15. use App\Admin\Repositories\NullRepository;
  16. use Dcat\Admin\Traits\HasUploadedFile;
  17. use Illuminate\Support\Facades\Cache;
  18. use function Symfony\Component\Translation\t;
  19. class SiteAlbumController extends AdminController
  20. {
  21. use HasUploadedFile;
  22. public function title()
  23. {
  24. return admin_trans( 'admin.album');
  25. }
  26. /**
  27. * page index
  28. */
  29. public function index(Content $content)
  30. {
  31. //记录folder_id
  32. $folderId = isset($_GET['folder_id']) ? intval($_GET['folder_id']) : 0;
  33. //保存临时变量
  34. setTempValue('folderId', $folderId);
  35. $html = $content
  36. ->header(admin_trans( 'admin.album'))
  37. ->body($this->indexForm());
  38. $html = $html->render();
  39. //删除第一个formID对应的JS代码
  40. preg_match('/<form[^>]*id="([^"]*)"[^>]*>/', $html, $matches);
  41. if (isset($matches[1])) {
  42. $formId = $matches[1]; // 获取 id 的值
  43. // echo "找到的 form id: " . $formId . "\n";
  44. // 2. 根据 id 值,删除对应的 JavaScript 代码
  45. $pattern = '/\$\(\'#' . preg_quote($formId, '/') . '\'\)\.form\(\{.*?\}\);/s';
  46. $html = preg_replace($pattern, '', $html);
  47. }
  48. //把第一个form标签替换成div标签
  49. $html = preg_replace('/<form([^>]*)>(.*?)<\/form>/s', '<div$1>$2</div>', $html, 1);
  50. return $html;
  51. }
  52. protected function indexForm()
  53. {
  54. return Form::make(new NullRepository(), function (Form $form) {
  55. $form->action('/site-album');
  56. $folderModel = new SiteAlbumFolderModel();
  57. $folderTree = $folderModel->allNodes();
  58. $form->block(2, function (Form\BlockForm $form) use ($folderTree) {
  59. $type = [
  60. 'default' => [
  61. 'icon' => true,
  62. ],
  63. ];
  64. $plugins = ['types'];
  65. $form->tree()
  66. ->setTitleColumn('title')
  67. ->nodes($folderTree)
  68. ->type($type)
  69. ->plugins($plugins)
  70. ->width(12,0);
  71. });
  72. $form->block(10, function (Form\BlockForm $form) {
  73. $form->html($this->grid())->width(12);
  74. });
  75. //以下JS代码用于点击文件夹时,自动跳转到相应页面
  76. Admin::script(
  77. <<<JS
  78. // 使用定时器检测容器是否存在
  79. const interval = setInterval(() => {
  80. const containerUl = document.getElementsByClassName('jstree-node');
  81. if (containerUl.length > 0) {
  82. clearInterval(interval); // 找到容器后停止检测
  83. // 以下是原有逻辑(已优化)
  84. const folderId = $('select[name="folder_id"]').data('value'); // 提取 folderId 到外层,避免重复查询[1](@ref)
  85. const anchors = document.querySelectorAll('a.jstree-anchor');
  86. anchors.forEach(anchor => {
  87. const id = anchor.id.split('_')[0];
  88. const href = `/prime-control/site-album?folder_id=`+id;
  89. // 绑定点击事件(阻止默认行为)
  90. anchor.addEventListener('click', event => {
  91. event.preventDefault();
  92. window.location.href = href;
  93. });
  94. // 高亮当前节点
  95. if (folderId == id) {
  96. anchor.classList.add('jstree-clicked');
  97. }
  98. });
  99. }
  100. }, 100); // 每100ms检测一次
  101. const firstCheckbox = document.querySelector('.vs-checkbox-primary');
  102. // 如果找到元素,则隐藏它
  103. if (firstCheckbox) {
  104. firstCheckbox.style.display = 'none';
  105. }
  106. //清空_previous_
  107. const input = document.querySelector('input[name="_previous_"]');
  108. if (input) {
  109. // 清空其值
  110. input.value = '';
  111. }
  112. JS
  113. );
  114. });
  115. }
  116. protected function grid()
  117. {
  118. return Grid::make(new SiteAlbum(), function (Grid $grid) {
  119. //默认分页条数
  120. $grid->paginate(config('admin.per_page'));
  121. $grid->column('id')->sortable();
  122. $grid->column('cover')->display(function ($images) {
  123. $images = json_decode($images);
  124. // 限制最多显示2个缩略图
  125. $dataImages = array_slice($images, 0, 1);
  126. return CommonHelper::displayImage($dataImages,80);
  127. });
  128. $grid->column('title',admin_trans_label('product_name'));
  129. //$grid->column('title_en');
  130. $grid->column('model');
  131. $grid->column('folder_id',admin_trans_label('folder'))
  132. ->display(function ($folder_id) {
  133. $folderModel = new SiteAlbumFolderModel();
  134. $folderName = $folderModel->find($folder_id)->title;
  135. return $folderName;
  136. });
  137. $grid->column('order');
  138. $grid->column('enabled')->using(admin_trans_array(config('dictionary.enabled'))) ->label([
  139. 0 => 'danger',
  140. 1 => 'success',
  141. ]);
  142. $grid->column('status')->display(function ($status) {
  143. $updated_at = $this->updated_at->format('Y-m-d H:i:s');
  144. //三个月前显示待更新
  145. if (time() - strtotime($updated_at) > 90 * 24 * 3600) {
  146. return '<span class="label label-warning" style="background:#dda451">待更新</span>';
  147. } else {
  148. return '<span class="label label-success" style="background:#21b978">正常</span>';
  149. }
  150. });
  151. $grid->column('missing_content')->display(function ($missing_content) {
  152. $missing_content = [];
  153. if ($this->cover == '[]') {$missing_content[] = '主图';}
  154. if ($this->text_detail == '') {$missing_content[] = '文字介绍';}
  155. if ($this->en_detail == '[]') {$missing_content[] = '英文详情页';}
  156. if ($this->cn_detail == '[]') {$missing_content[] = '中文详情页';}
  157. if ($this->video == '[]') {$missing_content[] = '视频';}
  158. if ($this->poster == '[]') {$missing_content[] = '海报';}
  159. if ($this->cert == '[]') {$missing_content[] = '证书';}
  160. if ($this->pdf == '[]') {$missing_content[] = 'PDF';}
  161. return implode(' / ', $missing_content);
  162. });
  163. // 筛选
  164. $grid->filter(function (Grid\Filter $filter) {
  165. $selectOptions = SiteAlbumFolderModel::selectOptions();
  166. unset($selectOptions[0]);
  167. $filter->panel();
  168. $filter->expand();
  169. $filter->like('model')->width(2);
  170. $filter->equal('folder_id',admin_trans_label('folder'))->select($selectOptions)->width(3);
  171. $filter->where('status', function ($query) {
  172. $status = $this->input;
  173. // 自定义查询逻辑
  174. if ($status == 1) {
  175. // 三个月内的
  176. $query->where('updated_at', '>=', date('Y-m-d H:i:s', strtotime('-3 month')));
  177. } else {
  178. // 三个月前的
  179. $query->where('updated_at', '<', date('Y-m-d H:i:s', strtotime('-3 month')));
  180. }
  181. })->select(admin_trans_array(config('dictionary.album_status')))->width(3);
  182. });
  183. $grid->disableViewButton();
  184. $grid->disablePerPages();
  185. $grid->disableRefreshButton();
  186. $grid->model()->orderBy('order', 'desc')->orderBy('id', 'desc');
  187. //弹窗大小
  188. $grid->setDialogFormDimensions('830px','670px');
  189. });
  190. }
  191. protected function form()
  192. {
  193. $thisObj = $this;
  194. return Form::make(new SiteAlbum(), function (Form $form) use ($thisObj) {
  195. if ($form->isEditing()) {
  196. $form->title("编辑 | " . $form->model()->title);
  197. }
  198. $form->width(9, 1);
  199. $form->disableViewButton();
  200. $form->disableViewCheck();
  201. $form->saving(function (Form $form) use ($thisObj) {
  202. //处理video
  203. $videos = $form->input('video');
  204. if ($videos) {
  205. foreach ($videos as $key => $value) {
  206. if (empty($value['cover']) && $value['_remove_'] != 1) {
  207. //自动生成封面
  208. $result = $thisObj->autoGenerateCover($value['video_src']);
  209. if ($result['status']) {
  210. $videos[$key]['cover'] = $result['path'];
  211. } else {
  212. return $form->response()->error($result['msg']);
  213. }
  214. }
  215. }
  216. } else {
  217. $videos = [];
  218. }
  219. $form->input('video', $videos);
  220. //处理pdf
  221. $pdfs = $form->input('pdf');
  222. $pdfs = empty($pdfs) ? [] : $pdfs;
  223. $form->input('pdf', $pdfs);
  224. //记录日志
  225. if (!$form->isCreating()) {
  226. $id = $form->getKey();
  227. $cacheKey = 'album_log_'.$id;
  228. Cache::add($cacheKey, json_encode($form->model()->toArray()), 3600);
  229. }
  230. });
  231. $form->saved(function (Form $form) {
  232. $id = $form->getKey();
  233. if (empty($form->input('model')) == false) {
  234. $action = $form->isCreating() ? 'add' : 'edit';
  235. if ($action == 'add') {
  236. $oldData = "[]";
  237. } else {
  238. $oldData = Cache::get('album_log_'. $id);
  239. Cache::forget('album_log_'. $id);
  240. }
  241. SiteAlbumLog::log($action, $id,$form->input('model'),$oldData);
  242. }
  243. //更新site_preview_video表
  244. $video = $form->input('video');
  245. $data = [];
  246. foreach ($video as $value) {
  247. if ($value['_remove_'] != 1){
  248. $data[] = ['cover'=>$value['cover'],'video_title'=>$value['video_title'],'video_en_title' => $value['video_en_title'],'video_src'=>$value['video_src']];
  249. }
  250. }
  251. SitePreviewVideo::updatePreviewVideo($id,$data);
  252. });
  253. $form->tab(admin_trans_label('basic_info'), function (Form $form) {
  254. $selectOptions = SiteAlbumFolder::selectOptions();
  255. unset($selectOptions[0]);
  256. $folderId = getTempValue('folderId');
  257. if ($folderId == 0) {
  258. $folderId = array_key_first($selectOptions);
  259. }
  260. $form->select('folder_id')->options($selectOptions)->default($folderId)->required();
  261. $form->text('title',admin_trans_label('product_name'))->required();
  262. $form->text('title_en',admin_trans_label('product_name_en'))->required();
  263. $form->text('model')->required();
  264. $form->table('parameters',admin_trans_label('attribute_name'), function (Form\NestedForm $table) {
  265. $table->text('key')->required();
  266. $table->text('value')->required();
  267. })->setView('admin.form_custom.hasmanytable')
  268. ->saving(function ($input) {
  269. return json_encode($input);
  270. });
  271. $form->number('order')
  272. ->default(0)
  273. ->rules('numeric');
  274. $form->switch('enabled')->default(1);
  275. })->tab(admin_trans_label('cover'), function (Form $form) {
  276. $form->multipleImage('cover')
  277. ->retainable()//禁止删OSS图
  278. ->sortable() // 可拖动排序
  279. ->removable() // 可移除图片
  280. ->autoUpload() // 自动上传
  281. ->uniqueName()
  282. ->limit(config('admin.upload.oss_image.limit'))
  283. ->accept(config('admin.upload.oss_image.accept'))
  284. ->maxSize(config('admin.upload.oss_image.max_size'))
  285. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  286. ->saving(function ($images) use ($form) {
  287. return json_encode($images);
  288. });
  289. })->tab(admin_trans_label('text_detail'), function (Form $form) {
  290. $form->editor('text_detail',admin_trans_label('text_detail'));
  291. })->tab(admin_trans_label('en_detail'), function (Form $form) {
  292. $form->multipleImage('en_detail')
  293. ->retainable()//禁止删OSS图
  294. ->sortable() // 可拖动排序
  295. ->removable() // 可移除图片
  296. ->autoUpload() // 自动上传
  297. ->uniqueName()
  298. ->limit(config('admin.upload.oss_image.limit'))
  299. ->accept(config('admin.upload.oss_image.accept'))
  300. ->maxSize(config('admin.upload.oss_image.max_size'))
  301. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  302. ->saving(function ($images) use ($form) {
  303. return json_encode($images);
  304. });
  305. })->tab(admin_trans_label('cn_detail'), function (Form $form) {
  306. $form->multipleImage('cn_detail')
  307. ->retainable()//禁止删OSS图
  308. ->sortable() // 可拖动排序
  309. ->removable() // 可移除图片
  310. ->autoUpload() // 自动上传
  311. ->uniqueName()
  312. ->limit(config('admin.upload.oss_image.limit'))
  313. ->accept(config('admin.upload.oss_image.accept'))
  314. ->maxSize(config('admin.upload.oss_image.max_size'))
  315. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  316. ->saving(function ($images) use ($form) {
  317. return json_encode($images);
  318. });
  319. })->tab(admin_trans_label('video'), function (Form $form) {
  320. $count = 0;
  321. $form->hasMany('video', function (Form\NestedForm $form) use (&$count) {
  322. $videos = $form->model()->video;
  323. $imgArray = "";
  324. if ($videos) {
  325. $videos = json_decode($videos,true);
  326. foreach ($videos as $key => $value) {
  327. if ($value['cover'] && $key == $count-1) {
  328. $imgArray = [$value['cover']];
  329. }
  330. }
  331. }
  332. $imgHtml = CommonHelper::displayImage($imgArray);
  333. $form->html($imgHtml,admin_trans_label('image_preview'));
  334. $count++;
  335. $form->text('video_title',admin_trans_label('video_title'))->required();
  336. $form->text('video_en_title',admin_trans_label('video_en_title'))->required();
  337. $form->hidden('cover',admin_trans_label('video_cover'))->placeholder('自动生成')->readOnly();
  338. $form->tradFile('video_src')
  339. ->retainable()//禁止删OSS图
  340. ->removable() // 可移除图片
  341. ->autoUpload() // 自动上传
  342. ->uniqueName()//
  343. ->downloadable()
  344. ->accept(config('admin.upload.oss_video.accept'))
  345. ->maxSize(config('admin.upload.oss_video.max_size'))
  346. ->dir(config("admin.upload.directory.video").'/uploads/'.date("Ym"))
  347. ->chunkSize(1024)
  348. ->required();
  349. })->useTable()
  350. ->customFormat(function ($data) {return json_decode($data,true);})
  351. ->setView('admin.form_custom.hasmanytable')
  352. ->saving(function ($input) {
  353. $data = [];
  354. foreach ($input as $value) {
  355. if ($value['_remove_'] != 1){
  356. $data[] = ['cover'=>$value['cover'],'video_title'=>$value['video_title'],'video_en_title' => $value['video_en_title'],'video_src'=>$value['video_src']];
  357. }
  358. }
  359. return json_encode($data);
  360. });
  361. })->tab(admin_trans_label('poster'), function (Form $form) {
  362. $form->multipleImage('poster')
  363. ->retainable()//禁止删OSS图
  364. ->sortable() // 可拖动排序
  365. ->removable() // 可移除图片
  366. ->autoUpload() // 自动上传
  367. ->uniqueName()
  368. ->limit(config('admin.upload.oss_image.limit'))
  369. ->accept(config('admin.upload.oss_image.accept'))
  370. ->maxSize(config('admin.upload.oss_image.max_size'))
  371. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  372. ->saving(function ($images) use ($form) {
  373. return json_encode($images);
  374. });
  375. })->tab(admin_trans_label('cert'), function (Form $form) {
  376. $form->multipleImage('cert')
  377. ->retainable()//禁止删OSS图
  378. ->sortable() // 可拖动排序
  379. ->removable() // 可移除图片
  380. ->autoUpload() // 自动上传
  381. ->uniqueName()
  382. ->limit(config('admin.upload.oss_image.limit'))
  383. ->accept(config('admin.upload.oss_image.accept'))
  384. ->maxSize(config('admin.upload.oss_image.max_size'))
  385. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  386. ->saving(function ($images) use ($form) {
  387. return json_encode($images);
  388. });
  389. })->tab(admin_trans_label('pdf'), function (Form $form) {
  390. $form->hasMany('pdf', function ($form) {
  391. $form->text('pdf_title')->required();
  392. $form->text('pdf_title_en')->required();
  393. $form->tradFile('pdf_src')
  394. ->retainable()//禁止删OSS图
  395. ->removable() // 可移除图片
  396. ->autoUpload() // 自动上传
  397. ->uniqueName()
  398. ->downloadable()
  399. ->accept(config('admin.upload.oss_pdf.accept'))
  400. ->maxSize(config('admin.upload.oss_pdf.max_size'))
  401. ->dir(config("admin.upload.directory.pdf").'/uploads/'.date("Ym"))
  402. ->chunkSize(1024)
  403. ->required();
  404. })->useTable()
  405. ->customFormat(function ($data) {
  406. return json_decode($data,true);
  407. })
  408. ->setView('admin.form_custom.hasmanytable')
  409. ->saving(function ($input) {
  410. $data = [];
  411. foreach ($input as $value) {
  412. if ($value['_remove_'] != 1){
  413. $data[] = ['pdf_title'=>$value['pdf_title'],'pdf_title_en' => $value['pdf_title_en'],'pdf_src'=>$value['pdf_src']];
  414. }
  415. }
  416. return json_encode($data);
  417. });
  418. });
  419. //以下JS代码用于点击列表时,带上folder_id参数,还有隐藏的tab切换功能
  420. $thisObj->formAddJS();
  421. });
  422. }
  423. /*
  424. * 自动生成视频封面,并上传到OSS
  425. */
  426. private function autoGenerateCover($videoSrc)
  427. {
  428. try {
  429. $cover = $videoSrc.'?x-oss-process=video/snapshot,t_2000,f_jpg,h_500,m_fast';
  430. $cover = CommonHelper::ossUrl($cover);
  431. $path = $this->upload($cover,'.jpg');
  432. } catch (\Exception $e) {
  433. $path = ['status'=>true,'path'=>'/static/common/images/no-image.jpg'];
  434. }
  435. return $path;
  436. }
  437. private function upload($file,$imgType='.jpg')
  438. {
  439. $disk = $this->disk('oss');
  440. $newName = uniqueCode("video_cover_").$imgType;
  441. $dir = config("admin.upload.directory.image").'/uploads/'.date("Ym").'/'.$newName;
  442. $contents = file_get_contents($file);
  443. if (!$contents) {
  444. return ['status'=>false,'msg'=>'图片上传失败,请检查PHP配置'];
  445. }
  446. $disk->put($dir, $contents);
  447. return ['status'=>true,'path'=>$dir];
  448. }
  449. private function formAddJS() {
  450. $folderTabs = SiteAlbumFolder::getAllFolderTabs();
  451. $album_tabs = config('dictionary.album_tabs');
  452. foreach ($folderTabs as $key => $value) {
  453. foreach ($value as $k => $v) {
  454. $folderTabs[$key][$k] = admin_trans_label($album_tabs[$v]);
  455. }
  456. }
  457. $folderTabs = json_encode($folderTabs);
  458. //以下JS作用:1.点击列表时,把folder_id参数传递给表单 2.切换文件夹时,显示隐藏相应的tab
  459. Admin::script(
  460. <<<JS
  461. const featherIcon = document.querySelector('i.feather.icon-list');
  462. if (featherIcon) {
  463. // 找到 <i> 的上级 <a> 元素
  464. const parentLink = featherIcon.closest('a');
  465. if (parentLink) {
  466. // 绑定 onclick 事件
  467. parentLink.onclick = function(event) {
  468. // 阻止默认行为(如跳转)
  469. event.preventDefault();
  470. // 获取 folder_id 的值
  471. let folderIdValue = $('select[name="folder_id"]').val();
  472. // 在 href 后追加 ?folder_id=xxx
  473. if (parentLink.href) {
  474. const newHref = parentLink.href + '?folder_id=' + folderIdValue;
  475. window.location.href = newHref; // 跳转到新的 URL
  476. }
  477. };
  478. }
  479. }
  480. // 监听 <select name="folder_id"> 的变化事件
  481. $('select[name="folder_id"]').change(function() {
  482. showHideTabs($(this).val());
  483. });
  484. folderIdValue = $('select[name="folder_id"]').val();
  485. showHideTabs(folderIdValue);
  486. function showHideTabs(fid) {
  487. // 获取当前选中的值
  488. let folderTabs = {$folderTabs};
  489. const folderIdValue = fid;
  490. // 获取当前 folderIdValue 对应的标签索引
  491. const tabIndexes = folderTabs[folderIdValue];
  492. console.log(tabIndexes);
  493. if (tabIndexes) {
  494. // 处理上方的 <li>,限定在 .nav-tabs 内
  495. $('.nav-tabs .nav-item').each(function(index) {
  496. if (index > 0) { // 跳过第一个固定元素
  497. $(this).hide();
  498. }
  499. });
  500. $('.nav-tabs .nav-item').each(function() {
  501. const navItem = $(this);
  502. const navLink = navItem.find('.nav-link');
  503. // 获取清理后的导航文本(包含处理空格和特殊字符)
  504. const tabText = navLink.clone().children().remove().end().text().trim();
  505. // 逻辑判断与显示控制
  506. if (tabIndexes.includes(tabText)) {
  507. navItem.show(); // 显式设置显示
  508. }
  509. });
  510. }
  511. }
  512. JS
  513. );
  514. }
  515. }