Page.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Fields;
  5. use addons\cms\model\Page as PageModel;
  6. use think\Config;
  7. /**
  8. * CMS单页控制器
  9. * Class Page
  10. * @package addons\cms\controller
  11. */
  12. class Page extends Base
  13. {
  14. public function index()
  15. {
  16. $diyname = $this->request->param('diyname');
  17. if ($diyname && !is_numeric($diyname)) {
  18. $page = PageModel::getByDiyname($diyname);
  19. } else {
  20. $id = $diyname ? $diyname : $this->request->param('id', '');
  21. $page = PageModel::get($id);
  22. }
  23. if (!$page || $page['status'] != 'normal') {
  24. $this->error(__('No specified page found'));
  25. }
  26. $page->setInc('views');
  27. $fieldsContentList = Fields::getFieldsContentList('page');
  28. Service::appendTextAttr($fieldsContentList, $page);
  29. $this->view->assign("__PAGE__", $page);
  30. //设置TKD
  31. Config::set('cms.title', isset($page['seotitle']) && $page['seotitle'] ? $page['seotitle'] : $page['title']);
  32. Config::set('cms.keywords', $page['keywords']);
  33. Config::set('cms.description', $page['description']);
  34. $template = preg_replace("/\.html$/i", "", $page['showtpl'] ? $page['showtpl'] : 'page');
  35. return $this->view->fetch('/' . $template);
  36. }
  37. /**
  38. * 赞与踩
  39. */
  40. public function vote()
  41. {
  42. $id = (int)$this->request->post("id");
  43. $type = trim($this->request->post("type", ""));
  44. if (!$id || !$type) {
  45. $this->error(__('Operation failed'));
  46. }
  47. $page = \addons\cms\model\Page::get($id);
  48. if (!$page) {
  49. $this->error(__('No specified page found'));
  50. }
  51. $page->where('id', $id)->setInc($type === 'like' ? 'likes' : 'dislikes', 1);
  52. $page = \addons\cms\model\Page::get($id);
  53. $this->success(__('Operation completed'), null, ['likes' => $page->likes, 'dislikes' => $page->dislikes, 'likeratio' => $page->likeratio]);
  54. }
  55. }