PageController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Admin\Extensions\Tools\DialogTool;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Page;
  6. use App\Models\Subsite;
  7. use Encore\Admin\Controllers\HasResourceActions;
  8. use Encore\Admin\Facades\Admin;
  9. use Encore\Admin\Form;
  10. use Encore\Admin\Grid;
  11. use Encore\Admin\Layout\Content;
  12. use Encore\Admin\Show;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Validation\Rule;
  15. class PageController extends Controller
  16. {
  17. use HasResourceActions;
  18. /**
  19. * Index interface.
  20. *
  21. * @param Content $content
  22. * @return Content
  23. */
  24. public function index(Content $content)
  25. {
  26. return $content
  27. ->header('页面管理列表')
  28. ->description('')
  29. ->body($this->grid());
  30. }
  31. /**
  32. * Show interface.
  33. *
  34. * @param mixed $id
  35. * @param Content $content
  36. * @return Content
  37. */
  38. public function show($id, Content $content)
  39. {
  40. return $content
  41. ->header('页面管理列表')
  42. ->description('详细')
  43. ->body($this->detail($id));
  44. }
  45. /**
  46. * Edit interface.
  47. *
  48. * @param mixed $id
  49. * @param Content $content
  50. * @return Content
  51. */
  52. public function edit($id, Content $content)
  53. {
  54. return $content
  55. ->header('页面管理列表')
  56. ->description('编辑')
  57. ->body($this->editForm($id)->edit($id));
  58. }
  59. /**
  60. * Create interface.
  61. *
  62. * @param Content $content
  63. * @return Content
  64. */
  65. public function create(Content $content)
  66. {
  67. return $content
  68. ->header('页面管理列表')
  69. ->description('创建')
  70. ->body($this->createForm());
  71. }
  72. /**
  73. * Make a grid builder.
  74. *
  75. * @return Grid
  76. */
  77. protected function grid()
  78. {
  79. $grid = new Grid(new Page);
  80. $grid->id('ID');
  81. $grid->pname('页面名称')->width(200);
  82. $grid->alias('别名')->width(200);
  83. $grid->systemclass('类型')->display(function ($systemclass) {
  84. return $systemclass ? '系统内置' : '自定义';
  85. });
  86. $grid->url('链接')->display(function ($url) {
  87. return $url ? '伪静态' : '原始链接';
  88. });
  89. $grid->caching('缓存')->display(function ($caching) {
  90. return $caching ? $caching.'分' : '已关闭';
  91. })->width(150);
  92. $grid->created_at('添加时间');
  93. $grid->updated_at('更新时间');
  94. $grid->actions(function ($actions) use ($grid) {
  95. if (Admin::user()->can('system_page_edit')) {
  96. $actions->disableEdit(false);
  97. }
  98. if (Admin::user()->can('system_page_delete')) {
  99. $actions->disableDelete(false);
  100. }
  101. });
  102. if (Admin::user()->can('system_page_delete')) {
  103. $grid->tools(function ($tools) {
  104. $tools->batch(function ($batch) {
  105. $batch->disableDelete(false);
  106. });
  107. });
  108. $grid->disableRowSelector(false);
  109. }
  110. if (Admin::user()->can('system_page_caching')) {
  111. $grid->tools(function ($tools) {
  112. /* $tools->batch(function ($batch) {
  113. $batch->disableDelete();
  114. $batch->add('设置缓存(3天)', new PageCache(4320));
  115. $batch->add('设置缓存(7天)', new PageCache(10080));
  116. $batch->add('设置缓存(15天)', new PageCache(21600));
  117. });*/
  118. $form = new \Encore\Admin\Widgets\Form();
  119. $form->action(route('admin.sys.caching'));
  120. $form->text('caching', '设置缓存')->default(200)->placeholder('单位:分钟');
  121. $tools->append(new DialogTool($form, [], '设置缓存'));
  122. });
  123. }
  124. if (Admin::user()->can('system_page_create')) {
  125. $grid->disableCreateButton(false);
  126. }
  127. return $grid;
  128. }
  129. /**
  130. * Make a show builder.
  131. *
  132. * @param mixed $id
  133. * @return Show
  134. */
  135. protected function detail($id)
  136. {
  137. $show = new Show(Page::findOrFail($id));
  138. $show->id('ID');
  139. $show->pname('页面名称');
  140. $show->alias('别名');
  141. $show->systemclass('类型')->as(function ($systemclass) {
  142. return $systemclass ? '系统内置' : '自定义';
  143. });
  144. $show->url('链接')->as(function ($url) {
  145. return $url ? '伪静态' : '原始链接';
  146. });
  147. $show->caching('缓存')->as(function ($caching) {
  148. return $caching ? $caching.'分' : '已关闭';
  149. });
  150. $show->created_at('添加时间');
  151. $show->updated_at('更新时间');
  152. return $show;
  153. }
  154. /**
  155. * Make a form builder.
  156. *
  157. * @return Form
  158. */
  159. protected function editForm($id)
  160. {
  161. $form = new Form(new Page);
  162. $model = Page::findOrfail($id);
  163. $systemclass = isset($model['pagetpye']) ? $model['pagetpye'] : 0;
  164. if ($systemclass) {
  165. $form->radio('systemclass', '类型')->options([1 => '系统内置']);
  166. } else {
  167. $form->radio('systemclass', '类型')->options([0 => '自定义']);
  168. }
  169. $form->display('alias', '别名')->setWidth(3);
  170. $form->display('pname', '页面名称')->setWidth(3);
  171. $form->text('tag', '导航关联标记')->rules([
  172. 'required',
  173. ])->setWidth(3)->setMustMark();
  174. $form->text('route', '路由')->rules([
  175. 'required',
  176. Rule::unique('pages')->ignore($id),
  177. ])->setWidth(3)->setMustMark();
  178. $form->radio('url', '链接优化')->options([1 => '伪静态'])->default(1);
  179. $form->radio('pagetpye', '页面类型')->options([1 => '首页或频道首页',2=>'信息列表页',3=>'信息内容页'])->default(1);
  180. if (config('aix.system.site_safety.subsite.close_subsite')==0) {
  181. $form->select('subsite_id', '分站信息')->options(
  182. Subsite::List()->pluck('sitename', 'id')
  183. )->setWidth(2);
  184. }
  185. $form->text('caching', '缓存时间')->placeholder('默认为分钟(0不设置缓存)')->rules([
  186. 'required',
  187. ])->setWidth(2)->setMustMark();
  188. $form->text('title', 'Seo标题')->rules([
  189. 'required',
  190. ])->setWidth(3)->setMustMark();
  191. $form->text('keywords', 'Seo关键字')->setWidth(3);
  192. $form->text('description', 'Seo描述')->setWidth(8);
  193. return $form;
  194. }
  195. protected function createForm()
  196. {
  197. $form = new Form(new Page);
  198. $form->radio('systemclass', '类型')->options([0 => '自定义'])->default(0);
  199. $form->text('alias', '别名')->rules([
  200. 'required',
  201. 'unique:pages',
  202. ])->setWidth(3)->setMustMark();
  203. $form->text('pname', '页面名称')->rules([
  204. 'required',
  205. 'unique:pages',
  206. ])->setWidth(3)->setMustMark();
  207. $form->text('tag', '导航关联标记')->rules([
  208. 'required',
  209. ])->setWidth(3)->setMustMark();
  210. $form->text('route', '路由')->rules([
  211. 'required',
  212. 'unique:pages',
  213. ])->setWidth(3)->setMustMark();
  214. $form->radio('url', '链接优化')->options([1 => '伪静态'])->default(1);
  215. $form->radio('pagetpye', '页面类型')->options([1 => '首页或频道首页',2=>'信息列表页',3=>'信息内容页'])->default(1);
  216. if (config('aix.system.site_safety.subsite.close_subsite')==0) {
  217. $form->select('subsite_id', '分站信息')->options(
  218. Subsite::List()->pluck('sitename', 'id')
  219. )->setWidth(2);
  220. }
  221. $form->text('caching', '缓存时间')->placeholder('默认为分钟(0不设置缓存)')->rules([
  222. 'required',
  223. ])->setWidth(2)->setMustMark();
  224. $form->text('title', 'Seo标题')->rules([
  225. 'required',
  226. ])->setWidth(3)->setMustMark();
  227. $form->text('keywords', 'Seo关键字')->setWidth(3);
  228. $form->text('description', 'Seo描述')->setWidth(8);
  229. return $form;
  230. }
  231. /**
  232. * Store a newly created resource in storage.
  233. *
  234. * @return mixed
  235. */
  236. public function store()
  237. {
  238. return $this->createForm()->store();
  239. }
  240. /**
  241. * Update the specified resource in storage.
  242. *
  243. * @param int $id
  244. *
  245. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  246. */
  247. public function update($id)
  248. {
  249. return $this->editForm($id)->update($id);
  250. }
  251. public function release(Request $request)
  252. {
  253. foreach (Page::find($request->get('ids')) as $post) {
  254. $post->caching = $request->get('action');
  255. $post->save();
  256. }
  257. }
  258. public function tag(Request $request)
  259. {
  260. $q = $request->get('q');
  261. dd($q);
  262. return Page::where('alias', $q)->get(['tag']);
  263. }
  264. /**
  265. * 设置缓存.
  266. */
  267. public function caching(Request $request)
  268. {
  269. $id = $request->post('ids', 0);
  270. $da = $request->post('caching', '');
  271. if (!$id) {
  272. return admin_error("错误", "请勾选设置的列");
  273. }
  274. if (!$da) {
  275. $caching = 0;
  276. } else {
  277. $caching = $da;
  278. }
  279. $res = Page::whereIn('id', explode(',', $id))->update(['caching'=>$caching]);
  280. if ($res) {
  281. return admin_success("成功", "设置成功");
  282. } else {
  283. return admin_error("失败", "设置失败");
  284. }
  285. }
  286. /**
  287. * Make a form builder.
  288. *
  289. * @return Form
  290. */
  291. protected function form()
  292. {
  293. $form = new Form(new Page);
  294. $form->display('ID');
  295. $form->display('Created at');
  296. $form->display('Updated at');
  297. return $form;
  298. }
  299. /**
  300. * 通过别名获取导航关联标记
  301. * @param Request $request
  302. * @return Page;
  303. */
  304. public function getTag(Request $request)
  305. {
  306. $q = $request->get('q');
  307. return Page::where('alias', $q)->get(['id','tag as text']);
  308. }
  309. }