BadwordController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Badword;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. class BadwordController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @param Content $content
  18. * @return Content
  19. */
  20. public function index(Content $content)
  21. {
  22. return $content
  23. ->header('关键词过滤列表')
  24. ->description('')
  25. ->body($this->grid());
  26. }
  27. /**
  28. * Show interface.
  29. *
  30. * @param mixed $id
  31. * @param Content $content
  32. * @return Content
  33. */
  34. public function show($id, Content $content)
  35. {
  36. return $content
  37. ->header('关键词过滤详情')
  38. ->description('')
  39. ->body($this->detail($id));
  40. }
  41. /**
  42. * Edit interface.
  43. *
  44. * @param mixed $id
  45. * @param Content $content
  46. * @return Content
  47. */
  48. public function edit($id, Content $content)
  49. {
  50. return $content
  51. ->header('关键词过滤编辑')
  52. ->description('')
  53. ->body($this->editForm()->edit($id));
  54. }
  55. /**
  56. * Create interface.
  57. *
  58. * @param Content $content
  59. * @return Content
  60. */
  61. public function create(Content $content)
  62. {
  63. return $content
  64. ->header('关键词过滤创建')
  65. ->description('')
  66. ->body($this->createForm());
  67. }
  68. /**
  69. * Make a grid builder.
  70. *
  71. * @return Grid
  72. */
  73. protected function grid()
  74. {
  75. $grid = new Grid(new Badword);
  76. $grid->id('ID');
  77. $grid->badword('关键词')->width(150);
  78. $grid->replace('替换词')->width(150);
  79. $states = [
  80. 'on' => ['value' => 1, 'text' => '开启', 'color' => 'success'],
  81. 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
  82. ];
  83. $grid->status('状态')->switch($states);
  84. $grid->created_at('添加时间');
  85. $grid->updated_at('更新时间');
  86. $grid->actions(function ($actions) use ($grid) {
  87. if (Admin::user()->can('system_badword_edit')) {
  88. $actions->disableEdit(false);
  89. }
  90. if (Admin::user()->can('system_badword_delete')) {
  91. $actions->disableDelete(false);
  92. }
  93. });
  94. if (Admin::user()->can('system_badword_delete')) {
  95. $grid->tools(function ($tools) {
  96. $tools->batch(function ($batch) {
  97. $batch->disableDelete(false);
  98. });
  99. });
  100. $grid->disableRowSelector(false);
  101. }
  102. if (Admin::user()->can('system_badword_create')) {
  103. $grid->disableCreateButton(false);
  104. }
  105. return $grid;
  106. }
  107. /**
  108. * Make a show builder.
  109. *
  110. * @param mixed $id
  111. * @return Show
  112. */
  113. protected function detail($id)
  114. {
  115. $show = new Show(Badword::findOrFail($id));
  116. $show->id('ID');
  117. $show->badword('关键词');
  118. $show->replace('替换词');
  119. $show->status('状态')->as(function ($status) {
  120. if ($status==1) {
  121. return '开启';
  122. } else {
  123. return '关闭';
  124. }
  125. });
  126. return $show;
  127. }
  128. /**
  129. * Make a form builder.
  130. *
  131. * @return Form
  132. */
  133. protected function form()
  134. {
  135. $form = new Form(new Badword);
  136. $form->display('ID');
  137. $form->display('Created at');
  138. $form->display('Updated at');
  139. return $form;
  140. }
  141. /**
  142. * Make a form builder.
  143. *
  144. * @return Form
  145. */
  146. protected function editForm()
  147. {
  148. $form = new Form(new Badword);
  149. $form->text('badword', '关键词')->rules([
  150. 'required',
  151. ])->setWidth(3)->setMustMark();
  152. $form->text('replace', '替换词')->rules([
  153. 'required',
  154. ])->setWidth(3)->setMustMark();
  155. $states = [
  156. 'on' => ['value' => 1, 'text' => '开启', 'color' => 'success'],
  157. 'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
  158. ];
  159. $form->switch('status', '状态')->states($states);
  160. $form->saving(function (Form $form) {
  161. if ($form->status=='on') {
  162. $form->status=1;
  163. } elseif ($form->status=='off') {
  164. $form->status=2;
  165. }
  166. });
  167. return $form;
  168. }
  169. protected function createForm()
  170. {
  171. $form = new Form(new Badword);
  172. $form->text('badword', '关键词')->rules([
  173. 'required',
  174. ])->setWidth(3)->setMustMark();
  175. $form->text('replace', '替换词')->rules([
  176. 'required',
  177. ])->setWidth(3)->setMustMark();
  178. $states = [
  179. 'on' => ['value' => 1, 'text' => '开启', 'color' => 'success'],
  180. 'off' => ['value' => 2, 'text' => '关闭', 'color' => 'danger'],
  181. ];
  182. $form->switch('status', '状态')->states($states);
  183. $form->saving(function (Form $form) {
  184. if ($form->status=='on') {
  185. $form->status=1;
  186. } elseif ($form->status=='off') {
  187. $form->status=2;
  188. }
  189. });
  190. return $form;
  191. }
  192. /**
  193. * Store a newly created resource in storage.
  194. *
  195. * @return mixed
  196. */
  197. public function store()
  198. {
  199. return $this->createForm()->store();
  200. }
  201. /**
  202. * Update the specified resource in storage.
  203. *
  204. * @param int $id
  205. *
  206. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  207. */
  208. public function update($id)
  209. {
  210. return $this->editForm()->update($id);
  211. }
  212. }