Topicdongtai.php 17 KB

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