Topicdongtai.php 14 KB

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