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

157 lines
5.0 KiB

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