ImportController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Import;
  4. use Encore\Admin\Form;
  5. use Encore\Admin\Grid;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Layout\Content;
  8. use App\Http\Controllers\Controller;
  9. use Encore\Admin\Controllers\ModelForm;
  10. use Illuminate\Support\Facades\Artisan;
  11. class ImportController extends Controller
  12. {
  13. use ModelForm;
  14. /**
  15. * Index interface.
  16. *
  17. * @return Content
  18. */
  19. public function index()
  20. {
  21. return Admin::content(function (Content $content) {
  22. $content->header('数据管理');
  23. $content->description('列表');
  24. $content->body($this->grid());
  25. });
  26. }
  27. /**
  28. * Edit interface.
  29. *
  30. * @param $id
  31. * @return Content
  32. */
  33. public function edit($id)
  34. {
  35. return Admin::content(function (Content $content) use ($id) {
  36. $content->header('数据管理');
  37. $content->description('编辑');
  38. $content->body($this->form()->edit($id));
  39. });
  40. }
  41. /**
  42. * Create interface.
  43. *
  44. * @return Content
  45. */
  46. public function create()
  47. {
  48. return Admin::content(function (Content $content) {
  49. $content->header('数据管理');
  50. $content->description('导入');
  51. $content->body($this->form());
  52. });
  53. }
  54. /**
  55. * Make a grid builder.
  56. *
  57. * @return Grid
  58. */
  59. protected function grid()
  60. {
  61. return Admin::grid(Import::class, function (Grid $grid) {
  62. $grid->disableExport();
  63. $grid->id('ID')->sortable();
  64. $grid->name('任务名');
  65. $grid->category('类型')->display(function ($v) {
  66. return collect(Import::$CATEGORY)->where('id', $v)->first()['name'];
  67. });
  68. $grid->is_import('是否导入')->display(function ($v) {
  69. if ($v === 'F') {
  70. return "<span class='label label-default'>未导入</span>";
  71. }
  72. return "<span class='label label-success'>已导入</span>";
  73. });
  74. $grid->created_at();
  75. $grid->updated_at();
  76. });
  77. }
  78. /**
  79. * Make a form builder.
  80. *
  81. * @return Form
  82. */
  83. protected function form()
  84. {
  85. return Admin::form(Import::class, function (Form $form) {
  86. $form->display('id', 'ID');
  87. $form->text('name', '任务名');
  88. $form->select('category', '类型')->options(collect(Import::$CATEGORY)->pluck('name', 'id'));
  89. $form->file('path', '选择文件');
  90. $states = [
  91. 'on' => ['value' => 'T', 'text' => '导入', 'color' => 'success'],
  92. 'off' => ['value' => 'F', 'text' => '不导入', 'color' => 'danger'],
  93. ];
  94. $form->switch('is_import', '是否导入')->states($states);
  95. $form->saved(function (Form $form) {
  96. $id = $form->model()->id;
  97. Artisan::queue('excel:load', ['id' => $id]);
  98. });
  99. });
  100. }
  101. }