News.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\data\controller\api\auth;
  3. use app\data\controller\api\Auth;
  4. use app\data\model\DataNewsXCollect;
  5. use app\data\service\NewsService;
  6. /**
  7. * 文章评论内容
  8. * Class News
  9. * @package app\data\controller\api\auth
  10. */
  11. class News extends Auth
  12. {
  13. /**
  14. * 用户评论内容
  15. * @throws \think\db\exception\DbException
  16. */
  17. public function addComment()
  18. {
  19. $data = $this->_vali([
  20. 'uuid.value' => $this->uuid,
  21. 'type.value' => 4,
  22. 'status.value' => 1,
  23. 'code.require' => '文章不能为空!',
  24. 'reply.require' => '评论不能为空!',
  25. ]);
  26. if (DataNewsXCollect::mk()->insert($data) !== false) {
  27. NewsService::instance()->syncNewsTotal($data['code']);
  28. $this->success('添加评论成功!');
  29. } else {
  30. $this->error('添加评论失败!');
  31. }
  32. }
  33. /**
  34. * 获取我的评论
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function getComment()
  40. {
  41. $query = $this->_query(DataNewsXCollect::class)->where(['uuid' => $this->uuid, 'type' => 4]);
  42. $result = $query->whereIn('status', [1, 2])->order('id desc')->page(true, false, false, 15);
  43. NewsService::instance()->buildListByUidAndCode($result);
  44. $this->success('获取评论列表成功', $result);
  45. }
  46. /**
  47. * 删除内容评论
  48. */
  49. public function delComment()
  50. {
  51. $data = $this->_vali([
  52. 'uuid.value' => $this->uuid,
  53. 'type.value' => 4,
  54. 'id.require' => '评论编号不能为空!',
  55. 'code.require' => '文章编号不能为空!',
  56. ]);
  57. if (DataNewsXCollect::mk()->where($data)->delete() !== false) {
  58. $this->success('评论删除成功!');
  59. } else {
  60. $this->error('认证删除失败!');
  61. }
  62. }
  63. /**
  64. * 添加内容收藏
  65. * @throws \think\db\exception\DbException
  66. */
  67. public function addCollect()
  68. {
  69. $data = $this->_vali([
  70. 'uuid.value' => $this->uuid,
  71. 'type.value' => 1,
  72. 'status.value' => 2,
  73. 'code.require' => '文章编号不能为空!',
  74. ]);
  75. if (DataNewsXCollect::mk()->where($data)->count() > 0) {
  76. $this->success('您已收藏!');
  77. }
  78. if (DataNewsXCollect::mk()->insert($data) !== false) {
  79. NewsService::instance()->syncNewsTotal($data['code']);
  80. $this->success('收藏成功!');
  81. } else {
  82. $this->error('收藏失败!');
  83. }
  84. }
  85. /**
  86. * 取消收藏文章
  87. * @throws \think\db\exception\DbException
  88. */
  89. public function delCollect()
  90. {
  91. $data = $this->_vali([
  92. 'uuid.value' => $this->uuid,
  93. 'type.value' => 1,
  94. 'code.require' => '文章编号不能为空!',
  95. ]);
  96. if (DataNewsXCollect::mk()->where($data)->delete() !== false) {
  97. NewsService::instance()->syncNewsTotal($data['code']);
  98. $this->success('取消收藏成功!');
  99. } else {
  100. $this->error('取消收藏失败!');
  101. }
  102. }
  103. /**
  104. * 获取用户收藏的资讯
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. public function getCollect()
  110. {
  111. $map = ['uuid' => $this->uuid, 'type' => 1];
  112. $query = $this->_query('DataNewsXCollect')->where($map);
  113. $result = $query->order('id desc')->page(true, false, false, 15);
  114. NewsService::instance()->buildListByUidAndCode($result['list']);
  115. $this->success('获取收藏记录成功!', $result);
  116. }
  117. /**
  118. * 添加内容点赞
  119. * @throws \think\db\exception\DbException
  120. */
  121. public function addLike()
  122. {
  123. $data = $this->_vali([
  124. 'uuid.value' => $this->uuid,
  125. 'type.value' => 2,
  126. 'status.value' => 2,
  127. 'code.require' => '文章编号不能为空!',
  128. ]);
  129. if (DataNewsXCollect::mk()->where($data)->count() > 0) {
  130. $this->success('您已点赞!');
  131. }
  132. if (DataNewsXCollect::mk()->insert($data) !== false) {
  133. NewsService::instance()->syncNewsTotal($data['code']);
  134. $this->success('点赞成功!');
  135. } else {
  136. $this->error('点赞失败!');
  137. }
  138. }
  139. /**
  140. * 取消内容点赞
  141. * @throws \think\db\exception\DbException
  142. */
  143. public function delLike()
  144. {
  145. $data = $this->_vali([
  146. 'uuid.value' => $this->uuid,
  147. 'type.value' => 2,
  148. 'code.require' => '文章编号不能为空!',
  149. ]);
  150. if (DataNewsXCollect::mk()->where($data)->delete() !== false) {
  151. NewsService::instance()->syncNewsTotal($data['code']);
  152. $this->success('取消点赞成功!');
  153. } else {
  154. $this->error('取消点赞失败!');
  155. }
  156. }
  157. /**
  158. * 获取用户收藏的资讯
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function getLike()
  164. {
  165. $query = $this->_query(DataNewsXCollect::class);
  166. $query->where(['uuid' => $this->uuid, 'type' => 2, 'status' => 2]);
  167. $result = $query->order('id desc')->page(true, false, false, 15);
  168. NewsService::instance()->buildListByUidAndCode($result['list']);
  169. $this->success('获取点赞记录成功!', $result);
  170. }
  171. /**
  172. * 添加用户的浏览历史
  173. * @throws \think\db\exception\DbException
  174. */
  175. public function addHistory()
  176. {
  177. $data = $this->_vali([
  178. 'uuid.value' => $this->uuid,
  179. 'type.value' => 2,
  180. 'status.value' => 2,
  181. 'code.require' => '文章编号不能为空!',
  182. ]);
  183. DataNewsXCollect::mk()->where($data)->delete();
  184. DataNewsXCollect::mk()->insert($data);
  185. $this->success('添加浏览历史成功!');
  186. }
  187. /**
  188. * 获取用户的浏览历史
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\DbException
  191. * @throws \think\db\exception\ModelNotFoundException
  192. */
  193. public function getHistory()
  194. {
  195. $query = $this->_query(DataNewsXCollect::class);
  196. $query->where(['uuid' => $this->uuid, 'type' => 3, 'status' => 2]);
  197. $result = $query->order('id desc')->page(true, false, false, 15);
  198. NewsService::instance()->buildListByUidAndCode($result['list']);
  199. $this->success('获取浏览历史成功!', $result);
  200. }
  201. }