Topicdongtai.php 19 KB

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