Topicdongtai.php 11 KB

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