ProductHotController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\Product;
  4. use App\Models\ProductCategory;
  5. use App\Models\ProductHot;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dingo\Api\Http\Request;
  11. class ProductHotController extends AdminController
  12. {
  13. const TYPE_normal = 'normal'; // 普通用户
  14. const TYPE_VIP = 'vip'; // VIP/设计师
  15. protected $types = ['normal' => 1, 'vip' => 2];
  16. protected $type = 1;
  17. public function __construct()
  18. {
  19. $route = \request()->route();
  20. $arr = explode("/",$route->uri);
  21. $this->type = $this->types[$arr[2]]??1;
  22. }
  23. /**
  24. * Make a grid builder.
  25. *
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. return Grid::make(new ProductHot(), function (Grid $grid) {
  31. $grid->model()->orderByDesc('sort');
  32. $grid->model()->where('type', $this->type);
  33. $grid->column('id')->sortable();
  34. $grid->column('cover_img')->image('',80);
  35. $grid->column('product_id')->display(function (){
  36. $this->products = $this->product_id;
  37. $names = $this->products->pluck('name')->toArray();
  38. return implode('、',$names);
  39. });
  40. $grid->column('is_opened')->switch();
  41. $grid->column('sort')->editable();
  42. $grid->column('created_at');
  43. // $grid->filter(function (Grid\Filter $filter) {
  44. // $filter->equal('id');
  45. //
  46. // });
  47. $grid->disableViewButton();
  48. });
  49. }
  50. /**
  51. * Make a show builder.
  52. *
  53. * @param mixed $id
  54. *
  55. * @return Show
  56. */
  57. protected function detail($id)
  58. {
  59. return Show::make($id, new ProductHot(), function (Show $show) {
  60. $show->field('id');
  61. $show->field('type');
  62. $show->field('product_id');
  63. $show->field('sort');
  64. $show->field('is_opened');
  65. $show->field('created_at');
  66. $show->field('updated_at');
  67. });
  68. }
  69. /**
  70. * Make a form builder.
  71. *
  72. * @return Form
  73. */
  74. protected function form()
  75. {
  76. return Form::make(new ProductHot(), function (Form $form) {
  77. $form->display('id');
  78. $form->hidden('type')->value($this->type);
  79. $products = Product::with('cate')->select(['id','name','cate_id'])
  80. ->where('is_opened',1)
  81. ->orderBy('cate_id')
  82. ->get();
  83. $options = [];
  84. foreach ($products as $product){
  85. $options[$product->id] = $product->name."({$product->cate->name})";
  86. }
  87. // $cates = ProductCategory::select(['id','name'])->where('is_opened',1)->get();
  88. // $form->select('cate_id','分类')
  89. // ->options($cates->pluck('name','id'))
  90. // ->load('product_id', 'product');
  91. $form->multipleSelect('product_id')
  92. ->options($options)->required();
  93. $form->image('cover_img','爆款图片(宽高比 1.34:1)')->saveFullUrl()
  94. ->uniqueName()->autoUpload()
  95. ->autoSave(false)
  96. ->removable(false)
  97. ->width(4)
  98. ->required();
  99. $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
  100. $form->number('sort');
  101. $form->saving(function (Form $form){
  102. /* $product_ids = [];
  103. foreach ($form->product_id as $product){
  104. $product = array_filter($product);
  105. if($product){
  106. $product_ids = array_merge($product_ids,$product);
  107. }
  108. }*/
  109. if(isset($form->product_id)){
  110. $form->product_id = array_filter($form->product_id );
  111. if(!count($form->product_id)){
  112. return $form->response()->error('请选择产品');
  113. }
  114. $form->deleteInput(['cate_id']);
  115. }
  116. //$form->product_id = array_filter($product_ids);
  117. });
  118. $form->disableViewButton();
  119. $form->disableDeleteButton();
  120. $form->disableListButton();
  121. $form->disableEditingCheck();
  122. $form->disableViewCheck();
  123. $form->disableCreatingCheck();
  124. });
  125. }
  126. }