Channel.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Archives;
  5. use addons\cms\model\Channel as ChannelModel;
  6. use addons\cms\model\Fields;
  7. use addons\cms\model\Modelx;
  8. use addons\cms\model\SpiderLog;
  9. use think\Cache;
  10. use think\Config;
  11. /**
  12. * 栏目控制器
  13. * Class Channel
  14. * @package addons\cms\controller
  15. */
  16. class Channel extends Base
  17. {
  18. public function index()
  19. {
  20. $config = get_addon_config('cms');
  21. $diyname = $this->request->param('diyname');
  22. if ($diyname && !is_numeric($diyname)) {
  23. $channel = ChannelModel::getByDiyname($diyname);
  24. } else {
  25. $id = $diyname ? $diyname : $this->request->param('id', '');
  26. $channel = ChannelModel::get($id);
  27. }
  28. if (!$channel || $channel['status'] != 'normal') {
  29. $this->error(__('No specified channel found'));
  30. }
  31. $filter = $this->request->get('filter/a', []);
  32. $orderby = $this->request->get('orderby', '');
  33. $orderway = $this->request->get('orderway', '');
  34. $multiple = $this->request->get('multiple/d', 0);
  35. $orderway = $orderway && in_array(strtolower($orderway), ['asc', 'desc']) ? $orderway : 'desc';
  36. $params = [];
  37. $filter = $this->request->get();
  38. $filter = array_diff_key($filter, array_flip(['orderby', 'orderway', 'page', 'multiple']));
  39. if (isset($filter['filter'])) {
  40. $filter = array_merge($filter, $filter['filter']);
  41. unset($filter['filter']);
  42. }
  43. if ($filter) {
  44. $filter = array_filter($filter, 'strlen');
  45. $params['filter'] = $filter;
  46. $params = $filter;
  47. }
  48. if ($orderby) {
  49. $params['orderby'] = $orderby;
  50. }
  51. if ($orderway) {
  52. $params['orderway'] = $orderway;
  53. }
  54. if ($multiple) {
  55. $params['multiple'] = $multiple;
  56. }
  57. if ($channel['type'] === 'link') {
  58. $this->redirect($channel['outlink']);
  59. }
  60. //加载模型数据
  61. $model = Modelx::get($channel['model_id']);
  62. if (!$model) {
  63. $this->error(__('No specified model found'));
  64. }
  65. //默认排序字段
  66. $orders = [
  67. ['name' => 'default', 'field' => 'weigh DESC,publishtime DESC', 'title' => __('Default')],
  68. ];
  69. //合并主表筛选字段
  70. $orders = array_merge($orders, $model->getOrderFields());
  71. //获取过滤列表
  72. list($filterList, $filter, $params, $fields, $multiValueFields, $fieldsList) = Service::getFilterList('model', $model['id'], $filter, $params, $multiple);
  73. //获取排序列表
  74. list($orderList, $orderby, $orderway) = Service::getOrderList($orderby, $orderway, $orders, $params, $fieldsList);
  75. //获取过滤的条件和绑定参数
  76. list($filterWhere, $filterBind) = Service::getFilterWhereBind($filter, $multiValueFields, $multiple);
  77. $filterChannel = function ($query) use ($channel) {
  78. $query->where(function ($query) use ($channel) {
  79. if ($channel['listtype'] <= 2) {
  80. $query->whereOr("channel_id", $channel['id']);
  81. }
  82. if ($channel['listtype'] == 1 || $channel['listtype'] == 3) {
  83. $query->whereOr('channel_id', 'in', function ($query) use ($channel) {
  84. $query->name("cms_channel")->where('parent_id', $channel['id'])->field("id");
  85. });
  86. }
  87. if ($channel['listtype'] == 0 || $channel['listtype'] == 4) {
  88. $childrenIds = \addons\cms\model\Channel::getChannelChildrenIds($channel['id'], false);
  89. if ($childrenIds) {
  90. $query->whereOr('channel_id', 'in', $childrenIds);
  91. }
  92. }
  93. })
  94. ->whereOr("(`channel_ids`!='' AND FIND_IN_SET('{$channel['id']}', `channel_ids`))");
  95. };
  96. //模板名称
  97. $template = ($this->request->isAjax() ? '/ajax/' : '/') . ($channel["{$channel['type']}tpl"] ?? '');
  98. $template = preg_replace('/\.html$/', '', $template);
  99. $pagelistParams = Service::getPagelistParams($template);
  100. //分页大小
  101. $pagesize = $pagelistParams['pagesize'] ?? $channel['pagesize'];
  102. //过滤条件
  103. $filterPagelist = function ($query) use ($pagelistParams) {
  104. if (isset($pagelistParams['condition'])) {
  105. $query->where($pagelistParams['condition']);
  106. }
  107. };
  108. //分页模式
  109. $simple = $config['loadmode'] == 'paging' && $config['pagemode'] == 'full' ? false : true;
  110. //缓存列表总数
  111. if (!$simple && ($config['cachelistcount'] ?? false)) {
  112. $simple = Archives::with(['channel', 'user'])->alias('a')
  113. ->where('a.status', 'normal')
  114. ->whereNull('a.deletetime')
  115. ->where($filterWhere)
  116. ->bind($filterBind)
  117. ->where($filterPagelist)
  118. ->where($filterChannel)
  119. ->where('model_id', $channel->model_id)
  120. ->join($model['table'] . ' n', 'a.id=n.id', 'LEFT')
  121. ->cache("cms-channel-list-" . $channel['id'] . '-' . md5(serialize($filter)), 3600) //总数缓存1小时
  122. ->count();
  123. }
  124. //加载列表数据
  125. $pageList = Archives::with(['channel', 'user'])->alias('a')
  126. ->where('a.status', 'normal')
  127. ->whereNull('a.deletetime')
  128. ->where($filterWhere)
  129. ->bind($filterBind)
  130. ->where($filterPagelist)
  131. ->where($filterChannel)
  132. ->where('model_id', $channel->model_id)
  133. ->join($model['table'] . ' n', 'a.id=n.id', 'LEFT')
  134. ->field('a.*')
  135. ->field('id,content', true, config('database.prefix') . $model['table'], 'n')
  136. ->order($orderby, $orderway)
  137. ->paginate($pagesize, $simple);
  138. Service::appendTextAndList('model', $model->id, $pageList, true);
  139. Service::appendTextAndList('channel', 0, $channel);
  140. $pageList->appends(array_filter($params));
  141. $this->view->assign("__FILTERLIST__", $filterList);
  142. $this->view->assign("__ORDERLIST__", $orderList);
  143. $this->view->assign("__PAGELIST__", $pageList);
  144. $this->view->assign("__CHANNEL__", $channel);
  145. SpiderLog::record('channel', $channel['id']);
  146. //设置TKD
  147. Config::set('cms.title', isset($channel['seotitle']) && $channel['seotitle'] ? $channel['seotitle'] : $channel['name']);
  148. Config::set('cms.keywords', $channel['keywords']);
  149. Config::set('cms.description', $channel['description']);
  150. Config::set('cms.image', isset($channel['image']) && $channel['image'] ? cdnurl($channel['image'], true) : '');
  151. //读取模板
  152. if (!$template) {
  153. $this->error('请检查栏目是否配置相应的模板');
  154. }
  155. if ($this->request->isAjax()) {
  156. $this->success("", "", $this->view->fetch($template));
  157. }
  158. return $this->view->fetch($template);
  159. }
  160. }