Topicdongtai.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. $dt_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  297. if($check){
  298. Db::name('topic_dongtai_good')->where($where)->delete();
  299. $down = Db::name('topic_dongtai')->where('id',$id)->setDec('goodnum');
  300. $this->success('已取消点赞');
  301. }else{
  302. Db::startTrans();
  303. $where['createtime'] = time();
  304. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  305. if(!$rs){
  306. Db::rollback();
  307. $this->error('点赞失败');
  308. }
  309. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  310. if($up === false){
  311. Db::rollback();
  312. $this->error('点赞失败');
  313. }
  314. //系统消息
  315. $msg_id = \app\common\model\Message::addMessage($dt_user_id,'动态点赞','有人赞了你的动态','dongtai_good',$id);
  316. Db::commit();
  317. $this->success('点赞成功');
  318. }
  319. }
  320. //评论
  321. public function answer(){
  322. $id = input('id',0);
  323. $content = input('content','');
  324. $to_user_id = input('to_user_id',0);
  325. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  326. $floor = input('floor',0);
  327. if(empty($content) || empty($id)){
  328. $this->error();
  329. }
  330. //关键字替换
  331. $content = Keyworld::sensitive($content);
  332. //判断
  333. if($level == 2 && $floor == 0){
  334. $this->error('楼层错误');
  335. }
  336. //回复楼主,最新楼层
  337. if($level == 1 || $floor == 0){
  338. $to_user_id = 0;
  339. $floor = 1; //默认一楼
  340. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  341. if($last_floor){
  342. $floor = $last_floor + 1;
  343. }
  344. }
  345. //判断user_id
  346. if($to_user_id){
  347. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  348. if(empty($to_user)){
  349. $this->error('被回复的用户不存在');
  350. }
  351. }
  352. //data
  353. $data = [
  354. 'dt_id' => $id,
  355. 'floor' => $floor,
  356. 'user_id' => $this->auth->id,
  357. 'content' => $content,
  358. 'to_user_id' => $to_user_id,
  359. 'level' => $level,
  360. 'createtime' => time(),
  361. 'updatetime' => time(),
  362. ];
  363. Db::startTrans();
  364. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  365. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  366. //系统消息
  367. if($level == 1){
  368. //发给动态用户
  369. $msg_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  370. $msg_title = '动态评论';
  371. $msg_content = '有人评论了你的动态';
  372. $infotype_id = $rs;
  373. }else{
  374. //发给层主
  375. $answer_info = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1,'floor'=>$floor])->find();
  376. $msg_user_id = $answer_info['user_id'];
  377. $msg_title = '动态评论点评';
  378. $msg_content = '有人点评了你的动态评论';
  379. $infotype_id = $answer_info['id'];
  380. }
  381. $msg_id = \app\common\model\Message::addMessage($msg_user_id,$msg_title,$msg_content,'dongtai_answer',$infotype_id);
  382. Db::commit();
  383. $this->success('评价成功');
  384. }
  385. //对评论点赞
  386. public function answer_good(){
  387. $dt_id = input('dt_id',0);
  388. $answer_id = input('answer_id',0);
  389. $where = [
  390. 'dt_id' => $dt_id,
  391. 'answer_id' => $answer_id,
  392. 'user_id' => $this->auth->id,
  393. ];
  394. $check = Db::name('topic_answer_good')->where($where)->find();
  395. if($check){
  396. Db::name('topic_answer_good')->where($where)->delete();
  397. Db::name('topic_dongtai_answer')->where('id',$answer_id)->setDec('goodnum');
  398. $this->success('已取消点赞');
  399. }else{
  400. Db::startTrans();
  401. $where['createtime'] = time();
  402. $rs = Db::name('topic_answer_good')->insertGetId($where);
  403. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  404. if($rs && $up !== false){
  405. Db::commit();
  406. $this->success('点赞成功');
  407. }
  408. Db::rollback();
  409. $this->error('点赞失败');
  410. }
  411. }
  412. //举报枚举
  413. /*public function report_enum(){
  414. $arr = [
  415. '侮辱谩骂',
  416. '色情低俗',
  417. '政治敏感',
  418. '违法违规',
  419. '其他',
  420. ];
  421. $this->success(1,$arr);
  422. }*/
  423. //举报
  424. /*public function report(){
  425. $field = ['dt_id','type','content','images'];
  426. $data = request_post_hub($field);
  427. $check = Db::name('topic_dongtai')->where('id',$data['dt_id'])->find();
  428. $data['user_id'] = $this->auth->id;
  429. $data['ruser_id'] = $check['user_id'];
  430. $data['createtime'] = time();
  431. Db::name('topic_dongtai_report')->insertGetId($data);
  432. $this->success('举报成功');
  433. }*/
  434. //收藏,取消收藏
  435. public function collect(){
  436. $where = [
  437. 'user_id' => $this->auth->id,
  438. 'table' => 'topic_dongtai',
  439. 'table_id' => input('id',0),
  440. ];
  441. $check = Db::name('user_collect')->where($where)->find();
  442. if($check){
  443. Db::name('user_collect')->where($where)->delete();
  444. $this->success('已取消收藏');
  445. }else{
  446. Db::name('user_collect')->insertGetId($where);
  447. $this->success('收藏成功');
  448. }
  449. }
  450. //我的收藏
  451. public function my_collect(){
  452. $collect_id = Db::name('user_collect')->where(['table'=>'topic_dongtai','user_id'=>$this->auth->id])->column('table_id');
  453. $where = ['dt.id'=>['IN',$collect_id]];
  454. $list = Db::name('topic_dongtai')->alias('dt')
  455. ->join('user','dt.user_id = user.id','LEFT')
  456. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  457. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  458. ->where($where)
  459. ->order('dt.id desc')->autopage()->select();
  460. $list = list_domain_image($list,['images','audio_file','avatar']);
  461. if(!empty($list)){
  462. foreach($list as $key => &$val){
  463. $val['aite'] = json_decode($val['aite'],true);
  464. //用户年龄
  465. $val['age'] = birthtime_to_age($val['birthday']);
  466. unset($val['birthday']);
  467. //用户vip
  468. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  469. unset($val['vip_endtime']);
  470. //追加点赞
  471. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  472. //时间
  473. $val['createtime_text'] = get_last_time($val['createtime']);
  474. //关注
  475. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  476. //收藏
  477. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  478. //拉黑
  479. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  480. //层主评论数量
  481. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  482. //话题
  483. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
  484. }
  485. }
  486. $this->success('success',$list);
  487. }
  488. //不感兴趣,屏蔽某条
  489. /*public function screen(){
  490. $data = [
  491. 'user_id' => $this->auth->id,
  492. 'dt_id' => input('dt_id',0),
  493. ];
  494. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  495. if($check){
  496. $this->success('操作成功');
  497. }
  498. Db::name('topic_dongtai_screen')->insertGetId($data);
  499. $this->success('操作成功');
  500. }*/
  501. //评论列表
  502. public function answer_list(){
  503. $dt_id = input('dt_id',0);
  504. //楼
  505. $floor_list = Db::name('topic_dongtai_answer')
  506. ->alias('a')
  507. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  508. ->join('user','a.user_id = user.id','LEFT')
  509. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
  510. $floor_list = list_domain_image($floor_list,['avatar']);
  511. //追加子评论
  512. if(!empty($floor_list)){
  513. foreach($floor_list as $key => &$val){
  514. //下面几条子回复,字符串
  515. $val['childremark'] = '';
  516. $map = [
  517. 'a.dt_id' => $dt_id,
  518. 'a.floor' => $val['floor'],
  519. 'a.level' => 2,
  520. ];
  521. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  522. if($number > 0){
  523. $answer_info = Db::name('topic_dongtai_answer')
  524. ->alias('a')
  525. ->field('user.nickname')
  526. ->join('user','a.user_id = user.id','LEFT')
  527. ->where($map)->order('a.id desc')->find();
  528. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  529. }
  530. //时间处理
  531. $val['createtime_text'] = get_last_time($val['createtime']);
  532. //回复是否已赞
  533. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  534. //用户年龄
  535. $val['age'] = birthtime_to_age($val['birthday']);
  536. unset($val['birthday']);
  537. }
  538. }
  539. $this->success(1,$floor_list);
  540. }
  541. //单独某一层的详细
  542. public function answer_info(){
  543. $answer_id = input('answer_id');
  544. //楼
  545. $floor_info = Db::name('topic_dongtai_answer')
  546. ->alias('a')
  547. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  548. ->join('user','a.user_id = user.id','LEFT')
  549. ->where(['a.id'=>$answer_id])->find();
  550. $floor_info = info_domain_image($floor_info,['avatar']);
  551. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  552. //用户年龄
  553. $floor_info['age'] = birthtime_to_age($floor_info['birthday']);
  554. unset($floor_info['birthday']);
  555. //回复是否已赞
  556. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  557. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  558. //层
  559. $floors = $floor_info['floor'];
  560. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  561. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  562. ->join('user','a.user_id = user.id','LEFT')
  563. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  564. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  565. if(!empty($child_lists)){
  566. foreach($child_lists as $key => &$answer){
  567. //用户年龄
  568. $answer['age'] = birthtime_to_age($answer['birthday']);
  569. unset($answer['birthday']);
  570. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  571. $answer['createtime_text'] = get_last_time($answer['createtime']);
  572. }
  573. }
  574. //合并
  575. $floor_info['child'] = $child_lists;
  576. $this->success('success',$floor_info);
  577. }
  578. //是否点赞
  579. private function is_good($dt_id,$uid){
  580. $where = [
  581. 'dt_id' => $dt_id,
  582. 'user_id' => $uid,
  583. ];
  584. $check = Db::name('topic_dongtai_good')->where($where)->find();
  585. if($check){
  586. return 1;
  587. }else{
  588. return 0;
  589. }
  590. }
  591. //回复是否点赞
  592. private function answer_is_good($answer_id,$uid){
  593. $where = [
  594. 'answer_id' => $answer_id,
  595. 'user_id' => $uid,
  596. ];
  597. $check = Db::name('topic_answer_good')->where($where)->find();
  598. if($check){
  599. return 1;
  600. }else{
  601. return 0;
  602. }
  603. }
  604. //动态是否收藏
  605. private function is_collect($dt_id,$uid){
  606. $where = [
  607. 'user_id' => $uid,
  608. 'table' => 'topic_dongtai',
  609. 'table_id' => $dt_id,
  610. ];
  611. $check = Db::name('user_collect')->where($where)->find();
  612. if($check){
  613. return 1;
  614. }else{
  615. return 0;
  616. }
  617. }
  618. //用户是否拉黑
  619. private function is_black($uid,$black_uid){
  620. $where = [
  621. 'uid' => $uid,
  622. 'black_uid' => $black_uid,
  623. ];
  624. $check = Db::name('user_black')->where($where)->find();
  625. if($check){
  626. return 1;
  627. }else{
  628. return 0;
  629. }
  630. }
  631. //我的评论
  632. public function my_answer(){
  633. $map = [
  634. 'a.user_id' => $this->auth->id,
  635. 'a.level' => 1,
  636. ];
  637. $list = Db::name('topic_dongtai_answer')->alias('a')
  638. ->field('a.id,a.createtime,a.content,a.dt_id,
  639. dt.images,dt.content as dt_content,dt.type as dt_type,dtuser.nickname as dtuser_nickname,dtuser.username as dtuser_username,user.username,
  640. user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  641. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  642. ->join('user dtuser','dt.user_id = dtuser.id','LEFT')
  643. ->join('user user','a.user_id = user.id','LEFT')
  644. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  645. ->where($map)->order('a.id desc')->autopage()->select();
  646. $list = list_domain_image($list,['avatar']);
  647. if(!empty($list)){
  648. foreach($list as $key => &$val){
  649. //用户年龄
  650. $val['age'] = birthtime_to_age($val['birthday']);
  651. unset($val['birthday']);
  652. //用户vip
  653. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  654. unset($val['vip_endtime']);
  655. }
  656. }
  657. $this->success(1,$list);
  658. }
  659. //删除我的某个评论
  660. public function delete_answer(){
  661. $id = input('id',0);
  662. if(!$id){
  663. $this->error();
  664. }
  665. Db::startTrans();
  666. $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();
  667. if(!$info){
  668. $this->error('不存在的动态评论');
  669. }
  670. if($info['level'] == 1){
  671. //楼层内都删
  672. $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');
  673. if(!empty($louceng_id)){
  674. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1
  675. Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉
  676. Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉
  677. }
  678. }
  679. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1
  680. Db::name('topic_dongtai_answer')->where('id',$id)->delete(); //评论删掉
  681. Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉
  682. Db::commit();
  683. $this->success();
  684. }
  685. //我点赞的动态
  686. public function my_good(){
  687. $where = ['good.user_id'=>$this->auth->id];
  688. $list = Db::name('topic_dongtai')->alias('dt')
  689. ->join('user','dt.user_id = user.id','LEFT')
  690. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  691. ->join('topic_dongtai_good good','dt.id = good.dt_id','LEFT')
  692. ->field('dt.*,dt.id as dt_id,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  693. ->where($where)
  694. ->order('dt.id desc')->autopage()->select();
  695. $list = list_domain_image($list,['images','audio_file','avatar']);
  696. if(!empty($list)){
  697. foreach($list as $key => &$val){
  698. $val['aite'] = json_decode($val['aite'],true);
  699. //用户年龄
  700. $val['age'] = birthtime_to_age($val['birthday']);
  701. unset($val['birthday']);
  702. //用户vip
  703. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  704. unset($val['vip_endtime']);
  705. //追加点赞
  706. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  707. //时间
  708. $val['createtime_text'] = get_last_time($val['createtime']);
  709. //关注
  710. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  711. //收藏
  712. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  713. //拉黑
  714. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  715. //层主评论数量
  716. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  717. //话题
  718. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->column('name');
  719. }
  720. }
  721. $this->success('success',$list);
  722. }
  723. }