// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\frontend\controller; use think\facade\Db; use think\facade\View; use ywxapp\controller\FrontendBase; use ywxapp\model\Notice as NoticeModel; /** * 前台站点公告(方案 B:列表 + 详情) */ class Notice extends FrontendBase { protected $noNeedLogin = ['index', 'read']; /** * 公告列表 */ public function index() { $page = $this->request->param('page/d', 1); $type = $this->request->param('type/d', 0); $list = NoticeModel::getList($page, 10, $type); // 附加类型文案 $typeList = NoticeModel::typeList(); $items = $list->items(); foreach ($items as &$it) { $it['type_text'] = $typeList[$it['type']] ?? '公告'; } unset($it); View::assign('title', '站点公告 - YwxApp'); return view('index', [ 'type' => $type, 'type_list' => $typeList, 'list' => $list, 'items' => $items, ]); } /** * 公告详情 */ public function read($id = null) { $id = $id ?: $this->request->param('id/d'); if (! $id) { $this->redirect('notice/index'); } $info = (new NoticeModel())->where('status', 1)->find($id); if (! $info) { $this->redirect('notice/index'); } // 浏览量自增(模型 view_count 为 readonly,用 Db 原生 inc 绕过) Db::name('notice')->where('id', $id)->inc('view_count')->update(); $info->view_count = ($info->view_count ?? 0) + 1; $typeList = NoticeModel::typeList(); $info->type_text = $typeList[$info['type']] ?? '公告'; View::assign('title', ($info->title ?? '公告详情') . ' - YwxApp'); return view('read', [ 'info' => $info, ]); } }