Archives.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\wwh\controller;
  3. use think\Db;
  4. use think\Request;
  5. use addons\wwh\model\Column as ColumnModel;
  6. class Archives extends Base
  7. {
  8. public function index()
  9. {
  10. $diyname = $this->request->param('diyname/s');
  11. $id = $this->request->param('id/d');
  12. // 验证参数
  13. if (empty($diyname) || empty($id)) {
  14. $this->error('参数错误!');
  15. }
  16. // 获取当前活动栏目
  17. $navCur = Db::name('wwh_column')->where('diyname', $diyname)->find();
  18. if (!$navCur) {
  19. $this->error('未知栏目!');
  20. }
  21. // 获取栏目详情及关联信息
  22. $archives = Db::name('wwh_archives')
  23. ->alias('a')
  24. ->join('wwh_column c', 'c.id = a.column_id')
  25. ->field('a.*,c.parent_id,c.diyname,c.name')
  26. ->where('a.status', '1')
  27. ->where('a.id', $id)
  28. ->find();
  29. if (!$archives) {
  30. $this->error('内容未审核或不存在!');
  31. }
  32. // 获取顶级栏目
  33. $topId = ColumnModel::getTopColumn($navCur);
  34. // 处理轮播图
  35. $archives['lunbo'] = !empty($archives['images']) ? explode(',', $archives['images']) : [];
  36. // 查询相邻文章
  37. $queryParams = [
  38. 'lang' => $this->lang,
  39. 'status' => '1',
  40. 'column_id' => $archives['column_id']
  41. ];
  42. $getAdjacent = function($type, $compare, $order) use ($id, $queryParams) {
  43. return Db::name('wwh_archives')
  44. ->where($queryParams)
  45. ->where('classify', $type)
  46. ->where('id', $compare, $id)
  47. ->order('id', $order)
  48. ->limit(1)
  49. ->find();
  50. };
  51. // 使用数组存储相邻文章
  52. $adjacentArticles = [
  53. 'cfront' => $getAdjacent('cases', '>', 'asc'),
  54. 'cafter' => $getAdjacent('cases', '<', 'desc'),
  55. 'nfront' => $getAdjacent('news', '>', 'asc'),
  56. 'nafter' => $getAdjacent('news', '<', 'desc')
  57. ];
  58. // 更新浏览数
  59. Db::name('wwh_archives')
  60. ->where('id', $id)
  61. ->update([
  62. 'views' => Db::raw('views+1'),
  63. 'updatetime' => time()
  64. ]);
  65. // 处理模板路径
  66. $template = preg_replace('/\.html$/', '', $archives['tpl']);
  67. // 分配所有模板变量
  68. $this->view->assign(array_merge([
  69. 'navCur' => $navCur,
  70. 'top' => $topId,
  71. 'archives' => $archives,
  72. 'template' => $template
  73. ], $adjacentArticles));
  74. return $this->view->fetch('/' . $template);
  75. }
  76. }