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

155 lines
4.9 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Renderable\SelectAgentProduct;
  4. use App\AdminAgent\Renderable\SelectProduct;
  5. use App\AdminAgent\Repositories\Slide;
  6. use App\Models\AgentProduct;
  7. use App\Models\Product;
  8. use Dcat\Admin\Admin;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. class SlideController extends AdminController
  14. {
  15. /**
  16. * Make a grid builder.
  17. *
  18. * @return Grid
  19. */
  20. protected function grid()
  21. {
  22. return Grid::make(new Slide(['agentProduct.product:id,title,pictures']), function (Grid $grid) {
  23. $grid->model()->where('agent_id', Admin::user()->id)->orderBy('sort');
  24. $grid->column('id')->sortable();
  25. $grid->column('title');
  26. $grid->column('picture')->image('', 60, 60);
  27. $grid->column('type')->using(['链接产品', '链接网址']);
  28. $grid->column('url')
  29. ->if(fn($column) => $this->type == 0)
  30. ->then(fn($column) => $column->display($this->agentProduct->product->title ?? ''))
  31. ->else(fn($column) => $column->display($this->url ?? ''));
  32. $grid->column('status')->switch();
  33. $grid->column('sort')->editable()->sortable()->width(120);
  34. $grid->filter(function (Grid\Filter $filter) {
  35. $filter->equal('id');
  36. });
  37. });
  38. }
  39. /**
  40. * Make a show builder.
  41. *
  42. * @param mixed $id
  43. *
  44. * @return Show
  45. */
  46. protected function detail($id)
  47. {
  48. return Show::make($id, new Slide(['agentProduct.product:id,title,pictures']), function (Show $show) {
  49. //不允许查看非自己的数据
  50. if ($show->model()->agent_id != Admin::user()->id) {
  51. Admin::exit('数据不存在');
  52. }
  53. $show->field('id');
  54. $show->field('title');
  55. $show->field('picture')->image('', 80, 80);
  56. $show->field('type')->using(['链接到产品详情', '链接到网址']);
  57. $show->field('url')
  58. ->as(function ($v) {
  59. if($this->type == 0) {
  60. return $this->agentProduct->product->title;
  61. }
  62. return $v;
  63. });
  64. $show->field('status')->using(['禁用', '启用']);
  65. $show->field('sort');
  66. $show->field('created_at');
  67. $show->field('updated_at');
  68. });
  69. }
  70. /**
  71. * Make a form builder.
  72. *
  73. * @return Form
  74. */
  75. protected function form()
  76. {
  77. return Form::make(new Slide(), function (Form $form) {
  78. //不允许查看非自己的数据
  79. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  80. return $form->response()->error('数据不存在');
  81. }
  82. $form->display('id');
  83. $form->text('title')
  84. ->help('主要用于后台显示,方便管理')
  85. ->required();
  86. $form->image('picture')
  87. ->required()->removable(false)->uniqueName()
  88. ->help('图片大小:750*360');
  89. $form->select('status')->options(['禁用', '启用'])->default(1)->required();
  90. $form->radio('type', '链接类型')
  91. ->options(['链接到产品详情', '链接到网址'])
  92. ->value(0)->default(0)
  93. ->when(0, function (Form $form) {
  94. $form->selectTable('url-0', '链接到产品')
  95. ->help('请选择要链接到的产品')
  96. ->title('选择在售产品')
  97. ->dialogWidth('80%;min-width:825px;')
  98. ->from(SelectAgentProduct::make(['id' => $form->url]))
  99. ->options(function ($v) {
  100. if (!$v || $this->type != 0) return [];
  101. $agent_product = AgentProduct::with('product:id,title')
  102. ->select(['id', 'product_id'])
  103. ->firstWhere(['id' => $v]);
  104. if (!$agent_product) return [];
  105. return [$agent_product->id => $agent_product->product->title];
  106. })
  107. ->pluck('product.title')
  108. ->value($form->model()->url);
  109. })
  110. ->when(1, function (Form $form) {
  111. $form->url('url-1', '链接到网址')
  112. ->placeholder('如:https://www.baidu.com/')
  113. ->customFormat(fn() => $this->type == 1 ? $this->url : '');
  114. });
  115. $form->text('sort')->default(255)->required();
  116. })->saving(function (Form $form) {
  117. //不允许修改非自己的数据
  118. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  119. return $form->response()->error('数据不存在');
  120. }
  121. //将null字段设置为空值,防止插入数据库出错
  122. foreach ($form->input() as $k => $v) {
  123. if (is_null($v)) {
  124. $form->$k = '';
  125. }
  126. }
  127. //处理特殊字段
  128. $form->hidden(['agent_id', 'url']);
  129. $form->agent_id = Admin::user()->id;
  130. $form->status = $form->status ? 1 : 0;
  131. $form->url = $form->{'url-' . $form->type};
  132. $form->deleteInput(['url-0', 'url-1']);
  133. //不允许编辑的字段
  134. $form->ignore(['id', 'created_at', 'updated_at']);
  135. })->deleting(function (Form $form) {
  136. //不允许删除非自己的数据
  137. if ($form->model()[0]['agent_id'] != Admin::user()->id) {
  138. return $form->response()->error('数据不存在');
  139. }
  140. });
  141. }
  142. }