Article.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\article;
  12. use app\common\repositories\user\UserVisitRepository;
  13. use crmeb\services\SwooleTaskService;
  14. use think\App;
  15. use app\common\repositories\article\ArticleRepository as repository;
  16. use crmeb\basic\BaseController;
  17. /**
  18. * Class Article
  19. * app\controller\api\article
  20. * 文章
  21. */
  22. class Article extends BaseController
  23. {
  24. /**
  25. * @var repository
  26. */
  27. protected $repository;
  28. /**
  29. * StoreBrand constructor.
  30. * @param App $app
  31. * @param repository $repository
  32. */
  33. public function __construct(App $app, repository $repository)
  34. {
  35. parent::__construct($app);
  36. $this->repository = $repository;
  37. }
  38. /**
  39. * 列表
  40. * @param $cid
  41. * @return mixed
  42. * @author Qinii
  43. */
  44. public function lst($cid)
  45. {
  46. [$page, $limit] = $this->getPage();
  47. $where = ['status' => 1, 'cid' => $cid];
  48. return app('json')->success($this->repository->search(0, $where, $page, $limit));
  49. }
  50. /**
  51. * 详情
  52. * @param $id
  53. * @return mixed
  54. * @author Qinii
  55. */
  56. public function detail($id)
  57. {
  58. if (!$this->repository->merApiExists($id))
  59. return app('json')->fail('文章不存在');
  60. $data = $this->repository->getWith($id, ['content']);
  61. if ($this->request->isLogin()) {
  62. $uid = $this->request->uid();
  63. $make = app()->make(UserVisitRepository::class);
  64. $count = $make->search(['uid' => $uid, 'type' => 'article'])->where('type_id', $id)->whereTime('UserVisit.create_time', '>', date('Y-m-d H:i:s', strtotime('- 300 seconds')))->count();
  65. if (!$count) {
  66. SwooleTaskService::visit(intval($uid), $id, 'article');
  67. $this->repository->incVisit($id);
  68. }
  69. }
  70. return app('json')->success($data);
  71. }
  72. /**
  73. * 列表筛选
  74. * @return mixed
  75. * @author Qinii
  76. */
  77. public function list()
  78. {
  79. $where = ['status' => 1];
  80. return app('json')->success($this->repository->search(0, $where, 1, 9));
  81. }
  82. }