News.php 6.4 KB

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