ImportProductController.php 14 KB

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