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

127 lines
3.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\Admin\Extensions\Form\Radio2;
  4. use App\AdminSupplier\Repositories\DiyForm;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Dcat\Admin\Widgets\Table;
  11. class DiyFormController extends AdminController
  12. {
  13. /**
  14. * Make a grid builder.
  15. *
  16. * @return Grid
  17. */
  18. protected function grid()
  19. {
  20. return Grid::make(new DiyForm(['fields']), function (Grid $grid) {
  21. $grid->disableViewButton();
  22. $grid->model()->where('supplier_id', Admin::user()->id)->orderBy('id', 'desc');
  23. $grid->column('id')->sortable();
  24. $grid->column('name');
  25. $grid->column('fields')
  26. ->display('查看')
  27. ->modal('字段列表', function ($modal) {
  28. $fields = array_map(function ($v) {
  29. $field_types = admin_trans('diy-form.options');
  30. return [
  31. $v['field'],
  32. $field_types[$v['type']] ?? '',
  33. $v['required'] ? '必填' : '选填',
  34. $v['sort'],
  35. ];
  36. }, $this->fields->toArray());
  37. return Table::make(['字段名', '字段类型', '是否必填', '排序'], $fields);
  38. });
  39. $grid->column('created_at');
  40. $grid->filter(function (Grid\Filter $filter) {
  41. $filter->equal('id')->width(2);
  42. $filter->like('name')->width(3);
  43. });
  44. });
  45. }
  46. /**
  47. * Make a show builder.
  48. *
  49. * @param mixed $id
  50. *
  51. * @return Show
  52. */
  53. protected function detail($id)
  54. {
  55. return Show::make($id, new DiyForm(['fields']), function (Show $show) {
  56. //不允许查看非自己的数据
  57. if ($show->model()->supplier_id != Admin::user()->id) {
  58. Admin::exit('数据不存在');
  59. }
  60. $show->field('id');
  61. $show->field('name');
  62. $show->field('fields');
  63. $show->field('created_at');
  64. $show->field('updated_at');
  65. });
  66. }
  67. /**
  68. * Make a form builder.
  69. *
  70. * @return Form
  71. */
  72. protected function form()
  73. {
  74. Form::extend('radio2', Radio2::class);
  75. return Form::make(new DiyForm(['fields']), function (Form $form) {
  76. $form->disableViewButton();
  77. //不允许编辑非自己数据
  78. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  79. return $form->response()->error('数据不存在');
  80. }
  81. $form->display('id');
  82. $form->text('name')->required();
  83. $form->hasMany('fields', function (Form\NestedForm $form) {
  84. $form->text('field', '字段名称')->required();
  85. $form->switch('required', '是否必填')->default(1)->required();
  86. $form->radio2('type', '字段类型')
  87. ->required()->default('text')
  88. ->options(admin_trans('diy-form.options'))
  89. ->when(['radio', 'checkbox'], function (Form\NestedForm $form) {
  90. $form->list('options', '选项列表');
  91. });
  92. $form->number('sort', '排序')
  93. ->help('数字越小越靠前')
  94. ->default(255)->required();
  95. });
  96. })->saving(function (Form $form) {
  97. //不允许编辑非自己数据
  98. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  99. return $form->response()->error('数据不存在');
  100. }
  101. if (!$form->fields) {
  102. return $form->response()->error('字段为空,请先新增字段');
  103. }
  104. $form->hidden(['supplier_id']);
  105. $form->supplier_id = Admin::user()->id;
  106. })->deleting(function (Form $form) {
  107. //不允许删除非自己的数据
  108. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  109. return $form->response()->error('数据不存在');
  110. }
  111. });
  112. }
  113. }