Friend.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\enterprise\controller;
  3. use app\BaseController;
  4. use app\enterprise\model\{ChatDelog, Friend as FriendModel,User,Message};
  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){
  41. return warning(lang('system.notNull'));
  42. }
  43. if($user_id==$this->uid){
  44. return warning(lang('friend.notAddOwn'));
  45. }
  46. // 查看是否限制了好友上限
  47. if($this->userInfo['friend_limit']!=0 && $this->userInfo['role']==0){
  48. $myFriend=FriendModel::where(['create_user'=>$this->userInfo['user_id']])->count();
  49. // 好友已达上限
  50. if($myFriend>$this->userInfo['friend_limit'] || $this->userInfo['friend_limit']<0){
  51. return warning(lang('friend.limit'));
  52. }
  53. }
  54. $friend=FriendModel::where(['friend_user_id'=>$user_id,'create_user'=>$this->uid])->find();
  55. if($friend){
  56. if($friend->status==1){
  57. return warning(lang('friend.already'));
  58. }elseif($friend->status==2){
  59. return warning(lang('friend.repeatApply'));
  60. }elseif($friend->status==3){
  61. return warning(lang('friend.refuse'));
  62. }
  63. }
  64. $status=2;
  65. $otherFriend=FriendModel::where(['friend_user_id'=>$this->uid,'create_user'=>$user_id])->find();
  66. if($otherFriend){
  67. if($otherFriend->status==3){
  68. return warning(lang('friend.refuse'));
  69. }
  70. if($otherFriend->status>0){
  71. $status=1;
  72. }
  73. }
  74. $model = new FriendModel();
  75. $data=[
  76. 'friend_user_id'=>$user_id,
  77. 'status'=>$status,
  78. 'create_user'=>$this->uid,
  79. 'remark'=>$param['remark'],
  80. 'is_invite'=>1 // 是否为发起方
  81. ];
  82. $model->save($data);
  83. $msg=[
  84. 'fromUser'=>[
  85. 'id'=>'system',
  86. 'displayName'=>lang('friend.new'),
  87. 'avatar'=>'',
  88. ],
  89. 'toContactId'=>'system',
  90. 'id'=>uniqid(),
  91. 'is_group'=>2,
  92. 'content'=>lang('friend.apply'),
  93. 'status'=>'succeed',
  94. 'sendTime'=>time()*1000,
  95. 'type'=>'event',
  96. 'fileSize'=>0,
  97. 'fileName'=>'',
  98. ];
  99. // 发送好友申请
  100. wsSendMsg($user_id,'simple',$msg);
  101. return success(lang('system.addOk'));
  102. }
  103. // 接受或者拒绝好友申请
  104. public function update()
  105. {
  106. $param = $this->request->param();
  107. $friend=FriendModel::find($param['friend_id']);
  108. if(!$friend){
  109. return warning(lang('friend.notApply'));
  110. }
  111. $map=[
  112. 'friend_id'=>$param['friend_id']
  113. ];
  114. FriendModel::where($map)->update(['status'=>$param['status']]);
  115. // 如果是接收,就添加到好友列表
  116. if($param['status']){
  117. $data=[
  118. 'friend_user_id'=>$friend->create_user,
  119. 'create_user'=>$this->uid,
  120. ];
  121. $newFriend=FriendModel::where($data)->find();
  122. if($newFriend){
  123. FriendModel::where($data)->update(['status'=>1]);
  124. return success(lang('friend.already'));
  125. }else{
  126. $data['status']=1;
  127. FriendModel::create($data);
  128. }
  129. $content=lang('friend.newChat');
  130. $userM=new User;
  131. // 将对方的信息发送给我,把我的信息发送对方
  132. $user=$userM->setContact($friend->create_user,0,'event',$content);
  133. if($user){
  134. wsSendMsg($this->uid,'appendContact',$user);
  135. }
  136. $myInfo=$userM->setContact($this->uid,0,'event',$content);
  137. if($myInfo){
  138. wsSendMsg($friend->create_user,'appendContact',$myInfo);
  139. }
  140. }
  141. return success(lang('system.success'));
  142. }
  143. // 删除好友
  144. public function del()
  145. {
  146. $param = $this->request->param();
  147. $map=['friend_user_id'=>$param['id'],'create_user'=>$this->uid];
  148. $friend=FriendModel::where($map)->find();
  149. if(!$friend){
  150. return warning(lang('friend.not'));
  151. }
  152. $is_black=$param['is_black'] ?? 0;
  153. // 如果是加入黑名单,则更新状态为3,禁止加好友
  154. if($is_black==1){
  155. FriendModel::where($map)->update(['status'=>3]);
  156. FriendModel::where(['friend_user_id'=>$this->uid,'create_user'=>$param['id']])->update(['status'=>3]);
  157. }else{
  158. // 需要删除双方的好友关系
  159. FriendModel::where($map)->delete();
  160. FriendModel::where(['friend_user_id'=>$this->uid,'create_user'=>$param['id']])->delete();
  161. }
  162. // 删除双方的聊天记录
  163. $chat_identify=chat_identify($this->uid,$param['id']);
  164. Message::where(['chat_identify'=>$chat_identify])->delete();
  165. ChatDelog::where(['to_user'=>$param['id'],'user_id'=>$this->uid,'is_group'=>0])->delete();
  166. // 性质和删除群聊一样
  167. wsSendMsg($param['id'],'removeGroup',['group_id'=>$this->uid]);
  168. return success(lang('system.delOk'));
  169. }
  170. // 设置好友备注
  171. public function setNickname()
  172. {
  173. $param = $this->request->param();
  174. if(!$param['nickname']){
  175. return warning(lang('system.notNull'));
  176. }
  177. FriendModel::update(['nickname'=>$param['nickname']],['friend_id'=>$param['friend_id']]);
  178. return success(lang('system.editOk'));
  179. }
  180. // 获取最新的一条和申请的总数
  181. public function getApplyMsg(){
  182. $model = new FriendModel();
  183. $map[]=['friend_user_id','=',$this->uid];
  184. $map[]=['status','=',2];
  185. $count=$model->where($map)->count();
  186. return success('', $count);
  187. }
  188. }