Article.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\content\Category;
  4. use app\common\model\content\Article as ArticleModel;
  5. /**
  6. * 地址
  7. */
  8. class Article extends Base
  9. {
  10. protected $noNeedLogin = ['getCategoryList', 'getArticleList', 'getArticleInfo'];
  11. /**
  12. * 分类列表
  13. */
  14. public function getCategoryList()
  15. {
  16. $parentId = $this->request->param('parent_id', 0, 'intval');
  17. $page = $this->request->param('page', 1, 'intval');
  18. $pageSize = $this->request->param('pageSize', 10, 'intval');
  19. // 如果存在parent_id,先查询父分类信息
  20. if ($parentId > 0) {
  21. $parentCategory = Category::get($parentId);
  22. if (!$parentCategory) {
  23. $this->error('父分类不存在');
  24. }
  25. // 处理父分类图片
  26. $parentCategory['image'] = $parentCategory['image'] ? cdnurl($parentCategory['image']) : '';
  27. // 获取子分类列表
  28. $children = Category::getCategoryList($parentId, $page, $pageSize);
  29. // 处理子分类图片
  30. $children->each(function ($item) {
  31. $item['image'] = $item['image'] ? cdnurl($item['image']) : '';
  32. });
  33. // 将子分类添加到父分类的children字段
  34. $parentCategory['children'] = $children;
  35. $parentCategory['total'] = $children->total();
  36. $parentCategory['per_page'] = $children->listRows();
  37. $parentCategory['current_page'] = $children->currentPage();
  38. $parentCategory['last_page'] = $children->lastPage();
  39. $this->success('', $parentCategory);
  40. } else {
  41. // 如果没有parent_id,直接返回顶级分类列表
  42. $list = Category::getCategoryList($parentId, $page, $pageSize);
  43. // 处理图片
  44. $list->each(function ($item) {
  45. $item['image'] = $item['image'] ? cdnurl($item['image']) : '';
  46. });
  47. $this->success('', $list);
  48. }
  49. }
  50. /**
  51. * 文章列表
  52. */
  53. public function getArticleList()
  54. {
  55. $categoryId = $this->request->param('category_id', 0, 'intval');
  56. $page = $this->request->param('page', 1, 'intval');
  57. $pageSize = $this->request->param('pageSize', 10, 'intval');
  58. $list = ArticleModel::getArticleList($categoryId, $page, $pageSize);
  59. $list->each(function ($item) {
  60. // 处理图像的
  61. $item['image'] = $item['image'] ? cdnurl($item['image']) : '';
  62. });
  63. $this->success('获取成功', $list);
  64. }
  65. //文章详情
  66. public function getArticleInfo()
  67. {
  68. $id = $this->request->param('id', 0, 'intval');
  69. // 验证id是否为空
  70. if (empty($id)) {
  71. $this->error('id不能为空');
  72. }
  73. $info = ArticleModel::getArticleInfo($id);
  74. //判断是否为空
  75. if (!empty($info)) {
  76. $info['image'] = $info['image'] ? cdnurl($info['image']) : '';
  77. }
  78. $this->success('获取成功', $info);
  79. }
  80. }