SiteAlbumController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Admin\Actions\Grid\RpcAlbumImport;
  4. use App\Admin\Forms\RpcAlbumImportForm;
  5. use App\Distributor\Repositories\NullRepository;
  6. use App\Distributor\Repositories\RpcAlbum;
  7. use App\Distributor\Repositories\RpcAlbumFolder;
  8. use App\Distributor\Repositories\DistAdminDistributor;
  9. use App\Libraries\CommonHelper;
  10. use Dcat\Admin\Admin;
  11. use Dcat\Admin\Form;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Http\Controllers\AdminController;
  14. use Dcat\Admin\Layout\Content;
  15. use Dcat\Admin\Show;
  16. class SiteAlbumController extends AdminController
  17. {
  18. public function title()
  19. {
  20. return admin_trans( 'admin.promotional_materials');
  21. }
  22. /**
  23. * page index
  24. */
  25. public function index(Content $content)
  26. {
  27. // return $content
  28. // ->header(admin_trans( 'admin.promotional_materials'))
  29. // ->breadcrumb(['text'=>'list','url'=>''])
  30. // ->body($this->grid());
  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.promotional_materials'))
  37. ->body($this->indexForm());
  38. $html = $html->render();
  39. return $this->filterHtml($html);
  40. }
  41. protected function indexForm()
  42. {
  43. return Form::make(new NullRepository(), function (Form $form) {
  44. $lang = config('app.locale');//当前语言
  45. $letf_block_width = 2;
  46. $right_block_width = 10;
  47. if ($lang == 'en') {
  48. $letf_block_width = 3;
  49. $right_block_width = 9;
  50. }
  51. $lang = config('app.locale');//当前语言
  52. $form->action('/site-album');
  53. $folderTree = RpcAlbumFolder::siteAlbumFolderAllNodes();
  54. //显示左边树形菜单
  55. $form->block($letf_block_width, function (Form\BlockForm $form) use ($lang,$folderTree) {
  56. $type = [
  57. 'default' => [
  58. 'icon' => true,
  59. ],
  60. ];
  61. $plugins = ['types'];
  62. if ($lang == 'en') {
  63. $form->tree()
  64. ->setTitleColumn('title_en')
  65. ->nodes($folderTree)
  66. ->type($type)
  67. ->plugins($plugins)
  68. ->width(12,0);
  69. } else {
  70. $form->tree()
  71. ->setTitleColumn('title')
  72. ->nodes($folderTree)
  73. ->type($type)
  74. ->plugins($plugins)
  75. ->width(12,0);
  76. }
  77. });
  78. //右边相删内容
  79. $form->block($right_block_width, function (Form\BlockForm $form) {
  80. $form->html($this->grid())->width(12);
  81. });
  82. //以下JS代码用于点击文件夹时,自动跳转到相应页面
  83. Admin::script(
  84. <<<JS
  85. // 使用定时器检测容器是否存在
  86. const interval = setInterval(() => {
  87. const containerUl = document.getElementsByClassName('jstree-node');
  88. if (containerUl.length > 0) {
  89. clearInterval(interval); // 找到容器后停止检测
  90. // 以下是原有逻辑(已优化)
  91. const folderId = $('select[name="folder_id"]').data('value'); // 提取 folderId 到外层,避免重复查询[1](@ref)
  92. const anchors = document.querySelectorAll('a.jstree-anchor');
  93. anchors.forEach(anchor => {
  94. const id = anchor.id.split('_')[0];
  95. const href = `/dist/site-album?folder_id=`+id;
  96. // 绑定点击事件(阻止默认行为)
  97. anchor.addEventListener('click', event => {
  98. event.preventDefault();
  99. window.location.href = href;
  100. });
  101. // 高亮当前节点
  102. if (folderId == id) {
  103. anchor.classList.add('jstree-clicked');
  104. }
  105. });
  106. }
  107. }, 100); // 每100ms检测一次
  108. const firstCheckbox = document.querySelector('.vs-checkbox-primary');
  109. // 如果找到元素,则隐藏它
  110. if (firstCheckbox) {
  111. firstCheckbox.style.display = 'none';
  112. }
  113. //清空_previous_
  114. const input = document.querySelector('input[name="_previous_"]');
  115. if (input) {
  116. // 清空其值
  117. input.value = '';
  118. }
  119. JS
  120. );
  121. });
  122. }
  123. /**
  124. * @return void 过滤html 把form去掉,修复form引起的BUG
  125. */
  126. private function filterHtml($html)
  127. {
  128. //删除第一个formID对应的JS代码
  129. preg_match('/<form[^>]*id="([^"]*)"[^>]*>/', $html, $matches);
  130. if (isset($matches[1])) {
  131. $formId = $matches[1]; // 获取 id 的值
  132. // echo "找到的 form id: " . $formId . "\n";
  133. // 2. 根据 id 值,删除对应的 JavaScript 代码
  134. $pattern = '/\$\(\'#' . preg_quote($formId, '/') . '\'\)\.form\(\{.*?\}\);/s';
  135. $html = preg_replace($pattern, '', $html);
  136. }
  137. //把第一个form标签替换成div标签
  138. $html = preg_replace('/<form([^>]*)>(.*?)<\/form>/s', '<div$1>$2</div>', $html, 1);
  139. return $html;
  140. }
  141. //屏蔽删除
  142. public function destroy($id)
  143. {
  144. abort(404);
  145. }
  146. //屏蔽创建
  147. public function create(Content $content)
  148. {
  149. abort(404);
  150. }
  151. //屏蔽编辑
  152. public function edit($id, Content $content)
  153. {
  154. abort(404);
  155. }
  156. /**
  157. * Make a grid builder.
  158. *
  159. * @return Grid
  160. */
  161. protected function grid()
  162. {
  163. //相册分类
  164. $lang = config('app.locale');//当前语言
  165. $rpcAlbumFolder = RpcAlbumFolder::selectOptions($lang);
  166. return Grid::make(new RpcAlbum(), function (Grid $grid) use ($rpcAlbumFolder, $lang) {
  167. $grid->view('admin.grid.table');
  168. $grid->column('id')->display(function () {
  169. return $this->_index+1;
  170. });
  171. $grid->column('cover')->display(function ($images) {
  172. $images = json_decode($images);
  173. // 限制最多显示2个缩略图
  174. $dataImages = array_slice($images, 0, 1);
  175. return CommonHelper::displayImage($dataImages,100,1024,2);
  176. });
  177. if ($lang == 'en') {
  178. $grid->column('title_en')->width('35%');
  179. } else {
  180. $grid->column('title')->width('35%');
  181. }
  182. $grid->column('model')->width('20%');
  183. $grid->column('updated_at')->sortable();
  184. // 筛选
  185. $grid->filter(function (Grid\Filter $filter) use ($rpcAlbumFolder, $lang) {
  186. $filter->panel();
  187. $filter->expand();
  188. //$filter->equal('model')->width(2);
  189. $lang = config('app.locale');//当前语言
  190. if ($lang == 'en') {
  191. $filter->equal('title_en')->width(3);
  192. $filter->equal('folder_id',admin_trans_label('product_category'))->select(RpcAlbumFolder::selectOptions($lang))->width(4);
  193. } else {
  194. $filter->equal('title')->width(3);
  195. $filter->equal('folder_id',admin_trans_label('product_category'))->select(RpcAlbumFolder::selectOptions($lang))->width(3);
  196. }
  197. // $filter->equal('folder_id',admin_trans_label('product_category'))->select($rpcAlbumFolder)->width(3);
  198. });
  199. // 删除新增按钮
  200. $grid->disableCreateButton();
  201. $grid->disableEditButton();
  202. $grid->disableDeleteButton();
  203. $grid->disableBatchDelete();
  204. // 添加批量复制操作
  205. $grid->batchActions(function ($batch) {
  206. //$batch->add(new BatchCopy()); 只能2选1
  207. });
  208. $grid->model()->where('enabled',1)->orderBy("order",'desc')->orderBy("created_at",'desc');
  209. });
  210. }
  211. /**
  212. * Make a show builder.
  213. *
  214. * @param mixed $id
  215. *
  216. * @return Show
  217. */
  218. protected function detail($id)
  219. {
  220. //JS图片下载放大
  221. CommonHelper::viewDownloadEnlarge();
  222. return Show::make($id, new RpcAlbum(), function (Show $show) {
  223. $lang = config('app.locale');//当前语言
  224. //名称
  225. if ($lang == 'en') {
  226. $show->field('title');
  227. } else {
  228. $show->field('title_en');
  229. }
  230. //型号
  231. if ($show->model()->model) {
  232. $show->field('model');
  233. }
  234. //属性
  235. if ($show->model()->parameters) {
  236. $show->field('parameters', admin_trans_label('attribute'))->as(function ($items) {
  237. $items = json_decode($items);
  238. if (is_array($items)) {
  239. // 创建表格的表头
  240. $table = '<table class="table table-bordered table-condensed">';
  241. // 遍历数组并将数据填充到表格中
  242. foreach ($items as $item) {
  243. $item = (array)$item;
  244. $table .= '<tr>';
  245. $table .= '<td style="vertical-align: middle !important;width: 20%">' . $item['key'] . '</td>'; // 商品名称
  246. $table .= '<td style="vertical-align: middle !important;">' . $item['value'] . '</td>'; // 数量
  247. $table .= '</tr>';
  248. }
  249. $table .= '</table>';
  250. return $table;
  251. }
  252. return ''; // 当没有数组数据时
  253. })->unescape();
  254. }
  255. //封面
  256. if (empty($show->model()->cover) == false && $show->model()->cover!= '[]') {
  257. $show->field('cover')->as(function ($images) {
  258. $images = json_decode($images);
  259. return CommonHelper::displayImage($images,150,1024,2,false);
  260. })->unescape();
  261. }
  262. //详情
  263. if (empty($show->model()->en_detail) == false && $show->model()->en_detail!= '[]') {
  264. $show->field('en_detail')->as(function ($images) {
  265. $images = json_decode($images);
  266. $html = '<div style="text-align: left">';
  267. foreach ($images as $key => $image) {
  268. $url = CommonHelper::albumUrl($image);
  269. $html .= '<img src="' . $url . '" style="width:300px;margin-bottom:10px">';
  270. }
  271. $html .= '</div>';
  272. return $html;
  273. })->unescape();
  274. }
  275. //视频
  276. if (empty($show->model()->video) == false && $show->model()->video!= '[]') {
  277. $show->field('video')->as(function ($items) {
  278. $items = json_decode($items);
  279. //倒序
  280. $items = array_reverse($items);
  281. return CommonHelper::displayVideo($items,'cover','video_src','150',2);
  282. })->unescape();
  283. }
  284. //海报
  285. if (empty($show->model()->poster) == false && $show->model()->poster!= '[]') {
  286. $show->field('poster')->as(function ($images) {
  287. $images = json_decode($images);
  288. return CommonHelper::displayImage($images,150,1024,2,false);
  289. })->unescape();
  290. }
  291. //证书
  292. if (empty($show->model()->cert) == false && $show->model()->cert != '[]') {
  293. $show->field('cert')->as(function ($images) {
  294. $images = json_decode($images);
  295. return CommonHelper::displayImage($images,150,1024,2,false);
  296. })->unescape();
  297. }
  298. //PDF
  299. if (empty($show->model()->pdf) == false && $show->model()->pdf != '[]') {
  300. $show->field('pdf')->as(function ($items) use ($lang) {
  301. $items = json_decode($items);
  302. if (is_array($items)) {
  303. //倒序
  304. $items = array_reverse($items);
  305. // 创建表格的表头
  306. $table = '<table class="table table-bordered table-condensed">';
  307. // 遍历数组并将数据填充到表格中
  308. foreach ($items as $item) {
  309. $pdf_title = $item->pdf_title;
  310. if ($lang == 'en') {
  311. $pdf_title = $item->pdf_title_en;
  312. }
  313. $table .= '<tr>';
  314. $table .= '<td style="vertical-align: middle !important;width: 20%">' . $pdf_title . '</td>'; // 商品名称
  315. $table .= '<td style="vertical-align: middle !important;"><a target="_blank" href="' . CommonHelper::albumUrl($item->pdf_src). '">查看</a></td>'; // 数量
  316. $table .= '</tr>';
  317. }
  318. $table .= '</table>';
  319. return $table;
  320. }
  321. return ''; // 当没有数组数据时
  322. })->unescape();
  323. }
  324. // 禁用操作
  325. $show->disableEditButton();
  326. $show->disableDeleteButton();
  327. $show->html(CommonHelper::viewDownloadEnlargeHtml());
  328. });
  329. }
  330. }