ProductHotController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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(ProductHot::with(['product']), 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. return $this->product->name;
  36. });
  37. $grid->column('is_opened')->switch();
  38. $grid->column('sort')->editable();
  39. $grid->column('created_at');
  40. // $grid->filter(function (Grid\Filter $filter) {
  41. // $filter->equal('id');
  42. //
  43. // });
  44. $grid->disableViewButton();
  45. });
  46. }
  47. /**
  48. * Make a show builder.
  49. *
  50. * @param mixed $id
  51. *
  52. * @return Show
  53. */
  54. protected function detail($id)
  55. {
  56. return Show::make($id, new ProductHot(), function (Show $show) {
  57. $show->field('id');
  58. $show->field('type');
  59. $show->field('product_id');
  60. $show->field('sort');
  61. $show->field('is_opened');
  62. $show->field('created_at');
  63. $show->field('updated_at');
  64. });
  65. }
  66. /**
  67. * Make a form builder.
  68. *
  69. * @return Form
  70. */
  71. protected function form()
  72. {
  73. return Form::make(new ProductHot(), function (Form $form) {
  74. $form->display('id');
  75. $form->hidden('type')->value($this->type);
  76. $cates = Product::select(['id','name'])->where('is_opened',1)->get()->toArray();
  77. $form->select('product_id')
  78. ->options(array_column($cates,'name','id'))
  79. ->required();
  80. $form->image('cover_img')->saveFullUrl()
  81. ->uniqueName()->autoUpload()
  82. ->autoSave(false)
  83. ->removable(false)
  84. ->width(4)
  85. ->required();
  86. $form->radio('is_opened')->options(config('global.bool_status'))->default(1);
  87. $form->number('sort');
  88. $form->disableViewButton();
  89. $form->disableDeleteButton();
  90. $form->disableListButton();
  91. $form->disableEditingCheck();
  92. $form->disableViewCheck();
  93. $form->disableCreatingCheck();
  94. });
  95. }
  96. }