ImMessageSendTypeEnum::USER_SEND],//区分是user_id发给service_id还是service_id发给user_id ['to_id','default','value'=>0],//发给导医和客服可能有会话id,没有接收人 ]; } public function attributeLabels() { return [ 'ims_id' => '会话id', 'from_id' => '发送人', 'to_id' => '接收人', 'content' => '消息内容', ]; } /** * @param $canOffline bool 是否可发送离线消息 如果在线会发送在线消息 * @return array * @throws Exception */ public function sendMessage($canOffline = false) { if (!$this->validate()) { throw new Exception($this->getErrorMsg()); } $imMessageSession = ImMessageSession::find()->where([ 'id' => $this->ims_id, 'is_delete' => 0, ])->one(); switch($this->type){ case 0: //user_id发 $user_id = $this->from_id; $service_id = $this->to_id; //检查用户信息 switch($imMessageSession->type) { case ImSessionTypeEnum::USER_USER: $to = User::findOne($service_id); if(!$to){ throw new Exception('该用户不存在'); } $to_role = UserRoleEnum::USER; break; case ImSessionTypeEnum::USER_DOC: $to = ServiceUser::findOne($service_id); if(!$to || $to->is_delete || $to->status!=2){ throw new Exception('该'.UserRoleEnum::NAME[$to->role].'不存在'); } $to_role = $to->role; break; case ImSessionTypeEnum::USER_LEAD: if($service_id){ $to = ServiceUser::findOne($service_id); if(!$to || $to->is_delete || $to->status!=2){ throw new Exception('该'.UserRoleEnum::NAME[$to->role].'不存在'); } $to_role = $to->role; }else{ //说明新创建的会话,没人接入,群发 $to_role = UserRoleEnum::LEADER; } break; case ImSessionTypeEnum::DOC_SERV: case ImSessionTypeEnum::DRUG_SERV: case ImSessionTypeEnum::LEAD_SERV: if($service_id){ $to = ServiceUser::findOne($service_id); if(!$to || $to->is_delete || $to->status!=2){ throw new Exception('该'.UserRoleEnum::NAME[$to->role].'不存在'); } $to_role = $to->role; }else{ //说明新创建的会话,没人接入,群发 $to_role = UserRoleEnum::SERVICE; } break; } break; case 1: //service_id发 $user_id = $this->to_id; $service_id = $this->from_id; //检查用户信息 switch($imMessageSession->type) { case ImSessionTypeEnum::USER_USER: case ImSessionTypeEnum::USER_DOC: case ImSessionTypeEnum::USER_LEAD: $to = User::findOne($user_id); if(!$to){ throw new Exception('该用户不存在'); } $to_role = UserRoleEnum::USER; break; case ImSessionTypeEnum::DOC_SERV: case ImSessionTypeEnum::DRUG_SERV: case ImSessionTypeEnum::LEAD_SERV: $to = ServiceUser::findOne($user_id); if(!$to || $to->is_delete || $to->status!=2){ throw new Exception('该'.UserRoleEnum::NAME[$to->role].'不存在'); } $to_role = $to->role; break; } break; default: throw new Exception('不支持的消息'); break; } if(!$imMessageSession || $imMessageSession->user_id!=$user_id || $imMessageSession->service_id!=$service_id){ throw new Exception('会话不存在'); } // if($imMessageSession->status == ImSessionStatusEnum::END){ // //会话已结束 // throw new Exception('会话已结束,无法发送消息'); // } // $ims_id = $imMessageSession->id; // // $t = \Yii::$app->db->beginTransaction(); // try { // ImMessage::sendMessage($ims_id,$user_id,$service_id,$this->content,$this->type); // // \Yii::$app->queue->delay(0)->push(new MessageSendJob([ // 'ims_id' => $ims_id, // 'to_id' => $this->to_id, // 'to_role' => $to_role, // 'content' => $this->content, // ])); // // $t->commit(); // }catch (\Exception $exception){ // $t->rollBack(); // throw new Exception('发送失败:'.$exception->getMessage()); // } // return []; $isEnd = $imMessageSession->status == ImSessionStatusEnum::END; if(!$canOffline && $isEnd){ //会话已结束 throw new Exception('会话已结束,无法发送消息'); } $ims_id = $imMessageSession->id; $t = \Yii::$app->db->beginTransaction(); try { ImMessage::sendMessage($ims_id,$user_id,$service_id,$this->content,$this->type); if (!$canOffline || !$isEnd) { \Yii::$app->queue->delay(0)->push(new MessageSendJob([ 'ims_id' => $ims_id, 'to_id' => $this->to_id, 'to_role' => $to_role, 'content' => $this->content, ])); } $t->commit(); }catch (\Exception $exception){ $t->rollBack(); throw new Exception('发送失败:'.$exception->getMessage()); } return []; } //接入的消息发送 public function sessionIn($ims_id,$to_id = 0,$to_role,$message) { \Yii::$app->queue->delay(0)->push(new MessageSendJob([ 'ims_id' => $ims_id, 'to_id' => $to_id, 'to_role' => $to_role, 'content' => $message, ])); } }