Userfollow.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 关注
  7. */
  8. class Userfollow extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //我的关注列表
  13. public function my_follow_list(){
  14. $user_id = input('user_id',0);
  15. if(empty($user_id)){
  16. $user_id = $this->auth->id;
  17. }
  18. //如果不是我,并且开了隐私,直接返回空
  19. if($user_id != $this->auth->id){
  20. $yinsi = $this->user_power($user_id,'yinsi');
  21. if($yinsi == 1){
  22. $this->success(1,[]);
  23. }
  24. }
  25. //列表
  26. $list = Db::name('user_follow')
  27. ->alias('follow')
  28. ->join('user','follow.follow_uid = user.id','LEFT')
  29. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  30. ->field('user.id,user.username,user.nickname,user.avatar,user.bio,user.birthday,user.gender,user.attribute,uw.vip_endtime')
  31. ->where('follow.uid',$user_id)->order('follow.id desc')->group('follow.follow_uid')->autopage()->select();
  32. $list = list_domain_image($list,['avatar']);
  33. $list = list_birthday_age($list);
  34. //我的关注uids
  35. $my_follow_uids = $this->my_follow_uids();
  36. if(!empty($list)){
  37. foreach($list as $key => &$val){
  38. //用户vip
  39. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  40. unset($val['vip_endtime']);
  41. //是否关注
  42. $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
  43. }
  44. }
  45. $this->success('success',$list);
  46. }
  47. //我的粉丝列表
  48. public function my_fans_list(){
  49. $user_id = input('user_id',0);
  50. if(empty($user_id)){
  51. $user_id = $this->auth->id;
  52. }
  53. //如果不是我,并且开了隐私,直接返回空
  54. if($user_id != $this->auth->id){
  55. $yinsi = $this->user_power($user_id,'yinsi');
  56. if($yinsi == 1){
  57. $this->success(1,[]);
  58. }
  59. }
  60. //列表
  61. $list = Db::name('user_follow')
  62. ->alias('follow')
  63. ->join('user','follow.uid = user.id','LEFT')
  64. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  65. ->join('user_power power','user.id = power.user_id','LEFT')
  66. ->field('user.id,user.username,user.nickname,user.avatar,user.bio,user.birthday,user.gender,user.attribute,uw.vip_endtime')
  67. ->where('follow.follow_uid',$user_id)
  68. ->where('power.yinsi',0)
  69. ->group('follow.uid')
  70. ->order('follow.id desc')->autopage()->select();
  71. $list = list_domain_image($list,['avatar']);
  72. $list = list_birthday_age($list);
  73. //我的关注uids
  74. $my_follow_uids = $this->my_follow_uids();
  75. if(!empty($list)){
  76. foreach($list as $key => &$val){
  77. //用户vip
  78. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  79. unset($val['vip_endtime']);
  80. //是否关注
  81. $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
  82. }
  83. }
  84. $this->success('success',$list);
  85. }
  86. //我的好友,拿粉丝列表改的
  87. public function my_friend_list(){
  88. $list = Db::name('user_follow')
  89. ->alias('follow')
  90. ->join('user','follow.uid = user.id','LEFT')
  91. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  92. ->field('user.id,user.username,user.nickname,user.avatar,user.bio,user.birthday,user.gender,user.attribute,uw.vip_endtime')
  93. ->where('follow.follow_uid',$this->auth->id)
  94. ->group('follow.uid')
  95. ->order('follow.id desc')->autopage()->select();
  96. $list = list_domain_image($list,['avatar']);
  97. $list = list_birthday_age($list);
  98. //我的关注uids
  99. $my_follow_uids = $this->my_follow_uids();
  100. $new_rs = []; //另起
  101. if(!empty($list)){
  102. foreach($list as $key => &$val){
  103. //用户vip
  104. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  105. unset($val['vip_endtime']);
  106. //是否关注
  107. $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
  108. //我的粉丝,且是我的关注的,另起
  109. if($val['is_follow'] == 1){
  110. $new_rs[] = $val;
  111. }
  112. }
  113. }
  114. $this->success('success',$new_rs);
  115. }
  116. //好友审核列表。粉丝列表改的。关注了我,我还没关注他的
  117. //接口废弃,使用 newfriend_list
  118. public function my_fans_unfollow_list(){
  119. //我的关注uids
  120. $my_follow_uids = $this->my_follow_uids();
  121. $list = Db::name('user_follow')
  122. ->alias('follow')
  123. ->join('user','follow.uid = user.id','LEFT')
  124. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  125. ->field('user.id,user.username,user.nickname,user.avatar,user.bio,user.birthday,user.gender,user.attribute,uw.vip_endtime')
  126. ->where('follow.follow_uid',$this->auth->id)->where('user.id','NOT IN',$my_follow_uids)->order('follow.id desc')->autopage()->select();
  127. $list = list_domain_image($list,['avatar']);
  128. $list = list_birthday_age($list);
  129. if(!empty($list)){
  130. foreach($list as $key => &$val){
  131. //用户vip
  132. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  133. unset($val['vip_endtime']);
  134. //是否关注
  135. $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
  136. }
  137. }
  138. $this->success('success',$list);
  139. }
  140. //好友待审核列表。
  141. public function newfriend_list(){
  142. //我的关注uids
  143. $my_follow_uids = $this->my_follow_uids();
  144. // dump($my_follow_uids);
  145. $list = Db::name('message')->alias('msg')
  146. ->join('user user','user.id = msg.infotype_id','LEFT')
  147. ->join('user_wallet uw','user.id = uw.user_id','LEFT')
  148. ->field('msg.id as msg_id,user.id,user.username,user.nickname,user.avatar,user.bio,user.birthday,user.gender,user.attribute,uw.vip_endtime')
  149. ->where(['msg.infotype'=>'newfriend','msg.user_id'=>$this->auth->id,'msg.audit_status'=>0])
  150. ->where('msg.infotype_id','NOT IN',$my_follow_uids)
  151. ->order('msg.id desc')->autopage()->select();
  152. $list = list_domain_image($list,['avatar']);
  153. $list = list_birthday_age($list);
  154. if(!empty($list)){
  155. foreach($list as $key => &$val){
  156. //用户vip
  157. $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
  158. unset($val['vip_endtime']);
  159. //是否关注
  160. $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
  161. }
  162. }
  163. $this->success('success',$list);
  164. }
  165. //拒绝好友请求
  166. public function refuse_newfriend(){
  167. $id = input('msg_id',0);
  168. $msg_info = Db::name('message')->where('id',$id)->find();
  169. Db::name('message')->where('id',$id)->update(['audit_status'=>1]);
  170. //拒绝给个消息
  171. if($msg_info['infotype'] == 'newfriend' && !empty($msg_info['infotype_id'])){
  172. $msg_id = \app\common\model\Message::addMessage($msg_info['infotype_id'],'好友申请被拒绝',$this->auth->nickname.'拒绝了您的好友请求');
  173. }
  174. $this->success('已拒绝');
  175. }
  176. //关注某人
  177. public function follow_one(){
  178. $follow_uid = input('follow_uid',0);
  179. $msg_id = input('msg_id',0);
  180. if(!$follow_uid){
  181. $this->error(__('Invalid parameters'));
  182. }
  183. $checkuser = Db::name('user')->find($follow_uid);
  184. if(empty($checkuser)){
  185. $this->error('此用户不存在');
  186. }
  187. if($follow_uid == $this->auth->id){
  188. $this->error('不能关注自己');
  189. }
  190. $map = [
  191. 'uid' => $this->auth->id,
  192. 'follow_uid' => $follow_uid,
  193. ];
  194. $check = Db::name('user_follow')->where($map)->find();
  195. if($check){
  196. //取关
  197. $rs = Db::name('user_follow')->where($map)->delete();
  198. $this->success('操作成功');
  199. }
  200. Db::startTrans();
  201. $id = Db::name('user_follow')->insertGetId($map);
  202. if(!$id){
  203. Db::rollback();
  204. $this->error('关注失败');
  205. }
  206. //被关注人如果没有关注我,发一条消息,让他加好友,也就是让他回关
  207. $map2 = [
  208. 'uid' => $follow_uid,
  209. 'follow_uid' => $this->auth->id,
  210. ];
  211. $check = Db::name('user_follow')->where($map2)->find();
  212. if(empty($check) && $this->user_power($this->auth->id,'yinsi') != 1){
  213. //防止重复刷关注,先删掉以前的,再加新的
  214. Db::name('message')
  215. ->where('user_id',$follow_uid)
  216. ->where('infotype','newfriend')
  217. ->where('infotype_id',$this->auth->id)
  218. ->delete();
  219. $message = [
  220. 'user_id' => $follow_uid,
  221. 'title' => '好友请求',
  222. 'content' => '有人关注了你,回关TA成为好友',
  223. 'createtime' => time(),
  224. 'status' => 0,
  225. 'infotype' => 'newfriend',//关注,请求好友
  226. 'infotype_id' => $this->auth->id,
  227. ];
  228. Db::name('message')->insertGetId($message);
  229. }
  230. //我从好友审核列表过来的,我又回关了
  231. if(!empty($msg_id)){
  232. Db::name('message')->where('id',$id)->update(['audit_status'=>1]);
  233. $msg_id = \app\common\model\Message::addMessage($follow_uid,'好友申请已通过',$this->auth->nickname.'同意了您的好友请求');
  234. }
  235. Db::commit();
  236. $this->success('操作成功',$id);
  237. }
  238. //取关某人
  239. public function un_follow_one(){
  240. $follow_uid = input('follow_uid',0);
  241. if(!$follow_uid){
  242. $this->error(__('Invalid parameters'));
  243. }
  244. $checkuser = Db::name('user')->find($follow_uid);
  245. if(empty($checkuser)){
  246. $this->error('此用户不存在');
  247. }
  248. $map = [
  249. 'uid' => $this->auth->id,
  250. 'follow_uid' => $follow_uid,
  251. ];
  252. //不检查,全删
  253. $rs = Db::name('user_follow')->where($map)->delete();
  254. $this->success('操作成功');
  255. }
  256. //我的关注uids
  257. private function my_follow_uids(){
  258. $list = Db::name('user_follow')->where('uid',$this->auth->id)->column('follow_uid');
  259. $list = array_flip($list);
  260. $list = array_flip($list);
  261. return $list;
  262. }
  263. //我的粉丝uids
  264. private function my_fans_uids(){
  265. $list = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  266. $list = array_flip($list);
  267. $list = array_flip($list);
  268. return $list;
  269. }
  270. }