Files

62 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace app\frontend\controller;
use think\facade\Db;
use ywxapp\controller\FrontendBase;
use ywxapp\model\Help as HelpModel;
/**
* 站点帮助中心(前台)
*/
class Help extends FrontendBase
{
/**
* 帮助中心首页:按分类分组列出所有启用项
*/
public function index()
{
$list = (new HelpModel())
->where('status', 1)
->order('sort', 'desc')
->order('id', 'desc')
->field('id,category,title,view_count')
->select()
->toArray();
$groups = [];
foreach ($list as $item) {
$cat = $item['category'] ?: '其他';
$groups[$cat][] = $item;
}
// 维护分类顺序(按首个条目出现顺序)
$categories = [];
foreach ($groups as $cat => $items) {
$categories[] = ['name' => $cat, 'items' => $items];
}
$this->assign('categories', $categories);
return $this->fetch();
}
/**
* 帮助详情
*/
public function read($id = null)
{
if (empty($id)) {
$this->error('参数错误');
}
$info = (new HelpModel())->where('id', $id)->where('status', 1)->find();
if (empty($info)) {
$this->error('帮助内容不存在或已下架');
}
// 浏览量自增
Db::name('help')->where('id', $id)->inc('view_count')->update();
$this->assign('info', $info);
return $this->fetch('read');
}
}