request->param(); $map = []; $map[]=['is_invite','=',1]; $isMine=$param['is_mine'] ?? 0; if($isMine){ // 我发起的 $map[]=['create_user','=',$this->uid]; }else{ // 我收到的 $map[]=['friend_user_id','=',$this->uid]; } $data=[]; $model = new FriendModel(); $list = $this->paginate($model->where($map)->order('friend_id desc')); if ($list) { $data = $list->toArray()['data']; $userList = User::matchUser($data, true, ['create_user','friend_user_id'], 120); foreach ($data as $k => $v) { $data[$k]['create_user_info'] = $userList[$v['create_user']] ?? []; $data[$k]['user_id_info'] = $userList[$v['friend_user_id']] ?? []; $data[$k]['is_group'] = 0; } } return success('', $data,$list->total(),$list->currentPage()); } // 添加好友 public function add() { $param = $this->request->param(); $user_id=$param['user_id'] ?? 0; if(!$user_id){ return warning(lang('system.notNull')); } if($user_id==$this->uid){ return warning(lang('friend.notAddOwn')); } // 查看是否限制了好友上限 if($this->userInfo['friend_limit']!=0 && $this->userInfo['role']==0){ $myFriend=FriendModel::where(['create_user'=>$this->userInfo['user_id']])->count(); // 好友已达上限 if($myFriend>$this->userInfo['friend_limit'] || $this->userInfo['friend_limit']<0){ return warning(lang('friend.limit')); } } $friend=FriendModel::where(['friend_user_id'=>$user_id,'create_user'=>$this->uid])->order('create_time', 'desc')->find(); if($friend){ if($friend->status==1){ return warning(lang('friend.already')); }elseif($friend->status==2){ return warning(lang('friend.repeatApply')); } } $status=2; $otherFriend=FriendModel::where(['friend_user_id'=>$this->uid,'create_user'=>$user_id])->order('create_time', 'desc')->find(); if($otherFriend){ if($otherFriend->status>0){ $status=1; // 双方都发起直接同意 $otherFriend->status = 1; $otherFriend->save(); $content=lang('friend.newChat'); $userM=new User; // 将对方的信息发送给我,把我的信息发送对方 $user=$userM->setContact($user_id,0,'event',$content); if($user){ wsSendMsg($this->uid,'appendContact',$user); } $myInfo=$userM->setContact($this->uid,0,'event',$content); if($myInfo){ wsSendMsg($user_id,'appendContact',$myInfo); } } } $model = new FriendModel(); $data=[ 'friend_user_id'=>$user_id, 'status'=>$status, 'create_user'=>$this->uid, 'remark'=>$param['remark'], 'is_invite'=>1 // 是否为发起方 ]; $model->save($data); $msg=[ 'fromUser'=>[ 'id'=>'system', 'displayName'=>lang('friend.new'), 'avatar'=>'', ], 'toContactId'=>'system', 'id'=>uniqid(), 'is_group'=>2, 'content'=>lang('friend.apply'), 'status'=>'succeed', 'sendTime'=>time()*1000, 'type'=>'event', 'fileSize'=>0, 'fileName'=>'', ]; // 发送好友申请 wsSendMsg($user_id,'simple',$msg); return success(lang('system.addOk')); } // 接受或者拒绝好友申请 public function update() { $param = $this->request->param(); $friend=FriendModel::find($param['friend_id']); if(!$friend){ return warning(lang('friend.notApply')); } $map=[ 'friend_id'=>$param['friend_id'] ]; FriendModel::where($map)->update(['status'=>$param['status']]); // 如果是接收,就添加到好友列表 if($param['status']){ $data=[ 'friend_user_id'=>$friend->create_user, 'create_user'=>$this->uid, ]; $newFriend=FriendModel::where($data)->find(); if($newFriend){ FriendModel::where($data)->update(['status'=>1]); return success(lang('friend.already')); }else{ $data['status']=1; FriendModel::create($data); } $content=lang('friend.newChat'); $userM=new User; // 将对方的信息发送给我,把我的信息发送对方 $user=$userM->setContact($friend->create_user,0,'event',$content); if($user){ wsSendMsg($this->uid,'appendContact',$user); } $myInfo=$userM->setContact($this->uid,0,'event',$content); if($myInfo){ wsSendMsg($friend->create_user,'appendContact',$myInfo); } } return success(lang('system.success')); } // 删除好友 public function del() { $param = $this->request->param(); $map=['friend_user_id'=>$param['id'],'create_user'=>$this->uid]; $friend=FriendModel::where($map)->find(); if(!$friend){ return warning(lang('friend.not')); } // 需要删除双方的好友关系 FriendModel::where($map)->delete(); FriendModel::where(['friend_user_id'=>$this->uid,'create_user'=>$param['id']])->delete(); // 删除自己和好友消息 $chat_identify=chat_identify($this->uid,$param['id']); $messageIds = Message::where(['chat_identify' => $chat_identify])->column('id'); if ($messageIds) { $res = Message::where([['id', 'in', $messageIds]])->select(); foreach ($res as $resMsg) { $resMsg->delete(); } } // 性质和删除群聊一样 wsSendMsg($param['id'],'removeGroup',['group_id'=>$this->uid]); return success(lang('system.delOk')); } // 设置好友备注 public function setNickname() { $nickname = $this->request->param('nickname',''); $friend_id = $this->request->param('friend_id',''); if(!$friend_id){ return warning(lang('system.notNull')); } $query = FriendModel::where(['friend_id' => $friend_id,'create_user' => $this->uid])->find(); if (!$query) { return warning(lang('system.fail')); } FriendModel::update(['nickname'=>$nickname],['friend_id'=>$friend_id]); return success(lang('system.editOk')); } // 获取最新的一条和申请的总数 public function getApplyMsg(){ $model = new FriendModel(); $map[]=['friend_user_id','=',$this->uid]; $map[]=['status','=',2]; $count=$model->where($map)->count(); return success('', $count); } // 加入/取消黑名单 public function isBlacklist() { $friend_user_id = $this->request->param('friend_user_id'); if (!$friend_user_id || !is_numeric($friend_user_id)) { return error(lang('system.fail')); } $chatSetting = $this->globalConfig['chatInfo']; $kefuUser = []; $status = $chatSetting['autoAddUser']['status'] ?? 0; if ($status == 1) { $kefuUser = $chatSetting['autoAddUser']['user_ids'] ?? []; } $manageUser=User::where([['status','=',1],['role','>',0]])->column('user_id'); $kefu=array_unique(array_merge($kefuUser,$manageUser)); $csUid = User::where('user_id', $friend_user_id)->value('cs_uid'); $manage=false; // 发送者和接受者是客服或者管理员不能拉黑 if(in_array($this->uid,$kefu) || in_array($friend_user_id,$kefu)){ $manage=true; } if ($csUid == $friend_user_id || $manage) { return warning(lang('friend.black_manage')); } $where = [ 'friend_user_id' => $friend_user_id, 'create_user' => $this->uid, 'status' => 1 ]; $friend = FriendModel::where($where)->find(); if (!$friend) { return error(lang('system.fail')); } $friend->is_blacklist = $friend->is_blacklist ? 0 : 1; $friend->update_time = time(); $friend->save(); return success(''); } // 获取好友备注 public function getFriendInfo() { $friend_user_id = $this->request->param('friend_user_id'); if (empty($friend_user_id) || !is_numeric($friend_user_id)) { return error(lang('system.fail')); } $where = [ 'friend_user_id' => $friend_user_id, 'create_user' => $this->uid, 'status' => 1 ]; $friend_user = FriendModel::where($where)->field('is_notice,is_top,is_blacklist,clear_msg_day')->find(); return success('', $friend_user); } // 用户设置聊天清除日期 public function setClearMsgDay() { $to_user = $this->request->param('to_user'); $day = $this->request->param('day', 0); $uid = $this->userInfo['user_id']; if (!is_numeric($day)) return error(lang('system.fail')); if (!is_numeric($to_user)) return error(lang('system.fail')); $config=Config::getSystemInfo(); $userMsgClear=$config['chatInfo']['userMsgClear'] ?? false; if (!$userMsgClear) { return warning(lang('system.fail')); } $dayArr=$config['chatInfo']['userMsgClearDay'] ?? []; $is_exists = false; $title = ''; foreach ($dayArr as $val) { if ($val['value'] == $day) { $is_exists = true; $title = $val['title']; } } if (!$is_exists) { return warning(lang('system.fail')); } $friendWhere = [ 'friend_user_id' => $to_user, 'create_user' => $uid ]; $friend = FriendModel::where($friendWhere)->find(); if (!$friend) { return warning(lang('system.fail')); } $friend->clear_msg_day = $day; $friend->save(); $friendWhere2 = [ 'friend_user_id' => $uid, 'create_user' => $to_user ]; $friend2 = FriendModel::where($friendWhere2)->find(); if (!$friend2) { return warning(lang('system.fail')); } $friend2->clear_msg_day = $day; $friend2->save(); $is_close = $day ? 0 : 1; $data = Message::setAutoDelMessageNotice($uid, $to_user, $title, 0, $is_close); $data['isMobile'] = $this->request->isMobile() ? 1 : 0; $otherData = $data; $otherData['content'] = lang('im.other'). $data['content']; wsSendMsg($to_user, 'undoMessage', $otherData); $data['content'] = lang('im.you'). $data['content']; // 多端同步 wsSendMsg($uid, 'undoMessage', $data); return success('', $data); } // 单聊一键清除消息 public function removeAllMessage() { $to_user = $this->request->param('to_user'); if (!is_numeric($to_user)) return error(lang('system.fail')); $uid = $this->userInfo['user_id']; $friendWhere = [ 'friend_user_id' => $to_user, 'create_user' => $uid ]; $friend = FriendModel::where($friendWhere)->find(); if (!$friend) { return warning(lang('system.fail')); } $chat_identify = chat_identify($uid, $to_user); $whereMsg = [ ['is_group', '=', 0], ['chat_identify', '=', $chat_identify] ]; $msgRes = Message::where($whereMsg)->select(); foreach ($msgRes as $msg) { $msg->force()->delete(); File::syncDelFile($msg->file_id); } wsSendMsg([$uid, $to_user], 'delMessageAll', ['form_user' => $uid, 'to_user' => $to_user]); return success(''); } /** * 获取移动端邀请链接 * @return \think\response\Json */ public function getMoveInviteLink(){ $uid=$this->userInfo['user_id']; // 邀请码仅两天有效 $code=\utils\Str::random(8); Cache::set($code,$uid,172800); $move_host = env('app.move_host',''); $url = $move_host.'/#/pages/login/register?inviteCode='.$code; return success('',$url); } }