Friend.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 lists(){
  14. $field = '
  15. friend.id,friend.user_id,friend.to_user_id,
  16. u.avatar as u_avatar,u.nickname as u_nickname,u.mobile as u_mobile,
  17. tu.avatar as tu_avatar,tu.nickname as tu_nickname,tu.mobile as tu_mobile
  18. ';
  19. $list = Db::name('friend')->field($field)
  20. ->join('user u' ,'friend.user_id = u.id','LEFT')
  21. ->join('user tu' ,'friend.to_user_id = tu.id','LEFT')
  22. ->where('(friend.user_id = '.$this->auth->id.') or (friend.to_user_id = '.$this->auth->id.')')
  23. ->order('friend.id desc')->autopage()
  24. ->select();
  25. $result = [];
  26. if(!empty($list)){
  27. foreach($list as $key => $val){
  28. $newval = [];
  29. if($val['user_id'] == $this->auth->id){
  30. $newval = [
  31. 'id' => $val['to_user_id'],
  32. 'nickname' => $val['tu_nickname'],
  33. 'avatar' => $val['tu_avatar'],
  34. 'mobile' => $val['tu_mobile'],
  35. ];
  36. }else{
  37. $newval = [
  38. 'id' => $val['user_id'],
  39. 'nickname' => $val['u_nickname'],
  40. 'avatar' => $val['u_avatar'],
  41. 'mobile' => $val['u_mobile'],
  42. ];
  43. }
  44. $result[] = $newval;
  45. }
  46. }
  47. $this->success(1,$result);
  48. }
  49. //移除好友
  50. public function remove(){
  51. $user_id = input('user_id',0);
  52. if(empty($user_id)){$this->error();}
  53. $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  54. $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  55. $this->success('操作成功');
  56. }
  57. //申请列表
  58. public function apply_list(){
  59. $list = Db::name('friend_apply')->field('user.id,user.avatar,user.nickname,user.mobile')
  60. ->join('user','friend_apply.user_id = user.id','LEFT')
  61. ->where('friend_apply.to_user_id',$this->auth->id)->where('friend_apply.status',0)->autopage()->select();
  62. $list = list_domain_image($list,['avatar']);
  63. $this->success(1,$list);
  64. }
  65. //同意与拒绝
  66. public function apply_audit(){
  67. $user_id = input('user_id',0);
  68. $status = input('status',0);
  69. if(empty($user_id) || empty($status)){
  70. $this->error();
  71. }
  72. if($status == 1){
  73. //同意
  74. Db::startTrans();
  75. $rs1 = Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  76. $rs2 = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  77. $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  78. $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  79. $data = [
  80. 'user_id' => $user_id,
  81. 'to_user_id' => $this->auth->id,
  82. ];
  83. $rs5 = Db::name('friend')->insertGetId($data);
  84. if($rs1 !== false && $rs2 !== false && $rs3 !== false && $rs4 !== false && $rs5 !== false){
  85. Db::commit();
  86. $this->success('操作成功');
  87. }else{
  88. Db::rollback();
  89. $this->error('操作失败');
  90. }
  91. }else{
  92. //拒绝
  93. Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  94. $this->success();
  95. }
  96. }
  97. //搜索
  98. public function search(){
  99. $keyword = input('keyword',0);
  100. if(!$keyword){$this->error();}
  101. $list = Db::name('user')->field('id,nickname,mobile,avatar')->where('mobile',$keyword)->select();
  102. $list = list_domain_image($list,['avatar']);
  103. $this->success(1,$list);
  104. }
  105. //申请好友
  106. public function apply(){
  107. $user_id = input('user_id',0);
  108. if(!$user_id){$this->error();}
  109. $check = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->find();
  110. if(!$check){
  111. $data = [
  112. 'user_id' => $this->auth->id,
  113. 'to_user_id' => $user_id,
  114. ];
  115. $id = Db::name('friend_apply')->insertGetId($data);
  116. }
  117. $this->success(1);
  118. }
  119. }