ImportProductController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. $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')->width('30%');
  149. } else {
  150. $grid->column('title')->width('30%');
  151. }
  152. //$grid->column('created_at')->sortable();
  153. $grid->column('updated_at')->sortable();
  154. $grid->column('imported')->display(function ($status) {
  155. $status = DistProduct::isAlbumImport($this->id);
  156. if ($status) {
  157. return '<span class="label label-success" style="background:#5cb85c">'.admin_trans_label('Yes').'</span>';
  158. } else {
  159. return '<span class="label label-default" style="background:#ccc">'.admin_trans_label('No').'</span>';
  160. }
  161. });
  162. // 筛选
  163. $grid->filter(function (Grid\Filter $filter) {
  164. $filter->panel();
  165. $filter->expand();
  166. $lang = config('app.locale');//当前语言
  167. if ($lang == 'en') {
  168. $filter->equal('title_cn')->width(3);
  169. } else {
  170. $filter->equal('title')->width(3);
  171. }
  172. $filter->equal('folder_id',admin_trans_label('product_category'))->select(RpcAlbumFolder::selectOptions($lang))->width(3);
  173. //是否导入
  174. $filter->equal('imported')->select([1 => admin_trans_label('Yes'),0 => admin_trans_label('No')])->width(3);
  175. });
  176. // 删除新增按钮
  177. $grid->disableCreateButton();
  178. //$grid->disableViewButton();
  179. $grid->disableEditButton();
  180. $grid->disableDeleteButton();
  181. $grid->disableBatchDelete();
  182. // 添加批量复制操作
  183. $grid->batchActions(function ($batch) {
  184. //$batch->add(new BatchCopy()); 只能2选1
  185. });
  186. $grid->tools([
  187. new DistProductImportForm(),
  188. ]);
  189. $grid->model()->where('enabled',1)->orderBy("order",'desc')->orderBy("created_at",'desc');
  190. });
  191. }
  192. protected function detail($id)
  193. {
  194. CommonHelper::viewDownloadEnlarge();
  195. return Show::make($id, new \App\Admin\Repositories\RpcAlbum(), function (Show $show) {
  196. $lang = config('app.locale');//当前语言
  197. if ($lang == 'en') {
  198. $show->field('title_en');
  199. } else {
  200. $show->field('title');
  201. }
  202. if ($show->model()->model) {
  203. $show->field('model');
  204. }
  205. if ($show->model()->parameters && $show->model()->parameters != '[]') {
  206. $show->field('parameters',admin_trans_label('attribute'))->as(function ($items) {
  207. $items = json_decode($items);
  208. if (is_array($items)) {
  209. // 创建表格的表头
  210. $table = '<table class="table table-bordered table-condensed">';
  211. // 遍历数组并将数据填充到表格中
  212. foreach ($items as $item) {
  213. $item = (array)$item;
  214. $table .= '<tr>';
  215. $table .= '<td style="vertical-align: middle !important;width: 20%">' . $item['key'] . '</td>'; // 商品名称
  216. $table .= '<td style="vertical-align: middle !important;">' . $item['value'] . '</td>'; // 数量
  217. $table .= '</tr>';
  218. }
  219. $table .= '</table>';
  220. return $table;
  221. }
  222. return ''; // 当没有数组数据时
  223. })->unescape();
  224. }
  225. if ($show->model()->cover && $show->model()->cover != '[]') {
  226. $show->field('cover')->as(function ($images) {
  227. $images = json_decode($images);
  228. return CommonHelper::displayImage($images,200,1024,2,false);
  229. })->unescape();
  230. }
  231. if ($show->model()->en_detail && $show->model()->en_detail != '[]') {
  232. $show->field('en_detail')->as(function ($images) {
  233. $images = json_decode($images);
  234. $html = '<div style="text-align: center">';
  235. foreach ($images as $key => $image) {
  236. $url = CommonHelper::albumUrl($image);
  237. $html .= '<img src="' . $url . '" style="max-width:90%;margin-bottom:10px">';
  238. }
  239. $html .= '</div>';
  240. return $html;
  241. })->unescape();
  242. }
  243. if ($show->model()->cn_detail && $show->model()->cn_detail != '[]') {
  244. $show->field('cn_detail')->as(function ($images) {
  245. $images = json_decode($images);
  246. $html = '<div style="text-align: center">';
  247. foreach ($images as $key => $image) {
  248. $url = CommonHelper::albumUrl($image);
  249. $html .= '<img src="' . $url . '" style="max-width:90%;margin-bottom:10px">';
  250. }
  251. $html .= '</div>';
  252. return $html;
  253. })->unescape();
  254. }
  255. if ($show->model()->video && $show->model()->video != '[]') {
  256. $show->field('video')->as(function ($items) {
  257. $items = json_decode($items);
  258. return CommonHelper::displayVideo($items,'cover','video_src','150',2);
  259. })->unescape();
  260. }
  261. if ($show->model()->poster && $show->model()->poster != '[]') {
  262. $show->field('poster')->as(function ($images) {
  263. $images = json_decode($images);
  264. return CommonHelper::displayImage($images,200,1024,2,false);
  265. })->unescape();
  266. }
  267. if ($show->model()->cert && $show->model()->cert != '[]') {
  268. $show->field('cert')->as(function ($images) {
  269. $images = json_decode($images);
  270. return CommonHelper::displayImage($images,200,1024,2,false);
  271. })->unescape();
  272. }
  273. if ($show->model()->pdf && $show->model()->pdf != '[]') {
  274. $show->field('pdf')->as(function ($items) {
  275. $items = json_decode($items);
  276. if (is_array($items)) {
  277. // 创建表格的表头
  278. $table = '<table class="table table-bordered table-condensed">';
  279. // 遍历数组并将数据填充到表格中
  280. foreach ($items as $item) {
  281. $table .= '<tr>';
  282. $table .= '<td style="vertical-align: middle !important;width: 20%">' . $item->pdf_title . '</td>'; // 商品名称
  283. $table .= '<td style="vertical-align: middle !important;"><a target="_blank" href="' . CommonHelper::albumUrl($item->pdf_src). '">查看</a></td>'; // 数量
  284. $table .= '</tr>';
  285. }
  286. $table .= '</table>';
  287. return $table;
  288. }
  289. return ''; // 当没有数组数据时
  290. })->unescape();
  291. }
  292. // 禁用操作
  293. $show->disableEditButton();
  294. $show->disableDeleteButton();
  295. $show->html(CommonHelper::viewDownloadEnlargeHtml());
  296. });
  297. }
  298. //屏蔽删除
  299. public function destroy($id)
  300. {
  301. abort(404);
  302. }
  303. //屏蔽创建
  304. public function create(Content $content)
  305. {
  306. abort(404);
  307. }
  308. //屏蔽编辑
  309. public function edit($id, Content $content)
  310. {
  311. abort(404);
  312. }
  313. }