Topicdongtai.php 17 KB

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