Index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Cms as CmsModel;
  5. use addons\cms\model\Page as PageModel;
  6. use think\facade\Db;
  7. class Index extends Cmsbase
  8. {
  9. protected $noNeedLogin = ['*'];
  10. protected $noNeedRight = [];
  11. protected $cms_model = null;
  12. protected function initialize()
  13. {
  14. parent::initialize();
  15. $this->cms_model = new CmsModel;
  16. }
  17. public function index()
  18. {
  19. $page = $this->request->param('page/d', 1);
  20. $seo = seo();
  21. $this->assign([
  22. 'SEO' => $seo,
  23. 'page' => $page,
  24. ]);
  25. if ($this->request->isAjax()) {
  26. $this->success('', '', $this->fetch('/common/index_ajax'));
  27. }
  28. return $this->fetch('/index');
  29. }
  30. public function lists()
  31. {
  32. $cat = $this->request->param('catid/d', 0);
  33. if (empty($cat)) {
  34. $cat = $this->request->param('catdir/s', '');
  35. }
  36. $page = $this->request->param('page/d', 1);
  37. $category = getCategory($cat);
  38. if (empty($category)) {
  39. throw new \think\exception\HttpException(404, '栏目不存在!');
  40. }
  41. $catid = $category['id'];
  42. $setting = $category['setting'];
  43. if ($category['type'] == 2) {
  44. $template = $setting['category_template'] ?? 'category';
  45. $template_list = $setting['list_template'] ?? 'list';
  46. $template = $category['child'] ? $template : $template_list;
  47. $seo = seo($catid, '', $setting['meta_description'], $setting['meta_keywords']);
  48. } elseif ($category['type'] == 1) {
  49. $template = $setting['page_template'] ?? 'page';
  50. [$cacheKey, $cacheExpire] = Service::getCacheConfig('page-' . $catid . '-' . $this->siteLang);
  51. $info = PageModel::getPageWithLangByCatid($catid, $this->siteLang);
  52. if (empty($info)) {
  53. throw new \think\exception\HttpException(404, '单页不存在!');
  54. }
  55. PageModel::where('id', $info['id'])->inc('hits')->save();
  56. $keywords = $info['keywords'] ?? $setting['meta_keywords'];
  57. $description = $info['description'] ?? $setting['meta_description'];
  58. $seo = seo($catid, '', $description, $keywords);
  59. $this->assign($info);
  60. if ((int)$catid === 26 && $this->request->has('project', 'get')) {
  61. $projectId = $this->request->param('project/d', 0);
  62. $supportProject = [];
  63. if ($projectId > 0) {
  64. $supportCatIds = [5, 6, 7];
  65. foreach ($supportCatIds as $supportCatId) {
  66. $project = $this->cms_model->getContent(getCategory($supportCatId, 'modelid'), ['catid' => $supportCatId, 'id' => $projectId], true);
  67. if (!empty($project)) {
  68. $supportProject = $project;
  69. $supportProject['source_catid'] = $supportCatId;
  70. break;
  71. }
  72. }
  73. }
  74. if (empty($supportProject)) {
  75. throw new \think\exception\HttpException(404, '产品不存在!');
  76. }
  77. $supportSections = [
  78. 'video' => ['catid' => 34, 'field' => 'url', 'projectField' => 'project_id'],
  79. 'faq' => ['catid' => 25, 'field' => 'url', 'projectField' => 'project_id'],
  80. 'guide' => ['catid' => 35, 'field' => 'url', 'projectField' => 'project_id'],
  81. 'download' => ['catid' => 40, 'field' => 'fileaddress', 'projectField' => 'project_id'],
  82. ];
  83. foreach ($supportSections as $sectionKey => $sectionConfig) {
  84. $sectionCatid = (int) $sectionConfig['catid'];
  85. $sectionTabs = Db::name('cms_category')->where('parentid', $sectionCatid)->where('status', 1)->order('listorder ASC,id ASC')->select()->toArray();
  86. $sectionMap = [];
  87. $sectionDirect = [];
  88. $buildProjectWhere = function ($catIds) use ($sectionConfig, $projectId) {
  89. $catIds = array_values(array_filter(array_map('intval', (array)$catIds)));
  90. if (empty($catIds)) {
  91. return '';
  92. }
  93. $where = '`catid` in(' . implode(',', $catIds) . ') AND `status` = 1';
  94. if (!empty($sectionConfig['projectField'])) {
  95. $where .= " AND `{$sectionConfig['projectField']}` = {$projectId}";
  96. }
  97. return $where;
  98. };
  99. if (!empty($sectionTabs)) {
  100. foreach ($sectionTabs as $sectionTab) {
  101. $childTabs = Db::name('cms_category')->where('parentid', $sectionTab['id'])->where('status', 1)->order('listorder ASC,id ASC')->select()->toArray();
  102. $tabCatIds = [$sectionTab['id']];
  103. if (!empty($childTabs)) {
  104. foreach ($childTabs as $childTab) {
  105. $tabCatIds[] = (int)$childTab['id'];
  106. }
  107. }
  108. $where = $buildProjectWhere($tabCatIds);
  109. $sectionMap[$sectionTab['id']] = [];
  110. if ($where !== '') {
  111. $sectionMap[$sectionTab['id']] = $this->cms_model->getList(getCategory($sectionTab['id'], 'modelid'), $where, 1, '*', 'listorder ASC,id DESC', 24, false);
  112. }
  113. }
  114. }
  115. $directWhere = $buildProjectWhere([$sectionCatid]);
  116. if ($directWhere !== '') {
  117. $sectionDirect = $this->cms_model->getList(getCategory($sectionCatid, 'modelid'), $directWhere, 1, '*', 'listorder ASC,id DESC', 24, false);
  118. }
  119. $hasSectionContent = !empty($sectionDirect);
  120. if (!$hasSectionContent && !empty($sectionMap)) {
  121. foreach ($sectionMap as $sectionItems) {
  122. if (!empty($sectionItems)) {
  123. $hasSectionContent = true;
  124. break;
  125. }
  126. }
  127. }
  128. $assignPrefix = 'support' . ucfirst($sectionKey);
  129. $this->assign($assignPrefix . 'Tabs', $sectionTabs);
  130. $this->assign($assignPrefix . 'Map', $sectionMap);
  131. $this->assign($assignPrefix . 'Direct', $sectionDirect);
  132. $this->assign($assignPrefix . 'Field', $sectionConfig['field']);
  133. $this->assign($assignPrefix . 'HasContent', $hasSectionContent);
  134. }
  135. $this->assign('supportProject', $supportProject);
  136. $this->assign('supportProjectId', $projectId);
  137. }
  138. }
  139. $template = pathinfo($template, PATHINFO_FILENAME);
  140. $top_parentid = $category['arrparentid'][0] ?? $catid;
  141. unset($category['id']);
  142. $this->assign([
  143. 'category' => $category,
  144. 'top_parentid' => $top_parentid,
  145. 'SEO' => $seo,
  146. 'catid' => $catid,
  147. 'page' => $page,
  148. 'modelid' => $category['modelid'],
  149. ]);
  150. if ($this->request->isAjax()) {
  151. $this->success('', '', $this->fetch('/common/' . $template . '_ajax'));
  152. }
  153. return $this->fetch('/' . $template);
  154. }
  155. public function shows()
  156. {
  157. $id = $this->request->param('id/d', 0);
  158. $cat = $this->request->param('catid/d', 0);
  159. if (empty($cat)) {
  160. $cat = $this->request->param('catdir/s', '');
  161. }
  162. $page = $this->request->param('page/d', 1);
  163. $page = max(1, $page);
  164. $category = getCategory($cat);
  165. if (empty($category)) {
  166. throw new \think\exception\HttpException(404, '栏目不存在!');
  167. }
  168. $catid = $category['catid'] = $category['id'];
  169. unset($category['id']);
  170. $modelid = $category['modelid'];
  171. $modelInfo = cms_cache('Model')[$modelid] ?? '';
  172. if (empty($modelInfo)) {
  173. throw new \think\exception\HttpException(404, '模型不存在!');
  174. }
  175. Db::name($modelInfo['tablename'])->where('id', $id)->setInc('hits');
  176. [$cacheKey, $cacheExpire] = Service::getCacheConfig('show-' . $catid . '-' . $id . '-' . $this->siteLang);
  177. $info = $this->cms_model->getContent($modelid, ['catid' => $catid, 'id' => $id], true, '*', '', $cacheKey, $cacheExpire);
  178. if (!$info) {
  179. throw new \think\exception\HttpException(404, '内容不存在!');
  180. }
  181. if ($info['status'] !== 1 && !\app\admin\library\Auth::instance()->isLogin() && $info['user_id'] !== $this->auth->id) {
  182. throw new \think\exception\HttpException(403, '内容未审核或没有权限!');
  183. }
  184. $paginator = isset($info['content']) ? strpos($info['content'], '[page]') : false;
  185. if ($paginator !== false) {
  186. $contents = array_filter(explode('[page]', $info['content']));
  187. $total = count($contents);
  188. $pages = \addons\cms\paginator\Page::make([], 1, $page, $total, false, ['path' => $this->request->baseUrl()]);
  189. if ($paginator < 7) {
  190. $info['content'] = $contents[$page];
  191. } else {
  192. $info['content'] = $contents[$page - 1];
  193. }
  194. $this->assign('pages', $pages);
  195. } else {
  196. $this->assign('pages', '');
  197. }
  198. $setting = $category['setting'];
  199. $template = isset($setting['show_template']) ? pathinfo($setting['show_template'], PATHINFO_FILENAME) : 'show';
  200. $info['readpoint'] = $info['readpoint'] ?? 0;
  201. $info['paytype'] = $info['paytype'] ?? 1;
  202. $is_pay = 1;
  203. $is_part_content_pay = 0;
  204. if (isset($info['content'])) {
  205. $info['content'] = str_replace(['###paidstart###', '###paidend###'], ['<paid>', '</paid>'], $info['content']);
  206. }
  207. if ($info['readpoint'] > 0) {
  208. $is_pay = \addons\cms\model\Order::check_payment($info);
  209. if (!$is_pay) {
  210. if (isset($info['content'])) {
  211. $pattern = '/<paid>(.*?)<\/paid>/is';
  212. if (preg_match($pattern, $info['content'])) {
  213. $info['content'] = preg_replace($pattern, "<div class='allow_visitor'>此处内容需要付费后方可阅读</div>", $info['content']);
  214. $is_part_content_pay = 1;
  215. }
  216. }
  217. } else {
  218. $is_pay = 1;
  219. }
  220. }
  221. $title = $info['title'] ?? ($setting['meta_title'] ?? '');
  222. $keywords = $info['keywords'] ?? ($setting['meta_keywords'] ?? '');
  223. $description = $info['description'] ?? ($setting['meta_description'] ?? '');
  224. $seo = seo($catid, $title, $description, $keywords);
  225. $top_parentid = $category['arrparentid'][0] ?? $catid;
  226. if ($template === 'show_fqa') {
  227. $fqaProjectId = (int) ($info['project_id'] ?? 0);
  228. $fqaChannels = [25, 35];
  229. $fqaSidebar = [];
  230. foreach ($fqaChannels as $channelCatid) {
  231. $channel = getCategory($channelCatid);
  232. if (empty($channel)) {
  233. continue;
  234. }
  235. $children = Db::name('cms_category')->where('parentid', $channelCatid)->where('status', 1)->order('listorder ASC,id ASC')->select()->toArray();
  236. $groups = [];
  237. if (!empty($children)) {
  238. foreach ($children as $child) {
  239. $where = "`catid` = {$child['id']} AND `status` = 1";
  240. if ($fqaProjectId > 0) {
  241. $where .= " AND `project_id` = {$fqaProjectId}";
  242. }
  243. $items = $this->cms_model->getList(getCategory($child['id'], 'modelid'), $where, 1, '*', 'listorder ASC,id DESC', 100, false);
  244. if (!empty($items)) {
  245. $groups[] = [
  246. 'title' => $child['catname'],
  247. 'items' => $items,
  248. ];
  249. }
  250. }
  251. } else {
  252. $where = "`catid` = {$channelCatid} AND `status` = 1";
  253. if ($fqaProjectId > 0) {
  254. $where .= " AND `project_id` = {$fqaProjectId}";
  255. }
  256. $items = $this->cms_model->getList(getCategory($channelCatid, 'modelid'), $where, 1, '*', 'listorder ASC,id DESC', 100, false);
  257. if (!empty($items)) {
  258. $groups[] = [
  259. 'title' => $channel['catname'],
  260. 'items' => $items,
  261. ];
  262. }
  263. }
  264. $fqaSidebar[] = [
  265. 'title' => $channel['catname'],
  266. 'groups' => $groups,
  267. ];
  268. }
  269. $this->assign('fqaSidebar', $fqaSidebar);
  270. }
  271. $this->assign($info);
  272. $this->assign([
  273. 'category' => $category,
  274. 'is_part_content_pay' => $is_part_content_pay,
  275. 'is_pay' => $is_pay,
  276. 'top_parentid' => $top_parentid,
  277. 'SEO' => $seo,
  278. 'catid' => $catid,
  279. 'page' => $page,
  280. 'modelid' => $modelid,
  281. ]);
  282. return $this->fetch('/' . $template);
  283. }
  284. }