Index.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\controller;
  4. use think\App;
  5. use think\exception\HttpException;
  6. use app\common\model\Category;
  7. use app\common\model\Article;
  8. use app\common\model\ArticleTags;
  9. use app\common\model\ArticleDolikeLog;
  10. class Index
  11. {
  12. /**
  13. * 构造方法
  14. * @access public
  15. * @param App $app 应用对象
  16. */
  17. public function __construct(App $app)
  18. {
  19. $this->app = $app;
  20. $this->request = $this->app->request;
  21. }
  22. public function index()
  23. {
  24. return '您好!这是一个[api]示例应用';
  25. }
  26. public function menu()
  27. {
  28. return json(Category::where('is_nav', 1)->order(['sort desc'])->select());
  29. }
  30. public function list()
  31. {
  32. $params = $this->app->request->param();
  33. $cid = isset($params['cid']) ? (int)$params['cid'] : 0;
  34. $list = Article::queryPage($params);
  35. $category = Category::find($cid);
  36. if ($category) {
  37. $baseUrl = $category->url;
  38. } else {
  39. $baseUrl = '/index/all';
  40. }
  41. return json([
  42. 'baseUrl' => $baseUrl,
  43. 'list' => $list->all(),
  44. 'total' => $list->total(),
  45. 'limit' => $list->listRows(),
  46. 'page' => $list->currentPage(),
  47. 'lastPage' => $list->lastPage(),
  48. 'cid' => $cid,
  49. ]);
  50. }
  51. /**
  52. * 文章详情
  53. */
  54. public function read($id = null)
  55. {
  56. $data = Article::getOne($id);
  57. if (!$data) {
  58. return json(['msg'=>'页面不存在'], 404);
  59. }
  60. $data->hits += 1;
  61. $data->save();
  62. $prev_next = Article::getNextPrev($id, $data->cid);
  63. if ($data->content_type == 1) {
  64. $parsedownExtension = new \ParsedownExtension();
  65. $parsedownExtension->setTocEnabled(true);
  66. $res = $parsedownExtension->text($data->content);
  67. $data->toc = $res['toc'];
  68. $data->content = $res['content'];
  69. }
  70. return json([
  71. 'cid' => $data->cid,
  72. 'data' => $data,
  73. 'prev_next' => $prev_next,
  74. 'seo' => [
  75. 'title' => $data->title,
  76. 'key' => $data->keywords,
  77. 'des' => $data->summary,
  78. ]
  79. ]);
  80. }
  81. }