123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- declare (strict_types = 1);
- namespace app\api\controller;
- use think\App;
- use think\exception\HttpException;
- use app\common\model\Category;
- use app\common\model\Article;
- use app\common\model\ArticleTags;
- use app\common\model\ArticleDolikeLog;
- class Index
- {
- /**
- * 构造方法
- * @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 \ParsedownExtension();
- $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,
- ]
- ]);
- }
- }
|