ImportProductController.php 14 KB

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