where('status', 1)->limit(10)->order('id desc')->select()->toArray(); View::assign('list', $list); return View::fetch('index'); } /** * 获取博客列表 * @return Json */ public function get_blog_list(): Json { try { if (!Request::isPost()) { throw new Exception('请求方式错误'); } $list = Db::name('blog')->where('status', 1)->limit(10)->order('id desc')->select()->toArray(); $result = [ 'code' => 200, 'message' => '获取成功', 'data' => $list, ]; } catch (Exception $exc) { $result = [ 'code' => $exc->getCode(), 'message' => $exc->getMessage(), ]; } return json($result); } /** * 博客详情页 * @param int $id * @return string */ public function detail(int $id = 0): string { if (!$id || !is_numeric($id)) { $this->error('参数错误'); } // $info = Db::name('blog')->where('id', $id)->find(); // if (!$info) { // $this->error('数据不存在'); // } // View::assign('info', $info); View::assign('id', $id); return View::fetch('detail'); } /** * 获取博客内容 * @return Json */ public function get_blog_info(): Json { try { $id = input('id', 0); if (!$id || !is_numeric($id)) { throw new Exception('参数错误'); } $info = Db::name('blog')->where('id', $id)->find(); if (!$info) { throw new Exception('数据不存在'); } $result = [ 'code' => 200, 'message' => '获取成功', 'data' => $info, ]; } catch (Exception $exc) { $result = [ 'code' => $exc->getCode(), 'message' => $exc->getMessage(), ]; } return json($result); } }