ImportProductController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Distributor\Actions\Extensions\DistProductImportForm;
  4. use App\Distributor\Repositories\RpcAlbum;
  5. use App\Distributor\Repositories\RpcAlbumFolder;
  6. use App\Distributor\Repositories\BaseProduct;
  7. use App\Libraries\CommonHelper;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Layout\Content;
  11. class ImportProductController extends AdminDistController
  12. {
  13. /**
  14. * page index
  15. */
  16. public function index(Content $content)
  17. {
  18. return $content
  19. ->header(admin_trans( 'admin.product_import'))
  20. ->description('<span style="color: red; font-weight: bold;">'.admin_trans_label('select_products_to_import').'</span>')
  21. ->breadcrumb(['text'=>'list','url'=>''])
  22. ->body($this->grid());
  23. }
  24. //屏蔽删除
  25. public function destroy($id)
  26. {
  27. abort(404);
  28. }
  29. //屏蔽创建
  30. public function create(Content $content)
  31. {
  32. abort(404);
  33. }
  34. //屏蔽编辑
  35. public function edit($id, Content $content)
  36. {
  37. abort(404);
  38. }
  39. /**
  40. * Make a grid builder.
  41. *
  42. * @return Grid
  43. */
  44. protected function grid()
  45. {
  46. return Grid::make(new RpcAlbum(), function (Grid $grid) {
  47. $lang = config('app.locale');//当前语言
  48. $grid->view('admin.grid.table');
  49. $grid->column('id')->display(function () {
  50. return $this->_index+1;
  51. })->width('8%');
  52. $grid->column('cover')->display(function ($images) {
  53. $images = json_decode($images);
  54. // 限制最多显示2个缩略图
  55. $dataImages = array_slice($images, 0, 1);
  56. return CommonHelper::displayImage($dataImages,100,1024,2);
  57. });
  58. if ($lang == 'en') {
  59. $grid->column('title_en');
  60. } else {
  61. $grid->column('title');
  62. }
  63. $grid->column('model');
  64. $grid->column('missing_content')->display(function ($missing_content) {
  65. $missing_content = [];
  66. if ($this->cover == '[]') {$missing_content[] = '主图';}
  67. if ($this->en_detail == '[]') {$missing_content[] = '英文详情';}
  68. if ($this->cn_detail == '[]') {$missing_content[] = '中文详情';}
  69. if ($this->video == '[]') {$missing_content[] = '视频';}
  70. if ($this->poster == '[]') {$missing_content[] = '海报';}
  71. if ($this->cert == '[]') {$missing_content[] = '证书';}
  72. if ($this->pdf == '[]') {$missing_content[] = 'PDF';}
  73. return implode(' / ', $missing_content);
  74. });
  75. $grid->column('created_at')->sortable();
  76. $grid->column('updated_at')->sortable();
  77. // 筛选
  78. $grid->filter(function (Grid\Filter $filter) {
  79. $filter->panel();
  80. $filter->expand();
  81. $filter->equal('model')->width(2);
  82. $lang = config('app.locale');//当前语言
  83. $filter->equal('folder_id',admin_trans_label('product_category'))->select(RpcAlbumFolder::selectOptions($lang))->width(3);
  84. });
  85. // 删除新增按钮
  86. $grid->disableCreateButton();
  87. //$grid->disableViewButton();
  88. $grid->disableEditButton();
  89. $grid->disableDeleteButton();
  90. $grid->disableBatchDelete();
  91. // 添加批量复制操作
  92. $grid->batchActions(function ($batch) {
  93. //$batch->add(new BatchCopy()); 只能2选1
  94. });
  95. $grid->tools([
  96. new DistProductImportForm(),
  97. ]);
  98. $grid->model()->where('enabled',1)->orderBy("order",'desc')->orderBy("created_at",'desc');
  99. });
  100. }
  101. protected function detail($id)
  102. {
  103. return Show::make($id, new \App\Admin\Repositories\RpcAlbum(), function (Show $show) {
  104. $lang = config('app.locale');//当前语言
  105. if ($lang == 'en') {
  106. $show->field('title_en');
  107. } else {
  108. $show->field('title');
  109. }
  110. $show->field('model');
  111. $show->field('parameters',admin_trans_label('attribute'))->as(function ($items) {
  112. $items = json_decode($items);
  113. if (is_array($items)) {
  114. // 创建表格的表头
  115. $table = '<table class="table table-bordered table-condensed">';
  116. // 遍历数组并将数据填充到表格中
  117. foreach ($items as $item) {
  118. $item = (array)$item;
  119. $table .= '<tr>';
  120. $table .= '<td style="vertical-align: middle !important;width: 20%">' . $item['key'] . '</td>'; // 商品名称
  121. $table .= '<td style="vertical-align: middle !important;">' . $item['value'] . '</td>'; // 数量
  122. $table .= '</tr>';
  123. }
  124. $table .= '</table>';
  125. return $table;
  126. }
  127. return ''; // 当没有数组数据时
  128. })->unescape();
  129. $show->field('cover')->as(function ($images) {
  130. $images = json_decode($images);
  131. return CommonHelper::displayImage($images,150,1024,2);
  132. })->unescape();
  133. $show->field('en_detail')->as(function ($images) {
  134. $images = json_decode($images);
  135. $html = '<div style="text-align: center">';
  136. foreach ($images as $key => $image) {
  137. $url = CommonHelper::albumUrl($image);
  138. $html .= '<img src="' . $url . '" style="max-width:90%;margin-bottom:10px">';
  139. }
  140. $html .= '</div>';
  141. return $html;
  142. })->unescape();
  143. $show->field('cn_detail')->as(function ($images) {
  144. $images = json_decode($images);
  145. $html = '<div style="text-align: center">';
  146. foreach ($images as $key => $image) {
  147. $url = CommonHelper::albumUrl($image);
  148. $html .= '<img src="' . $url . '" style="max-width:90%;margin-bottom:10px">';
  149. }
  150. $html .= '</div>';
  151. return $html;
  152. })->unescape();
  153. $show->field('video')->as(function ($items) {
  154. $items = json_decode($items);
  155. return CommonHelper::displayVideo($items,'cover','video_src','150',2);
  156. })->unescape();
  157. $show->field('poster')->as(function ($images) {
  158. $images = json_decode($images);
  159. return CommonHelper::displayImage($images,150,1024,2);
  160. })->unescape();
  161. $show->field('cert')->as(function ($images) {
  162. $images = json_decode($images);
  163. return CommonHelper::displayImage($images,150,1024,2);
  164. })->unescape();
  165. $show->field('cert')->as(function ($images) {
  166. $images = json_decode($images);
  167. return CommonHelper::displayImage($images,150,1024,2);
  168. })->unescape();
  169. $show->field('pdf')->as(function ($items) {
  170. $items = json_decode($items);
  171. if (is_array($items)) {
  172. // 创建表格的表头
  173. $table = '<table class="table table-bordered table-condensed">';
  174. // 遍历数组并将数据填充到表格中
  175. foreach ($items as $item) {
  176. $table .= '<tr>';
  177. $table .= '<td style="vertical-align: middle !important;width: 20%">' . $item->pdf_title . '</td>'; // 商品名称
  178. $table .= '<td style="vertical-align: middle !important;"><a target="_blank" href="' . CommonHelper::albumUrl($item->pdf_src). '">查看</a></td>'; // 数量
  179. $table .= '</tr>';
  180. }
  181. $table .= '</table>';
  182. return $table;
  183. }
  184. return ''; // 当没有数组数据时
  185. })->unescape();
  186. // 禁用操作
  187. $show->disableEditButton();
  188. $show->disableDeleteButton();
  189. });
  190. }
  191. }