Topicdongtai.php 31 KB

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