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

132 lines
4.1 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. $show->field('id');
  50. $show->field('title');
  51. $show->field('picture')->image('', 80, 80);
  52. $show->field('type')->using(['链接到产品详情', '链接到网址']);
  53. $show->field('url')
  54. ->as(function ($v) {
  55. if($this->type == 0) {
  56. return $this->agentProduct->product->title;
  57. }
  58. return $v;
  59. });
  60. $show->field('status')->using(['禁用', '启用']);
  61. $show->field('sort');
  62. $show->field('created_at');
  63. $show->field('updated_at');
  64. });
  65. }
  66. /**
  67. * Make a form builder.
  68. *
  69. * @return Form
  70. */
  71. protected function form()
  72. {
  73. return Form::make(new Slide(), function (Form $form) {
  74. $form->display('id');
  75. $form->text('title')
  76. ->help('主要用于后台显示,方便管理');
  77. $form->image('picture')
  78. ->required()->removable(false)->uniqueName()
  79. ->help('图片大小:750*360');
  80. $form->select('status')->options(['禁用', '启用'])->default(1)->required();
  81. $form->radio('type', '链接类型')
  82. ->options(['链接到产品详情', '链接到网址'])
  83. ->value(0)->default(0)
  84. ->when(0, function (Form $form) {
  85. $form->selectTable('url-0', '链接到产品')
  86. ->help('请选择要链接到的产品')
  87. ->title('选择在售产品')
  88. ->dialogWidth('80%;min-width:825px;')
  89. ->from(SelectAgentProduct::make(['id' => $form->url]))
  90. ->options(function ($v) {
  91. if (!$v) return [];
  92. $agent_product = AgentProduct::with('product:id,title')
  93. ->select(['id', 'product_id'])
  94. ->firstWhere(['id' => $v]);
  95. return [$agent_product->id => $agent_product->product->title];
  96. })
  97. ->pluck('product.title')
  98. ->value($form->model()->url);
  99. })
  100. ->when(1, function (Form $form) {
  101. $form->url('url-1', '链接到网址')
  102. ->placeholder('如:https://www.baidu.com/')
  103. ->customFormat(fn() => $this->type == 1 ? $this->url : '');
  104. });
  105. $form->text('sort')->default(255)->required();
  106. })->saving(function (Form $form) {
  107. //不允许编辑的字段
  108. $form->ignore(['id']);
  109. foreach ($form->input() as $k => $v) {
  110. if (is_null($v)) {
  111. $form->$k = '';
  112. }
  113. }
  114. //处理特殊字段
  115. $form->hidden(['agent_id', 'url']);
  116. $form->agent_id = Admin::user()->id;
  117. $form->status = $form->status ? 1 : 0;
  118. $form->url = $form->{'url-' . $form->type};
  119. $form->deleteInput(['url-0', 'url-1']);
  120. });
  121. }
  122. }