Topicdongtai.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. $aite = input('aite','','htmlspecialchars_decode');
  20. $type = input('type',1);
  21. $audio_second = input('audio_second',0);
  22. if(!$content && !$images && !$audio_file){
  23. $this->error(__('Invalid parameters'));
  24. }
  25. if(!$this->user_auth_limit()){
  26. $this->error('请先完成实名认证');
  27. }
  28. //关键字替换
  29. $content = Keyworld::sensitive($content);
  30. //只保留一个
  31. if($type == 1){
  32. $audio_file = '';
  33. $audio_second = 0;
  34. }else{
  35. $images = '';
  36. }
  37. $data = [
  38. 'user_id' => $this->auth->id,
  39. 'content' => $content,
  40. 'images' => $images,
  41. 'audio_file' => $audio_file,
  42. 'type' => $type,
  43. 'cityname' => input('cityname',''),
  44. 'provincename' => input('provincename',''),
  45. 'aite' => $aite,
  46. 'is_public' => input('is_public',1),
  47. 'audio_second' => $audio_second,
  48. 'longitude' => input('longitude',0),
  49. 'latitude' => input('latitude',0),
  50. 'createtime' => time(),
  51. 'updatetime' => time(),
  52. 'auditstatus' => 1, //默认通过
  53. ];
  54. //如果强制审核,默认不通过
  55. $dongtai_audit_switch = config('site.dongtai_audit_switch');
  56. if($dongtai_audit_switch == 1){
  57. $data['auditstatus'] = 0;
  58. }
  59. Db::startTrans();
  60. $id = Db::name('topic_dongtai')->insertGetId($data);
  61. //task任务
  62. //发第一条动态
  63. $task_count = Db::name('topic_dongtai')->where('user_id',$this->auth->id)->count();
  64. if($task_count == 1){
  65. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,16);
  66. if($task_rs === false){
  67. Db::rollback();
  68. $this->error('完成任务失败');
  69. }
  70. }
  71. //通知艾特的人
  72. if(!empty($aite)){
  73. $aite = json_decode($aite,true);
  74. if(!empty($aite) && is_array($aite)){
  75. foreach($aite as $key => $uid){
  76. //系统消息
  77. if(is_array($uid) && isset($uid['id'])){
  78. $msg_id = \app\common\model\Message::addMessage($uid['id'],'动态通知',$this->auth->nickname.'发布动态并@了你,快去看看吧','dongtai_aite',$id);
  79. }
  80. }
  81. }
  82. }
  83. Db::commit();
  84. $this->success('发布成功',$id);
  85. }
  86. //自己看列表
  87. //某用户的帖子列表
  88. public function my_lists(){
  89. $page = input('page',1);
  90. if($page > 1){
  91. $this->success('success',[]);
  92. }
  93. $uid = input('uid',$this->auth->id);
  94. if (empty($uid)) {
  95. $uid = $this->auth->id;
  96. }
  97. $where = [
  98. 'dt.user_id'=>$uid,
  99. ];
  100. if($uid != $this->auth->id){
  101. $where['dt.is_public'] = 1; //不是自己的,就只能看公开的。公开:1=公开,2=私密
  102. $where['dt.auditstatus'] = 1; //不是自己的,就只能看审核通过的
  103. }
  104. $list = Db::name('topic_dongtai')->alias('dt')
  105. ->join('user','dt.user_id = user.id','LEFT')
  106. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  107. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,uw.vip_endtime')
  108. ->where($where)
  109. ->order('dt.id desc')->select();
  110. $list = list_domain_image($list,['images','audio_file','avatar']);
  111. if(!empty($list)){
  112. foreach($list as $key => &$val){
  113. $val['aite'] = json_decode($val['aite'],true);
  114. //用户年龄
  115. $val['age'] = birthtime_to_age($val['birthday']);
  116. unset($val['birthday']);
  117. //用户vip
  118. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  119. unset($val['vip_endtime']);
  120. //追加点赞
  121. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  122. //时间
  123. $val['createtime_text'] = get_last_time($val['createtime']);
  124. //关注
  125. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  126. //收藏
  127. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  128. //拉黑
  129. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  130. //层主评论数量
  131. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  132. //话题
  133. $val['topic_text'] = [];
  134. }
  135. }
  136. $this->success('success',$list);
  137. }
  138. //动态删除
  139. public function delete(){
  140. $id = input('id',0);
  141. $where['id'] = $id;
  142. $where['user_id'] = $this->auth->id;
  143. Db::startTrans();
  144. $delRes = Db::name('topic_dongtai')->where('id',$id)->where('user_id',$this->auth->id)->delete();
  145. if (!$delRes) {
  146. Db::rollback();
  147. $this->error('动态删除失败');
  148. }
  149. //删除对应的评论,
  150. Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();
  151. //点赞,
  152. Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
  153. //评论点赞
  154. Db::name('topic_answer_good')->where('dt_id',$id)->delete();
  155. Db::commit();
  156. $this->success('删除成功');
  157. }
  158. //某个圈子里的动态列表,关注,最新,附近
  159. public function topic_list(){
  160. $where = [
  161. 'dt.is_public' => 1,
  162. 'dt.auditstatus' => 1,
  163. ];
  164. //最新
  165. $order = input('orderby','new');
  166. $orderby = 'dt.toptime desc,dt.id desc';
  167. //关注
  168. $where_follow = '';
  169. if($order == 'follow'){
  170. //关注的人
  171. $follow_user_ids = Db::name('user_follow')->where(['uid'=>$this->auth->id])->column('follow_uid');
  172. if(!empty($follow_user_ids)){
  173. $where_follow .= '(dt.user_id IN ('.implode(',',$follow_user_ids).'))';
  174. }
  175. //默认
  176. if($where_follow == ''){
  177. $where_follow = 'dt.id = 0';
  178. }
  179. }
  180. //附近,根据距离排序
  181. $having_dis = '';
  182. if($order == 'near'){
  183. // $where['dt.provincename'] = $this->auth->provincename;
  184. $having_dis = 'distance < ' . 100000; //100公里范围
  185. $orderby = 'dt.toptime desc,distance asc';
  186. }
  187. //性别
  188. $gender = input('gender','all');
  189. if($gender != 'all'){
  190. $where['user.gender'] = $gender;
  191. }
  192. //排除黑名单的
  193. $where_black = [];
  194. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  195. if(!empty($black_ids)){
  196. $where_black['dt.user_id'] = ['NOTIN',$black_ids];
  197. }
  198. //列表
  199. $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,uw.vip_endtime';
  200. if($order == 'near'){
  201. $field .= ',(st_distance(point (' . $this->auth->longitude . ', ' . $this->auth->latitude . '),point(dt.longitude,dt.latitude))*111195) as distance';
  202. }
  203. $list = Db::name('topic_dongtai')->alias('dt')
  204. ->join('user','dt.user_id = user.id','LEFT')
  205. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  206. ->field($field)
  207. ->where($where)
  208. ->where($where_follow)
  209. ->where($where_black);
  210. if($order == 'near'){
  211. $list = $list->having($having_dis);
  212. }
  213. $list = $list->order($orderby)
  214. ->autopage()->select();
  215. $list = list_domain_image($list,['images','audio_file','avatar']);
  216. if(!empty($list)){
  217. foreach($list as $key => &$val){
  218. //艾特
  219. $val['aite'] = json_decode($val['aite'],true);
  220. //距离
  221. $val['distance'] = isset($val['distance']) ? $val['distance'] : 0;
  222. $val['distance'] = bcdiv(intval($val['distance']),1000,1).'km';
  223. if($this->auth->longitude == 0 || $val['longitude'] == 0){
  224. $val['distance'] = '未知';
  225. }
  226. //vip如果开了隐私保护,需要隐藏距离
  227. $yinsi = $this->user_power($val['user_id'],'yinsi');
  228. if($yinsi == 1){
  229. $val['distance'] = '';
  230. }
  231. //用户年龄
  232. $val['age'] = birthtime_to_age($val['birthday']);
  233. unset($val['birthday']);
  234. //用户vip
  235. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  236. unset($val['vip_endtime']);
  237. //追加点赞
  238. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  239. //时间
  240. $val['createtime_text'] = get_last_time($val['createtime']);
  241. //关注
  242. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  243. //收藏
  244. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  245. //拉黑
  246. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  247. //层主评论数量
  248. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  249. //话题
  250. $val['topic_text'] = [];
  251. //艾特了谁
  252. // $val['aite_user'] = Db::name('user')->where('id','IN',$val['aite'])->column('nickname');
  253. }
  254. }
  255. $this->success('success',$list);
  256. }
  257. //详情
  258. public function info(){
  259. $id = input('id');
  260. $info = Db::name('topic_dongtai')->alias('dt')
  261. ->join('user','dt.user_id = user.id','LEFT')
  262. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  263. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,uw.vip_endtime')
  264. ->where('dt.id',$id)->find();
  265. $info = info_domain_image($info,['images','audio_file','avatar']);
  266. if($info){
  267. $info['aite'] = json_decode($info['aite'],true);
  268. //用户年龄
  269. $info['age'] = birthtime_to_age($info['birthday']);
  270. unset($info['birthday']);
  271. //用户vip
  272. $info['is_vip'] = $info['vip_endtime'] > time() ? 1 : 0;
  273. unset($info['vip_endtime']);
  274. //是否点赞过
  275. $info['isgood'] = $this->is_good($id,$this->auth->id);
  276. //时间
  277. $info['createtime_text'] = get_last_time($info['createtime']);
  278. //关注
  279. $info['is_follow'] = $this->is_follow($this->auth->id,$info['user_id']);
  280. //收藏
  281. $info['is_collect'] = $this->is_collect($id,$this->auth->id);
  282. //拉黑
  283. $info['is_black'] = $this->is_black($this->auth->id,$info['user_id']);
  284. //层主评论数量
  285. $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
  286. //话题
  287. $info['topic_text'] = [];
  288. }else{
  289. $this->error('此动态已被删除');
  290. }
  291. $this->success('success',$info);
  292. }
  293. //点赞,取消点赞
  294. public function good(){
  295. $id = input('id');
  296. $where = [
  297. 'dt_id' => $id,
  298. 'user_id' => $this->auth->id,
  299. ];
  300. $check = Db::name('topic_dongtai_good')->where($where)->find();
  301. $dt_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  302. if($check){
  303. Db::name('topic_dongtai_good')->where($where)->delete();
  304. $down = Db::name('topic_dongtai')->where('id',$id)->setDec('goodnum');
  305. $this->success('已取消点赞');
  306. }else{
  307. Db::startTrans();
  308. $where['createtime'] = time();
  309. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  310. if(!$rs){
  311. Db::rollback();
  312. $this->error('点赞失败');
  313. }
  314. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  315. if($up === false){
  316. Db::rollback();
  317. $this->error('点赞失败');
  318. }
  319. //系统消息
  320. if($dt_user_id != $this->auth->id){
  321. $msg_id = \app\common\model\Message::addMessage($dt_user_id,'动态点赞',$this->auth->nickname.'('.$this->auth->username.')赞了你的动态','dongtai_good',$id);
  322. }
  323. Db::commit();
  324. $this->success('点赞成功');
  325. }
  326. }
  327. //评论
  328. public function answer(){
  329. if($this->apiLimit(1,5000) == false){
  330. $this->error('抱歉,您在5秒内只能进行一次回复');
  331. }
  332. $id = input('id',0);
  333. $content = input('content','');
  334. $to_user_id = input('to_user_id',0);
  335. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  336. $floor = input('floor',0);
  337. if(empty($content) || empty($id)){
  338. $this->error();
  339. }
  340. //判断
  341. if($level == 2 && $floor == 0){
  342. $this->error('楼层错误');
  343. }
  344. //黑名单判断
  345. //是否被动态发布者拉黑
  346. $dongtai_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  347. $black_check = $this->is_black($dongtai_user_id,$this->auth->id);
  348. if($black_check){
  349. $this->error('您已被对方拉黑,禁止评论此动态');
  350. }
  351. //是否被层主拉黑
  352. if($level == 2){
  353. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  354. $black_check = $this->is_black($answer_info['user_id'],$this->auth->id);
  355. if($black_check){
  356. $this->error('您已被对方拉黑,禁止点评此评论');
  357. }
  358. }
  359. //关键字替换
  360. $content = Keyworld::sensitive($content);
  361. //回复楼主,最新楼层
  362. if($level == 1 || $floor == 0){
  363. $to_user_id = 0;
  364. $floor = 1; //默认一楼
  365. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  366. if($last_floor){
  367. $floor = $last_floor + 1;
  368. }
  369. }
  370. //判断user_id
  371. if($to_user_id){
  372. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  373. if(empty($to_user)){
  374. $this->error('被回复的用户不存在');
  375. }
  376. }
  377. //data
  378. $data = [
  379. 'dt_id' => $id,
  380. 'floor' => $floor,
  381. 'user_id' => $this->auth->id,
  382. 'content' => $content,
  383. 'to_user_id' => $to_user_id,
  384. 'level' => $level,
  385. 'createtime' => time(),
  386. 'updatetime' => time(),
  387. ];
  388. Db::startTrans();
  389. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  390. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  391. //系统消息
  392. if($level == 1){
  393. //发给动态用户
  394. $msg_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  395. $msg_title = '动态评论';
  396. $msg_content = $this->auth->nickname.'评论了你的动态';
  397. $infotype_id = $rs;
  398. }else{
  399. //发给层主
  400. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  401. $msg_user_id = $answer_info['user_id'];
  402. $msg_title = '动态评论点评';
  403. $msg_content = $this->auth->nickname.'点评了你的动态评论';
  404. $infotype_id = $answer_info['id'];
  405. }
  406. $msg_id = \app\common\model\Message::addMessage($msg_user_id,$msg_title,$msg_content,'dongtai_answer',$infotype_id);
  407. Db::commit();
  408. $this->success('评论成功');
  409. }
  410. //对评论点赞
  411. public function answer_good(){
  412. $dt_id = input('dt_id',0);
  413. $answer_id = input('answer_id',0);
  414. $where = [
  415. 'dt_id' => $dt_id,
  416. 'answer_id' => $answer_id,
  417. 'user_id' => $this->auth->id,
  418. ];
  419. $check = Db::name('topic_answer_good')->where($where)->find();
  420. if($check){
  421. Db::name('topic_answer_good')->where($where)->delete();
  422. Db::name('topic_dongtai_answer')->where('id',$answer_id)->setDec('goodnum');
  423. $this->success('已取消点赞');
  424. }else{
  425. Db::startTrans();
  426. $where['createtime'] = time();
  427. $rs = Db::name('topic_answer_good')->insertGetId($where);
  428. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  429. if($rs && $up !== false){
  430. Db::commit();
  431. $this->success('点赞成功');
  432. }
  433. Db::rollback();
  434. $this->error('点赞失败');
  435. }
  436. }
  437. //举报枚举
  438. /*public function report_enum(){
  439. $arr = [
  440. '侮辱谩骂',
  441. '色情低俗',
  442. '政治敏感',
  443. '违法违规',
  444. '其他',
  445. ];
  446. $this->success(1,$arr);
  447. }*/
  448. //举报
  449. public function report(){
  450. $dt_id = input('dt_id',0);
  451. $data['dt_id'] = $dt_id;
  452. $check = Db::name('topic_dongtai')->where('id',$data['dt_id'])->find();
  453. if(empty($check)){
  454. $this->error('不存在的动态');
  455. }
  456. $data['user_id'] = $this->auth->id;
  457. $data['to_user_id'] = $check['user_id'];
  458. $data['createtime'] = time();
  459. Db::name('topic_dongtai_report')->insertGetId($data);
  460. $this->success('举报成功');
  461. }
  462. //收藏,取消收藏
  463. public function collect(){
  464. $where = [
  465. 'user_id' => $this->auth->id,
  466. 'table' => 'topic_dongtai',
  467. 'table_id' => input('id',0),
  468. ];
  469. $check = Db::name('user_collect')->where($where)->find();
  470. if($check){
  471. Db::name('user_collect')->where($where)->delete();
  472. $this->success('已取消收藏');
  473. }else{
  474. Db::name('user_collect')->insertGetId($where);
  475. $this->success('收藏成功');
  476. }
  477. }
  478. //我的收藏
  479. public function my_collect(){
  480. $collect_id = Db::name('user_collect')->where(['table'=>'topic_dongtai','user_id'=>$this->auth->id])->column('table_id');
  481. $where = ['dt.id'=>['IN',$collect_id]];
  482. $list = Db::name('topic_dongtai')->alias('dt')
  483. ->join('user','dt.user_id = user.id','LEFT')
  484. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  485. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,uw.vip_endtime')
  486. ->where($where)
  487. ->order('dt.id desc')->autopage()->select();
  488. $list = list_domain_image($list,['images','audio_file','avatar']);
  489. if(!empty($list)){
  490. foreach($list as $key => &$val){
  491. $val['aite'] = json_decode($val['aite'],true);
  492. //用户年龄
  493. $val['age'] = birthtime_to_age($val['birthday']);
  494. unset($val['birthday']);
  495. //用户vip
  496. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  497. unset($val['vip_endtime']);
  498. //追加点赞
  499. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  500. //时间
  501. $val['createtime_text'] = get_last_time($val['createtime']);
  502. //关注
  503. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  504. //收藏
  505. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  506. //拉黑
  507. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  508. //层主评论数量
  509. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  510. //话题
  511. $val['topic_text'] = [];
  512. }
  513. }
  514. $this->success('success',$list);
  515. }
  516. //不感兴趣,屏蔽某条
  517. /*public function screen(){
  518. $data = [
  519. 'user_id' => $this->auth->id,
  520. 'dt_id' => input('dt_id',0),
  521. ];
  522. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  523. if($check){
  524. $this->success('操作成功');
  525. }
  526. Db::name('topic_dongtai_screen')->insertGetId($data);
  527. $this->success('操作成功');
  528. }*/
  529. //评论列表
  530. public function answer_list(){
  531. $dt_id = input('dt_id',0);
  532. //楼
  533. $floor_list = Db::name('topic_dongtai_answer')
  534. ->alias('a')
  535. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status')
  536. ->join('user','a.user_id = user.id','LEFT')
  537. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
  538. $floor_list = list_domain_image($floor_list,['avatar']);
  539. //追加子评论
  540. if(!empty($floor_list)){
  541. foreach($floor_list as $key => &$val){
  542. //下面几条子回复,字符串
  543. $val['childremark'] = '';
  544. $map = [
  545. 'a.dt_id' => $dt_id,
  546. 'a.floor' => $val['floor'],
  547. 'a.level' => 2,
  548. ];
  549. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  550. if($number > 0){
  551. $answer_info = Db::name('topic_dongtai_answer')
  552. ->alias('a')
  553. ->field('user.nickname')
  554. ->join('user','a.user_id = user.id','LEFT')
  555. ->where($map)->order('a.id desc')->find();
  556. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  557. }
  558. //时间处理
  559. $val['createtime_text'] = get_last_time($val['createtime']);
  560. //回复是否已赞
  561. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  562. //用户年龄
  563. $val['age'] = birthtime_to_age($val['birthday']);
  564. unset($val['birthday']);
  565. }
  566. }
  567. $this->success(1,$floor_list);
  568. }
  569. //单独某一层的详细
  570. public function answer_info(){
  571. $answer_id = input('answer_id');
  572. //楼
  573. $floor_info = Db::name('topic_dongtai_answer')
  574. ->alias('a')
  575. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status')
  576. ->join('user','a.user_id = user.id','LEFT')
  577. ->where(['a.id'=>$answer_id])->find();
  578. if(empty($floor_info)){
  579. $this->success(1,[]);
  580. }
  581. $floor_info = info_domain_image($floor_info,['avatar']);
  582. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  583. //用户年龄
  584. $floor_info['age'] = birthtime_to_age($floor_info['birthday']);
  585. unset($floor_info['birthday']);
  586. //回复是否已赞
  587. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  588. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  589. //层
  590. $floors = $floor_info['floor'];
  591. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  592. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status')
  593. ->join('user','a.user_id = user.id','LEFT')
  594. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  595. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  596. if(!empty($child_lists)){
  597. foreach($child_lists as $key => &$answer){
  598. //用户年龄
  599. $answer['age'] = birthtime_to_age($answer['birthday']);
  600. unset($answer['birthday']);
  601. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  602. $answer['createtime_text'] = get_last_time($answer['createtime']);
  603. }
  604. }
  605. //合并
  606. $floor_info['child'] = $child_lists;
  607. $this->success('success',$floor_info);
  608. }
  609. //是否点赞
  610. private function is_good($dt_id,$uid){
  611. $where = [
  612. 'dt_id' => $dt_id,
  613. 'user_id' => $uid,
  614. ];
  615. $check = Db::name('topic_dongtai_good')->where($where)->find();
  616. if($check){
  617. return 1;
  618. }else{
  619. return 0;
  620. }
  621. }
  622. //回复是否点赞
  623. private function answer_is_good($answer_id,$uid){
  624. $where = [
  625. 'answer_id' => $answer_id,
  626. 'user_id' => $uid,
  627. ];
  628. $check = Db::name('topic_answer_good')->where($where)->find();
  629. if($check){
  630. return 1;
  631. }else{
  632. return 0;
  633. }
  634. }
  635. //动态是否收藏
  636. private function is_collect($dt_id,$uid){
  637. $where = [
  638. 'user_id' => $uid,
  639. 'table' => 'topic_dongtai',
  640. 'table_id' => $dt_id,
  641. ];
  642. $check = Db::name('user_collect')->where($where)->find();
  643. if($check){
  644. return 1;
  645. }else{
  646. return 0;
  647. }
  648. }
  649. //用户是否拉黑
  650. private function is_black($uid,$black_uid){
  651. $where = [
  652. 'uid' => $uid,
  653. 'black_uid' => $black_uid,
  654. ];
  655. $check = Db::name('user_black')->where($where)->find();
  656. if($check){
  657. return 1;
  658. }else{
  659. return 0;
  660. }
  661. }
  662. //我的评论
  663. public function my_answer(){
  664. $map = [
  665. 'a.user_id' => $this->auth->id,
  666. 'a.level' => 1,
  667. ];
  668. $list = Db::name('topic_dongtai_answer')->alias('a')
  669. ->field('a.id,a.createtime,a.content,a.dt_id,
  670. dt.images,dt.content as dt_content,dt.type as dt_type,dtuser.nickname as dtuser_nickname,dtuser.username as dtuser_username,user.username,
  671. user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,uw.vip_endtime')
  672. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  673. ->join('user dtuser','dt.user_id = dtuser.id','LEFT')
  674. ->join('user user','a.user_id = user.id','LEFT')
  675. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  676. ->where($map)->order('a.id desc')->autopage()->select();
  677. $list = list_domain_image($list,['avatar']);
  678. if(!empty($list)){
  679. foreach($list as $key => &$val){
  680. //用户年龄
  681. $val['age'] = birthtime_to_age($val['birthday']);
  682. unset($val['birthday']);
  683. //用户vip
  684. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  685. unset($val['vip_endtime']);
  686. }
  687. }
  688. $this->success(1,$list);
  689. }
  690. //删除我的某个评论
  691. public function delete_answer(){
  692. $id = input('id',0);
  693. if(!$id){
  694. $this->error();
  695. }
  696. Db::startTrans();
  697. $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();
  698. if(!$info){
  699. $this->error('不存在的动态评论');
  700. }
  701. if($info['level'] == 1){
  702. //楼层内都删
  703. $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');
  704. if(!empty($louceng_id)){
  705. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1
  706. Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉
  707. Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉
  708. }
  709. }
  710. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1
  711. Db::name('topic_dongtai_answer')->where('id',$id)->delete(); //评论删掉
  712. Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉
  713. Db::commit();
  714. $this->success();
  715. }
  716. //我点赞的动态
  717. public function my_good(){
  718. $where = ['good.user_id'=>$this->auth->id];
  719. $list = Db::name('topic_dongtai')->alias('dt')
  720. ->join('user','dt.user_id = user.id','LEFT')
  721. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  722. ->join('topic_dongtai_good good','dt.id = good.dt_id','LEFT')
  723. ->field('dt.*,dt.id as dt_id,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,uw.vip_endtime')
  724. ->where($where)
  725. ->order('dt.id desc')->autopage()->select();
  726. $list = list_domain_image($list,['images','audio_file','avatar']);
  727. if(!empty($list)){
  728. foreach($list as $key => &$val){
  729. $val['aite'] = json_decode($val['aite'],true);
  730. //用户年龄
  731. $val['age'] = birthtime_to_age($val['birthday']);
  732. unset($val['birthday']);
  733. //用户vip
  734. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  735. unset($val['vip_endtime']);
  736. //追加点赞
  737. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  738. //时间
  739. $val['createtime_text'] = get_last_time($val['createtime']);
  740. //关注
  741. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  742. //收藏
  743. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  744. //拉黑
  745. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  746. //层主评论数量
  747. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  748. //话题
  749. $val['topic_text'] = [];
  750. }
  751. }
  752. $this->success('success',$list);
  753. }
  754. }