Friend.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\api\controller\tvuser;
  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,tv_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 remove(){
  32. $user_id = input('user_id',0);
  33. if(empty($user_id)){$this->error();}
  34. $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  35. $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  36. $this->success('操作成功');
  37. }
  38. //申请列表
  39. public function apply_list(){
  40. $list = Db::name('friend_apply')->field('user.id,user.avatar,user.nickname,user.tv_mobile')
  41. ->join('user','friend_apply.user_id = user.id','LEFT')
  42. ->where('friend_apply.to_user_id',$this->auth->id)->where('friend_apply.status',0)->autopage()->select();
  43. $list = list_domain_image($list,['avatar']);
  44. $this->success(1,$list);
  45. }
  46. //同意与拒绝
  47. public function apply_audit(){
  48. $user_id = input('user_id',0);
  49. $status = input('status',0);
  50. if(empty($user_id) || empty($status)){
  51. $this->error();
  52. }
  53. if($status == 1){
  54. //同意
  55. Db::startTrans();
  56. $rs1 = Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  57. $rs2 = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  58. $rs3 = Db::name('friend')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  59. $rs4 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->delete();
  60. $data = [
  61. 'user_id' => $user_id,
  62. 'to_user_id' => $this->auth->id,
  63. ];
  64. $rs5 = Db::name('friend')->insertGetId($data);
  65. if($rs1 !== false && $rs2 !== false && $rs3 !== false && $rs4 !== false && $rs5 !== false){
  66. Db::commit();
  67. $this->success('操作成功');
  68. }else{
  69. Db::rollback();
  70. $this->error('操作失败');
  71. }
  72. }else{
  73. //拒绝
  74. Db::name('friend_apply')->where('user_id',$user_id)->where('to_user_id',$this->auth->id)->delete();
  75. $this->success();
  76. }
  77. }
  78. //搜索
  79. public function search(){
  80. $keyword = input('keyword',0);
  81. if(!$keyword){
  82. $this->error();
  83. }
  84. $list = Db::name('user')->field('id,nickname,tv_mobile,avatar')->where('comefrom',2)->where('tv_mobile',$keyword)->where('id','neq',$this->auth->id)->select();
  85. $list = list_domain_image($list,['avatar']);
  86. if(!empty($list)){
  87. foreach($list as $key => $val){
  88. //是否申请过了
  89. $val['is_apply'] = 0;
  90. $check_apply = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
  91. if($check_apply){
  92. $val['is_apply'] = 1;
  93. }
  94. //是否是好友
  95. $val['is_friend'] = 0;
  96. $check_friend1 = Db::name('friend')->where('user_id',$this->auth->id)->where('to_user_id',$val['id'])->find();
  97. $check_friend2 = Db::name('friend')->where('user_id',$val['id'])->where('to_user_id',$this->auth->id)->find();
  98. if($check_friend1 || $check_friend2){
  99. $val['is_friend'] = 1;
  100. }
  101. //
  102. $list[$key] = $val;
  103. }
  104. }
  105. $this->success(1,$list);
  106. }
  107. //申请好友
  108. public function apply(){
  109. $user_id = input('user_id',0);
  110. if(!$user_id){$this->error();}
  111. if($user_id == $this->auth->id){
  112. $this->error('不能添加自己为好友');
  113. }
  114. $check = Db::name('friend_apply')->where('user_id',$this->auth->id)->where('to_user_id',$user_id)->find();
  115. if(!$check){
  116. $data = [
  117. 'user_id' => $this->auth->id,
  118. 'to_user_id' => $user_id,
  119. ];
  120. $id = Db::name('friend_apply')->insertGetId($data);
  121. }
  122. $this->success();
  123. }
  124. }