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

116 lines
3.5 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Renderable\SelectAgentProduct;
  4. use App\AdminAgent\Repositories\Article;
  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. class ArticleController extends AdminController
  12. {
  13. /**
  14. * Make a grid builder.
  15. *
  16. * @return Grid
  17. */
  18. protected function grid()
  19. {
  20. return Grid::make(new Article(), function (Grid $grid) {
  21. $grid->model()->where('agent_id', Admin::user()->id)
  22. ->orderBy('sort')->orderBy('id', 'desc');
  23. $grid->column('id')->sortable();
  24. $grid->column('author');
  25. $grid->column('title');
  26. $grid->column('image')->image('', 60, 60);
  27. $grid->column('sort')->editable()->width(120)->help('数字超小越靠前');
  28. $grid->column('type')->using(['普通列表', '大图显示']);
  29. $grid->column('created_at');
  30. $grid->column('updated_at')->sortable();
  31. $grid->filter(function (Grid\Filter $filter) {
  32. $filter->panel();
  33. $filter->equal('id')->width(2);
  34. $filter->like('title')->width(3);
  35. });
  36. });
  37. }
  38. /**
  39. * Make a show builder.
  40. *
  41. * @param mixed $id
  42. *
  43. * @return Show
  44. */
  45. protected function detail($id)
  46. {
  47. return Show::make($id, new Article(), function (Show $show) {
  48. //不允许查看非自己的数据
  49. if ($show->model()->agent_id != Admin::user()->id) {
  50. Admin::exit('数据不存在');
  51. }
  52. $show->field('id');
  53. $show->field('author');
  54. $show->field('title');
  55. $show->field('image')->image('', 80, 80);
  56. $show->field('content')->unescape();
  57. $show->field('sort');
  58. $show->field('type')->using(['普通列表', '大图显示']);
  59. $show->field('created_at');
  60. $show->field('updated_at');
  61. });
  62. }
  63. /**
  64. * Make a form builder.
  65. *
  66. * @return Form
  67. */
  68. protected function form()
  69. {
  70. return Form::make(new Article(), function (Form $form) {
  71. //不允许查看非自己的数据
  72. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  73. return $form->response()->error('数据不存在');
  74. }
  75. $form->display('id');
  76. $form->text('author')->default(Admin::user()->name);
  77. $form->text('title')->required();
  78. $form->image('image')->required()->removable(false)->uniqueName();
  79. $form->editor('content')->required();
  80. $form->text('sort')->default(255);
  81. $form->select('type')->options(['普通列表', '大图显示'])->default(0)->required();
  82. $form->multipleSelectTable('agent_product_ids')
  83. ->dialogWidth('80%;min-width:825px;')
  84. ->from(new SelectAgentProduct)
  85. ->model(AgentProduct::class);
  86. })->saving(function (Form $form) {
  87. //不允许修改非自己的数据
  88. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  89. return $form->response()->error('数据不存在');
  90. }
  91. //特殊字段处理
  92. $form->hidden(['agent_id']);
  93. $form->agent_id = Admin::user()->id;
  94. $form->sort = $form->sort ?? 255;
  95. //不允许编辑的字段
  96. $form->ignore(['id', 'created_at', 'updated_at']);
  97. })->deleting(function (Form $form) {
  98. //不允许删除非自己的数据
  99. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  100. return $form->response()->error('数据不存在');
  101. }
  102. });
  103. }
  104. }