Topicdongtai.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. $gender = input('gender','all');
  145. if($gender != 'all'){
  146. $where['user.gender'] = $gender;
  147. }
  148. //属性
  149. $attribute = input('attribute','all');
  150. if($attribute != 'all'){
  151. $where['user.attribute'] = $attribute;
  152. }
  153. //排除黑名单的
  154. $where2 = [];
  155. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  156. if(!empty($black_ids)){
  157. $where2['dt.user_id'] = ['NOTIN',$black_ids];
  158. }
  159. //列表
  160. $list = Db::name('topic_dongtai')->alias('dt')
  161. ->join('user','dt.user_id = user.id','LEFT')
  162. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  163. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  164. ->where($where)
  165. ->where($where2)
  166. ->where($where_exp)
  167. ->order($orderby)->autopage()->select();
  168. $list = list_domain_image($list,['images','audio_file','avatar']);
  169. if(!empty($list)){
  170. foreach($list as $key => &$val){
  171. //用户年龄
  172. $val['age'] = birthtime_to_age($val['birthday']);
  173. unset($val['birthday']);
  174. //用户vip
  175. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  176. unset($val['vip_endtime']);
  177. //追加点赞
  178. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  179. //时间
  180. $val['createtime_text'] = get_last_time($val['createtime']);
  181. //关注
  182. $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
  183. //层主评论数量
  184. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  185. //话题
  186. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
  187. //艾特了谁
  188. $val['aite_user'] = Db::name('user')->where('id','IN',$val['aite'])->column('nickname');
  189. }
  190. }
  191. $this->success('success',$list);
  192. }
  193. //详情
  194. public function info(){
  195. $id = input('id');
  196. $info = Db::name('topic_dongtai')->alias('dt')
  197. ->join('user','dt.user_id = user.id','LEFT')
  198. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  199. ->field('dt.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  200. ->where('dt.id',$id)->find();
  201. $info = info_domain_image($info,['images','audio_file','avatar']);
  202. if($info){
  203. //用户年龄
  204. $info['age'] = birthtime_to_age($info['birthday']);
  205. unset($info['birthday']);
  206. //用户vip
  207. $info['is_vip'] = $info['vip_endtime'] > time() ? 1 : 0;
  208. unset($info['vip_endtime']);
  209. //是否点赞过
  210. $info['isgood'] = $this->is_good($id,$this->auth->id);
  211. //时间
  212. $info['createtime_text'] = get_last_time($info['createtime']);
  213. //关注
  214. $info['is_follow'] = $this->is_follow($info['user_id'],$this->auth->id);
  215. //层主评论数量
  216. $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
  217. //话题
  218. $info['topic_text'] = Db::name('topic_hub')->where('id','IN',$info['topic_ids'])->column('name');
  219. }
  220. $this->success('success',$info);
  221. }
  222. //点赞
  223. public function good(){
  224. $id = input('id');
  225. $where = [
  226. 'dt_id' => $id,
  227. 'user_id' => $this->auth->id,
  228. ];
  229. $check = Db::name('topic_dongtai_good')->where($where)->find();
  230. if($check){
  231. $this->error('已经赞过了');
  232. }
  233. Db::startTrans();
  234. $where['createtime'] = time();
  235. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  236. if(!$rs){
  237. Db::rollback();
  238. $this->error('点赞失败');
  239. }
  240. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  241. if($up === false){
  242. Db::rollback();
  243. $this->error('点赞失败');
  244. }
  245. // \app\common\model\TaskLog::tofinish($this->auth->id,"VpXtablCsZ",1);
  246. Db::commit();
  247. $this->success('点赞成功');
  248. }
  249. //评论
  250. public function answer(){
  251. $id = input('id',0);
  252. $content = input('content','');
  253. $to_user_id = input('to_user_id',0);
  254. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  255. $floor = input('floor',0);
  256. if(empty($content) || empty($id)){
  257. $this->error();
  258. }
  259. //关键字替换
  260. $content = Keyworld::sensitive($content);
  261. //判断
  262. if($level == 2 && $floor == 0){
  263. $this->error('楼层错误');
  264. }
  265. //回复楼主,最新楼层
  266. if($level == 1 || $floor == 0){
  267. $to_user_id = 0;
  268. $floor = 1; //默认一楼
  269. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  270. if($last_floor){
  271. $floor = $last_floor + 1;
  272. }
  273. }
  274. //判断user_id
  275. if($to_user_id){
  276. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  277. if(empty($to_user)){
  278. $this->error('被回复的用户不存在');
  279. }
  280. }
  281. //data
  282. $data = [
  283. 'dt_id' => $id,
  284. 'floor' => $floor,
  285. 'user_id' => $this->auth->id,
  286. 'content' => $content,
  287. 'to_user_id' => $to_user_id,
  288. 'level' => $level,
  289. 'createtime' => time(),
  290. 'updatetime' => time(),
  291. ];
  292. Db::startTrans();
  293. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  294. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  295. Db::commit();
  296. $this->success('评价成功');
  297. }
  298. //对评论点赞
  299. public function answer_good(){
  300. $dt_id = input('dt_id',0);
  301. $answer_id = input('answer_id',0);
  302. $where = [
  303. 'dt_id' => $dt_id,
  304. 'answer_id' => $answer_id,
  305. 'user_id' => $this->auth->id,
  306. ];
  307. $check = Db::name('topic_answer_good')->where($where)->find();
  308. if($check){
  309. $this->error('已经赞过了');
  310. }
  311. Db::startTrans();
  312. $where['createtime'] = time();
  313. $rs = Db::name('topic_answer_good')->insertGetId($where);
  314. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  315. if($rs && $up !== false){
  316. Db::commit();
  317. $this->success('点赞成功');
  318. }
  319. Db::rollback();
  320. $this->error('点赞失败');
  321. }
  322. //举报枚举
  323. public function report_enum(){
  324. $arr = [
  325. '侮辱谩骂',
  326. '色情低俗',
  327. '政治敏感',
  328. '违法违规',
  329. '其他',
  330. ];
  331. $this->success(1,$arr);
  332. }
  333. //举报
  334. public function report(){
  335. $field = ['dt_id','type','content','images'];
  336. $data = request_post_hub($field);
  337. $check = Db::name('topic_dongtai')->where('id',$data['dt_id'])->find();
  338. $data['user_id'] = $this->auth->id;
  339. $data['ruser_id'] = $check['user_id'];
  340. $data['createtime'] = time();
  341. Db::name('topic_dongtai_report')->insertGetId($data);
  342. $this->success('举报成功');
  343. }
  344. //收藏
  345. public function collect(){
  346. $where = [
  347. 'user_id' => $this->auth->id,
  348. 'table' => 'topic_dongtai',
  349. 'table_id' => input('id',0),
  350. ];
  351. $check = Db::name('user_collect')->where($where)->find();
  352. if($check){
  353. $this->success('已经收藏过了');
  354. }else{
  355. Db::name('user_collect')->insertGetId($where);
  356. $this->success('收藏成功');
  357. }
  358. }
  359. //不感兴趣,屏蔽某条
  360. /*public function screen(){
  361. $data = [
  362. 'user_id' => $this->auth->id,
  363. 'dt_id' => input('dt_id',0),
  364. ];
  365. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  366. if($check){
  367. $this->success('操作成功');
  368. }
  369. Db::name('topic_dongtai_screen')->insertGetId($data);
  370. $this->success('操作成功');
  371. }*/
  372. //评论列表
  373. public function answer_list(){
  374. $dt_id = input('dt_id',0);
  375. //楼
  376. $floor_list = Db::name('topic_dongtai_answer')
  377. ->alias('a')
  378. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  379. ->join('user','a.user_id = user.id','LEFT')
  380. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
  381. $floor_list = list_domain_image($floor_list,['avatar']);
  382. //追加子评论
  383. if(!empty($floor_list)){
  384. foreach($floor_list as $key => &$val){
  385. //下面几条子回复,字符串
  386. $val['childremark'] = '';
  387. $map = [
  388. 'a.dt_id' => $dt_id,
  389. 'a.floor' => $val['floor'],
  390. 'a.level' => 2,
  391. ];
  392. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  393. if($number > 0){
  394. $answer_info = Db::name('topic_dongtai_answer')
  395. ->alias('a')
  396. ->field('user.nickname')
  397. ->join('user','a.user_id = user.id','LEFT')
  398. ->where($map)->order('a.id desc')->find();
  399. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  400. }
  401. //时间处理
  402. $val['createtime_text'] = get_last_time($val['createtime']);
  403. //回复是否已赞
  404. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  405. //用户年龄
  406. $val['age'] = birthtime_to_age($val['birthday']);
  407. unset($val['birthday']);
  408. }
  409. }
  410. $this->success(1,$floor_list);
  411. }
  412. //单独某一层的详细
  413. public function answer_info(){
  414. $answer_id = input('answer_id');
  415. //楼
  416. $floor_info = Db::name('topic_dongtai_answer')
  417. ->alias('a')
  418. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  419. ->join('user','a.user_id = user.id','LEFT')
  420. ->where(['a.id'=>$answer_id])->find();
  421. $floor_info = info_domain_image($floor_info,['avatar']);
  422. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  423. //用户年龄
  424. $floor_info['age'] = birthtime_to_age($floor_info['birthday']);
  425. unset($floor_info['birthday']);
  426. //回复是否已赞
  427. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  428. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  429. //层
  430. $floors = $floor_info['floor'];
  431. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  432. ->field('a.*,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  433. ->join('user','a.user_id = user.id','LEFT')
  434. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  435. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  436. if(!empty($child_lists)){
  437. foreach($child_lists as $key => &$answer){
  438. //用户年龄
  439. $answer['age'] = birthtime_to_age($answer['birthday']);
  440. unset($answer['birthday']);
  441. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  442. $answer['createtime_text'] = get_last_time($answer['createtime']);
  443. }
  444. }
  445. //合并
  446. $floor_info['child'] = $child_lists;
  447. $this->success('success',$floor_info);
  448. }
  449. //是否点赞
  450. private function is_good($dt_id,$uid){
  451. $where = [
  452. 'dt_id' => $dt_id,
  453. 'user_id' => $uid,
  454. ];
  455. $check = Db::name('topic_dongtai_good')->where($where)->find();
  456. if($check){
  457. return 1;
  458. }else{
  459. return 0;
  460. }
  461. }
  462. //回复是否点赞
  463. private function answer_is_good($answer_id,$uid){
  464. $where = [
  465. 'answer_id' => $answer_id,
  466. 'user_id' => $uid,
  467. ];
  468. $check = Db::name('topic_answer_good')->where($where)->find();
  469. if($check){
  470. return 1;
  471. }else{
  472. return 0;
  473. }
  474. }
  475. //是否关注
  476. private function is_follow($uid,$follow_uid){
  477. $where = [
  478. 'uid' => $uid,
  479. 'follow_uid' => $follow_uid,
  480. ];
  481. $check = Db::name('user_follow')->where($where)->find();
  482. if($check){
  483. return 1;
  484. }else{
  485. return 0;
  486. }
  487. }
  488. ////////////////////////////////////////////////////////////
  489. //消息-互动消息-评论
  490. //谁评论了我
  491. public function msg_answer(){
  492. $map = [
  493. 'dt.user_id' => $this->auth->id,
  494. 'a.level' => 1,
  495. ];
  496. $list = Db::name('topic_dongtai_answer')->alias('a')
  497. ->field('a.id,a.createtime,user.nickname,user.gender,user.avatar')
  498. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  499. ->join('user','a.user_id = user.id','LEFT')
  500. ->where($map)->order('a.id desc')->autopage()->select();
  501. $list = list_domain_image($list,['avatar']);
  502. if(!empty($list)){
  503. foreach($list as $key => &$val){
  504. //时间
  505. $val['createtime_text'] = get_last_time($val['createtime']);
  506. }
  507. }
  508. $this->success(1,$list);
  509. }
  510. //消息-互动消息-获赞
  511. //谁赞了我
  512. public function msg_good(){
  513. $map = [
  514. 'dt.user_id' => $this->auth->id,
  515. ];
  516. $list = Db::name('topic_dongtai_good')->alias('g')
  517. ->field('g.id,g.createtime,user.nickname,user.gender,user.avatar')
  518. ->join('topic_dongtai dt','g.dt_id = dt.id','LEFT')
  519. ->join('user','g.user_id = user.id','LEFT')
  520. ->where($map)->order('g.id desc')->autopage()->select();
  521. $list = list_domain_image($list,['avatar']);
  522. if(!empty($list)){
  523. foreach($list as $key => &$val){
  524. //时间
  525. $val['createtime_text'] = get_last_time($val['createtime']);
  526. }
  527. }
  528. $this->success(1,$list);
  529. }
  530. }