Topicdongtai.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. $audio_file = input('audio_file','');
  19. $audio_second = input('audio_second',0);
  20. $video_file = input('video_file',0);
  21. $type = input('type',1);//媒体类型:1=图片,2=语音,3=视频
  22. if(!$content && !$images && !$audio_file && !$video_file){
  23. $this->error(__('Invalid parameters'));
  24. }
  25. //关键字替换
  26. $content = Keyworld::sensitive($content);
  27. //只保留一个
  28. if($type == 1){
  29. $audio_file = '';
  30. $audio_second = 0;
  31. $video_file = '';
  32. }elseif($type == 2){
  33. $images = '';
  34. $video_file = '';
  35. }else{
  36. $audio_file = '';
  37. $audio_second = 0;
  38. $images = '';
  39. }
  40. $data = [
  41. 'user_id' => $this->auth->id,
  42. 'content' => $content,
  43. 'images' => $images,
  44. 'audio_file' => $audio_file,
  45. 'audio_second' => $audio_second,
  46. 'video_file' => $video_file,
  47. 'type' => $type,
  48. 'createtime' => time(),
  49. 'updatetime' => time(),
  50. 'auditstatus' => 0, //默认不通过
  51. ];
  52. Db::startTrans();
  53. $id = Db::name('topic_dongtai')->insertGetId($data);
  54. //task任务
  55. //发第一条动态
  56. /*if($data['auditstatus'] == 1){
  57. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,10);
  58. if($task_rs === false){
  59. Db::rollback();
  60. $this->error('完成任务失败');
  61. }
  62. }*/
  63. Db::commit();
  64. $this->success('发布成功',$id);
  65. }
  66. //自己看列表
  67. //某用户的帖子列表
  68. public function my_lists(){
  69. $uid = input('uid',$this->auth->id);
  70. if (empty($uid)) {
  71. $uid = $this->auth->id;
  72. }
  73. $where = [
  74. 'dt.user_id'=>$uid,
  75. ];
  76. if($uid != $this->auth->id){
  77. $where['dt.auditstatus'] = 1; //不是自己的,就只能看审核通过的
  78. }else{
  79. $wherep['dt.auditstatus'] = ['neq',2];
  80. }
  81. $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status';
  82. $list = Db::name('topic_dongtai')->alias('dt')
  83. ->join('user','dt.user_id = user.id','LEFT')
  84. ->field($field)
  85. ->where($where)
  86. ->order('dt.id desc')->autopage()->select();
  87. $list = list_domain_image($list,['images','audio_file','avatar','video_file']);
  88. if(!empty($list)){
  89. foreach($list as $key => &$val){
  90. //用户年龄
  91. $val['age'] = birthtime_to_age($val['birthday']);
  92. unset($val['birthday']);
  93. //追加点赞
  94. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  95. //时间
  96. $val['createtime_text'] = get_last_time($val['createtime']);
  97. }
  98. }
  99. $this->success('success',$list);
  100. }
  101. //动态删除
  102. public function delete(){
  103. $id = input('id',0);
  104. $where['id'] = $id;
  105. $where['user_id'] = $this->auth->id;
  106. $dongtai = Db::name('topic_dongtai')->field('id')->where($where)->find();
  107. if (empty($dongtai)) {
  108. $this->error('未找到动态信息');
  109. }
  110. Db::startTrans();
  111. $delRes = Db::name('topic_dongtai')->where('id',$id)->delete();
  112. if (!$delRes) {
  113. Db::rollback();
  114. $this->error('动态删除失败');
  115. }
  116. //点赞
  117. Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
  118. Db::commit();
  119. $this->success('删除成功');
  120. }
  121. //某个圈子里的动态列表,关注,最新,附近
  122. public function topic_list(){
  123. $where = [
  124. 'dt.auditstatus' => 1,
  125. ];
  126. //最新
  127. $order = input('orderby','new');
  128. $orderby = 'dt.goodnum desc,dt.id desc';
  129. //关注
  130. $where_follow = '';
  131. if($order == 'follow'){
  132. //关注的人
  133. $follow_user_ids = Db::name('user_follow')->where(['uid'=>$this->auth->id])->column('follow_uid');
  134. if(!empty($follow_user_ids)){
  135. $where_follow .= '(dt.user_id IN ('.implode(',',$follow_user_ids).'))';
  136. }
  137. //默认
  138. if($where_follow == ''){
  139. $where_follow = 'dt.id = 0';
  140. }
  141. }
  142. //性别
  143. $where['user.gender'] = ['neq',$this->auth->gender];
  144. //排除黑名单的
  145. $where_black = [];
  146. $black_ids = Db::name('user_black')->where(['uid'=>$this->auth->id])->column('black_uid');
  147. if(!empty($black_ids)){
  148. $where_black['dt.user_id'] = ['NOTIN',$black_ids];
  149. }
  150. //列表
  151. $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status';
  152. $list = Db::name('topic_dongtai')->alias('dt')
  153. ->join('user','dt.user_id = user.id','LEFT')
  154. ->field($field)
  155. ->where($where)
  156. ->where($where_follow)
  157. ->where($where_black)
  158. ->order($orderby)
  159. ->autopage()->select();
  160. $list = list_domain_image($list,['images','audio_file','avatar']);
  161. if(!empty($list)){
  162. foreach($list as $key => &$val){
  163. //用户年龄
  164. $val['age'] = birthtime_to_age($val['birthday']);
  165. unset($val['birthday']);
  166. //追加点赞
  167. $val['isgood'] = $this->is_good($val['id'],$this->auth->id);
  168. //时间
  169. $val['createtime_text'] = get_last_time($val['createtime']);
  170. }
  171. }
  172. $this->success('success',$list);
  173. }
  174. //详情
  175. public function info(){
  176. $id = input('id');
  177. $field = 'dt.*,user.username,user.nickname,user.avatar,user.gender,user.birthday,user.idcard_status,user.real_status';
  178. $info = Db::name('topic_dongtai')->alias('dt')
  179. ->join('user','dt.user_id = user.id','LEFT')
  180. ->field($field)
  181. ->where('dt.id',$id)->find();
  182. $info = info_domain_image($info,['images','audio_file','avatar','video_file']);
  183. if($info){
  184. //用户年龄
  185. $info['age'] = birthtime_to_age($info['birthday']);
  186. unset($info['birthday']);
  187. //是否点赞过
  188. $info['isgood'] = $this->is_good($id,$this->auth->id);
  189. //时间
  190. $info['createtime_text'] = get_last_time($info['createtime']);
  191. }else{
  192. $this->error('此动态已被删除');
  193. }
  194. $this->success('success',$info);
  195. }
  196. //点赞,取消点赞
  197. public function good(){
  198. $id = input('id');
  199. $where = [
  200. 'dt_id' => $id,
  201. 'user_id' => $this->auth->id,
  202. ];
  203. $check = Db::name('topic_dongtai_good')->where($where)->find();
  204. $dt_user_id = Db::name('topic_dongtai')->where('id',$id)->value('user_id');
  205. if($check){
  206. Db::name('topic_dongtai_good')->where($where)->delete();
  207. $down = Db::name('topic_dongtai')->where('id',$id)->setDec('goodnum');
  208. $this->success('已取消点赞');
  209. }else{
  210. Db::startTrans();
  211. $where['createtime'] = time();
  212. $rs = Db::name('topic_dongtai_good')->insertGetId($where);
  213. if(!$rs){
  214. Db::rollback();
  215. $this->error('点赞失败');
  216. }
  217. $up = Db::name('topic_dongtai')->where('id',$id)->setInc('goodnum');
  218. if($up === false){
  219. Db::rollback();
  220. $this->error('点赞失败');
  221. }
  222. //系统消息
  223. if($dt_user_id != $this->auth->id){
  224. //入库
  225. $data = [];
  226. $data['user_id'] = $dt_user_id;
  227. $data['dt_id'] = $id;
  228. $data['from_user_id'] = $this->auth->id;
  229. $data['title'] = '点赞了你的动态';
  230. $data['createtime'] = time();
  231. Db::name('topic_dongtai_message')->insertGetId($data);
  232. }
  233. Db::commit();
  234. $this->success('点赞成功');
  235. }
  236. }
  237. //动态通知
  238. public function message(){
  239. $list = Db::name('topic_dongtai_message')->alias('msg')->field('msg.*,user.avatar,user.nickname')
  240. ->join('user','msg.from_user_id = user.id','LEFT')
  241. ->where('msg.user_id',$this->auth->id)->autopage()->select();
  242. $list = list_domain_image($list,['avatar']);
  243. $this->success(1,$list);
  244. }
  245. //举报
  246. public function report(){
  247. $dt_id = input('dt_id',0);
  248. $check = Db::name('topic_dongtai')->where('id',$dt_id)->find();
  249. if(empty($check)){
  250. $this->error('不存在的动态');
  251. }
  252. $data['dt_id'] = $dt_id;
  253. $data['user_id'] = $this->auth->id;
  254. $data['to_user_id'] = $check['user_id'];
  255. $data['createtime'] = time();
  256. Db::name('topic_dongtai_report')->insertGetId($data);
  257. $this->success('举报成功');
  258. }
  259. //是否点赞
  260. private function is_good($dt_id,$uid){
  261. $where = [
  262. 'dt_id' => $dt_id,
  263. 'user_id' => $uid,
  264. ];
  265. $check = Db::name('topic_dongtai_good')->where($where)->find();
  266. if($check){
  267. return 1;
  268. }else{
  269. return 0;
  270. }
  271. }
  272. }