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

133 lines
3.9 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Renderable\SelectAgentProduct;
  4. use App\AdminAgent\Repositories\Special;
  5. use App\Models\AgentProduct;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Dcat\Admin\Widgets\Table;
  12. use Illuminate\Support\Facades\Storage;
  13. class SpecialController extends AdminController
  14. {
  15. /**
  16. * Make a grid builder.
  17. *
  18. * @return Grid
  19. */
  20. protected function grid()
  21. {
  22. return Grid::make(new Special(), function (Grid $grid) {
  23. $grid->disableFilterButton();
  24. $grid->model()->where('agent_id', Admin::user()->id);
  25. $grid->column('id')->sortable();
  26. $grid->column('picture_ad')->image('', 60, 60);
  27. $grid->column('agent_product_id', '专题产品')
  28. ->display('查看')
  29. ->modal(function ($modal) use ($grid) {
  30. $data = AgentProduct::with('product:id,title,pictures')
  31. ->whereIn('id', $this->agent_product_id)
  32. ->get(['id', 'product_id', 'sale', 'stock']);
  33. $result = [];
  34. $prefix = Storage::disk('public')->url('');
  35. foreach ($data as $k => $v) {
  36. $result[] = [
  37. $v->id,
  38. $v->product->title,
  39. '<img data-action="preview-img" src="'.$prefix.$v->product->picture.'" style="max-width:60px;max-height:60px;cursor:pointer" class="img img-thumbnail">',
  40. $v->sale,
  41. $v->stock,
  42. ];
  43. }
  44. return Table::make(['产品ID', '标题', '图片', '销量', '库存'], $result);
  45. });
  46. $grid->column('sort');
  47. $grid->column('created_at');
  48. $grid->column('updated_at')->sortable();
  49. $grid->filter(function (Grid\Filter $filter) {
  50. $filter->panel();
  51. $filter->equal('id');
  52. });
  53. });
  54. }
  55. /**
  56. * Make a show builder.
  57. *
  58. * @param mixed $id
  59. *
  60. * @return Show
  61. */
  62. protected function detail($id)
  63. {
  64. return Show::make($id, new Special(), function (Show $show) {
  65. $show->field('id');
  66. $show->field('picture_ad')->image('', 80, 80);
  67. $show->field('picture')->image('', 80, 80);
  68. $show->field('agent_product_id', '产品')
  69. ->unescape()
  70. ->as(function ($v) {
  71. $data = AgentProduct::with('product:id,title')
  72. ->whereIn('id', $v)
  73. ->orderBy('id')->get(['id', 'product_id']);
  74. return join("<br>", $data->map(fn($v) => $v->product->title)->toArray());
  75. });
  76. $show->field('sort');
  77. $show->field('created_at');
  78. $show->field('updated_at');
  79. });
  80. }
  81. /**
  82. * Make a form builder.
  83. *
  84. * @return Form
  85. */
  86. protected function form()
  87. {
  88. return Form::make(new Special(), function (Form $form) {
  89. $form->display('id');
  90. $form->image('picture_ad')
  91. ->required()->removable(false)->uniqueName()
  92. ->help('图片大小:750*230');
  93. $form->multipleImage('picture')
  94. ->required()->removable(false)->uniqueName()
  95. ->help('图片大小:750*490');
  96. $form->multipleSelectTable('agent_product_id')
  97. ->title('选择在售产品')->required()
  98. ->dialogWidth('80%;min-width:825px;')
  99. ->from(SelectAgentProduct::make())
  100. ->options(function ($v) {
  101. if (!$v) return [];
  102. $agent_product = AgentProduct::with('product:id,title')
  103. ->select(['id', 'product_id'])
  104. ->whereIn('id', $v)
  105. ->orderBy('id')->get();
  106. $result = [];
  107. foreach ($agent_product as $v) {
  108. $result[$v->id] = $v->product->title ?? '';
  109. }
  110. return $result;
  111. })
  112. ->pluck('product.title')
  113. ->value(join(',', $form->model()->agent_product_id ?? []));
  114. $form->text('sort')->default(255);
  115. })->saving(function (Form $form) {
  116. //处理特殊字段
  117. $form->hidden(['agent_id', 'created_at', 'updated_at']);
  118. $form->agent_id = Admin::user()->id;
  119. //不允许编辑的字段
  120. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at']);
  121. });
  122. }
  123. }