海南旅游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.

142 lines
4.2 KiB

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