HotwordController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Hotword;
  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 HotwordController 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 Hotword);
  76. $grid->model()->orderBy('list_order', 'DESC')->orderBy('w_hot', 'DESC');
  77. $grid->id('ID');
  78. $grid->w_word('关键词')->width(150);
  79. $grid->type('搜索类型')->width(150)->display(function () {
  80. if ($this->type == '2') {
  81. return '企业搜索';
  82. } elseif ($this->type == '3') {
  83. return '个人简历搜索';
  84. } elseif ($this->type == '1') {
  85. return '职位搜索';
  86. } else {
  87. return '资讯搜索';
  88. }
  89. });
  90. $grid->w_hot('搜索次数')->width(200);
  91. $grid->list_order('显示顺序')->editable()->width(150);
  92. $grid->created_at('添加时间');
  93. $grid->updated_at('更新时间');
  94. $grid->actions(function ($actions) use ($grid) {
  95. if (Admin::user()->can('system_hotword_edit')) {
  96. $actions->disableEdit(false);
  97. }
  98. if (Admin::user()->can('system_hotword_delete')) {
  99. $actions->disableDelete(false);
  100. }
  101. $actions->disableView();
  102. });
  103. $grid->filter(function ($filter) {
  104. $type_option = ['' => '不限', '1' => '职位搜索', '2' => '企业搜索', '3' => '个人简历搜索', '4' => '资讯搜索'];
  105. $filter->equal('type', '类型')->select($type_option)->default('');
  106. });
  107. if (Admin::user()->can('system_hotword_delete')) {
  108. $grid->tools(function ($tools) {
  109. $tools->batch(function ($batch) {
  110. $batch->disableDelete(false);
  111. });
  112. });
  113. $grid->disableRowSelector(false);
  114. }
  115. if (Admin::user()->can('system_hotword_create')) {
  116. $grid->disableCreateButton(false);
  117. }
  118. return $grid;
  119. }
  120. /**
  121. * Make a show builder.
  122. *
  123. * @param mixed $id
  124. * @return Show
  125. */
  126. protected function detail($id)
  127. {
  128. $show = new Show(Hotword::findOrFail($id));
  129. $show->id('ID');
  130. $show->w_word('关键词');
  131. $show->type('搜索类型')->as(function ($type) {
  132. if ($type == 2) {
  133. return '企业搜索';
  134. } elseif ($type == 3) {
  135. return '个人简历搜索';
  136. } elseif ($type == 1) {
  137. return '职位搜索';
  138. } else {
  139. return '资讯搜索';
  140. }
  141. });
  142. $show->w_hot('搜索次数');
  143. $show->list_order('显示顺序');
  144. return $show;
  145. }
  146. /**
  147. * Make a form builder.
  148. *
  149. * @return Form
  150. */
  151. protected function form()
  152. {
  153. $form = new Form(new Hotword);
  154. $form->display('ID');
  155. $form->display('Created at');
  156. $form->display('Updated at');
  157. return $form;
  158. }
  159. /**
  160. * Make a form builder.
  161. *
  162. * @return Form
  163. */
  164. protected function editForm()
  165. {
  166. $form = new Form(new Hotword);
  167. $form->text('w_word', '关键词')->rules([
  168. 'required',
  169. ])->setWidth(3)->setMustMark();
  170. //搜索类型
  171. $type_arr = ['1' => '职位搜索', '2' => '企业搜索', '3' => '个人简历搜索', '4' => '资讯搜索'];
  172. $form->select('type', '搜索类型')
  173. ->options($type_arr)
  174. ->rules('required', ['required' => '请选择搜索类型。'])
  175. ->setWidth(3)
  176. ->setMustMark();
  177. $form->number('w_hot', '搜索次数')->min(0)->default(0);
  178. $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
  179. return $form;
  180. }
  181. protected function createForm()
  182. {
  183. $form = new Form(new Hotword);
  184. $form->text('w_word', '关键词')->rules([
  185. 'required',
  186. ])->setWidth(3)->setMustMark();
  187. //搜索类型
  188. $type_arr = ['1' => '职位搜索', '2' => '企业搜索', '3' => '个人简历搜索', '4' => '资讯搜索'];
  189. $form->select('type', '搜索类型')
  190. ->options($type_arr)
  191. ->default(key($type_arr))
  192. ->rules('required', ['required' => '请选择搜索类型。'])
  193. ->setWidth(3)
  194. ->setMustMark();
  195. $form->number('w_hot', '搜索次数')->min(0)->default(0);
  196. $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
  197. return $form;
  198. }
  199. /**
  200. * Store a newly created resource in storage.
  201. *
  202. * @return mixed
  203. */
  204. public function store()
  205. {
  206. return $this->createForm()->store();
  207. }
  208. /**
  209. * Update the specified resource in storage.
  210. *
  211. * @param int $id
  212. *
  213. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  214. */
  215. public function update($id)
  216. {
  217. return $this->editForm()->update($id);
  218. }
  219. }