Topicdongtai.php 14 KB

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