Topicdongtai.php 34 KB

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