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

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