Topicdongtai.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Keyworld;
  6. /**
  7. * 圈子动态
  8. */
  9. class Topicdongtai extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //发布动态
  14. public function addone(){
  15. $content = input('content','');
  16. $images = input('images','');
  17. $topic_id = input('topic_id','');
  18. if(!$content && !$images){
  19. $this->error(__('Invalid parameters'));
  20. }
  21. //关键字替换
  22. //$content = Keyworld::sensitive($content);
  23. $data = [
  24. 'topic_id' => $topic_id,
  25. 'user_id' => $this->auth->id,
  26. 'content' => $content,
  27. 'images' => $images,
  28. 'type' => input('type',1),
  29. 'createtime' => time(),
  30. 'updatetime' => time(),
  31. ];
  32. Db::startTrans();
  33. $id = Db::name('topic_dongtai')->insertGetId($data);
  34. //圈子新增一个贴
  35. $rs = Db::name('topic_hub')->where('id',$topic_id)->setInc('t_number');
  36. Db::commit();
  37. $this->success('发布成功',$id);
  38. }
  39. //自己看列表
  40. //某用户的帖子列表
  41. public function my_lists(){
  42. $uid = input('uid',$this->auth->id);
  43. $list = Db::name('topic_dongtai')->alias('dt')
  44. ->join('user','dt.user_id = user.id','LEFT')
  45. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  46. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  47. ->where('dt.user_id',$uid)
  48. ->order('dt.id desc')->autopage()->select();
  49. $list = list_domain_image($list,['images','avatar']);
  50. if(!empty($list)){
  51. foreach($list as $key => &$val){
  52. //追加点赞
  53. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  54. //创建视频缩略图
  55. $val['images_thumb'] = '';
  56. if ($val['type'] == 2) {
  57. $images_url = explode('.', $val['images']);
  58. unset($images_url[count($images_url) - 1]);
  59. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  60. }
  61. //时间
  62. $val['createtime_text'] = get_last_time($val['createtime']);
  63. //关注
  64. $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
  65. //评论
  66. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  67. }
  68. }
  69. $this->success('success',$list);
  70. }
  71. //是否点赞
  72. private function is_good($dt_id,$uid){
  73. $where = [
  74. 'dt_id' => $dt_id,
  75. 'user_id' => $uid,
  76. ];
  77. $check = Db::name('topic_dongtai_good')->where($where)->find();
  78. if($check){
  79. return 1;
  80. }else{
  81. return 0;
  82. }
  83. }
  84. //回复是否点赞
  85. private function answer_is_good($answer_id,$uid){
  86. $where = [
  87. 'answer_id' => $answer_id,
  88. 'user_id' => $uid,
  89. ];
  90. $check = Db::name('topic_answer_good')->where($where)->find();
  91. if($check){
  92. return 1;
  93. }else{
  94. return 0;
  95. }
  96. }
  97. //是否关注
  98. private function is_follow($user_id,$fans_id){
  99. $where = [
  100. 'user_id' => $user_id,
  101. 'fans_id' => $fans_id,
  102. ];
  103. $check = Db::name('user_fans_follow')->where($where)->find();
  104. if($check){
  105. return 1;
  106. }else{
  107. return 0;
  108. }
  109. }
  110. //详情
  111. public function info(){
  112. $id = input('id');
  113. $info = Db::name('topic_dongtai')->alias('dt')
  114. ->join('user','dt.user_id = user.id','LEFT')
  115. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  116. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  117. ->where('dt.id',$id)->find();
  118. $info = info_domain_image($info,['images','avatar']);
  119. if($info){
  120. //是否点赞过
  121. $info['isgood'] = $this->is_good($id,$this->auth->id);
  122. //创建视频缩略图
  123. $info['images_thumb'] = '';
  124. if ($info['type'] == 2) {
  125. $images_url = explode('.', $info['images']);
  126. unset($images_url[count($images_url) - 1]);
  127. $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
  128. }
  129. //时间
  130. $info['createtime_text'] = get_last_time($info['createtime']);
  131. //关注
  132. $info['is_follow'] = $this->is_follow($info['user_id'],$this->auth->id);
  133. //评论
  134. $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
  135. //$info['answer'] = $this->answer_list($id);
  136. }
  137. $this->success('success',$info);
  138. }
  139. //点赞
  140. public function good(){
  141. $id = input('id');
  142. $where = [
  143. 'dt_id' => $id,
  144. 'user_id' => $this->auth->id,
  145. ];
  146. $check = Db::name('topic_dongtai_good')->where($where)->find();
  147. if($check){
  148. $this->error('已经赞过了');
  149. }
  150. Db::startTrans();
  151. $where['createtime'] = time();
  152. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  153. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  154. if($rs && $up !== false){
  155. Db::commit();
  156. $this->success('点赞成功');
  157. }
  158. Db::rollback();
  159. $this->error('点赞失败');
  160. }
  161. //评论
  162. public function answer(){
  163. $id = input('id',0);
  164. $content = input('content','');
  165. $to_user_id = input('to_user_id',0);
  166. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  167. $floor = input('floor',0);
  168. if(empty($content) || empty($id)){
  169. $this->error();
  170. }
  171. //关键字替换
  172. //$content = Keyworld::sensitive($content);
  173. //判断
  174. if($level == 2 && $floor == 0){
  175. $this->error('楼层错误');
  176. }
  177. //回复楼主,最新楼层
  178. if($level == 1 || $floor == 0){
  179. $to_user_id = 0;
  180. $floor = 1; //默认一楼
  181. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  182. if($last_floor){
  183. $floor = $last_floor + 1;
  184. }
  185. }
  186. //判断user_id
  187. if($to_user_id){
  188. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  189. if(empty($to_user)){
  190. $this->error('被回复的用户不存在');
  191. }
  192. }
  193. //data
  194. $data = [
  195. 'dt_id' => $id,
  196. 'floor' => $floor,
  197. 'user_id' => $this->auth->id,
  198. 'content' => $content,
  199. 'to_user_id' => $to_user_id,
  200. 'level' => $level,
  201. 'createtime' => time(),
  202. 'updatetime' => time(),
  203. ];
  204. Db::startTrans();
  205. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  206. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  207. Db::commit();
  208. $this->success('评价成功');
  209. }
  210. //对评论点赞
  211. public function answer_good(){
  212. $dt_id = input('dt_id',0);
  213. $answer_id = input('answer_id',0);
  214. $where = [
  215. 'dt_id' => $dt_id,
  216. 'answer_id' => $answer_id,
  217. 'user_id' => $this->auth->id,
  218. ];
  219. $check = Db::name('topic_answer_good')->where($where)->find();
  220. if($check){
  221. $this->error('已经赞过了');
  222. }
  223. Db::startTrans();
  224. $where['createtime'] = time();
  225. $rs = Db::name('topic_answer_good')->insertGetId($where);
  226. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  227. if($rs && $up !== false){
  228. Db::commit();
  229. $this->success('点赞成功');
  230. }
  231. Db::rollback();
  232. $this->error('点赞失败');
  233. }
  234. //举报枚举
  235. public function report_enum(){
  236. $arr = [
  237. '侮辱谩骂',
  238. '色情低俗',
  239. '整治敏感',
  240. '违法违规',
  241. '其他',
  242. ];
  243. $this->success(1,$arr);
  244. }
  245. //举报
  246. public function report(){
  247. $field = ['dt_id','type','content','images'];
  248. $data = request_post_hub($field);
  249. $check = Db::name('topic_dongtai')->where('id',$data['dt_id'])->find();
  250. $data['user_id'] = $this->auth->id;
  251. $data['ruser_id'] = $check['user_id'];
  252. $data['createtime'] = time();
  253. Db::name('topic_dongtai_report')->insertGetId($data);
  254. $this->success('举报成功');
  255. }
  256. //不感兴趣,屏蔽某条
  257. public function screen(){
  258. $data = [
  259. 'user_id' => $this->auth->id,
  260. 'dt_id' => input('dt_id',0),
  261. ];
  262. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  263. if($check){
  264. $this->success('操作成功');
  265. }
  266. Db::name('topic_dongtai_screen')->insertGetId($data);
  267. $this->success('操作成功');
  268. }
  269. //评论列表
  270. public function answer_list(){
  271. $dt_id = input('dt_id',0);
  272. //楼
  273. $floor_list = Db::name('topic_dongtai_answer')
  274. ->alias('a')
  275. ->field('a.*,user.nickname,user.avatar,user.gender')
  276. ->join('user','a.user_id = user.id','LEFT')
  277. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('id asc')->autopage()->select();
  278. $floor_list = list_domain_image($floor_list,['avatar']);
  279. //追加子评论
  280. if(!empty($floor_list)){
  281. foreach($floor_list as $key => &$val){
  282. //下面几条子回复,字符串
  283. $val['childremark'] = '';
  284. $map = [
  285. 'a.dt_id' => $dt_id,
  286. 'a.floor' => $val['floor'],
  287. 'a.level' => 2,
  288. ];
  289. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  290. if($number > 0){
  291. $answer_info = Db::name('topic_dongtai_answer')
  292. ->alias('a')
  293. ->field('user.nickname')
  294. ->join('user','a.user_id = user.id','LEFT')
  295. ->where($map)->order('a.id desc')->find();
  296. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  297. }
  298. //时间处理
  299. $val['createtime_text'] = get_last_time($val['createtime']);
  300. //回复是否已赞
  301. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  302. }
  303. }
  304. $this->success(1,$floor_list);
  305. }
  306. //单独某一层的详细
  307. public function answer_info(){
  308. $answer_id = input('answer_id');
  309. //楼
  310. $floor_info = Db::name('topic_dongtai_answer')
  311. ->alias('a')
  312. ->field('a.*,user.nickname,user.avatar,user.gender')
  313. ->join('user','a.user_id = user.id','LEFT')
  314. ->where(['a.id'=>$answer_id])->find();
  315. $floor_info = info_domain_image($floor_info,['avatar']);
  316. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  317. //回复是否已赞
  318. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  319. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  320. //层
  321. $floors = $floor_info['floor'];
  322. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  323. ->field('a.*,user.nickname,user.avatar,user.gender,tuser.nickname as to_nickname,tuser.avatar as to_avatar,tuser.gender as to_gender')
  324. ->join('user','a.user_id = user.id','LEFT')
  325. ->join('user tuser','a.to_user_id = tuser.id','LEFT')
  326. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('id asc')->autopage()->select();
  327. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  328. if(!empty($child_lists)){
  329. foreach($child_lists as $key => &$answer){
  330. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  331. $answer['createtime_text'] = get_last_time($answer['createtime']);
  332. }
  333. }
  334. //合并
  335. $floor_info['child'] = $child_lists;
  336. $this->success('success',$floor_info);
  337. }
  338. //某个圈子里的动态列表,最新,推荐
  339. public function topic_list(){
  340. $topic_id = input('topic_id',0);
  341. $order = input('orderby','new');
  342. $orderby = 'dt.id desc';
  343. if($order == 'hot'){
  344. $orderby = 'dt.goodnum desc';
  345. }
  346. $where = [];
  347. if($topic_id){
  348. $where['dt.topic_id'] = $topic_id;
  349. }
  350. if($order == 'follow'){
  351. $follow_user_ids = Db::name('user_fans_follow')->where(['fans_id'=>$this->auth->id])->column('user_id');
  352. $where['dt.user_id'] = ['IN',$follow_user_ids];
  353. }
  354. //排除屏蔽的
  355. $screen_ids = Db::name('topic_dongtai_screen')->where('user_id',$this->auth->id)->column('dt_id');
  356. if(!empty($screen_ids)){
  357. $where['dt.id'] = ['NOTIN',$screen_ids];
  358. }
  359. //排除黑名单的
  360. $black_ids = Db::name('user_blacklist')->where('user_id',$this->auth->id)->column('black_user_id');
  361. if(!empty($black_ids)){
  362. $where['dt.user_id'] = ['NOTIN',$black_ids];
  363. }
  364. //
  365. $list = Db::name('topic_dongtai')->alias('dt')
  366. ->join('user','dt.user_id = user.id','LEFT')
  367. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  368. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  369. ->where($where)
  370. ->order($orderby)->autopage()->select();
  371. $list = list_domain_image($list,['images','avatar']);
  372. if(!empty($list)){
  373. foreach($list as $key => &$val){
  374. //追加点赞
  375. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  376. //创建视频缩略图
  377. $val['images_thumb'] = '';
  378. if ($val['type'] == 2) {
  379. $images_url = explode('.', $val['images']);
  380. unset($images_url[count($images_url) - 1]);
  381. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  382. }
  383. //时间
  384. $val['createtime_text'] = get_last_time($val['createtime']);
  385. //关注
  386. $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
  387. //评论
  388. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  389. }
  390. }
  391. $this->success('success',$list);
  392. }
  393. ////////////////////////////////////////////////////////////
  394. //消息-互动消息-评论
  395. //谁评论了我
  396. public function msg_answer(){
  397. $map = [
  398. 'dt.user_id' => $this->auth->id,
  399. 'a.level' => 1,
  400. ];
  401. $list = Db::name('topic_dongtai_answer')->alias('a')
  402. ->field('a.id,a.createtime,user.nickname,user.gender,user.avatar')
  403. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  404. ->join('user','a.user_id = user.id','LEFT')
  405. ->where($map)->order('a.id desc')->autopage()->select();
  406. $list = list_domain_image($list,['avatar']);
  407. if(!empty($list)){
  408. foreach($list as $key => &$val){
  409. //时间
  410. $val['createtime_text'] = get_last_time($val['createtime']);
  411. }
  412. }
  413. $this->success(1,$list);
  414. }
  415. //消息-互动消息-获赞
  416. //谁赞了我
  417. public function msg_good(){
  418. $map = [
  419. 'dt.user_id' => $this->auth->id,
  420. ];
  421. $list = Db::name('topic_dongtai_good')->alias('g')
  422. ->field('g.id,g.createtime,user.nickname,user.gender,user.avatar')
  423. ->join('topic_dongtai dt','g.dt_id = dt.id','LEFT')
  424. ->join('user','g.user_id = user.id','LEFT')
  425. ->where($map)->order('g.id desc')->autopage()->select();
  426. $list = list_domain_image($list,['avatar']);
  427. if(!empty($list)){
  428. foreach($list as $key => &$val){
  429. //时间
  430. $val['createtime_text'] = get_last_time($val['createtime']);
  431. }
  432. }
  433. $this->success(1,$list);
  434. }
  435. }