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

145 lines
4.3 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('agent_product_id', '专题产品')
  27. ->display('查看')
  28. ->modal(function ($modal) use ($grid) {
  29. $data = AgentProduct::with('product:id,title,pictures')
  30. ->whereIn('id', $this->agent_product_id)
  31. ->get(['id', 'product_id', 'sale', 'stock']);
  32. $result = [];
  33. $prefix = Storage::disk('public')->url('');
  34. foreach ($data as $k => $v) {
  35. $result[] = [
  36. $v->id,
  37. $v->product->title,
  38. '<img data-action="preview-img" src="'.$prefix.$v->product->picture.'" style="max-width:60px;max-height:60px;cursor:pointer" class="img img-thumbnail">',
  39. $v->sale,
  40. $v->stock,
  41. ];
  42. }
  43. return Table::make(['产品ID', '标题', '图片', '销量', '库存'], $result);
  44. });
  45. $grid->column('created_at');
  46. $grid->column('updated_at')->sortable();
  47. $grid->filter(function (Grid\Filter $filter) {
  48. $filter->panel();
  49. $filter->equal('id')->width(2);
  50. });
  51. });
  52. }
  53. /**
  54. * Make a show builder.
  55. *
  56. * @param mixed $id
  57. *
  58. * @return Show
  59. */
  60. protected function detail($id)
  61. {
  62. return Show::make($id, new Special(), function (Show $show) {
  63. //不允许查看非自己的数据
  64. if ($show->model()->agent_id != Admin::user()->id) {
  65. Admin::exit('数据不存在');
  66. }
  67. $show->field('id');
  68. $show->field('picture')->image('', 80, 80);
  69. $show->field('agent_product_id', '产品')
  70. ->unescape()
  71. ->as(function ($v) {
  72. $data = AgentProduct::with('product:id,title')
  73. ->whereIn('id', $v)
  74. ->orderBy('id')->get(['id', 'product_id']);
  75. return join("<br>", $data->map(fn($v) => $v->product->title)->toArray());
  76. });
  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. //不允许查看非自己的数据
  90. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  91. return $form->response()->error('数据不存在');
  92. }
  93. $form->display('id');
  94. $form->multipleImage('picture')
  95. ->required()->removable(false)->uniqueName()
  96. ->help('图片大小:750*490');
  97. $form->multipleSelectTable('agent_product_id')
  98. ->title('选择在售产品')->required()
  99. ->dialogWidth('80%;min-width:825px;')
  100. ->from(SelectAgentProduct::make())
  101. ->options(function ($v) {
  102. if (!$v) return [];
  103. $agent_product = AgentProduct::with('product:id,title')
  104. ->select(['id', 'product_id'])
  105. ->whereIn('id', $v)
  106. ->orderBy('id')->get();
  107. $result = [];
  108. foreach ($agent_product as $v) {
  109. $result[$v->id] = $v->product->title ?? '';
  110. }
  111. return $result;
  112. })
  113. ->pluck('product.title')
  114. ->value(join(',', $form->model()->agent_product_id ?? []));
  115. })->saving(function (Form $form) {
  116. //不允许修改非自己的数据
  117. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  118. return $form->response()->error('数据不存在');
  119. }
  120. //处理特殊字段
  121. $form->hidden(['agent_id', 'created_at', 'updated_at']);
  122. $form->agent_id = Admin::user()->id;
  123. //不允许编辑的字段
  124. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at']);
  125. })->deleting(function (Form $form) {
  126. //不允许删除非自己的数据
  127. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  128. return $form->response()->error('数据不存在');
  129. }
  130. });
  131. }
  132. }