Topicdongtai.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 = ['info','floor_info','topic_list'];
  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 is_follow($user_id,$fans_id){
  80. $where = [
  81. 'user_id' => $user_id,
  82. 'fans_id' => $fans_id,
  83. ];
  84. $check = Db::name('user_fans_follow')->where($where)->find();
  85. if($check){
  86. return 1;
  87. }else{
  88. return 0;
  89. }
  90. }
  91. //详情
  92. public function info(){
  93. $id = input('id');
  94. $info = Db::name('topic_dongtai')->alias('dt')
  95. ->join('user','dt.user_id = user.id','LEFT')
  96. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  97. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  98. ->where('dt.id',$id)->find();
  99. $info = info_domain_image($info,['images','avatar']);
  100. //是否点赞过
  101. if($info){
  102. $info['isgood'] = $this->is_good($id,$this->auth->id);
  103. //创建视频缩略图
  104. $info['images_thumb'] = '';
  105. if ($info['type'] == 2) {
  106. $images_url = explode('.', $info['images']);
  107. unset($images_url[count($images_url) - 1]);
  108. $info['images_thumb'] = join('.', $images_url) . '_0.jpg';
  109. }
  110. $info['createtime_text'] = $this->get_last_time($info['createtime']);
  111. }
  112. //评论
  113. if($info){
  114. $info['answer'] = $this->answer_list($id);
  115. }
  116. $this->success('success',$info);
  117. }
  118. //点赞
  119. public function good(){
  120. $id = input('id');
  121. $where = [
  122. 'dt_id' => $id,
  123. 'user_id' => $this->auth->id,
  124. ];
  125. $check = Db::name('topic_dongtai_good')->where($where)->find();
  126. if($check){
  127. $this->error('已经赞过了');
  128. }
  129. Db::startTrans();
  130. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  131. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  132. if($rs && $up !== false){
  133. Db::commit();
  134. $this->success('点赞成功');
  135. }
  136. Db::rollback();
  137. $this->error('点赞失败');
  138. }
  139. //评论
  140. public function answer(){
  141. $id = input('id',0);
  142. $content = input('content','');
  143. $to_user_id = input('to_user_id',0);
  144. $level = input('level',1); //回复类型:1=层主回复楼主,2=层中回复
  145. $floor = input('floor',0);
  146. if(empty($content) || empty($id)){
  147. $this->error();
  148. }
  149. //关键字替换
  150. //$content = Keyworld::sensitive($content);
  151. //判断
  152. if($level == 2 && $floor == 0){
  153. $this->error('楼层错误');
  154. }
  155. //回复楼主,最新楼层
  156. if($level == 1 || $floor == 0){
  157. $to_user_id = 0;
  158. $floor = 1; //默认一楼
  159. $last_floor = Db::name('topic_dongtai_answer')->where(['dt_id'=>$id,'level'=>1])->order('floor desc')->value('floor');
  160. if($last_floor){
  161. $floor = $last_floor + 1;
  162. }
  163. }
  164. //判断user_id
  165. if($to_user_id){
  166. $to_user = Db::name('user')->where('id',$to_user_id)->value('id');
  167. if(empty($to_user)){
  168. $this->error('被回复的用户不存在');
  169. }
  170. }
  171. //data
  172. $data = [
  173. 'dt_id' => $id,
  174. 'floor' => $floor,
  175. 'user_id' => $this->auth->id,
  176. 'content' => $content,
  177. 'to_user_id' => $to_user_id,
  178. 'level' => $level,
  179. 'createtime' => time(),
  180. 'updatetime' => time(),
  181. ];
  182. Db::startTrans();
  183. $rs = Db::name('topic_dongtai_answer')->insertGetId($data);
  184. Db::name('topic_dongtai')->setInc('answernum');
  185. Db::commit();
  186. $this->success('评价成功');
  187. }
  188. //评论列表
  189. private function answer_list($dt_id){
  190. //楼
  191. $floor_list = Db::name('topic_dongtai_answer')
  192. ->alias('a')
  193. ->field('a.*,user.nickname,user.avatar,user.gender')
  194. ->join('user','a.user_id = user.id','LEFT')
  195. ->where(['a.dt_id'=>$dt_id,'a.level'=>1])->order('id asc')->autopage()->select();
  196. $floor_list = list_domain_image($floor_list,['avatar']);
  197. //追加子评论
  198. foreach($floor_list as $key => &$val){
  199. $val['childremark'] = '';
  200. $map = [
  201. 'a.dt_id' => $dt_id,
  202. 'a.floor' => $val['floor'],
  203. 'a.level' => 2,
  204. ];
  205. $number = Db::name('topic_dongtai_answer')->alias('a')->where($map)->count();
  206. if($number > 0){
  207. $answer_info = Db::name('topic_dongtai_answer')
  208. ->alias('a')
  209. ->field('user.nickname')
  210. ->join('user','a.user_id = user.id','LEFT')
  211. ->where($map)->order('a.id desc')->find();
  212. $val['childremark'] = $answer_info['nickname'].'...等人,共'.$number.'条回复';
  213. }
  214. }
  215. return $floor_list;
  216. }
  217. //单独某一层的详细
  218. public function floor_info(){
  219. $floor_id = input('floor_id');
  220. //楼
  221. $floor_info = Db::name('topic_dongtai_answer')
  222. ->alias('a')
  223. ->field('a.*,user.nickname,user.avatar,user.gender')
  224. ->join('user','a.user_id = user.id','LEFT')
  225. ->where(['a.id'=>$floor_id])->find();
  226. $floor_info = info_domain_image($floor_info,['avatar']);
  227. //层
  228. $floors = $floor_info['floor'];
  229. $child_lists = Db::name('topic_dongtai_answer')->alias('a')
  230. ->field('a.*,user.nickname,user.avatar,user.gender,tuser.nickname as to_nickname,tuser.avatar as to_avatar,tuser.gender as to_gender')
  231. ->join('user','a.user_id = user.id','LEFT')
  232. ->join('user tuser','a.to_user_id = tuser.id','LEFT')
  233. ->where(['a.floor'=>$floors,'a.level'=>2])->order('id asc')->autopage()->select();
  234. $child_lists = list_domain_image($child_lists,['avatar','to_avatar']);
  235. //合并
  236. $floor_info['child'] = $child_lists;
  237. $this->success('success',$floor_info);
  238. }
  239. //某个圈子里的动态列表,最新,推荐
  240. public function topic_list(){
  241. $topic_id = input('topic_id',0);
  242. $order = input('orderby','new');
  243. $orderby = 'dt.id desc';
  244. if($order == 'hot'){
  245. $orderby = 'dt.goodnum desc';
  246. }
  247. $where = [];
  248. if($topic_id){
  249. $where['dt.topic_id'] = $topic_id;
  250. }
  251. if($order == 'follow'){
  252. $follow_user_ids = Db::name('user_fans_follow')->where(['fans_id'=>$this->auth->id])->column('user_id');
  253. $where['dt.user_id'] = ['IN',$follow_user_ids];
  254. }
  255. $list = Db::name('topic_dongtai')->alias('dt')
  256. ->join('user','dt.user_id = user.id','LEFT')
  257. ->join('topic_hub topic','dt.topic_id = topic.id','LEFT')
  258. ->field('dt.*,user.nickname,user.avatar,user.gender,topic.name as topic_name')
  259. ->where($where)
  260. ->order($orderby)->autopage()->select();
  261. $list = list_domain_image($list,['images','avatar']);
  262. if(!empty($list)){
  263. foreach($list as $key => &$val){
  264. //追加点赞
  265. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  266. //时间
  267. $val['createtime_text'] = $this->get_last_time($val['createtime']);
  268. //关注
  269. $val['is_follow'] = $this->is_follow($val['user_id'],$this->auth->id);
  270. }
  271. }
  272. $this->success('success',$list);
  273. }
  274. /**
  275. * 评论时间转换
  276. * @param null $time
  277. * @return false|string
  278. */
  279. private function get_last_time($time = NULL) {
  280. $text = '';
  281. $time = $time === NULL || $time > time() ? time() : intval($time);
  282. $t = time() - $time; //时间差 (秒)
  283. $y = date('Y', $time)-date('Y', time());//是否跨年
  284. switch($t){
  285. case $t == 0:
  286. $text = '刚刚';
  287. break;
  288. case $t < 60:
  289. $text = $t . '秒前'; // 一分钟内
  290. break;
  291. case $t < 60 * 60:
  292. $text = floor($t / 60) . '分钟前'; //一小时内
  293. break;
  294. case $t < 60 * 60 * 24:
  295. $text = floor($t / (60 * 60)) . '小时前'; // 一天内
  296. break;
  297. case $t < 60 * 60 * 24 * 3:
  298. $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
  299. break;
  300. case $t < 60 * 60 * 24 * 30:
  301. $text = date('m月d日 H:i', $time); //一个月内
  302. break;
  303. case $t < 60 * 60 * 24 * 365&&$y==0:
  304. $text = date('m月d日', $time); //一年内
  305. break;
  306. default:
  307. $text = date('Y年m月d日', $time); //一年以前
  308. break;
  309. }
  310. return $text;
  311. }
  312. }