海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Extensions\Grid\AuditProduct;
  4. use App\Admin\Repositories\Product;
  5. use App\Common\ProductStatus;
  6. use App\Common\UserStatus;
  7. use App\Models\Category;
  8. use App\Models\Supplier;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Illuminate\Support\Facades\Route;
  14. class ProductController extends AdminController
  15. {
  16. /**
  17. * Make a grid builder.
  18. *
  19. * @return Grid
  20. */
  21. protected function grid()
  22. {
  23. return Grid::make(new Product(['supplier:id,name', 'category:id,name']), function (Grid $grid) {
  24. $grid->disableCreateButton();
  25. //如果是审核页面,多加where条件判断
  26. if (strpos(Route::current()->uri, 'audit')) {
  27. $grid->model()->where('status', ProductStatus::UNAUDITED);
  28. }
  29. $grid->column('id')->sortable();
  30. $grid->column('category.name', '分类');
  31. $grid->column('picture')->image(60, 60);
  32. $grid->column('title');
  33. $grid->column('original_price');
  34. $grid->column('price');
  35. $grid->column('sale');
  36. $grid->column('stock');
  37. $grid->column('supplier.name', '供应商');
  38. $grid->column('status')
  39. ->if(fn() => $this->status == ProductStatus::UNAUDITED)
  40. ->display('')
  41. ->then(function ($column) {
  42. $column->append((new AuditProduct(null, 1))->setKey($this->id))->append('&nbsp;');
  43. $column->append((new AuditProduct(null, 2))->setKey($this->id));
  44. })
  45. ->else()
  46. ->using(ProductStatus::array())
  47. ->dot([
  48. ProductStatus::ON_SALE => 'success',
  49. ProductStatus::UNAUDITED => '',
  50. ProductStatus::REFUSE => 'danger',
  51. ProductStatus::SOLD_OUT => 'warning',
  52. ], 'primary');
  53. $grid->column('created_at');
  54. $grid->column('updated_at');
  55. $grid->filter(function (Grid\Filter $filter) {
  56. $filter->panel();
  57. $filter->equal('id')->width(2);
  58. $filter->like('title')->width(3);
  59. $filter->equal('status')->select(ProductStatus::array())->width(2);
  60. });
  61. });
  62. }
  63. /**
  64. * Make a show builder.
  65. *
  66. * @param mixed $id
  67. *
  68. * @return Show
  69. */
  70. protected function detail($id)
  71. {
  72. return Show::make($id, new Product(['supplier:id,name', 'category:id,name']), function (Show $show) {
  73. $show->field('id');
  74. $show->field('category.name', '所属分类');
  75. $show->field('title');
  76. $show->field('pictures')->image(80, 80);
  77. $show->field('original_price');
  78. $show->field('price');
  79. $show->field('sale');
  80. $show->field('stock');
  81. $show->field('status')->using(ProductStatus::array());
  82. $show->field('supplier.name', '供应商');
  83. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  84. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  85. $show->field('created_at');
  86. $show->field('updated_at');
  87. });
  88. }
  89. /**
  90. * Make a form builder.
  91. *
  92. * @return Form
  93. */
  94. protected function form()
  95. {
  96. return Form::make(new Product(), function (Form $form) {
  97. $form->display('id');
  98. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  99. $form->select('category_id', '所属分类')
  100. ->options(array_slice($options, 1))
  101. ->required();
  102. $form->text('title')->required();
  103. $form->multipleImage('pictures')->required();
  104. $form->text('original_price')->required();
  105. $form->text('price')->required();
  106. $form->text('sale')->default(0);
  107. $form->text('stock')->default(9999)->required();
  108. $form->select('status')
  109. ->options(ProductStatus::array())
  110. ->default(ProductStatus::ON_SALE)
  111. ->required();
  112. $form->select('supplier_id', '供应商')
  113. ->options(Supplier::where('status', UserStatus::NORMAL)->pluck('name', 'id'))
  114. ->required();
  115. $form->editor('know');
  116. $form->editor('content');
  117. })->saving(function (Form $form) {
  118. //不允许编辑的字段
  119. if ($form->isEditing()) {
  120. $form->ignore(['id', 'created_at', 'updated_at', 'deleted_at']);
  121. }
  122. //特殊字段处理
  123. $form->sale = $form->sale ?? 0;
  124. //过滤null字段
  125. foreach ($form->input() as $k => $v) {
  126. if (is_null($v)) {
  127. $form->$k = '';
  128. }
  129. }
  130. });
  131. }
  132. }