Topicdongtai.php 18 KB

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