Friend.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\enterprise\controller;
  3. use app\BaseController;
  4. use app\enterprise\model\{Friend as FriendModel,User};
  5. class Friend extends BaseController
  6. {
  7. // 好友申请列表
  8. public function index()
  9. {
  10. $param = $this->request->param();
  11. $map = [];
  12. $map[]=['is_invite','=',1];
  13. $isMine=$param['is_mine'] ?? 0;
  14. if($isMine){
  15. // 我发起的
  16. $map[]=['create_user','=',$this->uid];
  17. }else{
  18. // 我收到的
  19. $map[]=['friend_user_id','=',$this->uid];
  20. }
  21. $data=[];
  22. $model = new FriendModel();
  23. $list = $this->paginate($model->where($map)->order('friend_id desc'));
  24. if ($list) {
  25. $data = $list->toArray()['data'];
  26. $userList = User::matchUser($data, true, ['create_user','friend_user_id'], 120);
  27. foreach ($data as $k => $v) {
  28. $data[$k]['create_user_info'] = $userList[$v['create_user']] ?? [];
  29. $data[$k]['user_id_info'] = $userList[$v['friend_user_id']] ?? [];
  30. $data[$k]['is_group'] = 0;
  31. }
  32. }
  33. return success('', $data,$list->total(),$list->currentPage());
  34. }
  35. // 添加好友
  36. public function add()
  37. {
  38. $param = $this->request->param();
  39. $user_id=$param['user_id'] ?? 0;
  40. if($user_id==$this->uid){
  41. return warning('不能添加自己为好友');
  42. }
  43. $friend=FriendModel::where(['friend_user_id'=>$user_id,'create_user'=>$this->uid])->find();
  44. if($friend){
  45. if($friend->status==1){
  46. return warning('你们已经是好友了');
  47. }elseif($friend->status==2){
  48. return warning('你已经申请过了,请等待对方同意');
  49. }
  50. }
  51. $status=2;
  52. $otherFriend=FriendModel::where(['friend_user_id'=>$this->uid,'create_user'=>$user_id])->find();
  53. if($otherFriend){
  54. if($otherFriend->status>0){
  55. $status=1;
  56. }
  57. }
  58. $model = new FriendModel();
  59. $data=[
  60. 'friend_user_id'=>$user_id,
  61. 'status'=>$status,
  62. 'create_user'=>$this->uid,
  63. 'remark'=>$param['remark'],
  64. 'is_invite'=>1 // 是否为发起方
  65. ];
  66. $model->save($data);
  67. $msg=[
  68. 'fromUser'=>[
  69. 'id'=>'system',
  70. 'displayName'=>'新朋友',
  71. 'avatar'=>'',
  72. ],
  73. 'toContactId'=>'system',
  74. 'id'=>uniqid(),
  75. 'is_group'=>2,
  76. 'content'=>"添加您为好友",
  77. 'status'=>'succeed',
  78. 'sendTime'=>time()*1000,
  79. 'type'=>'event',
  80. 'fileSize'=>0,
  81. 'fileName'=>'',
  82. ];
  83. // 发送好友申请
  84. wsSendMsg($user_id,'simple',$msg);
  85. return success('添加成功');
  86. }
  87. // 接受或者拒绝好友申请
  88. public function update()
  89. {
  90. $param = $this->request->param();
  91. $friend=FriendModel::find($param['friend_id']);
  92. if(!$friend){
  93. return warning('申请不存在');
  94. }
  95. $map=[
  96. 'friend_id'=>$param['friend_id']
  97. ];
  98. FriendModel::where($map)->update(['status'=>$param['status']]);
  99. // 如果是接收,就添加到好友列表
  100. if($param['status']){
  101. $data=[
  102. 'friend_user_id'=>$friend->create_user,
  103. 'create_user'=>$this->uid,
  104. ];
  105. $newFriend=FriendModel::where($data)->find();
  106. if($newFriend){
  107. FriendModel::where($data)->update(['status'=>1]);
  108. return success('你们已经是好友了');
  109. }else{
  110. $data['status']=1;
  111. FriendModel::create($data);
  112. }
  113. // 将对方的信息发送给我,把我的信息发送对方
  114. $user=User::setContact($friend->create_user);
  115. if($user){
  116. wsSendMsg($this->uid,'appendContact',$user);
  117. }
  118. $myInfo=User::setContact($this->uid);
  119. if($myInfo){
  120. wsSendMsg($friend->create_user,'appendContact',$myInfo);
  121. }
  122. }
  123. return success('操作成功');
  124. }
  125. // 删除好友
  126. public function del()
  127. {
  128. $param = $this->request->param();
  129. $map=['friend_user_id'=>$param['id'],'create_user'=>$this->uid];
  130. $friend=FriendModel::where($map)->find();
  131. if(!$friend){
  132. return warning('好友不存在');
  133. }
  134. // 需要删除双方的好友关系
  135. FriendModel::where($map)->delete();
  136. FriendModel::where(['friend_user_id'=>$this->uid,'create_user'=>$param['id']])->delete();
  137. // 性质和删除群聊一样
  138. wsSendMsg($param['id'],'removeGroup',['group_id'=>$this->uid]);
  139. return success('删除成功');
  140. }
  141. // 设置好友备注
  142. public function setNickname()
  143. {
  144. $param = $this->request->param();
  145. if(!$param['nickname']){
  146. return warning('备注不能为空');
  147. }
  148. FriendModel::update(['nickname'=>$param['nickname']],['friend_id'=>$param['friend_id']]);
  149. return success('设置成功');
  150. }
  151. // 获取最新的一条和申请的总数
  152. public function getApplyMsg(){
  153. $model = new FriendModel();
  154. $map[]=['friend_user_id','=',$this->uid];
  155. $map[]=['status','=',2];
  156. $count=$model->where($map)->count();
  157. return success('', $count);
  158. }
  159. }