Topicdongtai.php 34 KB

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