Topicdongtai.php 31 KB

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