mine = $data['mine']; $this->user = $data['user']; } /** * Execute the job. * * @return void */ public function handle() { sleep(1); $this->sendFriendQuestion($this->mine, $this->user); } //用户在发送打招呼后收到的第一条信息 public function sendFriendQuestion($mine, $user) { try { //第一条消息 $count = ChatMessage::where('user_id', $mine->id)->where('other_user_id', $user->id)->count(); if ($count == 1) { //他的破冰问题 $content = $user->friendQuestion(); if (empty($content)) return true; //网易聊天消息发送 $im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $body = ['msg'=>$content]; $ext = ['type'=>'friend_question', 'is_used'=>$mine->friendQuestion()?1:0]; //friend_question破冰问题,is_used 自己是否设置破冰问题 $result = $im_service->sendMsg($user->id,$ope=0,$mine->id,$type=0,$body,$option=array("push"=>false,"roam"=>true,"history"=>true,"sendersync"=>true, "route"=>false),$pushcontent='',$ext,1001); if ($result['code'] != 200) { throw new \Exception("网易发送单聊信息失败-".$result['desc'], 1); } //创建聊天记录 ChatMessage::create([ 'user_id'=>$user->id, 'other_user_id'=>$mine->id, 'type'=>'CHAT', 'content'=>$content, 'status'=>1, 'system_type'=>'question', ]); } return true; } catch (\Exception $e) { // $this->getError($e); return false; } } }