DistProductController.php 8.5 KB

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