SiteAlbumController.php 21 KB

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