Topicdongtai.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. $data['user_id'] = $this->auth->id;
  479. $data['ruser_id'] = $check['user_id'];
  480. $data['createtime'] = time();
  481. Db::name('topic_dongtai_report')->insertGetId($data);
  482. $this->success('举报成功');
  483. }*/
  484. //收藏,取消收藏
  485. public function collect(){
  486. $where = [
  487. 'user_id' => $this->auth->id,
  488. 'table' => 'topic_dongtai',
  489. 'table_id' => input('id',0),
  490. ];
  491. $check = Db::name('user_collect')->where($where)->find();
  492. if($check){
  493. Db::name('user_collect')->where($where)->delete();
  494. $this->success('已取消收藏');
  495. }else{
  496. Db::name('user_collect')->insertGetId($where);
  497. $this->success('收藏成功');
  498. }
  499. }
  500. //我的收藏
  501. public function my_collect(){
  502. $collect_id = Db::name('user_collect')->where(['table'=>'topic_dongtai','user_id'=>$this->auth->id])->column('table_id');
  503. $where = ['dt.id'=>['IN',$collect_id]];
  504. $list = Db::name('topic_dongtai')->alias('dt')
  505. ->join('user','dt.user_id = user.id','LEFT')
  506. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  507. ->field('dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  508. ->where($where)
  509. ->order('dt.id desc')->autopage()->select();
  510. $list = list_domain_image($list,['images','audio_file','avatar']);
  511. if(!empty($list)){
  512. foreach($list as $key => &$val){
  513. $val['aite'] = json_decode($val['aite'],true);
  514. //用户年龄
  515. $val['age'] = birthtime_to_age($val['birthday']);
  516. unset($val['birthday']);
  517. //用户vip
  518. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  519. unset($val['vip_endtime']);
  520. //追加点赞
  521. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  522. //时间
  523. $val['createtime_text'] = get_last_time($val['createtime']);
  524. //关注
  525. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  526. //收藏
  527. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  528. //拉黑
  529. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  530. //层主评论数量
  531. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  532. //话题
  533. $ids_str = $val['topic_ids'];
  534. if($ids_str){
  535. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->orderRaw('field(id,'.$ids_str.')')->column('name');
  536. }else{
  537. $val['topic_text'] = [];
  538. }
  539. }
  540. }
  541. $this->success('success',$list);
  542. }
  543. //不感兴趣,屏蔽某条
  544. /*public function screen(){
  545. $data = [
  546. 'user_id' => $this->auth->id,
  547. 'dt_id' => input('dt_id',0),
  548. ];
  549. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  550. if($check){
  551. $this->success('操作成功');
  552. }
  553. Db::name('topic_dongtai_screen')->insertGetId($data);
  554. $this->success('操作成功');
  555. }*/
  556. //评论列表
  557. public function answer_list(){
  558. $dt_id = input('dt_id',0);
  559. //楼
  560. $floor_list = Db::name('topic_dongtai_answer')
  561. ->alias('a')
  562. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  563. ->join('user','a.user_id = user.id','LEFT')
  564. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('a.id desc')->autopage()->select();
  565. $floor_list = list_domain_image($floor_list,['avatar']);
  566. //追加子评论
  567. if(!empty($floor_list)){
  568. foreach($floor_list as $key => &$val){
  569. //下面几条子回复,字符串
  570. $val['childremark'] = '';
  571. $map = [
  572. 'a.dt_id' => $dt_id,
  573. 'a.floor' => $val['floor'],
  574. 'a.level' => 2,
  575. ];
  576. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  577. if($number > 0){
  578. $answer_info = Db::name('topic_dongtai_answer')
  579. ->alias('a')
  580. ->field('user.nickname')
  581. ->join('user','a.user_id = user.id','LEFT')
  582. ->where($map)->order('a.id desc')->find();
  583. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  584. }
  585. //时间处理
  586. $val['createtime_text'] = get_last_time($val['createtime']);
  587. //回复是否已赞
  588. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  589. //用户年龄
  590. $val['age'] = birthtime_to_age($val['birthday']);
  591. unset($val['birthday']);
  592. }
  593. }
  594. $this->success(1,$floor_list);
  595. }
  596. //单独某一层的详细
  597. public function answer_info(){
  598. $answer_id = input('answer_id');
  599. //楼
  600. $floor_info = Db::name('topic_dongtai_answer')
  601. ->alias('a')
  602. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  603. ->join('user','a.user_id = user.id','LEFT')
  604. ->where(['a.id'=>$answer_id])->find();
  605. if(empty($floor_info)){
  606. $this->success(1,[]);
  607. }
  608. $floor_info = info_domain_image($floor_info,['avatar']);
  609. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  610. //用户年龄
  611. $floor_info['age'] = birthtime_to_age($floor_info['birthday']);
  612. unset($floor_info['birthday']);
  613. //回复是否已赞
  614. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  615. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  616. //层
  617. $floors = $floor_info['floor'];
  618. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  619. ->field('a.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute')
  620. ->join('user','a.user_id = user.id','LEFT')
  621. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('a.id desc')->autopage()->select();
  622. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  623. if(!empty($child_lists)){
  624. foreach($child_lists as $key => &$answer){
  625. //用户年龄
  626. $answer['age'] = birthtime_to_age($answer['birthday']);
  627. unset($answer['birthday']);
  628. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  629. $answer['createtime_text'] = get_last_time($answer['createtime']);
  630. }
  631. }
  632. //合并
  633. $floor_info['child'] = $child_lists;
  634. $this->success('success',$floor_info);
  635. }
  636. //是否点赞
  637. private function is_good($dt_id,$uid){
  638. $where = [
  639. 'dt_id' => $dt_id,
  640. 'user_id' => $uid,
  641. ];
  642. $check = Db::name('topic_dongtai_good')->where($where)->find();
  643. if($check){
  644. return 1;
  645. }else{
  646. return 0;
  647. }
  648. }
  649. //回复是否点赞
  650. private function answer_is_good($answer_id,$uid){
  651. $where = [
  652. 'answer_id' => $answer_id,
  653. 'user_id' => $uid,
  654. ];
  655. $check = Db::name('topic_answer_good')->where($where)->find();
  656. if($check){
  657. return 1;
  658. }else{
  659. return 0;
  660. }
  661. }
  662. //动态是否收藏
  663. private function is_collect($dt_id,$uid){
  664. $where = [
  665. 'user_id' => $uid,
  666. 'table' => 'topic_dongtai',
  667. 'table_id' => $dt_id,
  668. ];
  669. $check = Db::name('user_collect')->where($where)->find();
  670. if($check){
  671. return 1;
  672. }else{
  673. return 0;
  674. }
  675. }
  676. //用户是否拉黑
  677. private function is_black($uid,$black_uid){
  678. $where = [
  679. 'uid' => $uid,
  680. 'black_uid' => $black_uid,
  681. ];
  682. $check = Db::name('user_black')->where($where)->find();
  683. if($check){
  684. return 1;
  685. }else{
  686. return 0;
  687. }
  688. }
  689. //我的评论
  690. public function my_answer(){
  691. $map = [
  692. 'a.user_id' => $this->auth->id,
  693. 'a.level' => 1,
  694. ];
  695. $list = Db::name('topic_dongtai_answer')->alias('a')
  696. ->field('a.id,a.createtime,a.content,a.dt_id,
  697. dt.images,dt.content as dt_content,dt.type as dt_type,dtuser.nickname as dtuser_nickname,dtuser.username as dtuser_username,user.username,
  698. user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  699. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  700. ->join('user dtuser','dt.user_id = dtuser.id','LEFT')
  701. ->join('user user','a.user_id = user.id','LEFT')
  702. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  703. ->where($map)->order('a.id desc')->autopage()->select();
  704. $list = list_domain_image($list,['avatar']);
  705. if(!empty($list)){
  706. foreach($list as $key => &$val){
  707. //用户年龄
  708. $val['age'] = birthtime_to_age($val['birthday']);
  709. unset($val['birthday']);
  710. //用户vip
  711. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  712. unset($val['vip_endtime']);
  713. }
  714. }
  715. $this->success(1,$list);
  716. }
  717. //删除我的某个评论
  718. public function delete_answer(){
  719. $id = input('id',0);
  720. if(!$id){
  721. $this->error();
  722. }
  723. Db::startTrans();
  724. $info = Db::name('topic_dongtai_answer')->where('id',$id)->where('user_id',$this->auth->id)->find();
  725. if(!$info){
  726. $this->error('不存在的动态评论');
  727. }
  728. if($info['level'] == 1){
  729. //楼层内都删
  730. $louceng_id = Db::name('topic_dongtai_answer')->where('dt_id',$info['dt_id'])->where('level',2)->where('floor',$info['floor'])->column('id');
  731. if(!empty($louceng_id)){
  732. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum',count($louceng_id));//回复数减1
  733. Db::name('topic_dongtai_answer')->where('id','IN',$louceng_id)->delete();//评论删掉
  734. Db::name('topic_answer_good')->where('answer_id','IN',$louceng_id)->delete();//评论点赞删掉
  735. }
  736. }
  737. Db::name('topic_dongtai')->where('id',$info['dt_id'])->setDec('answernum');//回复数减1
  738. Db::name('topic_dongtai_answer')->where('id',$id)->delete(); //评论删掉
  739. Db::name('topic_answer_good')->where('answer_id',$id)->delete();//评论点赞删掉
  740. Db::commit();
  741. $this->success();
  742. }
  743. //我点赞的动态
  744. public function my_good(){
  745. $where = ['good.user_id'=>$this->auth->id];
  746. $list = Db::name('topic_dongtai')->alias('dt')
  747. ->join('user','dt.user_id = user.id','LEFT')
  748. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  749. ->join('topic_dongtai_good good','dt.id = good.dt_id','LEFT')
  750. ->field('dt.*,dt.id as dt_id,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
  751. ->where($where)
  752. ->order('dt.id desc')->autopage()->select();
  753. $list = list_domain_image($list,['images','audio_file','avatar']);
  754. if(!empty($list)){
  755. foreach($list as $key => &$val){
  756. $val['aite'] = json_decode($val['aite'],true);
  757. //用户年龄
  758. $val['age'] = birthtime_to_age($val['birthday']);
  759. unset($val['birthday']);
  760. //用户vip
  761. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  762. unset($val['vip_endtime']);
  763. //追加点赞
  764. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  765. //时间
  766. $val['createtime_text'] = get_last_time($val['createtime']);
  767. //关注
  768. $val['is_follow'] = $this->is_follow($this->auth->id,$val['user_id']);
  769. //收藏
  770. $val['is_collect'] = $this->is_collect($val['id'],$this->auth->id);
  771. //拉黑
  772. $val['is_black'] = $this->is_black($this->auth->id,$val['user_id']);
  773. //层主评论数量
  774. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  775. //话题
  776. $ids_str = $val['topic_ids'];
  777. if($ids_str){
  778. $val['topic_text'] = Db::name('topic_hub')->where('id','IN',$val['topic_ids'])->orderRaw('field(id,'.$ids_str.')')->column('name');
  779. }else{
  780. $val['topic_text'] = [];
  781. }
  782. }
  783. }
  784. $this->success('success',$list);
  785. }
  786. }