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

130 lines
4.0 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Repositories\WaterfallAd;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. class WaterfallAdController extends AdminController
  10. {
  11. /**
  12. * Make a grid builder.
  13. *
  14. * @return Grid
  15. */
  16. protected function grid()
  17. {
  18. return Grid::make(new WaterfallAd('agentProduct.product:id,title'), function (Grid $grid) {
  19. $grid->model()->where('agent_id', Admin::user()->id)->orderBy('sort')->orderBy('id', 'DESC');
  20. $grid->column('id')->sortable();
  21. $grid->column('title');
  22. $grid->column('picture')->image('', 60, 60);
  23. $grid->column('type')->using(['链接内部页面', '链接网址']);
  24. $grid->column('url');
  25. $grid->column('status')->switch();
  26. $grid->column('sort')->editable()->sortable()->width(120);
  27. $grid->filter(function (Grid\Filter $filter) {
  28. $filter->panel();
  29. $filter->equal('id')->width(2);
  30. $filter->like('title')->width(3);
  31. });
  32. });
  33. }
  34. /**
  35. * Make a show builder.
  36. *
  37. * @param mixed $id
  38. *
  39. * @return Show
  40. */
  41. protected function detail($id)
  42. {
  43. return Show::make($id, new WaterfallAd('agentProduct.product:id,title'), function (Show $show) {
  44. //不允许查看非自己的数据
  45. if ($show->model()->agent_id != Admin::user()->id) {
  46. Admin::exit('数据不存在');
  47. }
  48. $show->field('id');
  49. $show->field('title');
  50. $show->field('picture')->image('', 80, 80);
  51. $show->field('type')->using(['链接内部页面', '链接网址']);
  52. $show->field('url');
  53. $show->field('status')->using(['禁用', '启用']);
  54. $show->field('sort');
  55. $show->field('created_at');
  56. $show->field('updated_at');
  57. });
  58. }
  59. /**
  60. * Make a form builder.
  61. *
  62. * @return Form
  63. */
  64. protected function form()
  65. {
  66. return Form::make(new WaterfallAd(), function (Form $form) {
  67. //不允许查看非自己的数据
  68. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  69. return $form->response()->error('数据不存在');
  70. }
  71. $form->display('id');
  72. $form->text('title')->required();
  73. $form->image('picture')
  74. ->required()->removable(false)->uniqueName();
  75. $form->radio('type', '链接类型')
  76. ->options(['链接到内部页面', '链接到网址'])
  77. ->value(0)->default(0)
  78. ->when(0, function (Form $form) {
  79. $form->text('url-0', '内部页面地址')
  80. ->customFormat(fn() => $this->type == 0 ? $this->url : '')
  81. ->help('格式如下:<br>产品详情页:/pages/goodsDetail/index?goods_id=产品ID
  82. <br>文章详情页:/pages/notice/article?article_id=文章ID
  83. <br>公告详情页:/pages/notice/notice?notice_id=公告ID');
  84. })
  85. ->when(1, function (Form $form) {
  86. $form->url('url-1', '链接到网址')
  87. ->placeholder('如:https://www.baidu.com/')
  88. ->customFormat(fn() => $this->type == 1 ? $this->url : '');
  89. });
  90. $form->text('sort')->default(255)->required();
  91. })->saving(function (Form $form) {
  92. //不允许修改非自己的数据
  93. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  94. return $form->response()->error('数据不存在');
  95. }
  96. //将null字段设置为空值,防止插入数据库出错
  97. foreach ($form->input() as $k => $v) {
  98. if (is_null($v)) {
  99. $form->$k = '';
  100. }
  101. }
  102. //处理特殊字段
  103. $form->hidden(['agent_id', 'url']);
  104. $form->agent_id = Admin::user()->id;
  105. $form->status = $form->status ? 1 : 0;
  106. $form->url = $form->{'url-' . $form->type};
  107. $form->deleteInput(['url-0', 'url-1']);
  108. //不允许编辑的字段
  109. $form->ignore(['id', 'created_at', 'updated_at']);
  110. })->deleting(function (Form $form) {
  111. //不允许删除非自己的数据
  112. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  113. return $form->response()->error('数据不存在');
  114. }
  115. });
  116. }
  117. }