Topicdongtai.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. $info['createtime_text'] = get_last_time($info['createtime']);
  124. //评论
  125. $info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->count();
  126. //$info['answer'] = $this->answer_list($id);
  127. }
  128. $this->success('success',$info);
  129. }
  130. //点赞
  131. public function good(){
  132. $id = input('id');
  133. $where = [
  134. 'dt_id' => $id,
  135. 'user_id' => $this->auth->id,
  136. ];
  137. $check = Db::name('topic_dongtai_good')->where($where)->find();
  138. if($check){
  139. $this->error('已经赞过了');
  140. }
  141. Db::startTrans();
  142. $where['createtime'] = time();
  143. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  144. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  145. if($rs && $up !== false){
  146. Db::commit();
  147. $this->success('点赞成功');
  148. }
  149. Db::rollback();
  150. $this->error('点赞失败');
  151. }
  152. //评论
  153. public function answer(){
  154. $id = input('id',0);
  155. $content = input('content','');
  156. $to_user_id = input('to_user_id',0);
  157. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  158. $floor = input('floor',0);
  159. if(empty($content) || empty($id)){
  160. $this->error();
  161. }
  162. //关键字替换
  163. //$content = Keyworld::sensitive($content);
  164. //判断
  165. if($level == 2 && $floor == 0){
  166. $this->error('楼层错误');
  167. }
  168. //回复楼主,最新楼层
  169. if($level == 1 || $floor == 0){
  170. $to_user_id = 0;
  171. $floor = 1; //默认一楼
  172. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  173. if($last_floor){
  174. $floor = $last_floor + 1;
  175. }
  176. }
  177. //判断user_id
  178. if($to_user_id){
  179. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  180. if(empty($to_user)){
  181. $this->error('被回复的用户不存在');
  182. }
  183. }
  184. //data
  185. $data = [
  186. 'dt_id' => $id,
  187. 'floor' => $floor,
  188. 'user_id' => $this->auth->id,
  189. 'content' => $content,
  190. 'to_user_id' => $to_user_id,
  191. 'level' => $level,
  192. 'createtime' => time(),
  193. 'updatetime' => time(),
  194. ];
  195. Db::startTrans();
  196. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  197. Db::name('topic_dongtai')->where('id',$id)->setInc('answernum');
  198. Db::commit();
  199. $this->success('评价成功');
  200. }
  201. //对评论点赞
  202. public function answer_good(){
  203. $dt_id = input('dt_id',0);
  204. $answer_id = input('answer_id',0);
  205. $where = [
  206. 'dt_id' => $dt_id,
  207. 'answer_id' => $answer_id,
  208. 'user_id' => $this->auth->id,
  209. ];
  210. $check = Db::name('topic_answer_good')->where($where)->find();
  211. if($check){
  212. $this->error('已经赞过了');
  213. }
  214. Db::startTrans();
  215. $where['createtime'] = time();
  216. $rs = Db::name('topic_answer_good')->insertGetId($where);
  217. $up = Db::name('topic_dongtai_answer')->where('id',$answer_id)->setInc('goodnum');
  218. if($rs && $up !== false){
  219. Db::commit();
  220. $this->success('点赞成功');
  221. }
  222. Db::rollback();
  223. $this->error('点赞失败');
  224. }
  225. //举报枚举
  226. public function report_enum(){
  227. $arr = [
  228. '侮辱谩骂',
  229. '色情低俗',
  230. '整治敏感',
  231. '违法违规',
  232. '其他',
  233. ];
  234. $this->success(1,$arr);
  235. }
  236. //举报
  237. public function report(){
  238. $field = ['dt_id','type','content','images'];
  239. $data = request_post_hub($field);
  240. $data['user_id'] = $this->auth->id;
  241. $data['createtime'] = time();
  242. Db::name('topic_dongtai_report')->insertGetId($data);
  243. $this->success('举报成功');
  244. }
  245. //不感兴趣,屏蔽某条
  246. public function screen(){
  247. $data = [
  248. 'user_id' => $this->auth->id,
  249. 'dt_id' => input('dt_id',0),
  250. ];
  251. $check = Db::name('topic_dongtai_screen')->where($data)->find();
  252. if($check){
  253. $this->success('操作成功');
  254. }
  255. Db::name('topic_dongtai_screen')->insertGetId($data);
  256. $this->success('操作成功');
  257. }
  258. //评论列表
  259. public function answer_list(){
  260. $dt_id = input('dt_id',0);
  261. //楼
  262. $floor_list = Db::name('topic_dongtai_answer')
  263. ->alias('a')
  264. ->field('a.*,user.nickname,user.avatar,user.gender')
  265. ->join('user','a.user_id = user.id','LEFT')
  266. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('id asc')->autopage()->select();
  267. $floor_list = list_domain_image($floor_list,['avatar']);
  268. //追加子评论
  269. if(!empty($floor_list)){
  270. foreach($floor_list as $key => &$val){
  271. //下面几条子回复,字符串
  272. $val['childremark'] = '';
  273. $map = [
  274. 'a.dt_id' => $dt_id,
  275. 'a.floor' => $val['floor'],
  276. 'a.level' => 2,
  277. ];
  278. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  279. if($number > 0){
  280. $answer_info = Db::name('topic_dongtai_answer')
  281. ->alias('a')
  282. ->field('user.nickname')
  283. ->join('user','a.user_id = user.id','LEFT')
  284. ->where($map)->order('a.id desc')->find();
  285. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  286. }
  287. //时间处理
  288. $val['createtime_text'] = get_last_time($val['createtime']);
  289. //回复是否已赞
  290. $val['is_good'] = $this->answer_is_good($val['id'],$this->auth->id);
  291. }
  292. }
  293. $this->success(1,$floor_list);
  294. }
  295. //单独某一层的详细
  296. public function answer_info(){
  297. $answer_id = input('answer_id');
  298. //楼
  299. $floor_info = Db::name('topic_dongtai_answer')
  300. ->alias('a')
  301. ->field('a.*,user.nickname,user.avatar,user.gender')
  302. ->join('user','a.user_id = user.id','LEFT')
  303. ->where(['a.id'=>$answer_id])->find();
  304. $floor_info = info_domain_image($floor_info,['avatar']);
  305. $floor_info['createtime_text'] = get_last_time($floor_info['createtime']);
  306. //回复是否已赞
  307. $floor_info['is_good'] = $this->answer_is_good($answer_id,$this->auth->id);
  308. $floor_info['answernumber'] = Db::name('topic_dongtai_answer')->where(['dt_id'=>$floor_info['dt_id'],'floor'=>$floor_info['floor'],'level'=>2])->count();
  309. //层
  310. $floors = $floor_info['floor'];
  311. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  312. ->field('a.*,user.nickname,user.avatar,user.gender,tuser.nickname as to_nickname,tuser.avatar as to_avatar,tuser.gender as to_gender')
  313. ->join('user','a.user_id = user.id','LEFT')
  314. ->join('user tuser','a.to_user_id = tuser.id','LEFT')
  315. ->where(['a.dt_id'=>$floor_info['dt_id'],'a.floor'=>$floors,'a.level'=>2])->order('id asc')->autopage()->select();
  316. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  317. if(!empty($child_lists)){
  318. foreach($child_lists as $key => &$answer){
  319. $answer['is_good'] = $this->answer_is_good($answer['id'],$this->auth->id);
  320. $answer['createtime_text'] = get_last_time($answer['createtime']);
  321. }
  322. }
  323. //合并
  324. $floor_info['child'] = $child_lists;
  325. $this->success('success',$floor_info);
  326. }
  327. //某个圈子里的动态列表,最新,推荐
  328. public function topic_list(){
  329. $topic_id = input('topic_id',0);
  330. $order = input('orderby','new');
  331. $orderby = 'dt.id desc';
  332. if($order == 'hot'){
  333. $orderby = 'dt.goodnum desc';
  334. }
  335. $where = [];
  336. if($topic_id){
  337. $where['dt.topic_id'] = $topic_id;
  338. }
  339. if($order == 'follow'){
  340. $follow_user_ids = Db::name('user_fans_follow')->where(['fans_id'=>$this->auth->id])->column('user_id');
  341. $where['dt.user_id'] = ['IN',$follow_user_ids];
  342. }
  343. //排除屏蔽的
  344. $screen_ids = Db::name('topic_dongtai_screen')->where('user_id',$this->auth->id)->column('dt_id');
  345. if(!empty($screen_ids)){
  346. $where['dt.id'] = ['NOTIN',$screen_ids];
  347. }
  348. //排除黑名单的
  349. $black_ids = Db::name('user_blacklist')->where('user_id',$this->auth->id)->column('black_user_id');
  350. if(!empty($black_ids)){
  351. $where['dt.user_id'] = ['NOTIN',$black_ids];
  352. }
  353. //
  354. $list = Db::name('topic_dongtai')->alias('dt')
  355. ->join('user','dt.user_id = user.id','LEFT')
  356. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  357. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  358. ->where($where)
  359. ->order($orderby)->autopage()->select();
  360. $list = list_domain_image($list,['images','avatar']);
  361. if(!empty($list)){
  362. foreach($list as $key => &$val){
  363. //追加点赞
  364. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  365. //时间
  366. $val['createtime_text'] = get_last_time($val['createtime']);
  367. //关注
  368. $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
  369. }
  370. }
  371. $this->success('success',$list);
  372. }
  373. ////////////////////////////////////////////////////////////
  374. //消息-互动消息-评论
  375. //谁评论了我
  376. public function msg_answer(){
  377. $map = [
  378. 'dt.user_id' => $this->auth->id,
  379. 'a.level' => 1,
  380. ];
  381. $list = Db::name('topic_dongtai_answer')->alias('a')
  382. ->field('a.id,a.createtime,user.nickname,user.gender,user.avatar')
  383. ->join('topic_dongtai dt','a.dt_id = dt.id','LEFT')
  384. ->join('user','a.user_id = user.id','LEFT')
  385. ->where($map)->order('a.id desc')->autopage()->select();
  386. $list = list_domain_image($list,['avatar']);
  387. if(!empty($list)){
  388. foreach($list as $key => &$val){
  389. //时间
  390. $val['createtime_text'] = get_last_time($val['createtime']);
  391. }
  392. }
  393. $this->success(1,$list);
  394. }
  395. //消息-互动消息-获赞
  396. //谁赞了我
  397. public function msg_good(){
  398. $map = [
  399. 'dt.user_id' => $this->auth->id,
  400. ];
  401. $list = Db::name('topic_dongtai_good')->alias('g')
  402. ->field('g.id,g.createtime,user.nickname,user.gender,user.avatar')
  403. ->join('topic_dongtai dt','g.dt_id = dt.id','LEFT')
  404. ->join('user','g.user_id = user.id','LEFT')
  405. ->where($map)->order('g.id desc')->autopage()->select();
  406. $list = list_domain_image($list,['avatar']);
  407. if(!empty($list)){
  408. foreach($list as $key => &$val){
  409. //时间
  410. $val['createtime_text'] = get_last_time($val['createtime']);
  411. }
  412. }
  413. $this->success(1,$list);
  414. }
  415. }