SiteAlbumController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. if ($form->input('title')) {
  203. //处理video
  204. $videos = $form->input('video');
  205. if ($videos) {
  206. foreach ($videos as $key => $value) {
  207. if (empty($value['cover']) && $value['_remove_'] != 1) {
  208. //自动生成封面
  209. $result = $thisObj->autoGenerateCover($value['video_src']);
  210. if ($result['status']) {
  211. $videos[$key]['cover'] = $result['path'];
  212. } else {
  213. return $form->response()->error($result['msg']);
  214. }
  215. }
  216. }
  217. } else {
  218. $videos = [];
  219. }
  220. $form->input('video', $videos);
  221. //处理pdf
  222. $pdfs = $form->input('pdf');
  223. $pdfs = empty($pdfs) ? [] : $pdfs;
  224. $form->input('pdf', $pdfs);
  225. //记录日志
  226. if (!$form->isCreating()) {
  227. $id = $form->getKey();
  228. $cacheKey = 'album_log_'.$id;
  229. Cache::add($cacheKey, json_encode($form->model()->toArray()), 3600);
  230. }
  231. }
  232. });
  233. $form->saved(function (Form $form) {
  234. if ($form->input('title')) {
  235. $id = $form->getKey();
  236. if (empty($form->input('model')) == false) {
  237. $action = $form->isCreating() ? 'add' : 'edit';
  238. if ($action == 'add') {
  239. $oldData = "[]";
  240. } else {
  241. $oldData = Cache::get('album_log_'. $id);
  242. Cache::forget('album_log_'. $id);
  243. }
  244. SiteAlbumLog::log($action, $id,$form->input('model'),$oldData);
  245. }
  246. //更新site_preview_video表
  247. $video = $form->input('video');
  248. $data = [];
  249. foreach ($video as $value) {
  250. if ($value['_remove_'] != 1){
  251. $data[] = ['cover'=>$value['cover'],'video_title'=>$value['video_title'],'video_en_title' => $value['video_en_title'],'video_src'=>$value['video_src']];
  252. }
  253. }
  254. SitePreviewVideo::updatePreviewVideo($id,$data);
  255. }
  256. });
  257. $form->tab(admin_trans_label('basic_info'), function (Form $form) {
  258. $selectOptions = SiteAlbumFolder::selectOptions();
  259. unset($selectOptions[0]);
  260. $folderId = getTempValue('folderId');
  261. if ($folderId == 0) {
  262. $folderId = array_key_first($selectOptions);
  263. }
  264. $form->select('folder_id')->options($selectOptions)->default($folderId)->required();
  265. $form->text('title',admin_trans_label('product_name'))->required();
  266. $form->text('title_en',admin_trans_label('product_name_en'))->required();
  267. $form->text('model')->required();
  268. $form->table('parameters',admin_trans_label('attribute_name'), function (Form\NestedForm $table) {
  269. $table->text('key')->required();
  270. $table->text('value')->required();
  271. })->setView('admin.form_custom.hasmanytable')
  272. ->saving(function ($input) {
  273. return json_encode($input);
  274. });
  275. $form->number('order')
  276. ->default(0)
  277. ->rules('numeric');
  278. $form->switch('enabled')->default(1);
  279. })->tab(admin_trans_label('cover'), function (Form $form) {
  280. $form->multipleImage('cover')
  281. ->retainable()//禁止删OSS图
  282. ->sortable() // 可拖动排序
  283. ->removable() // 可移除图片
  284. ->autoUpload() // 自动上传
  285. ->uniqueName()
  286. ->limit(config('admin.upload.oss_image.limit'))
  287. ->accept(config('admin.upload.oss_image.accept'))
  288. ->maxSize(config('admin.upload.oss_image.max_size'))
  289. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  290. ->saving(function ($images) use ($form) {
  291. return json_encode($images);
  292. });
  293. })->tab(admin_trans_label('text_detail'), function (Form $form) {
  294. $form->editor('text_detail',admin_trans_label('text_detail'));
  295. })->tab(admin_trans_label('en_detail'), function (Form $form) {
  296. $form->multipleImage('en_detail')
  297. ->retainable()//禁止删OSS图
  298. ->sortable() // 可拖动排序
  299. ->removable() // 可移除图片
  300. ->autoUpload() // 自动上传
  301. ->uniqueName()
  302. ->limit(config('admin.upload.oss_image.limit'))
  303. ->accept(config('admin.upload.oss_image.accept'))
  304. ->maxSize(config('admin.upload.oss_image.max_size'))
  305. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  306. ->saving(function ($images) use ($form) {
  307. return json_encode($images);
  308. });
  309. })->tab(admin_trans_label('cn_detail'), function (Form $form) {
  310. $form->multipleImage('cn_detail')
  311. ->retainable()//禁止删OSS图
  312. ->sortable() // 可拖动排序
  313. ->removable() // 可移除图片
  314. ->autoUpload() // 自动上传
  315. ->uniqueName()
  316. ->limit(config('admin.upload.oss_image.limit'))
  317. ->accept(config('admin.upload.oss_image.accept'))
  318. ->maxSize(config('admin.upload.oss_image.max_size'))
  319. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  320. ->saving(function ($images) use ($form) {
  321. return json_encode($images);
  322. });
  323. })->tab(admin_trans_label('video'), function (Form $form) {
  324. $count = 0;
  325. $form->hasMany('video', function (Form\NestedForm $form) use (&$count) {
  326. $videos = $form->model()->video;
  327. $imgArray = "";
  328. if ($videos) {
  329. $videos = json_decode($videos,true);
  330. foreach ($videos as $key => $value) {
  331. if ($value['cover'] && $key == $count-1) {
  332. $imgArray = [$value['cover']];
  333. }
  334. }
  335. }
  336. $imgHtml = CommonHelper::displayImage($imgArray);
  337. $form->html($imgHtml,admin_trans_label('image_preview'));
  338. $count++;
  339. $form->text('video_title',admin_trans_label('video_title'))->required();
  340. $form->text('video_en_title',admin_trans_label('video_en_title'))->required();
  341. $form->hidden('cover',admin_trans_label('video_cover'))->placeholder('自动生成')->readOnly();
  342. $form->tradFile('video_src')
  343. ->retainable()//禁止删OSS图
  344. ->removable() // 可移除图片
  345. ->autoUpload() // 自动上传
  346. ->uniqueName()//
  347. ->downloadable()
  348. ->accept(config('admin.upload.oss_video.accept'))
  349. ->maxSize(config('admin.upload.oss_video.max_size'))
  350. ->dir(config("admin.upload.directory.video").'/uploads/'.date("Ym"))
  351. ->chunkSize(1024)
  352. ->required();
  353. })->useTable()
  354. ->customFormat(function ($data) {return json_decode($data,true);})
  355. ->setView('admin.form_custom.hasmanytable')
  356. ->saving(function ($input) {
  357. $data = [];
  358. foreach ($input as $value) {
  359. if ($value['_remove_'] != 1){
  360. $data[] = ['cover'=>$value['cover'],'video_title'=>$value['video_title'],'video_en_title' => $value['video_en_title'],'video_src'=>$value['video_src']];
  361. }
  362. }
  363. return json_encode($data);
  364. });
  365. })->tab(admin_trans_label('poster'), function (Form $form) {
  366. $form->multipleImage('poster')
  367. ->retainable()//禁止删OSS图
  368. ->sortable() // 可拖动排序
  369. ->removable() // 可移除图片
  370. ->autoUpload() // 自动上传
  371. ->uniqueName()
  372. ->limit(config('admin.upload.oss_image.limit'))
  373. ->accept(config('admin.upload.oss_image.accept'))
  374. ->maxSize(config('admin.upload.oss_image.max_size'))
  375. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  376. ->saving(function ($images) use ($form) {
  377. return json_encode($images);
  378. });
  379. })->tab(admin_trans_label('cert'), function (Form $form) {
  380. $form->multipleImage('cert')
  381. ->retainable()//禁止删OSS图
  382. ->sortable() // 可拖动排序
  383. ->removable() // 可移除图片
  384. ->autoUpload() // 自动上传
  385. ->uniqueName()
  386. ->limit(config('admin.upload.oss_image.limit'))
  387. ->accept(config('admin.upload.oss_image.accept'))
  388. ->maxSize(config('admin.upload.oss_image.max_size'))
  389. ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
  390. ->saving(function ($images) use ($form) {
  391. return json_encode($images);
  392. });
  393. })->tab(admin_trans_label('pdf'), function (Form $form) {
  394. $form->hasMany('pdf', function ($form) {
  395. $form->text('pdf_title')->required();
  396. $form->text('pdf_title_en')->required();
  397. $form->tradFile('pdf_src')
  398. ->retainable()//禁止删OSS图
  399. ->removable() // 可移除图片
  400. ->autoUpload() // 自动上传
  401. ->uniqueName()
  402. ->downloadable()
  403. ->accept(config('admin.upload.oss_pdf.accept'))
  404. ->maxSize(config('admin.upload.oss_pdf.max_size'))
  405. ->dir(config("admin.upload.directory.pdf").'/uploads/'.date("Ym"))
  406. ->chunkSize(1024)
  407. ->required();
  408. })->useTable()
  409. ->customFormat(function ($data) {
  410. return json_decode($data,true);
  411. })
  412. ->setView('admin.form_custom.hasmanytable')
  413. ->saving(function ($input) {
  414. $data = [];
  415. foreach ($input as $value) {
  416. if ($value['_remove_'] != 1){
  417. $data[] = ['pdf_title'=>$value['pdf_title'],'pdf_title_en' => $value['pdf_title_en'],'pdf_src'=>$value['pdf_src']];
  418. }
  419. }
  420. return json_encode($data);
  421. });
  422. });
  423. //以下JS代码用于点击列表时,带上folder_id参数,还有隐藏的tab切换功能
  424. $thisObj->formAddJS();
  425. });
  426. }
  427. /*
  428. * 自动生成视频封面,并上传到OSS
  429. */
  430. private function autoGenerateCover($videoSrc)
  431. {
  432. try {
  433. $cover = $videoSrc.'?x-oss-process=video/snapshot,t_2000,f_jpg,h_500,m_fast';
  434. $cover = CommonHelper::ossUrl($cover);
  435. $path = $this->upload($cover,'.jpg');
  436. } catch (\Exception $e) {
  437. $path = ['status'=>true,'path'=>'/static/common/images/no-image.jpg'];
  438. }
  439. return $path;
  440. }
  441. private function upload($file,$imgType='.jpg')
  442. {
  443. $disk = $this->disk('oss');
  444. $newName = uniqueCode("video_cover_").$imgType;
  445. $dir = config("admin.upload.directory.image").'/uploads/'.date("Ym").'/'.$newName;
  446. $contents = file_get_contents($file);
  447. if (!$contents) {
  448. return ['status'=>false,'msg'=>'图片上传失败,请检查PHP配置'];
  449. }
  450. $disk->put($dir, $contents);
  451. return ['status'=>true,'path'=>$dir];
  452. }
  453. private function formAddJS() {
  454. $folderTabs = SiteAlbumFolder::getAllFolderTabs();
  455. $album_tabs = config('dictionary.album_tabs');
  456. foreach ($folderTabs as $key => $value) {
  457. foreach ($value as $k => $v) {
  458. $folderTabs[$key][$k] = admin_trans_label($album_tabs[$v]);
  459. }
  460. }
  461. $folderTabs = json_encode($folderTabs);
  462. //以下JS作用:1.点击列表时,把folder_id参数传递给表单 2.切换文件夹时,显示隐藏相应的tab
  463. Admin::script(
  464. <<<JS
  465. const featherIcon = document.querySelector('i.feather.icon-list');
  466. if (featherIcon) {
  467. // 找到 <i> 的上级 <a> 元素
  468. const parentLink = featherIcon.closest('a');
  469. if (parentLink) {
  470. // 绑定 onclick 事件
  471. parentLink.onclick = function(event) {
  472. // 阻止默认行为(如跳转)
  473. event.preventDefault();
  474. // 获取 folder_id 的值
  475. let folderIdValue = $('select[name="folder_id"]').val();
  476. // 在 href 后追加 ?folder_id=xxx
  477. if (parentLink.href) {
  478. const newHref = parentLink.href + '?folder_id=' + folderIdValue;
  479. window.location.href = newHref; // 跳转到新的 URL
  480. }
  481. };
  482. }
  483. }
  484. // 监听 <select name="folder_id"> 的变化事件
  485. $('select[name="folder_id"]').change(function() {
  486. showHideTabs($(this).val());
  487. });
  488. folderIdValue = $('select[name="folder_id"]').val();
  489. showHideTabs(folderIdValue);
  490. function showHideTabs(fid) {
  491. // 获取当前选中的值
  492. let folderTabs = {$folderTabs};
  493. const folderIdValue = fid;
  494. // 获取当前 folderIdValue 对应的标签索引
  495. const tabIndexes = folderTabs[folderIdValue];
  496. console.log(tabIndexes);
  497. if (tabIndexes) {
  498. // 处理上方的 <li>,限定在 .nav-tabs 内
  499. $('.nav-tabs .nav-item').each(function(index) {
  500. if (index > 0) { // 跳过第一个固定元素
  501. $(this).hide();
  502. }
  503. });
  504. $('.nav-tabs .nav-item').each(function() {
  505. const navItem = $(this);
  506. const navLink = navItem.find('.nav-link');
  507. // 获取清理后的导航文本(包含处理空格和特殊字符)
  508. const tabText = navLink.clone().children().remove().end().text().trim();
  509. // 逻辑判断与显示控制
  510. if (tabIndexes.includes(tabText)) {
  511. navItem.show(); // 显式设置显示
  512. }
  513. });
  514. }
  515. }
  516. JS
  517. );
  518. }
  519. }