Archives.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Archives as ArchivesModel;
  5. use addons\cms\model\Channel;
  6. use addons\cms\model\Fields;
  7. use addons\cms\model\Modelx;
  8. use think\Config;
  9. use think\Exception;
  10. /**
  11. * 文档控制器
  12. * Class Archives
  13. * @package addons\cms\controller
  14. */
  15. class Archives extends Base
  16. {
  17. public function index()
  18. {
  19. $action = $this->request->post("action");
  20. if ($action && $this->request->isPost()) {
  21. return $this->$action();
  22. }
  23. $diyname = $this->request->param('diyname');
  24. if ($diyname && !is_numeric($diyname)) {
  25. $archives = ArchivesModel::getByDiyname($diyname);
  26. } else {
  27. $id = $diyname ? $diyname : $this->request->param('id', '');
  28. $archives = ArchivesModel::get($id, ['channel']);
  29. }
  30. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  31. $this->error(__('No specified article found'));
  32. }
  33. if (!$this->auth->id && !$archives['isguest']) {
  34. $this->error(__('Please login first'), 'index/user/login');
  35. }
  36. $channel = Channel::get($archives['channel_id']);
  37. if (!$channel) {
  38. $this->error(__('No specified channel found'));
  39. }
  40. $model = Modelx::get($channel['model_id'], [], true);
  41. if (!$model) {
  42. $this->error(__('No specified model found'));
  43. }
  44. $addon = db($model['table'])->where('id', $archives['id'])->find();
  45. if ($addon) {
  46. if ($model->fields) {
  47. $fieldsContentList = Fields::getFieldsContentList('model', $model->id);
  48. Service::appendTextAttr($fieldsContentList, $addon);
  49. }
  50. $archives->setData($addon);
  51. } else {
  52. $this->error(__('No specified addon article found'));
  53. }
  54. //PC支持内容分页
  55. $page = (int)$this->request->request("page", 1);
  56. $page = max(1, $page);
  57. $contentArr = array_filter(explode("##pagebreak##", $archives->content));
  58. $content = $contentArr ? (isset($contentArr[$page - 1]) ? $contentArr[$page - 1] : $contentArr[0]) : '';
  59. $archives->content = $content . $archives->getPagerHTML($page, count($contentArr));
  60. $archives->setInc("views", 1);
  61. $this->view->assign("__ARCHIVES__", $archives);
  62. $this->view->assign("__CHANNEL__", $channel);
  63. $this->view->assign("__MODEL__", $model);
  64. //设置TKD
  65. Config::set('cms.title', isset($archives['seotitle']) && $archives['seotitle'] ? $archives['seotitle'] : $archives['title']);
  66. Config::set('cms.keywords', $archives['keywords']);
  67. Config::set('cms.description', $archives['description']);
  68. //是否跳转链接
  69. if (isset($archives['outlink']) && $archives['outlink']) {
  70. $regex = "/^((?:[a-z]+:)?\/\/)(.*)/i";
  71. if (preg_match($regex, $archives['outlink'])) {
  72. return $this->view->fetch("/outlink", ['remainseconds' => 10]);
  73. } else {
  74. $this->redirect($archives['outlink']);
  75. }
  76. }
  77. $template = preg_replace('/\.html$/', '', $channel['showtpl']);
  78. if (!$template) {
  79. $this->error('请检查栏目是否配置相应的模板');
  80. }
  81. return $this->view->fetch('/' . $template);
  82. }
  83. /**
  84. * 赞与踩
  85. */
  86. public function vote()
  87. {
  88. $id = (int)$this->request->post("id");
  89. $type = trim($this->request->post("type", ""));
  90. if (!$id || !$type) {
  91. $this->error(__('Operation failed'));
  92. }
  93. $archives = ArchivesModel::get($id);
  94. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  95. $this->error(__('No specified article found'));
  96. }
  97. $archives->where('id', $id)->setInc($type === 'like' ? 'likes' : 'dislikes', 1);
  98. $archives = ArchivesModel::get($id);
  99. $this->success(__('Operation completed'), null, ['likes' => $archives->likes, 'dislikes' => $archives->dislikes, 'likeratio' => $archives->likeratio]);
  100. }
  101. /**
  102. * 下载次数
  103. */
  104. public function download()
  105. {
  106. $id = (int)$this->request->post("id");
  107. if (!$id) {
  108. $this->error(__('Operation failed'));
  109. }
  110. $archives = ArchivesModel::get($id, ['model']);
  111. if (!$archives || ($archives['user_id'] != $this->auth->id && $archives['status'] != 'normal') || $archives['deletetime']) {
  112. $this->error(__('No specified article found'));
  113. }
  114. try {
  115. $table = $archives->getRelation('model')->getData('table');
  116. \think\Db::name($table)->where('id', $id)->setInc('downloads');
  117. } catch (Exception $e) {
  118. //
  119. }
  120. $this->success(__('Operation completed'), null);
  121. }
  122. }