Topicdongtai.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Keyworld;
  6. /**
  7. * 圈子动态
  8. */
  9. class Topicdongtai extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //发布动态
  14. public function addone(){
  15. $content = input('content','');
  16. $images = input('images','');
  17. $topic_id = input('topic_id','');
  18. if(!$content && !$images){
  19. $this->error(__('Invalid parameters'));
  20. }
  21. //关键字替换
  22. //$content = Keyworld::sensitive($content);
  23. $data = [
  24. 'topic_id' => $topic_id,
  25. 'user_id' => $this->auth->id,
  26. 'content' => $content,
  27. 'images' => $images,
  28. 'type' => input('type',1),
  29. 'createtime' => time(),
  30. 'updatetime' => time(),
  31. ];
  32. Db::startTrans();
  33. $id = Db::name('topic_dongtai')->insertGetId($data);
  34. //圈子新增一个贴
  35. $rs = Db::name('topic_hub')->where('id',$topic_id)->setInc('t_number');
  36. Db::commit();
  37. $this->success('发布成功',$id);
  38. }
  39. //自己看列表
  40. //某用户的帖子列表
  41. public function my_lists(){
  42. $uid = input('uid',$this->auth->id);
  43. $list = Db::name('topic_dongtai')->alias('dt')
  44. ->join('user','dt.user_id = user.id','LEFT')
  45. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  46. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  47. ->where('dt.user_id',$uid)
  48. ->order('dt.id desc')->autopage()->select();
  49. $list = list_domain_image($list,['images','avatar']);
  50. if(!empty($list)){
  51. foreach($list as $key => &$val){
  52. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  53. //创建视频缩略图
  54. $val['images_thumb'] = '';
  55. if ($val['type'] == 2) {
  56. $images_url = explode('.', $val['images']);
  57. unset($images_url[count($images_url) - 1]);
  58. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  59. }
  60. $val['createtime_text'] = get_last_time($val['createtime']);
  61. }
  62. }
  63. $this->success('success',$list);
  64. }
  65. //是否点赞
  66. private function is_good($dt_id,$uid){
  67. $where = [
  68. 'dt_id' => $dt_id,
  69. 'user_id' => $uid,
  70. ];
  71. $check = Db::name('topic_dongtai_good')->where($where)->find();
  72. if($check){
  73. return 1;
  74. }else{
  75. return 0;
  76. }
  77. }
  78. //回复是否点赞
  79. private function answer_is_good($answer_id,$uid){
  80. $where = [
  81. 'answer_id' => $answer_id,
  82. 'user_id' => $uid,
  83. ];
  84. $check = Db::name('topic_answer_good')->where($where)->find();
  85. if($check){
  86. return 1;
  87. }else{
  88. return 0;
  89. }
  90. }
  91. //是否关注
  92. private function is_follow($user_id,$fans_id){
  93. $where = [
  94. 'user_id' => $user_id,
  95. 'fans_id' => $fans_id,
  96. ];
  97. $check = Db::name('user_fans_follow')->where($where)->find();
  98. if($check){
  99. return 1;
  100. }else{
  101. return 0;
  102. }
  103. }
  104. //详情
  105. public function info(){
  106. $id = input('id');
  107. $info = Db::name('topic_dongtai')->alias('dt')
  108. ->join('user','dt.user_id = user.id','LEFT')
  109. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  110. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  111. ->where('dt.id',$id)->find();
  112. $info = info_domain_image($info,['images','avatar']);
  113. if($info){
  114. //是否点赞过
  115. $info['isgood'] = $this->is_good($id,$this->auth->id);
  116. //创建视频缩略图
  117. $info['images_thumb'] = '';
  118. if ($info['type'] == 2) {
  119. $images_url = explode('.', $info['images']);
  120. unset($images_url[count($images_url) - 1]);
  121. $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
  122. }
  123. //时间
  124. $info['createtime_text'] = get_last_time($info['createtime']);
  125. //关注
  126. $val['is_follow'] = $this->is_follow($info['user_id'],$this->auth->id);
  127. //评论
  128. $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
  129. //$info['answer'] = $this->answer_list($id);
  130. }
  131. $this->success('success',$info);
  132. }
  133. //点赞
  134. public function good(){
  135. $id = input('id');
  136. $where = [
  137. 'dt_id' => $id,
  138. 'user_id' => $this->auth->id,
  139. ];
  140. $check = Db::name('topic_dongtai_good')->where($where)->find();
  141. if($check){
  142. $this->error('已经赞过了');
  143. }
  144. Db::startTrans();
  145. $where['createtime'] = time();
  146. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  147. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  148. if($rs && $up !== false){
  149. Db::commit();
  150. $this->success('点赞成功');
  151. }
  152. Db::rollback();
  153. $this->error('点赞失败');
  154. }
  155. //评论
  156. public function answer(){
  157. $id = input('id',0);
  158. $content = input('content','');
  159. $to_user_id = input('to_user_id',0);
  160. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  161. $floor = input('floor',0);
  162. if(empty($content) || empty($id)){
  163. $this->error();
  164. }
  165. //关键字替换
  166. //$content = Keyworld::sensitive($content);
  167. //判断
  168. if($level == 2 && $floor == 0){
  169. $this->error('楼层错误');
  170. }
  171. //回复楼主,最新楼层
  172. if($level == 1 || $floor == 0){
  173. $to_user_id = 0;
  174. $floor = 1; //默认一楼
  175. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  176. if($last_floor){
  177. $floor = $last_floor + 1;
  178. }
  179. }
  180. //判断user_id
  181. if($to_user_id){
  182. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  183. if(empty($to_user)){
  184. $this->error('被回复的用户不存在');
  185. }
  186. }
  187. //data
  188. $data = [
  189. 'dt_id' => $id,
  190. 'floor' => $floor,
  191. 'user_id' => $this->auth->id,
  192. 'content' => $content,
  193. 'to_user_id' => $to_user_id,
  194. 'level' => $level,
  195. 'createtime' => time(),
  196. 'updatetime' => time(),
  197. ];
  198. Db::startTrans();
  199. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  200. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  201. Db::commit();
  202. $this->success('评价成功');
  203. }
  204. //对评论点赞
  205. public function answer_good(){
  206. $dt_id = input('dt_id',0);
  207. $answer_id = input('answer_id',0);
  208. $where = [
  209. 'dt_id' => $dt_id,
  210. 'answer_id' => $answer_id,
  211. 'user_id' => $this->auth->id,
  212. ];
  213. $check = Db::name('topic_answer_good')->where($where)->find();
  214. if($check){
  215. $this->error('已经赞过了');
  216. }
  217. Db::startTrans();
  218. $where['createtime'] = time();
  219. $rs = Db::name('topic_answer_good')->insertGetId($where);
  220. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  221. if($rs && $up !== false){
  222. Db::commit();
  223. $this->success('点赞成功');
  224. }
  225. Db::rollback();
  226. $this->error('点赞失败');
  227. }
  228. //举报枚举
  229. public function report_enum(){
  230. $arr = [
  231. '侮辱谩骂',
  232. '色情低俗',
  233. '整治敏感',
  234. '违法违规',
  235. '其他',
  236. ];
  237. $this->success(1,$arr);
  238. }
  239. //举报
  240. public function report(){
  241. $field = ['dt_id','type','content','images'];
  242. $data = request_post_hub($field);
  243. $check = Db::name('topic_dongtai')->where('id',$data['dt_id'])->find();
  244. $data['user_id'] = $this->auth->id;
  245. $data['ruser_id'] = $check['user_id'];
  246. $data['createtime'] = time();
  247. Db::name('topic_dongtai_report')->insertGetId($data);
  248. $this->success('举报成功');
  249. }
  250. //不感兴趣,屏蔽某条
  251. public function screen(){
  252. $data = [
  253. 'user_id' => $this->auth->id,
  254. 'dt_id' => input('dt_id',0),
  255. ];
  256. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  257. if($check){
  258. $this->success('操作成功');
  259. }
  260. Db::name('topic_dongtai_screen')->insertGetId($data);
  261. $this->success('操作成功');
  262. }
  263. //评论列表
  264. public function answer_list(){
  265. $dt_id = input('dt_id',0);
  266. //楼
  267. $floor_list = Db::name('topic_dongtai_answer')
  268. ->alias('a')
  269. ->field('a.*,user.nickname,user.avatar,user.gender')
  270. ->join('user','a.user_id = user.id','LEFT')
  271. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('id asc')->autopage()->select();
  272. $floor_list = list_domain_image($floor_list,['avatar']);
  273. //追加子评论
  274. if(!empty($floor_list)){
  275. foreach($floor_list as $key => &$val){
  276. //下面几条子回复,字符串
  277. $val['childremark'] = '';
  278. $map = [
  279. 'a.dt_id' => $dt_id,
  280. 'a.floor' => $val['floor'],
  281. 'a.level' => 2,
  282. ];
  283. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  284. if($number > 0){
  285. $answer_info = Db::name('topic_dongtai_answer')
  286. ->alias('a')
  287. ->field('user.nickname')
  288. ->join('user','a.user_id = user.id','LEFT')
  289. ->where($map)->order('a.id desc')->find();
  290. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  291. }
  292. //时间处理
  293. $val['createtime_text'] = get_last_time($val['createtime']);
  294. //回复是否已赞
  295. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  296. }
  297. }
  298. $this->success(1,$floor_list);
  299. }
  300. //单独某一层的详细
  301. public function answer_info(){
  302. $answer_id = input('answer_id');
  303. //楼
  304. $floor_info = Db::name('topic_dongtai_answer')
  305. ->alias('a')
  306. ->field('a.*,user.nickname,user.avatar,user.gender')
  307. ->join('user','a.user_id = user.id','LEFT')
  308. ->where(['a.id'=>$answer_id])->find();
  309. $floor_info = info_domain_image($floor_info,['avatar']);
  310. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  311. //回复是否已赞
  312. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  313. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  314. //层
  315. $floors = $floor_info['floor'];
  316. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  317. ->field('a.*,user.nickname,user.avatar,user.gender,tuser.nickname as to_nickname,tuser.avatar as to_avatar,tuser.gender as to_gender')
  318. ->join('user','a.user_id = user.id','LEFT')
  319. ->join('user tuser','a.to_user_id = tuser.id','LEFT')
  320. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('id asc')->autopage()->select();
  321. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  322. if(!empty($child_lists)){
  323. foreach($child_lists as $key => &$answer){
  324. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  325. $answer['createtime_text'] = get_last_time($answer['createtime']);
  326. }
  327. }
  328. //合并
  329. $floor_info['child'] = $child_lists;
  330. $this->success('success',$floor_info);
  331. }
  332. //某个圈子里的动态列表,最新,推荐
  333. public function topic_list(){
  334. $topic_id = input('topic_id',0);
  335. $order = input('orderby','new');
  336. $orderby = 'dt.id desc';
  337. if($order == 'hot'){
  338. $orderby = 'dt.goodnum desc';
  339. }
  340. $where = [];
  341. if($topic_id){
  342. $where['dt.topic_id'] = $topic_id;
  343. }
  344. if($order == 'follow'){
  345. $follow_user_ids = Db::name('user_fans_follow')->where(['fans_id'=>$this->auth->id])->column('user_id');
  346. $where['dt.user_id'] = ['IN',$follow_user_ids];
  347. }
  348. //排除屏蔽的
  349. $screen_ids = Db::name('topic_dongtai_screen')->where('user_id',$this->auth->id)->column('dt_id');
  350. if(!empty($screen_ids)){
  351. $where['dt.id'] = ['NOTIN',$screen_ids];
  352. }
  353. //排除黑名单的
  354. $black_ids = Db::name('user_blacklist')->where('user_id',$this->auth->id)->column('black_user_id');
  355. if(!empty($black_ids)){
  356. $where['dt.user_id'] = ['NOTIN',$black_ids];
  357. }
  358. //
  359. $list = Db::name('topic_dongtai')->alias('dt')
  360. ->join('user','dt.user_id = user.id','LEFT')
  361. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  362. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  363. ->where($where)
  364. ->order($orderby)->autopage()->select();
  365. $list = list_domain_image($list,['images','avatar']);
  366. if(!empty($list)){
  367. foreach($list as $key => &$val){
  368. //追加点赞
  369. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  370. //创建视频缩略图
  371. $val['images_thumb'] = '';
  372. if ($val['type'] == 2) {
  373. $images_url = explode('.', $val['images']);
  374. unset($images_url[count($images_url) - 1]);
  375. $val['images_thumb'] = join('.', $images_url) . '_0.jpg';
  376. }
  377. //时间
  378. $val['createtime_text'] = get_last_time($val['createtime']);
  379. //关注
  380. $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
  381. //评论
  382. $val['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$val['id'],'level'=>1])->count();
  383. }
  384. }
  385. $this->success('success',$list);
  386. }
  387. ////////////////////////////////////////////////////////////
  388. //消息-互动消息-评论
  389. //谁评论了我
  390. public function msg_answer(){
  391. $map = [
  392. 'dt.user_id' => $this->auth->id,
  393. 'a.level' => 1,
  394. ];
  395. $list = Db::name('topic_dongtai_answer')->alias('a')
  396. ->field('a.id,a.createtime,user.nickname,user.gender,user.avatar')
  397. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  398. ->join('user','a.user_id = user.id','LEFT')
  399. ->where($map)->order('a.id desc')->autopage()->select();
  400. $list = list_domain_image($list,['avatar']);
  401. if(!empty($list)){
  402. foreach($list as $key => &$val){
  403. //时间
  404. $val['createtime_text'] = get_last_time($val['createtime']);
  405. }
  406. }
  407. $this->success(1,$list);
  408. }
  409. //消息-互动消息-获赞
  410. //谁赞了我
  411. public function msg_good(){
  412. $map = [
  413. 'dt.user_id' => $this->auth->id,
  414. ];
  415. $list = Db::name('topic_dongtai_good')->alias('g')
  416. ->field('g.id,g.createtime,user.nickname,user.gender,user.avatar')
  417. ->join('topic_dongtai dt','g.dt_id = dt.id','LEFT')
  418. ->join('user','g.user_id = user.id','LEFT')
  419. ->where($map)->order('g.id desc')->autopage()->select();
  420. $list = list_domain_image($list,['avatar']);
  421. if(!empty($list)){
  422. foreach($list as $key => &$val){
  423. //时间
  424. $val['createtime_text'] = get_last_time($val['createtime']);
  425. }
  426. }
  427. $this->success(1,$list);
  428. }
  429. }