HotwordController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if (Admin::user()->can('system_hotword_delete')) {
  104. $grid->tools(function ($tools) {
  105. $tools->batch(function ($batch) {
  106. $batch->disableDelete(false);
  107. });
  108. });
  109. $grid->disableRowSelector(false);
  110. }
  111. if (Admin::user()->can('system_hotword_create')) {
  112. $grid->disableCreateButton(false);
  113. }
  114. return $grid;
  115. }
  116. /**
  117. * Make a show builder.
  118. *
  119. * @param mixed $id
  120. * @return Show
  121. */
  122. protected function detail($id)
  123. {
  124. $show = new Show(Hotword::findOrFail($id));
  125. $show->id('ID');
  126. $show->w_word('关键词');
  127. $show->type('搜索类型')->as(function ($type) {
  128. if ($type == 2) {
  129. return '企业搜索';
  130. } elseif ($type == 3) {
  131. return '个人简历搜索';
  132. } elseif ($type == 1) {
  133. return '职位搜索';
  134. } else {
  135. return '资讯搜索';
  136. }
  137. });
  138. $show->w_hot('搜索次数');
  139. $show->list_order('显示顺序');
  140. return $show;
  141. }
  142. /**
  143. * Make a form builder.
  144. *
  145. * @return Form
  146. */
  147. protected function form()
  148. {
  149. $form = new Form(new Hotword);
  150. $form->display('ID');
  151. $form->display('Created at');
  152. $form->display('Updated at');
  153. return $form;
  154. }
  155. /**
  156. * Make a form builder.
  157. *
  158. * @return Form
  159. */
  160. protected function editForm()
  161. {
  162. $form = new Form(new Hotword);
  163. $form->text('w_word', '关键词')->rules([
  164. 'required',
  165. ])->setWidth(3)->setMustMark();
  166. //搜索类型
  167. $type_arr = ['1'=>'职位搜索','2'=>'企业搜索','3'=>'个人简历搜索','4'=>'资讯搜索'];
  168. $form->select('type', '搜索类型')
  169. ->options($type_arr)
  170. ->rules('required', array('required'=>'请选择搜索类型。'))
  171. ->setWidth(3)
  172. ->setMustMark();
  173. $form->number('w_hot', '搜索次数')->min(0)->default(0);
  174. $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
  175. return $form;
  176. }
  177. protected function createForm()
  178. {
  179. $form = new Form(new Hotword);
  180. $form->text('w_word', '关键词')->rules([
  181. 'required',
  182. ])->setWidth(3)->setMustMark();
  183. //搜索类型
  184. $type_arr = ['1'=>'职位搜索','2'=>'企业搜索','3'=>'个人简历搜索','4'=>'资讯搜索'];
  185. $form->select('type', '搜索类型')
  186. ->options($type_arr)
  187. ->default(key($type_arr))
  188. ->rules('required', array('required'=>'请选择搜索类型。'))
  189. ->setWidth(3)
  190. ->setMustMark();
  191. $form->number('w_hot', '搜索次数')->min(0)->default(0);
  192. $form->number('list_order', '显示排序')->min(0)->default(0)->help('(越大越靠前)');
  193. return $form;
  194. }
  195. /**
  196. * Store a newly created resource in storage.
  197. *
  198. * @return mixed
  199. */
  200. public function store()
  201. {
  202. return $this->createForm()->store();
  203. }
  204. /**
  205. * Update the specified resource in storage.
  206. *
  207. * @param int $id
  208. *
  209. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  210. */
  211. public function update($id)
  212. {
  213. return $this->editForm()->update($id);
  214. }
  215. }