|
@@ -0,0 +1,100 @@
|
|
|
|
+<?php
|
|
|
|
+declare (strict_types = 1);
|
|
|
|
+
|
|
|
|
+namespace app\api\controller;
|
|
|
|
+
|
|
|
|
+use app\BaseController;
|
|
|
|
+use think\App;
|
|
|
|
+use think\exception\HttpException;
|
|
|
|
+
|
|
|
|
+use app\model\Category;
|
|
|
|
+use app\model\Article;
|
|
|
|
+use app\utils\ParsedownUtils;
|
|
|
|
+
|
|
|
|
+class Index extends BaseController
|
|
|
|
+{
|
|
|
|
+ /**
|
|
|
|
+ * 构造方法
|
|
|
|
+ * @access public
|
|
|
|
+ * @param App $app 应用对象
|
|
|
|
+ */
|
|
|
|
+ public function __construct(App $app)
|
|
|
|
+ {
|
|
|
|
+ $this->app = $app;
|
|
|
|
+ $this->request = $this->app->request;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function index()
|
|
|
|
+ {
|
|
|
|
+ return '您好!这是一个[api]示例应用';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function menu()
|
|
|
|
+ {
|
|
|
|
+ return json(Category::where('is_nav', 1)->order(['sort desc'])->select());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function list()
|
|
|
|
+ {
|
|
|
|
+ $params = $this->app->request->param();
|
|
|
|
+
|
|
|
|
+ $cid = isset($params['cid']) ? (int)$params['cid'] : 0;
|
|
|
|
+
|
|
|
|
+ $list = Article::queryPage($params);
|
|
|
|
+
|
|
|
|
+ $category = Category::find($cid);
|
|
|
|
+
|
|
|
|
+ if ($category) {
|
|
|
|
+ $baseUrl = $category->url;
|
|
|
|
+ } else {
|
|
|
|
+ $baseUrl = '/index/all';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return json([
|
|
|
|
+ 'baseUrl' => $baseUrl,
|
|
|
|
+ 'list' => $list->all(),
|
|
|
|
+ 'total' => $list->total(),
|
|
|
|
+ 'limit' => $list->listRows(),
|
|
|
|
+ 'page' => $list->currentPage(),
|
|
|
|
+ 'lastPage' => $list->lastPage(),
|
|
|
|
+ 'cid' => $cid,
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 文章详情
|
|
|
|
+ */
|
|
|
|
+ public function read($id = null)
|
|
|
|
+ {
|
|
|
|
+ $data = Article::getOne($id);
|
|
|
|
+
|
|
|
|
+ if (!$data) {
|
|
|
|
+ return json(['msg'=>'页面不存在'], 404);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $data->hits += 1;
|
|
|
|
+
|
|
|
|
+ $data->save();
|
|
|
|
+
|
|
|
|
+ $prev_next = Article::getNextPrev($id, $data->cid);
|
|
|
|
+
|
|
|
|
+ if ($data->content_type == 1) {
|
|
|
|
+ $parsedownExtension = new ParsedownUtils();
|
|
|
|
+ $parsedownExtension->setTocEnabled(true);
|
|
|
|
+ $res = $parsedownExtension->text($data->content);
|
|
|
|
+ $data->toc = $res['toc'];
|
|
|
|
+ $data->content = $res['content'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return json([
|
|
|
|
+ 'cid' => $data->cid,
|
|
|
|
+ 'data' => $data,
|
|
|
|
+ 'prev_next' => $prev_next,
|
|
|
|
+ 'seo' => [
|
|
|
|
+ 'title' => $data->title,
|
|
|
|
+ 'key' => $data->keywords,
|
|
|
|
+ 'des' => $data->summary,
|
|
|
|
+ ]
|
|
|
|
+ ]);
|
|
|
|
+ }
|
|
|
|
+}
|