Topicdongtai.php 22 KB

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