Topicdongtai.php 29 KB

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