ListsController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: wuwu <15093565100@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\portal\controller;
  10. use api\portal\model\PortalCategoryModel;
  11. use api\portal\service\PortalPostService;
  12. use cmf\controller\RestBaseController;
  13. class ListsController extends RestBaseController
  14. {
  15. /**
  16. * 推荐文章列表
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function recommended()
  22. {
  23. $param = $this->request->param();
  24. $param['recommended'] = true;
  25. $portalPostService = new PortalPostService();
  26. $articles = $portalPostService->postArticles($param);
  27. //是否需要关联模型
  28. if (!$articles->isEmpty()) {
  29. if (!empty($param['relation'])) {
  30. $allowedRelations = allowed_relations(['user', 'categories'], $param['relation']);
  31. if (!empty($allowedRelations)) {
  32. $articles->load($allowedRelations);
  33. $articles->append($allowedRelations);
  34. }
  35. }
  36. }
  37. $this->success('ok', ['list' => $articles]);
  38. }
  39. /**
  40. * 分类文章列表
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function getCategoryPostLists()
  46. {
  47. $categoryId = $this->request->param('category_id', 0, 'intval');
  48. $portalCategoryModel = new PortalCategoryModel();
  49. $findCategory = $portalCategoryModel->where('id', $categoryId)->find();
  50. //分类是否存在
  51. if (empty($findCategory)) {
  52. $this->error('分类不存在!');
  53. }
  54. $param = $this->request->param();
  55. $portalPostService = new PortalPostService();
  56. $articles = $portalPostService->postArticles($param);
  57. //是否需要关联模型
  58. if (!$articles->isEmpty()) {
  59. if (!empty($param['relation'])) {
  60. $allowedRelations = allowed_relations(['user', 'categories'], $param['relation']);
  61. if (!empty($allowedRelations)) {
  62. $articles->load($allowedRelations);
  63. $articles->append($allowedRelations);
  64. }
  65. }
  66. }
  67. $this->success('ok', ['list' => $articles]);
  68. }
  69. }