ProductHotController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. class ProductHotController extends AdminController
  11. {
  12. const TYPE_normal = 'normal'; // 普通用户
  13. const TYPE_VIP = 'vip'; // VIP/设计师
  14. protected $types = ['normal' => 1, 'vip' => 2];
  15. protected $type = 1;
  16. public function __construct()
  17. {
  18. $route = \request()->route();
  19. $arr = explode("/",$route->uri);
  20. $this->type = $this->types[$arr[2]]??1;
  21. }
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. return Grid::make(new ProductHot(), function (Grid $grid) {
  30. $grid->model()->orderByDesc('sort');
  31. $grid->model()->where('type', $this->type);
  32. $grid->column('id')->sortable();
  33. $grid->column('cover_img')->image('',80);
  34. $grid->column('product_id')->display(function (){
  35. $this->products = $this->product_id;
  36. $names = $this->products->pluck('name')->toArray();
  37. return implode('、',$names);
  38. });
  39. $grid->column('is_opened')->switch();
  40. $grid->column('sort')->editable();
  41. $grid->column('created_at');
  42. // $grid->filter(function (Grid\Filter $filter) {
  43. // $filter->equal('id');
  44. //
  45. // });
  46. $grid->disableViewButton();
  47. });
  48. }
  49. /**
  50. * Make a show builder.
  51. *
  52. * @param mixed $id
  53. *
  54. * @return Show
  55. */
  56. protected function detail($id)
  57. {
  58. return Show::make($id, new ProductHot(), function (Show $show) {
  59. $show->field('id');
  60. $show->field('type');
  61. $show->field('product_id');
  62. $show->field('sort');
  63. $show->field('is_opened');
  64. $show->field('created_at');
  65. $show->field('updated_at');
  66. });
  67. }
  68. /**
  69. * Make a form builder.
  70. *
  71. * @return Form
  72. */
  73. protected function form()
  74. {
  75. return Form::make(new ProductHot(), function (Form $form) {
  76. $form->display('id');
  77. $form->hidden('type')->value($this->type);
  78. $cates = Product::select(['id','name'])->where('is_opened',1)->get()->toArray();
  79. $form->multipleSelect('product_id')
  80. ->options(array_column($cates,'name','id'))
  81. ->required();
  82. $form->image('cover_img','爆款图片(宽高比 1.34:1)')->saveFullUrl()
  83. ->uniqueName()->autoUpload()
  84. ->autoSave(false)
  85. ->removable(false)
  86. ->width(4)
  87. ->required();
  88. $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
  89. $form->number('sort');
  90. $form->disableViewButton();
  91. $form->disableDeleteButton();
  92. $form->disableListButton();
  93. $form->disableEditingCheck();
  94. $form->disableViewCheck();
  95. $form->disableCreatingCheck();
  96. });
  97. }
  98. }