Topicdongtai.php 33 KB

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