ImportProductController.php 13 KB

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