DistProductController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use Dcat\Admin\Layout\Content;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Http\Controllers\AdminController;
  8. use App\Distributor\Repositories\DistProduct;
  9. use App\Distributor\Repositories\DistProductCategory;
  10. use App\Distributor\Repositories\DistProductImage;
  11. use Illuminate\Http\Request;
  12. use App\Libraries\CommonHelper;
  13. class DistProductController extends AdminController
  14. {
  15. /**
  16. * page index
  17. */
  18. public function index(Content $content)
  19. {
  20. return $content
  21. ->view('distributor.layouts.content')
  22. ->header(admin_trans( 'admin.products_list'))
  23. ->description(admin_trans('admin.all'))
  24. ->description('all')
  25. ->breadcrumb(['text'=>'Product Management','url'=>''])
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Edit interface.
  30. *
  31. * @param mixed $id
  32. * @param Content $content
  33. * @return Content
  34. */
  35. public function edit($id, Content $content)
  36. {
  37. return $content
  38. ->view('distributor.layouts.content')
  39. ->translation($this->translation())
  40. ->title($this->title())
  41. ->description($this->description()['edit'] ?? trans('admin.edit'))
  42. ->body($this->form()->edit($id));
  43. }
  44. /**
  45. * Create interface.
  46. *
  47. * @param Content $content
  48. * @return Content
  49. */
  50. public function create(Content $content)
  51. {
  52. return $content
  53. ->view('distributor.layouts.content')
  54. ->translation($this->translation())
  55. ->title($this->title())
  56. ->description($this->description()['create'] ?? trans('admin.create'))
  57. ->body($this->form());
  58. }
  59. /**
  60. * Make a grid builder.
  61. *
  62. * @return Grid
  63. */
  64. protected function grid()
  65. {
  66. return Grid::make(DistProduct::with(['distProductCategory','images']), function (Grid $grid) {
  67. $grid->column('id','ID')->sortable();
  68. $grid->column('title');
  69. $grid->column('sku');
  70. $grid->column('dist_product_category.name',admin_trans_label('category_name'));
  71. $grid->column('issuance_date');
  72. $grid->column('images')->display(function ($images) {
  73. $images = $images->toArray();
  74. $dataImages = array_column($images, 'image_url');
  75. return CommonHelper::displayImage($dataImages,150);
  76. });
  77. $grid->column('order')->orderable();
  78. $grid->column('is_pinned')->switch();
  79. $grid->column('enabled')->switch();
  80. $grid->column('created_at');
  81. $grid->column('updated_at')->sortable();
  82. // 筛选
  83. $grid->filter(function (Grid\Filter $filter) {
  84. $filter->equal('sku');
  85. $filter->like('title');
  86. $filter->equal('category_id','Category')->select(DistProductCategory::selectOptions());
  87. $filter->equal('enabled', 'enabled')->select(config('dictionary.enabled'));
  88. });
  89. //排序
  90. $grid->model()->orderBy("is_pinned",'desc')->orderBy("order",'desc');
  91. });
  92. }
  93. /**
  94. * Make a show builder.
  95. *
  96. * @param mixed $id
  97. *
  98. * @return Show
  99. */
  100. protected function detail($id)
  101. {
  102. return Show::make($id, DistProduct::with(['distProductCategory','images']), function (Show $show) {
  103. $show->field('id');
  104. $show->field('title');
  105. $show->field('keywords');
  106. $show->field('description');
  107. $show->field('sku');
  108. $show->field('dist_product_category.name','Category Name');
  109. $show->field('issuance_date');
  110. $show->field('parameters')->as(function ($items) {
  111. if (is_array($items)) {
  112. // 创建表格的表头
  113. $table = '<table class="table">';
  114. $table .= '<tr><th>key</th><th>value</th></tr>';
  115. // 遍历数组并将数据填充到表格中
  116. foreach ($items as $item) {
  117. $table .= '<tr>';
  118. $table .= '<td>' . $item['key'] . '</td>'; // 商品名称
  119. $table .= '<td>' . $item['value'] . '</td>'; // 数量
  120. $table .= '</tr>';
  121. }
  122. $table .= '</table>';
  123. return $table;
  124. }
  125. return ''; // 当没有数组数据时
  126. })->unescape();
  127. $show->field('images')->as(function ($images) {
  128. // 开始生成 HTML
  129. $dataImages = array_column($images, 'image_url');
  130. return CommonHelper::displayImage($dataImages,150);
  131. })->unescape();
  132. $show->field('content');
  133. $show->field('created_at');
  134. $show->field('updated_at');
  135. $show->field('order');
  136. $show->field('enabled')->using(config('dictionary.enabled'));
  137. });
  138. }
  139. /**
  140. * Make a form builder.
  141. *
  142. * @return Form
  143. */
  144. protected function form()
  145. {
  146. return Form::make(DistProduct::with('images'), function (Form $form) {
  147. $form->display('id');
  148. $form->select('category_id', 'Category Name')
  149. ->options(BaseProductCategory::selectOptions())
  150. ->required();
  151. $form->text('title')->required();
  152. $form->text('keywords');
  153. $form->textarea('description');
  154. $form->text('sku')->required();
  155. $form->date('issuance_date');
  156. $form->table('parameters','Parameters', function (Form\NestedForm $table) {
  157. $table->text('key')->required();
  158. $table->text('value')->required();
  159. });
  160. // 多图上传
  161. $form->multipleImage('images', 'images')
  162. ->sortable() // 可拖动排序
  163. ->removable() // 可移除图片
  164. ->autoUpload() // 自动上传
  165. ->uniqueName()
  166. ->accept(config('distributor.upload.oss_image.accept'))
  167. ->maxSize(config('distributor.upload.oss_image.max_size'))
  168. ->dir('dist_images/product/'.date("Ymd"))
  169. ->customFormat(function () {
  170. // 数据格式化为数组['1.jpg','2.jpg'] 编辑时用到
  171. return array_column($this->images, 'image_url');
  172. })
  173. ->saving(function ($images) {
  174. return array_map(function($image) {
  175. return ['image_url' => $image];
  176. }, $images);
  177. });
  178. $form->editor('content');
  179. $form->switch('is_pinned')->default(0);
  180. $form->switch('enabled')->default(1);
  181. //插入JS
  182. $this->addParametersJs();
  183. });
  184. }
  185. /*
  186. * 以json型式返回产品参数
  187. */
  188. public static function parameter(Request $request)
  189. {
  190. $id = $request->query('q');
  191. $content = BaseProductCategory::getParameter($id);
  192. return $content;
  193. }
  194. /**
  195. * 分类与参数联动JS
  196. * @return void
  197. */
  198. private function addParametersJs()
  199. {
  200. $prefix = config('admin.route.prefix');
  201. //插入JS
  202. Admin::script(
  203. <<<JS
  204. JS
  205. );
  206. }
  207. }