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

153 lines
4.6 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. //不允许查看非自己的数据
  66. if ($show->model()->agent_id != Admin::user()->id) {
  67. Admin::exit('数据不存在');
  68. }
  69. $show->field('id');
  70. $show->field('picture_ad')->image('', 80, 80);
  71. $show->field('picture')->image('', 80, 80);
  72. $show->field('agent_product_id', '产品')
  73. ->unescape()
  74. ->as(function ($v) {
  75. $data = AgentProduct::with('product:id,title')
  76. ->whereIn('id', $v)
  77. ->orderBy('id')->get(['id', 'product_id']);
  78. return join("<br>", $data->map(fn($v) => $v->product->title)->toArray());
  79. });
  80. $show->field('sort');
  81. $show->field('created_at');
  82. $show->field('updated_at');
  83. });
  84. }
  85. /**
  86. * Make a form builder.
  87. *
  88. * @return Form
  89. */
  90. protected function form()
  91. {
  92. return Form::make(new Special(), function (Form $form) {
  93. //不允许查看非自己的数据
  94. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  95. return $form->response()->error('数据不存在');
  96. }
  97. $form->display('id');
  98. $form->image('picture_ad')
  99. ->required()->removable(false)->uniqueName()
  100. ->help('图片大小:750*230');
  101. $form->multipleImage('picture')
  102. ->required()->removable(false)->uniqueName()
  103. ->help('图片大小:750*490');
  104. $form->multipleSelectTable('agent_product_id')
  105. ->title('选择在售产品')->required()
  106. ->dialogWidth('80%;min-width:825px;')
  107. ->from(SelectAgentProduct::make())
  108. ->options(function ($v) {
  109. if (!$v) return [];
  110. $agent_product = AgentProduct::with('product:id,title')
  111. ->select(['id', 'product_id'])
  112. ->whereIn('id', $v)
  113. ->orderBy('id')->get();
  114. $result = [];
  115. foreach ($agent_product as $v) {
  116. $result[$v->id] = $v->product->title ?? '';
  117. }
  118. return $result;
  119. })
  120. ->pluck('product.title')
  121. ->value(join(',', $form->model()->agent_product_id ?? []));
  122. $form->text('sort')->default(255);
  123. })->saving(function (Form $form) {
  124. //不允许修改非自己的数据
  125. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  126. return $form->response()->error('数据不存在');
  127. }
  128. //处理特殊字段
  129. $form->hidden(['agent_id', 'created_at', 'updated_at']);
  130. $form->agent_id = Admin::user()->id;
  131. //不允许编辑的字段
  132. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at']);
  133. })->deleting(function (Form $form) {
  134. //不允许删除非自己的数据
  135. if ($form->model()[0]['agent_id'] != Admin::user()->id) {
  136. return $form->response()->error('数据不存在');
  137. }
  138. });
  139. }
  140. }