request->isPost()){ return $this->fail('请求方式错误'); } //获取请求参数 $params = $this->request->post(['client_id','user_id']); if(empty($params['client_id']) || empty($params['user_id'])){ return $this->fail('参数错误'); } Gateway::bindUid($params['client_id'], $params['user_id']); return $this->success('绑定成功'); } public function sendMessage(): Json { //验证请求方式 if(!$this->request->isPost()){ return $this->fail('请求方式错误'); } //获取请求参数 $params = $this->request->post(['msg_id','from_user_id','from_user_name','from_user_avatar','to_user_id','to_user_name','to_user_avatar','content','type']); if(empty($params['msg_id']) || empty($params['from_user_id']) || empty($params['from_user_name']) || empty($params['from_user_avatar']) || empty($params['to_user_id']) || empty($params['to_user_name']) || empty($params['to_user_avatar']) || empty($params['content']) || empty($params['type'])){ return $this->fail('参数错误'); } if(!in_array($params['type'],['text','image','video'])){ return $this->fail('消息类型错误'); } //判断消息是否存在 $msgData = UserImMessage::where('msg_id',$params['msg_id'])->findOrEmpty(); if(!$msgData->isEmpty()){ return $this->fail('该消息已发送'); } //保存消息 try { $model = new UserImMessage(); $result = $model->save($params); if($result){ Gateway::sendToUid($params['to_user_id'], json_encode($params)); return $this->success('发送成功'); }else{ return $this->fail('发送失败'); } }catch (\Exception $e) { return $this->fail($e->getMessage()); } } public function upload(): Json { //验证请求方式 if(!$this->request->isPost()){ return $this->fail('请求方式错误'); } //获取请求参数 $params = $this->request->post(['msg_id','from_user_id','from_user_name','from_user_avatar','to_user_id','to_user_name','to_user_avatar','type']); //获取参数 $file = $this->request->file('file'); if(empty($file) || empty($params['msg_id']) || empty($params['from_user_id']) || empty($params['from_user_name']) || empty($params['from_user_avatar']) || empty($params['to_user_id']) || empty($params['to_user_name']) || empty($params['to_user_avatar']) || empty($params['type'])){ return $this->fail('参数错误'); } if(!in_array($params['type'],['image','video'])){ return $this->fail('消息类型错误'); } //判断消息是否存在 $msgData = UserImMessage::where('msg_id',$params['msg_id'])->findOrEmpty(); if(!$msgData->isEmpty()){ return $this->fail('该消息已发送'); } $filePath =$file->getRealPath(); $fileType = $file->extension(); $fileSize = $file->getSize(); $ali_oss = new alioss(); $result = ''; switch ($params['type']) { case 'image': $result = $ali_oss -> uploadImg($filePath,$fileType,$fileSize); break; case 'video': $result = $ali_oss -> uploadVideo($filePath,$fileType,$fileSize); break; } if($result && $result['code'] == 1){ $params['content'] = $result['data']; if($params['type'] == 'video'){ $params['extends']= json_encode(['poster_img'=>$result['data'].'?x-oss-process=video/snapshot,t_1000,m_fast,w_800,f_png']); } //保存消息 try { $model = new UserImMessage(); $result = $model->save($params); if($result){ Gateway::sendToUid($params['to_user_id'], json_encode($params)); return $this->success('发送成功'); }else{ return $this->fail('发送失败'); } }catch (\Exception $e) { return $this->fail($e->getMessage()); } }else{ return $this->fail($result['msg']); } } }