ImportProductController.php 12 KB

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