Topicdongtai.php 18 KB

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