Archives.php 5.6 KB

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