Topicdongtai.php 31 KB

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