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

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