ProductController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Repositories\Product;
  4. use App\Admin\Repositories\ProductType;
  5. use App\Models\Msg;
  6. use App\Models\User;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. class ProductController extends AdminController
  12. {
  13. /**
  14. * Make a grid builder.
  15. *
  16. * @return Grid
  17. */
  18. protected function grid()
  19. {
  20. return Grid::make(new Product(), function (Grid $grid) {
  21. $grid->model()->with('user:id,name,email,member_type')->orderByDesc('id');
  22. $grid->column('id')->sortable();
  23. $grid->column('user.name',admin_trans_field('user_id'));
  24. $grid->column('user.email',admin_trans_field('email'));
  25. $grid->column('user.member_type_text',admin_trans_field('member_type'));
  26. $grid->column('name');
  27. $grid->column('content');
  28. $grid->column('image')->image('',40);
  29. $grid->column('url')->link();
  30. $grid->column('type',admin_trans_field('type'))->display(function (){
  31. $arr = \App\Models\ProductType::query()->whereIn('id',$this->type)->pluck('zh_name');
  32. return $arr;
  33. })->label();
  34. $grid->column('status')->switch();
  35. $grid->column('created_at');
  36. $grid->disableCreateButton();
  37. $grid->filter(function (Grid\Filter $filter) {
  38. $filter->panel();
  39. $filter->like('name')->width(3);
  40. $filter->where('type',function ($query){
  41. $query->whereJsonContains('type',[intval($this->input)]);
  42. })->select(\App\Models\ProductType::selectOptions())->width(4);
  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 Product(), function (Show $show) {
  56. $show->field('id');
  57. $show->field('type');
  58. $show->field('user_id');
  59. $show->field('name');
  60. $show->field('content');
  61. $show->field('image');
  62. $show->field('status');
  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. return Form::make(new Product(), function (Form $form) {
  75. $form->display('id')->width(4);
  76. $form->multipleSelect('type')
  77. ->options(\App\Models\ProductType::selectOptions())
  78. ->saving(function ($value) {
  79. // 转化成json字符串保存到数据库
  80. return json_encode($value);
  81. });
  82. // $form->select('type')->options(\App\Models\ProductType::selectOptions())->required()->width(4);
  83. $form->select('user_id')->width(4)->options(User::query()->where('status',1)->pluck('name','id'))->required();
  84. $form->text('name')->required()->width(4);
  85. $form->textarea('content')->required()->width(4);
  86. $form->image('image')->autoUpload()->uniqueName()->required()->width(4);
  87. $form->url('url')->width(4);
  88. $form->switch('status')->default(1);
  89. $form->saving(function (Form $form) {
  90. if($form->status == "0" && $form->isEditing()){
  91. $id = $form->getKey();
  92. $product = \App\Models\Product::query()->where('id',$id)->first();
  93. $msg = [
  94. 'type' => 4,//下架通知
  95. 'title' => "下架通知",
  96. 'content' => "很抱歉的告诉您,您上传的内容涉及敏感内容,我们已处理下架",
  97. 'user_id' => $product->user_id,
  98. 'to_user_id' => $product->user_id,
  99. ];
  100. Msg::query()->create($msg); // 添加通知消息
  101. }
  102. });
  103. });
  104. }
  105. }