News.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\data\controller\api;
  3. use app\data\service\NewsService;
  4. use think\admin\Controller;
  5. /**
  6. * 文章接口控制器
  7. * Class News
  8. * @package app\data\controller\api
  9. */
  10. class News extends Controller
  11. {
  12. /**
  13. * 获取文章标签列表
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\DbException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. */
  18. public function getMark()
  19. {
  20. $query = $this->_query('DataNewsMark')->like('name');
  21. $query->where(['status' => 1, 'deleted' => 0])->withoutField('sort,status,deleted');
  22. $this->success('获取文章标签', $query->order('sort desc,id desc')->page(false, false));
  23. }
  24. /**
  25. * 获取文章内容列表
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function getItem()
  31. {
  32. if ($code = input('code', '')) {
  33. $this->app->db->name('DataNewsItem')->where(['code' => $code])->inc('num_read')->update();
  34. if (($uid = input('uid', 0)) > 0) {
  35. $data = ['uid' => $uid, 'code' => $code, 'type' => 3, 'status' => 2];
  36. $this->app->db->name('DataNewsXCollect')->where($data)->delete();
  37. $this->app->db->name('DataNewsXCollect')->insert($data);
  38. }
  39. }
  40. $query = $this->_query('DataNewsItem')->like('name,mark')->equal('id,code');
  41. $query->where(['deleted' => 0, 'status' => 1])->withoutField('sort,status,deleted');
  42. $result = $query->order('sort desc,id desc')->page(true, false, false, 15);
  43. NewsService::instance()->buildListState($result['list'], input('uid', 0));
  44. $this->success('获取文章内容', $result);
  45. }
  46. /**
  47. * 获取文章评论
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getComment()
  53. {
  54. $map = $this->_vali(['code.require' => '文章不能为空!']);
  55. $query = $this->_query('DataNewsXCollect')->where(['type' => 4, 'status' => 2]);
  56. $result = $query->where($map)->order('id desc')->page(true, false, false, 15);
  57. NewsService::instance()->buildListByUidAndCode($result['list']);
  58. $this->success('获取评论成功', $result);
  59. }
  60. }