Friend.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Tenim;
  6. /**
  7. * 好友
  8. */
  9. class Friend extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //好友列表
  14. public function friend_lists(){
  15. $ids = Db::name('friend')->where('user_id',$this->auth->id)->column('to_user_id');
  16. $ids2 = Db::name('friend')->where('to_user_id',$this->auth->id)->column('user_id');
  17. $ids3 = array_merge($ids,$ids2);
  18. $ids4 = array_unique($ids3);
  19. /*dump($ids);
  20. dump($ids2);
  21. dump($ids3);
  22. dump($ids4);*/
  23. $list = [];
  24. if(!empty($ids4)){
  25. $order = 'field(id,'.implode(',',$ids4).')';
  26. $list = Db::name('user')->field('id,nickname,avatar,mobile')->where('id','IN',$ids4)->orderRaw($order)->autopage()->select();
  27. $list = list_domain_image($list,['avatar']);
  28. }
  29. $this->success(1,$list);
  30. }
  31. //没做防重复的好友列表,废弃
  32. public function aaa(){
  33. $field = '
  34. friend.id,friend.user_id,friend.to_user_id,
  35. u.avatar as u_avatar,u.nickname as u_nickname,u.mobile as u_mobile,
  36. tu.avatar as tu_avatar,tu.nickname as tu_nickname,tu.mobile as tu_mobile
  37. ';
  38. $list = Db::name('friend')->field($field)
  39. ->join('user u' ,'friend.user_id = u.id','LEFT')
  40. ->join('user tu' ,'friend.to_user_id = tu.id','LEFT')
  41. ->where('(friend.user_id = '.$this->auth->id.') or (friend.to_user_id = '.$this->auth->id.')')
  42. ->order('friend.id desc')->autopage()
  43. ->select();
  44. $result = [];
  45. if(!empty($list)){
  46. foreach($list as $key => $val){
  47. $newval = [];
  48. if($val['user_id'] == $this->auth->id){
  49. $newval = [
  50. 'id' => $val['to_user_id'],
  51. 'nickname' => $val['tu_nickname'],
  52. 'avatar' => localpath_to_netpath($val['tu_avatar']),
  53. 'mobile' => $val['tu_mobile'],
  54. ];
  55. }else{
  56. $newval = [
  57. 'id' => $val['user_id'],
  58. 'nickname' => $val['u_nickname'],
  59. 'avatar' => localpath_to_netpath($val['u_avatar']),
  60. 'mobile' => $val['u_mobile'],
  61. ];
  62. }
  63. $result[] = $newval;
  64. }
  65. }
  66. $this->success(1,$result);
  67. }
  68. //移除好友
  69. public function remove(){
  70. $user_id = input('user_id',0);
  71. if(empty($user_id)){$this->error();}
  72. $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  73. $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  74. //IM删除好友
  75. /*$tenim = new Tenim();
  76. $rs = $tenim->friend_del();*/
  77. $this->success('操作成功');
  78. }
  79. //申请列表
  80. public function apply_list(){
  81. $list = Db::name('friend_apply')->field('user.id,user.avatar,user.nickname,user.mobile')
  82. ->join('user','friend_apply.user_id = user.id','LEFT')
  83. ->where('friend_apply.to_user_id',$this->auth->id)->where('friend_apply.status',0)->autopage()->select();
  84. $list = list_domain_image($list,['avatar']);
  85. $this->success(1,$list);
  86. }
  87. //同意与拒绝
  88. public function apply_audit(){
  89. $user_id = input('user_id',0);
  90. $status = input('status',0);
  91. if(empty($user_id) || empty($status)){
  92. $this->error();
  93. }
  94. if($status == 1){
  95. //同意
  96. Db::startTrans();
  97. $rs1 = Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  98. $rs2 = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  99. $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  100. $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  101. $data = [
  102. 'user_id' => $user_id,
  103. 'to_user_id' => $this->auth->id,
  104. ];
  105. $rs5 = Db::name('friend')->insertGetId($data);
  106. if($rs1 !== false && $rs2 !== false && $rs3 !== false && $rs4 !== false && $rs5 !== false){
  107. Db::commit();
  108. //IM添加好友
  109. /*$tenim = new Tenim();
  110. $rs = $tenim->friend_add();*/
  111. $this->success('操作成功');
  112. }else{
  113. Db::rollback();
  114. $this->error('操作失败');
  115. }
  116. }else{
  117. //拒绝
  118. Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  119. $this->success();
  120. }
  121. }
  122. //搜索
  123. public function search(){
  124. $keyword = input('keyword',0);
  125. if(!$keyword){$this->error();}
  126. $list = Db::name('user')->field('id,nickname,mobile,avatar')->where('mobile',$keyword)->where('id','neq',$this->auth->id)->select();
  127. $list = list_domain_image($list,['avatar']);
  128. if(!empty($list)){
  129. foreach($list as $key => $val){
  130. //是否申请过了
  131. $val['is_apply'] = 0;
  132. $check_apply = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
  133. if($check_apply){
  134. $val['is_apply'] = 1;
  135. }
  136. //是否是好友
  137. $val['is_friend'] = 0;
  138. $check_friend1 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
  139. $check_friend2 = Db::name('friend')->where('user_id',$val['id'])->where('to_user_id',$this->auth->id)->find();
  140. if($check_friend1 || $check_friend2){
  141. $val['is_friend'] = 1;
  142. }
  143. //
  144. $list[$key] = $val;
  145. }
  146. }
  147. $this->success(1,$list);
  148. }
  149. //申请好友
  150. public function apply(){
  151. $user_id = input('user_id',0);
  152. if(!$user_id){$this->error();}
  153. if($user_id == $this->auth->id){
  154. $this->error('不能添加自己为好友');
  155. }
  156. $check = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->find();
  157. if(!$check){
  158. $data = [
  159. 'user_id' => $this->auth->id,
  160. 'to_user_id' => $user_id,
  161. ];
  162. $id = Db::name('friend_apply')->insertGetId($data);
  163. }
  164. $this->success();
  165. }
  166. }