ArticleController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\system\controller;
  3. use app\model\api\ArticleModel;
  4. use think\Request;
  5. class ArticleController
  6. {
  7. // 获取文章列表
  8. public function getArticleList(Request $request)
  9. {
  10. $page = $request->param('page', 1);
  11. $pageSize = $request->param('pageSize', 10);
  12. $title = $request->param('title', '');
  13. $query = (new ArticleModel())->order('create_time', 'desc');
  14. if ($title) {
  15. $query->where('title', 'like', "%$title%");
  16. }
  17. $articles = $query->page($page, $pageSize)->select();
  18. $count = $query->count();
  19. $data['list'] = $articles;
  20. $data['count'] = $count;
  21. return app('json')->success($data);
  22. }
  23. //获取文章详情
  24. public function getArticleDetail(Request $request)
  25. {
  26. $id = $request->param('id');
  27. $article = ArticleModel::where('id', $id)->find();
  28. if (!$article) {
  29. return app('json')->fail('文章不存在');
  30. }
  31. return app('json')->success(['list' => [$article], 'count' => 1]);
  32. }
  33. // 添加文章
  34. public function addArticle(Request $request)
  35. {
  36. $data = $request->param();
  37. $article = new ArticleModel();
  38. $article->title = $data['title'];
  39. $article->content = $data['content'];
  40. $article->create_time = time();
  41. $article->save();
  42. return app('json')->success();
  43. }
  44. // 修改文章
  45. public function updateArticle(Request $request)
  46. {
  47. $id = $request->param('id');
  48. $data = $request->param();
  49. $article = ArticleModel::where('id', $id)->find();
  50. if (!$article) {
  51. return app('json')->fail('文章不存在');
  52. }
  53. $article->title = $data['title'];
  54. $article->content = $data['content'];
  55. $article->save();
  56. return app('json')->success();
  57. }
  58. // 删除文章
  59. public function deleteArticle(Request $request)
  60. {
  61. $id = $request->param('id');
  62. $article = ArticleModel::where('id', $id)->find();
  63. if (!$article) {
  64. return app('json')->fail('文章不存在');
  65. }
  66. $article->delete();
  67. return app('json')->success();
  68. }
  69. }