From cff43dbd99d99189864acd591f856a38e87c5b09 Mon Sep 17 00:00:00 2001
From: chengkun <chengkun@ishangstudy.com>
Date: Mon, 15 Sep 2025 17:03:50 +0800
Subject: [PATCH] 提交

---
 app/home/controller/Blog.php |   78 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/app/home/controller/Blog.php b/app/home/controller/Blog.php
index 3ec3077..43f4528 100644
--- a/app/home/controller/Blog.php
+++ b/app/home/controller/Blog.php
@@ -2,12 +2,90 @@
 
 namespace app\home\controller;
 
+use think\db\exception\DataNotFoundException;
+use think\db\exception\DbException;
+use think\db\exception\ModelNotFoundException;
+use think\Exception;
+use think\facade\Db;
 use think\facade\View;
 use think\facade\Request;
 use app\BaseController;
+use think\response\Json;
 
 class Blog extends BaseController {
     public function index() {
+        $list = Db::name('blog')->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);
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.0