ArticleController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\publics;
  12. use app\services\article\ArticleServices;
  13. /**
  14. * 文章类
  15. * Class ArticleController
  16. * @package app\api\controller\publics
  17. */
  18. class ArticleController
  19. {
  20. protected $services;
  21. public function __construct(ArticleServices $services)
  22. {
  23. $this->services = $services;
  24. }
  25. /**
  26. * 文章列表
  27. * @param $cid
  28. * @return mixed
  29. * @throws \ReflectionException
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function lst($cid)
  35. {
  36. if ($cid == 0) {
  37. $where = ['is_hot' => 1];
  38. } else {
  39. $where = ['cid' => $cid];
  40. }
  41. [$page, $limit] = $this->services->getPageValue();
  42. $list = $this->services->getList($where, $page, $limit)['list'];
  43. foreach ($list as &$item){
  44. $item['add_time'] = date('Y-m-d H:i', $item['add_time']);
  45. }
  46. return app('json')->success($list);
  47. }
  48. /**
  49. * 文章详情
  50. * @param $id
  51. * @return mixed
  52. * @throws \ReflectionException
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function details($id)
  58. {
  59. $info = $this->services->getInfo($id);
  60. return app('json')->success($info);
  61. }
  62. /**
  63. * 获取热门文章
  64. * @return mixed
  65. * @throws \ReflectionException
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function hot()
  71. {
  72. [$page, $limit] = $this->services->getPageValue();
  73. $list = $this->services->getList(['is_hot' => 1], $page, $limit)['list'];
  74. foreach ($list as &$item){
  75. $item['add_time'] = date('Y-m-d H:i', $item['add_time']);
  76. }
  77. return app('json')->success($list);
  78. }
  79. /**
  80. * @return mixed
  81. * @throws \ReflectionException
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function new()
  87. {
  88. [$page, $limit] = $this->services->getPageValue();
  89. $list = $this->services->getList([], $page, $limit)['list'];
  90. foreach ($list as &$item){
  91. $item['add_time'] = date('Y-m-d H:i', $item['add_time']);
  92. }
  93. return app('json')->success($list);
  94. }
  95. /**
  96. * 获取顶部banner文章
  97. * @return mixed
  98. * @throws \ReflectionException
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public function banner()
  104. {
  105. [$page, $limit] = $this->services->getPageValue();
  106. $list = $this->services->getList(['is_banner' => 1], $page, $limit)['list'];
  107. foreach ($list as &$item){
  108. $item['add_time'] = date('Y-m-d H:i', $item['add_time']);
  109. }
  110. return app('json')->success($list);
  111. }
  112. }