Topicdongtai.php 33 KB

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