Friend.php 5.5 KB

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