SiteAlbumController.php 24 KB

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