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

143 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::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('url', '专题链接')
  45. ->display(fn() => '/pages/activityList/index?special_id=' . $this->id)->copyable();
  46. $grid->column('created_at');
  47. $grid->column('updated_at')->sortable();
  48. $grid->filter(function (Grid\Filter $filter) {
  49. $filter->panel();
  50. $filter->equal('id')->width(2);
  51. });
  52. });
  53. }
  54. /**
  55. * Make a show builder.
  56. *
  57. * @param mixed $id
  58. *
  59. * @return Show
  60. */
  61. protected function detail($id)
  62. {
  63. return Show::make($id, new Special(), function (Show $show) {
  64. //不允许查看非自己的数据
  65. if ($show->model()->agent_id != Admin::user()->id) {
  66. Admin::exit('数据不存在');
  67. }
  68. $show->field('id');
  69. $show->field('picture')->image('', 80, 80);
  70. $show->field('agent_product_id', '产品')
  71. ->unescape()
  72. ->as(function ($v) {
  73. $data = AgentProduct::whereIn('id', $v)
  74. ->orderBy('id')->get(['id', 'product_id']);
  75. return join("<br>", $data->map(fn($v) => $v->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')->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::select(['id', 'product_id', 'title'])
  103. ->whereIn('id', $v)
  104. ->orderBy('id')->get();
  105. $result = [];
  106. foreach ($agent_product as $v) {
  107. $result[$v->id] = $v->title ?? '';
  108. }
  109. return $result;
  110. })
  111. ->pluck('title', 'id')
  112. ->value(join(',', $form->model()->agent_product_id ?? []));
  113. })->saving(function (Form $form) {
  114. //不允许修改非自己的数据
  115. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  116. return $form->response()->error('数据不存在');
  117. }
  118. //处理特殊字段
  119. $form->hidden(['agent_id', 'created_at', 'updated_at']);
  120. $form->agent_id = Admin::user()->id;
  121. //不允许编辑的字段
  122. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at']);
  123. })->deleting(function (Form $form) {
  124. //不允许删除非自己的数据
  125. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  126. return $form->response()->error('数据不存在');
  127. }
  128. });
  129. }
  130. }