diff --git a/.env.example b/.env.example index 942d3083d00d969f7000b888c30d4c1a2c410f72..5d673c655bb0212a92a2fe0dfbfa09a721504c1b 100644 --- a/.env.example +++ b/.env.example @@ -2,7 +2,7 @@ APP_NAME=Blog2 APP_ENV=local APP_KEY=base64:Q+o4O+Ct51x574y4ekQgph1e0MoloZm9RySSLXX6Q2A= -# 生存环境下为false +# 生产环境下为false APP_DEBUG=true # 博客地址 APP_URL=127.0.0.1 diff --git a/README.md b/README.md index 68a778d402705ee908642175816cc5b550345ca6..76dc232a58ec42ac904dcf935bb8e4665ac805f4 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,12 @@ server { - [x] 日志世界线 - [x] Search - [ ] 直接将账号密码写入.env(?) +- [ ] [WIP] API + +### 拓展功能-[WIP] API +- [x] 搜索/api/search?q={q} +- [x] 文字内容/api/article/{$id} + ### 演示站点 https://blog2.iabu.xyz/ diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index 35bd57c1038c3a35b86a3a1b0d99abe225dfb1d9..2913ebb777920f4f49272bdc85af676d36b8b40b 100644 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -16,20 +16,21 @@ class ArticleController extends Controller public function article($id = 'new') { if ($id == 'new'){ - $article = DB::table('article')->orderBy('aid', 'desc')->first(); - // $article = DB::table('article')->where('aid', $new->aid)->get(); + $article = DB::table('articles')->orderBy('aid', 'desc')->first(); + // $article = DB::table('articles')->where('aid', $new->aid)->get(); $title = '最新文章'; $id = $article->aid; }else{ - $article = DB::table('article')->where('aid', $id)->first(); + $article = DB::table('articles')->where('aid', $id)->first(); } + $article->content = str_replace("`", "\`", $article->content); return view('article', ['title'=>$id, 'article'=>$article, 'id'=>$id]); } // 文章列表页(主页) public function articleList() { - $articles = DB::table('article')->get(); + $articles = DB::table('articles')->get(); return view('home', ['articles'=>$articles, 'title'=>'主页']); } @@ -43,7 +44,7 @@ class ArticleController extends Controller return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); } - $articleList = DB::table('article')->get(); + $articleList = DB::table('articles')->get(); return view('admin.article', ['title'=>'文章管理', 'list'=>$articleList]); } @@ -58,7 +59,7 @@ class ArticleController extends Controller if ($id == null) { return response('错误的参数{id}')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); } - $delete = DB::table('article')->where('aid', $id)->delete(); + $delete = DB::table('articles')->where('aid', $id)->delete(); if ($delete != 0) { // 日志 DB::table('logs')->insert([ @@ -84,7 +85,7 @@ class ArticleController extends Controller return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); } - // $article = DB::table('article')->where('aid', $id)->first(); + // $article = DB::table('articles')->where('aid', $id)->first(); return view('insert', ['title'=>'文章发布'.$id]); } // 文章发布处理 @@ -101,7 +102,7 @@ class ArticleController extends Controller $content = $_POST['content']; // 发布 - $update = DB::table('article') + $update = DB::table('articles') ->insert([ 'img' => $img, 'img_alt' => $img_alt, @@ -143,7 +144,7 @@ class ArticleController extends Controller return response('错误的参数id')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); } - $article = DB::table('article')->where('aid', $id)->first(); + $article = DB::table('articles')->where('aid', $id)->first(); return view('update', ['title'=>'文章编辑'.$id, 'article'=>$article]); } // 文章修改处理 @@ -161,8 +162,10 @@ class ArticleController extends Controller $title2 = $_POST['title2']; $content = $_POST['content']; + // return response(dump($content));//debug + // 获取 - $update = DB::table('article') + $update = DB::table('articles') ->where('aid', $aid) ->update([ 'title' => $title1, @@ -196,7 +199,7 @@ class ArticleController extends Controller if (session('uEmail', 'null') == 'null') { return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); } - $delete = DB::table('article')->truncate(); + $delete = DB::table('articles')->truncate(); // 日志 DB::table('logs')->insert([ @@ -225,4 +228,35 @@ class ArticleController extends Controller // return response('未开发该功能')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); } + + // 博客检索api + public function searchAPI() + { + $where = $_GET['q']; + $articles = Article::where('aid', 'like', "%$where%") + ->orWhere('title', 'like', "%$where%") + ->orWhere('title2', 'like', "%$where%") + ->orWhere('content', 'like', "%$where%") + ->get(); + + return view('api.search', ['articles'=>$articles, 'msg'=>$where]); + + // return response(dump($articles)); + + // return response('未开发该功能')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); + } + // 单独文章页api + public function articleAPI($id = 'new') + { + if ($id == 'new'){ + $article = DB::table('articles')->orderBy('aid', 'desc')->first(); + // $article = DB::table('articles')->where('aid', $new->aid)->get(); + $title = '最新文章'; + $id = $article->aid; + }else{ + $article = DB::table('articles')->where('aid', $id)->first(); + } + + return view('api.article', ['title'=>$id, 'article'=>$article, 'id'=>$id]); + } } \ No newline at end of file diff --git a/app/Http/Controllers/userController.php b/app/Http/Controllers/userController.php index 7c9f61d7a5ce4dcb3d5b680ccc51679054932e16..71e6aeac8900b438b9e53a3b2eae69741245de58 100644 --- a/app/Http/Controllers/userController.php +++ b/app/Http/Controllers/userController.php @@ -26,7 +26,7 @@ class userController extends Controller $uPwd = $_POST['pwd']??'passwd'; // 查询数据库 - $data = DB::table('user')->where('uemail', $uEmail)->first(); + $data = DB::table('users')->where('uemail', $uEmail)->first(); // $data = DB::select('select * from user where uemail = ?', [$uEmail]); // 判断数据 @@ -113,14 +113,14 @@ class userController extends Controller $uOldPwd = $_POST['uOldPwd']??'passwd'; $uNewPwd = $_POST['uNewPwd']??'passwd'; - $data = DB::table('user')->where('uemail', $uEmail)->first(); + $data = DB::table('users')->where('uemail', $uEmail)->first(); if ($data == NULL){ return response('你没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); }elseif ($uOldPwd == $uNewPwd){ return response('新旧密码一致')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL')); }elseif ($uEmail == $data->uemail){ - $updata = DB::table('user')->where('uemail', $uEmail)->update(['upasswd'=>$uNewPwd]); + $updata = DB::table('users')->where('uemail', $uEmail)->update(['upasswd'=>$uNewPwd]); if ($updata) { DB::table('logs')->insert([ 'uid' => '1', diff --git a/resources/views/api/article.blade.php b/resources/views/api/article.blade.php new file mode 100644 index 0000000000000000000000000000000000000000..fe3d2edf085216deb54a922e83b0f5ffa95c9b88 --- /dev/null +++ b/resources/views/api/article.blade.php @@ -0,0 +1,11 @@ +{{-- resources\views\api\article.blade.php --}} +@php + header('content-type:text/plain;charset=utf-8'); + // 设置时区 + date_default_timezone_set(env('USER_TIMEZONE', 'PRC')); +@endphp +{{-- @dump($articles) --}} +@include('custom.phpFunction') + @php + echo json_encode($article); + @endphp diff --git a/resources/views/api/search.blade.php b/resources/views/api/search.blade.php new file mode 100644 index 0000000000000000000000000000000000000000..0dde08f36f4788f353ace932ae069058aa0d685d --- /dev/null +++ b/resources/views/api/search.blade.php @@ -0,0 +1,15 @@ +{{-- resources\views\api\article.blade.php --}} +@php + header('content-type:text/plain;charset=utf-8'); + // 设置时区 + date_default_timezone_set(env('USER_TIMEZONE', 'PRC')); + $articlesJSON = []; +@endphp +{{-- @dump($articles) --}} +@include('custom.phpFunction') + @foreach ($articles as $article) + @php + array_push($articlesJSON,json_encode($article)); + @endphp + @endforeach + {{ json_encode($articlesJSON) }} \ No newline at end of file diff --git a/resources/views/article.blade.php b/resources/views/article.blade.php index 014405c670cf19e8ff1e01d4f50356fe7da7ea6b..105608bfe5d8a8a7758dc6b3824afbc21cdb3d1d 100644 --- a/resources/views/article.blade.php +++ b/resources/views/article.blade.php @@ -13,6 +13,7 @@ {{-- 主体 --}}
@include('article.main') + {{-- @include('article.footer') --}}
{{-- 尾部 --}} diff --git a/resources/views/article/footer.blade.php b/resources/views/article/footer.blade.php new file mode 100644 index 0000000000000000000000000000000000000000..6f9bfeab6ba1c6c28d32e75e9cd926068e9c119b --- /dev/null +++ b/resources/views/article/footer.blade.php @@ -0,0 +1,26 @@ +{{-- resources\views\article\header.blade.php --}} +
+ {{-- ... --}} +
+
+
+
+

内容

+ +
+
+
+
+
\ No newline at end of file diff --git a/resources/views/article/header.blade.php b/resources/views/article/header.blade.php index f2a2dab0fcaf65d4c6f24a9a192f842e6b3efdbe..d911790424b75ebb721d8dcb592fd6e75d5f1047 100644 --- a/resources/views/article/header.blade.php +++ b/resources/views/article/header.blade.php @@ -1,6 +1,6 @@ {{-- resources\views\article\header.blade.php --}}
-
- {{-- @dump($articles) --}} + @include('custom.phpFunction') @foreach ($articles as $article) @php @@ -11,7 +11,7 @@
diff --git a/resources/views/update.blade.php b/resources/views/update.blade.php index ee050d1b08908142978e2ccfcae1c2e6ccbe6446..ced5c986a1b27411078b6dbaff9f749a313f638b 100644 --- a/resources/views/update.blade.php +++ b/resources/views/update.blade.php @@ -59,5 +59,4 @@
- @endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index a191e49d5adf4bdffa5dc38622824fd62913577c..ebc347574aca48183c1ea9d1bd88c62f199fac83 100644 --- a/routes/web.php +++ b/routes/web.php @@ -54,6 +54,13 @@ Route::get('test', function () { }); +//api +// 文章Search搜索 +Route::get('api/search', [ArticleController::class, 'searchAPI'])->name('searchapi'); +// 文章内容页 +Route::get('api/article/{id?}', [ArticleController::class, 'articleAPI'])->name('articleapi'); + + // 404 // 指定异常时的路由 Route::fallback(function () {