// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | cms管理
// +----------------------------------------------------------------------
namespace app\cms\controller;
use app\cms\model\Cms as Cms_Model;
use think\Db;
use think\Exception;
class Index extends Cmsbase
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = [];
protected function initialize()
{
parent::initialize();
$this->Cms_Model = new Cms_Model;
}
// 首页
public function index()
{
$page = $this->request->param('page/d', 1);
$seo = seo();
$this->assign([
'SEO' => $seo,
'page' => $page,
]);
return $this->fetch('/index');
}
// 列表页
public function lists()
{
$cat = $this->request->param('catid/d', 0);
if (empty($cat)) {
$cat = $this->request->param('catdir/s', '');
}
$page = $this->request->param('page/d', 1);
//获取栏目数据
$category = getCategory($cat);
if (empty($category)) {
throw new \think\exception\HttpException(404, '栏目不存在!');
}
$catid = $category['id'];
//模型ID
$modelid = $category['modelid'];
$models = cache('Model');
//栏目扩展配置信息
$setting = $category['setting'];
//类型为列表的栏目
if ($category['type'] == 2) {
//栏目首页模板
$template = $setting['category_template'] ?: 'category';
//栏目列表页模板
$template_list = $setting['list_template'] ?: 'list';
//判断使用模板类型,如果有子栏目使用频道页模板
$template = $category['child'] ? $template : $template_list;
$seo = seo($catid, '', $setting['meta_description'], $setting['meta_keywords']);
//单页
} elseif ($category['type'] == 1) {
$template = $setting['page_template'] ?: 'page';
$ifcache = $this->cmsConfig['site_cache_time'] ?: false;
$info = model('Page')->getPage($catid, $ifcache);
if (empty($info)) {
throw new \think\exception\HttpException(404, '单页不存在!');
}
$info->setInc('hits');
$info = $info->toArray();
//SEO
$keywords = $info['keywords'] ?? $setting['meta_keywords'];
$description = $info['description'] ?? $setting['meta_description'];
$seo = seo($catid, '', $description, $keywords);
$this->assign($info);
}
$tpar = explode(".", $template, 2);
$template = $tpar[0];
unset($tpar);
if ($this->request->isAjax()) {
$this->success('', '', $this->fetch('/' . $template . '_ajax'));
}
//获取顶级栏目ID
$category['arrparentid'] = explode(',', $category['arrparentid']);
$top_parentid = isset($category['arrparentid'][1]) ? $category['arrparentid'][1] : $catid;
unset($category['id']);
$this->assign([
'category' => $category,
'top_parentid' => $top_parentid,
'SEO' => $seo,
'catid' => $catid,
'page' => $page,
'modelid' => $modelid,
]);
return $this->fetch('/' . $template);
}
// 内容页
public function shows()
{
//ID
$id = $this->request->param('id/d', 0);
$cat = $this->request->param('catid/d', 0);
if (empty($cat)) {
$cat = $this->request->param('catdir/s', '');
}
$page = $this->request->param('page/d', 1);
$page = max(1, $page);
//获取栏目数据
$category = getCategory($cat);
if (empty($category)) {
throw new \think\exception\HttpException(404, '栏目不存在!');
}
$catid = $category['catid'] = $category['id'];
unset($category['id']);
//模型ID
$modelid = $category['modelid'];
$modelInfo = cache('Model')[$modelid];
if (empty($modelInfo)) {
throw new \think\exception\HttpException(404, '模型不存在!');
}
//更新点击量
Db::name($modelInfo['tablename'])->where('id', $id)->setInc('hits');
//内容所有字段
$ifcache = $this->cmsConfig['site_cache_time'] ?: false;
$info = $this->Cms_Model->getContent($modelid, ['catid' => $catid, 'id' => $id], true, '*', '', $ifcache);
if (!$info || ($info['status'] !== 1 && !\app\admin\service\User::instance()->isLogin())) {
throw new \think\exception\HttpException(404, '内容不存在或未审核!');
}
//内容分页
$paginator = isset($info['content']) ? strpos($info['content'], '[page]') : false;
if ($paginator !== false) {
$contents = array_filter(explode('[page]', $info['content']));
$total = count($contents);
$pages = \app\cms\paginator\Page::make([], 1, $page, $total, false, ['path' => $this->request->baseUrl()]);
//判断[page]出现的位置是否在第一位
if ($paginator < 7) {
$info['content'] = $contents[$page];
} else {
$info['content'] = $contents[$page - 1];
}
$this->assign("pages", $pages);
} else {
$this->assign("pages", '');
}
//栏目扩展配置信息
$setting = $category['setting'];
//内容页模板
$template = $setting['show_template'] ?: 'show';
//去除模板文件后缀
$newstempid = explode(".", $template);
$template = $newstempid[0];
unset($newstempid);
//阅读收费
$info['readpoint'] = $info['readpoint'] ?? 0; //金额
$info['paytype'] = $info['paytype'] ?? 1; //1钱 2积分
$is_pay = 1;
$is_part_content_pay = 0; //是否部分内容付费
if (isset($info['content'])) {
$info['content'] = str_replace(['###paidstart###', '###paidend###'], ['', ''], $info['content']);
}
if ($info['readpoint'] > 0) {
//检查是否支付过
$is_pay = \app\cms\model\Order::check_payment($info);
if (!$is_pay) {
//部分内容阅读付费
if (isset($info['content'])) {
$pattern = '/(.*?)<\/paid>/is';
if (preg_match($pattern, $info['content'])) {
//$payurl = url('cms/index/readpoint', ['allow_visitor' => $allow_visitor]);
$info['content'] = preg_replace($pattern, "此处内容需要付费后方可阅读
", $info['content']);
$is_part_content_pay = 1;
}
}
} else {
$is_pay = 1;
}
}
//SEO
$keywords = $info['keywords'] ? $info['keywords'] : $setting['meta_keywords'];
$title = $info['title'] ? $info['title'] : $setting['meta_title'];
$description = $info['description'] ? $info['description'] : $setting['meta_description'];
$seo = seo($catid, $title, $description, $keywords);
//获取顶级栏目ID
$arrparentid = explode(',', $category['arrparentid']);
$top_parentid = $arrparentid[1] ?? $catid;
$this->assign($info);
$this->assign([
'category' => $category,
//'readpoint' => $readpoint,
'is_part_content_pay' => $is_part_content_pay,
'is_pay' => $is_pay,
'top_parentid' => $top_parentid,
'arrparentid' => $arrparentid,
'SEO' => $seo,
'catid' => $catid,
'page' => $page,
'modelid' => $modelid,
]);
return $this->fetch('/' . $template);
}
// tags
public function tags()
{
$page = $page = $this->request->param('page/d', 1);
$tag = $this->request->param('tag', '');
$where = [];
if ($tag && is_numeric($tag)) {
$where['id'] = $tag;
} else {
$where['tag'] = $tag;
}
//如果条件为空,则显示标签首页
if (empty($tag)) {
$data = Db::name('Tags')->order(['listorder' => 'DESC', 'hits' => 'DESC'])->limit(100)->select();
$this->assign("SEO", seo('', '标签'));
$this->assign('list', $data);
return $this->fetch('/tags_list');
}
//根据条件获取tag信息
$info = Db::name('Tags')->where($where)->find();
if (empty($info)) {
$this->error('抱歉,沒有找到您需要的内容!');
}
//访问数+1
Db::name('Tags')->where($where)->setInc("hits");
$this->assign($info);
$this->assign("SEO", seo('', $info['tag'], $info['seo_description'], $info['seo_keyword']));
$this->assign("page", $page);
$this->assign($info);
return $this->fetch('/tags');
}
}