Message.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace addons\ddrive\controller;
  3. use addons\ddrive\library\Wechat;
  4. use addons\ddrive\model\MessageComment;
  5. use app\common\controller\Api;
  6. use think\Db;
  7. /**
  8. * 话题接口
  9. */
  10. class Message extends Api
  11. {
  12. protected $noNeedLogin = ['index', 'info', 'comments','notice','notice_info'];
  13. protected $noNeedRight = ['*'];
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new \addons\ddrive\model\Message;
  18. }
  19. /**
  20. * 话题接口
  21. *
  22. * @return void
  23. */
  24. public function index()
  25. {
  26. $model = $this->model;
  27. $pageSize = $this->request->param('pageSize', 10);
  28. $map = [];
  29. $query = $model->order('weigh desc,id desc');
  30. // 查找文章标题或用户昵称
  31. if ($this->request->param('keywords')) {
  32. $query->where(function ($querys) {
  33. $querys->where('title', 'LIKE', '%' . $this->request->param('keywords') . '%');
  34. $userIds = Db::name('user')->where('nickname', 'LIKE', '%' . $this->request->param('keywords') . '%')->column('id');
  35. if ($userIds) {
  36. $querys->whereOr('user_id', 'in', $userIds);
  37. }
  38. });
  39. }
  40. $list = $query->paginate($pageSize)->each(function ($item) {
  41. $item['images'] = $item['images'] ? explode(',', $item['images']) : [];
  42. $item['user'] = $item['user'];
  43. return $item;
  44. });
  45. $this->success("", $list);
  46. }
  47. /**
  48. * 我创建的话题
  49. *
  50. * @return void
  51. */
  52. public function my()
  53. {
  54. $model = $this->model;
  55. $pageSize = $this->request->param('pageSize', 10);
  56. $map = [];
  57. $map['user_id'] = $this->auth->id;
  58. if ($this->request->param('keywords')) {
  59. $map['title'] = ['LIKE', '%' . $this->request->param('keywords') . '%'];
  60. }
  61. $list = $model->where($map)->order('weigh desc,id desc')->paginate($pageSize)->each(function ($item) {
  62. $item['user'] = $item['user'];
  63. return $item;
  64. });
  65. $this->success("", $list);
  66. }
  67. /**
  68. * 创建话题
  69. *
  70. * @return void
  71. */
  72. public function add()
  73. {
  74. $data = [
  75. 'title' => $this->request->post('title'),
  76. 'content' => $this->request->post('content'),
  77. 'images' => $this->request->post('images'),
  78. 'user_id' => $this->auth->id,
  79. ];
  80. if (!$data['title']) {
  81. $this->error('请填写标题');
  82. }
  83. // 内容安全检测
  84. if (!Wechat::msgSecCheck($data['title'])) {
  85. $this->error('标题含有敏感内容,请修改后重新提交');
  86. }
  87. if (!Wechat::msgSecCheck($data['content'])) {
  88. $this->error('内容含有敏感内容,请修改后重新提交');
  89. }
  90. $model = $this->model;
  91. $res = $model->data($data)->save();
  92. if ($res) {
  93. // 增加会员积分
  94. $pointLib = new \addons\ddrive\library\Point;
  95. $pointLib->messageAdd($data);
  96. $this->success('发布成功');
  97. } else {
  98. $this->error('发布失败');
  99. }
  100. }
  101. /**
  102. * 获取详情
  103. *
  104. * @return void
  105. */
  106. public function info()
  107. {
  108. $messageId = $this->request->param('message_id');
  109. $info = $this->model->get($messageId);
  110. $info['user'] = $info['user'];
  111. $this->success('', $info);
  112. }
  113. /**
  114. * 删除
  115. *
  116. * @return void
  117. */
  118. public function delete()
  119. {
  120. $messageId = $this->request->param('message_id');
  121. $info = $this->model->where('id', $messageId)->where('user_id', $this->auth->id)->find();
  122. if (!$info) {
  123. $this->error('话题不存在');
  124. }
  125. $res = $this->model->where('id', $messageId)->where('user_id', $this->auth->id)->delete();
  126. if ($res) {
  127. // 删除评论
  128. MessageComment::where('message_id', $messageId)->delete();
  129. $this->success("删除成功");
  130. } else {
  131. $this->error('删除失败');
  132. }
  133. }
  134. /**
  135. * 评论列表
  136. *
  137. * @return void
  138. */
  139. public function comments()
  140. {
  141. $model = new MessageComment();
  142. $messageId = $this->request->param('message_id');
  143. $pageSize = $this->request->param('pageSize', 10);
  144. $map = [];
  145. $map['message_id'] = $messageId;
  146. $list = $model->where($map)->order('id desc')->paginate($pageSize)->each(function ($item) {
  147. $item['user'] = $item['user'];
  148. return $item;
  149. });
  150. $this->success("", $list);
  151. }
  152. /**
  153. * 评论
  154. *
  155. * @return void
  156. */
  157. public function addComment()
  158. {
  159. $messageId = $this->request->param('message_id');
  160. $info = $this->model->where('id', $messageId)->find();
  161. if (!$info) {
  162. $this->error('话题不存在');
  163. }
  164. $comment = $this->request->param('comment');
  165. $MessageComment = new MessageComment;
  166. if (!Wechat::msgSecCheck($comment)) {
  167. $this->error('评论内容含有敏感内容,请修改后重新提交');
  168. }
  169. $res = $MessageComment->data([
  170. 'user_id' => $this->auth->id,
  171. 'message_id' => $messageId,
  172. 'comment' => $comment,
  173. ])->save();
  174. if ($res) {
  175. // 增加会员积分
  176. $pointLib = new \addons\ddrive\library\Point;
  177. $pointLib->commentAdd($info);
  178. $this->success("评论成功");
  179. } else {
  180. $this->error('评论失败');
  181. }
  182. }
  183. /**
  184. * 文章
  185. * @ApiMethod (POST)
  186. * @param File $file 文件流
  187. */
  188. public function notice()
  189. {
  190. $category_id = $this->request->param('category_id');
  191. $list = Db::name('new_info')
  192. ->where('category_id', $category_id)
  193. ->field('title,id,createtime,new_info_img')
  194. ->order('id desc')
  195. ->limit(10)
  196. ->select();
  197. if ($list) {
  198. foreach ($list as &$v) {
  199. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  200. $v['new_info_img'] = cdnurl($v['new_info_img']);
  201. }
  202. }
  203. $this->success('成功',['list_array'=>$list ? $list : []]);
  204. }
  205. /**
  206. * 文章详情
  207. * @ApiMethod (POST)
  208. * @param File $file 文件流
  209. */
  210. public function notice_info()
  211. {
  212. $id = $this->request->param('id');
  213. if (empty($id)) {
  214. $this->error('参数错误');
  215. }
  216. $list = Db::name('new_info')
  217. ->where('id', $id)
  218. ->field('title,id,createtime,content')
  219. ->find();
  220. if (!$list) {
  221. $this->error('该文章不存在');
  222. }
  223. $list['createtime'] = date('Y-m-d H:i:s', $list['createtime']);
  224. $this->success('成功',['list_array'=>$list ? $list : []]);
  225. }
  226. }