Userfollow.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 follow_one(){
  14. $follow_uid = input('doctor_id',0);
  15. if(!$follow_uid){
  16. $this->error(__('Invalid parameters'));
  17. }
  18. $checkuser = Db::name('doctor')->find($follow_uid);
  19. if(empty($checkuser)){
  20. $this->error('此医生不存在');
  21. }
  22. $map = [
  23. 'uid' => $this->auth->id,
  24. 'follow_uid' => $follow_uid,
  25. ];
  26. $check = Db::name('user_follow')->where($map)->find();
  27. if($check){
  28. //取关
  29. $rs = Db::name('user_follow')->where($map)->delete();
  30. $this->success('操作成功');
  31. }
  32. $id = Db::name('user_follow')->insertGetId($map);
  33. $this->success('操作成功',$id);
  34. }
  35. //我的关注uids
  36. public function my_follow_uids(){
  37. $list = Db::name('user_follow')->where('uid',$this->auth->id)->column('follow_uid');
  38. $list = array_flip($list);
  39. $list = array_flip($list);
  40. return $list;
  41. }
  42. ////////////////////////////////////////////////////////////
  43. //我的关注列表
  44. public function my_follow_list(){
  45. $user_id = input('user_id',0);
  46. if(empty($user_id)){
  47. $user_id = $this->auth->id;
  48. }
  49. //列表
  50. $list = Db::name('user_follow')
  51. ->alias('follow')
  52. ->join('user','follow.follow_uid = user.id','LEFT')
  53. ->field('user.id,user.username,user.nickname,user.avatar,user.birthday,user.gender')
  54. ->where('follow.uid',$user_id)->order('follow.id desc')->group('follow.follow_uid')->autopage()->select();
  55. $list = list_domain_image($list,['avatar']);
  56. $list = list_birthday_age($list);
  57. //我的关注uids
  58. $my_follow_uids = $this->my_follow_uids();
  59. if(!empty($list)){
  60. foreach($list as $key => &$val){
  61. //是否关注
  62. $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
  63. }
  64. }
  65. $this->success('success',$list);
  66. }
  67. //我的粉丝列表
  68. public function my_fans_list(){
  69. $user_id = input('user_id',0);
  70. if(empty($user_id)){
  71. $user_id = $this->auth->id;
  72. }
  73. //列表
  74. $list = Db::name('user_follow')
  75. ->alias('follow')
  76. ->join('user','follow.uid = user.id','LEFT')
  77. ->field('user.id,user.username,user.nickname,user.avatar,user.birthday,user.gender')
  78. ->where('follow.follow_uid',$user_id)
  79. ->group('follow.uid')
  80. ->order('follow.id desc')->autopage()->select();
  81. $list = list_domain_image($list,['avatar']);
  82. $list = list_birthday_age($list);
  83. //我的关注uids
  84. $my_follow_uids = $this->my_follow_uids();
  85. if(!empty($list)){
  86. foreach($list as $key => &$val){
  87. //是否关注
  88. $val['is_follow'] = in_array($val['id'],$my_follow_uids) ? 1 : 0;
  89. }
  90. }
  91. $this->success('success',$list);
  92. }
  93. //我的好友,拿粉丝列表改的
  94. public function my_friend_list(){
  95. //我的关注uids
  96. $my_follow_uids = $this->my_follow_uids();
  97. //我的粉丝uids
  98. $my_fans_uids = $this->my_fans_uids();
  99. //合集就是朋友
  100. $friend_ids = array_intersect($my_follow_uids,$my_fans_uids);
  101. //dump($friend_ids);
  102. //好友列表
  103. $list = Db::name('user')->alias('user')
  104. ->field('user.id,user.nickname,user.avatar,a.requesttime')
  105. ->join('user_active a','user.id = a.user_id','LEFT')
  106. ->where('user.id','IN',$friend_ids)->autopage()->order('user.id asc')->select();
  107. if(empty($list)){
  108. $this->success(1,[]);
  109. }
  110. //dump($list);
  111. $list_ids = implode(',',array_column($list,'id'));
  112. //dump($list_ids);
  113. //亲密度列表
  114. $intimacy = Db::name('user_intimacy')->where('(uid = '.$this->auth->id.' and other_uid in ('.$list_ids.')) or (uid in ('.$list_ids.') and other_uid = '.$this->auth->id.')')->select();
  115. //dump($intimacy);
  116. foreach($list as $key => &$val){
  117. $val['intimacy'] = 0;
  118. $val['requesttime_text'] = get_last_time($val['requesttime']);
  119. foreach($intimacy as $k => $v){
  120. if($v['uid'] == $this->auth->id && $v['other_uid'] == $val['id']){
  121. $val['intimacy'] = $v['value'];
  122. }
  123. if($v['uid'] == $val['id'] && $v['other_uid'] == $this->auth->id){
  124. $val['intimacy'] = $v['value'];
  125. }
  126. }
  127. }
  128. $this->success(1,$list);
  129. }
  130. //我的粉丝uids
  131. private function my_fans_uids(){
  132. $list = Db::name('user_follow')->where('follow_uid',$this->auth->id)->column('uid');
  133. $list = array_flip($list);
  134. $list = array_flip($list);
  135. return $list;
  136. }
  137. }