Topicdongtai.php 33 KB

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