mRoomInfo = new room_info($cinfos); $this->mRoomid = $this->room_id(); $this->mRelayMsgs['reply'] = []; //转发回复包 $this->mRelayMsgs['broadcast'] = []; //广播包 $this->mParticipants = $participants; $this->mod_room = Model('room'); } public function room_id() { return $this->mRoomInfo->room_id(); } public function creator() { return $this->mRoomInfo->creator(); } public function room_info() { return $this->mRoomInfo->format(); } public function room_name() { return $this->mRoomInfo->name(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public function invite($inviter,$invitees,&$newusers) { $this->clear(); if($this->verify_inviter($inviter)) { $users = $this->add($inviter,$invitees,$newusers); if($users === false) { $ret = ['room' => $this->mRoomid,'invitees' => [],'newusers' => []]; } else { $ret = ['room' => $this->mRoomid,'invitees' => $users,'newusers' => $newusers]; if(!empty($newusers)) { $content = $this->format_invite($inviter,$newusers); if($content !== false) { $type = proto_type::to_msgtype(proto_type::msg_stype_plain); $msgid = $this->record_message(0,$type,$content); $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $content,'send_time' => time()]); } } } return $ret; } else { return false; } } public function usercount() { return count($this->mParticipants); } //返回被删除的所有userid 数组 public function kickout($manager,$users) { $this->clear(); $fAdmin = $this->mRoomInfo->isAdmin($manager); if($fAdmin == false) return false; $uids = []; $kicks = []; foreach ($users as $user) { $fAdmin = $this->mRoomInfo->isAdmin($user); if($fAdmin || $manager == $user) continue; $userinfo = $this->find($user); if($userinfo == false) continue; $kicks[] = $userinfo; unset($this->mParticipants[$user]); $this->mod_room->kickout($this->room_id(),$user); $uids[] = $user; } if(empty($kicks)) { return false; } else { $content = $this->format_kickout($manager,$kicks); $type = proto_type::to_msgtype(proto_type::msg_stype_plain); $msgid = $this->record_message(0,$type,$content); $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $content,'send_time' => time()]); return $uids; } } private function format_kickout($user, $kickinfos) { $user = $this->userinfos($user); $str = "{$user['nickname']}将"; $contents = []; foreach ($kickinfos as $user) { $contents[] = "{$user['nickname']}"; } $str .= implode('、',$contents); $str .= "移除群聊"; return $str; } private function format_invite($inviter,$invitees) { $invitees = $this->userinfos($invitees); $inviter = $this->userinfos($inviter); if( count($invitees)==1 && $invitees[0]['userid']==$inviter['userid']){ return false; } $str = "{$inviter['nickname']}邀请"; $contents = []; foreach ($invitees as $user) { $contents[] = "{$user['nickname']}"; } $str .= implode('、',$contents); $str .= "加入群聊。也可以把群分享到微信邀请好友哦~"; return $str; } private function donate_msg($user,$steps,$amount) { try { $minfo = $this->userinfos($user); $str = "{$minfo['nickname']}贡献了{$amount}元助力共享基金,兑换步数{$steps}步。一起捐赠步数吧!"; return $str; } catch (Exception $ex) { return false; } } private function spend_msg($user,$amount) { try { $minfo = $this->userinfos($user); $str = "{$minfo['nickname']}使用了{$amount}元共享基金,成功购物。一起捐赠步数吧!"; return $str; } catch (Exception $ex) { return false; } } private function verify_inviter($inviter) { if($this->creator() == $inviter) { return true; } else { $ret = $this->find($inviter); return ($ret === false) ? false : true; } } private function add($inviter,$invitees,&$newusers) { $newusers = []; $invitees = array_unique($invitees); $items = Model('member')->getMemberList(['member_id' => ['in',$invitees]]); if(empty($items)) return false; $users = []; foreach ($items as $item) { $userid = intval($item['member_id']); $users[$userid] = $item; } $result = []; foreach ($invitees as $invitee) { try { if(!array_key_exists($invitee,$users)) continue; $result[] = $invitee; $ret = $this->find($invitee); if($ret !== false) continue; $ret = $this->mod_room->invite($this->room_id(),$invitee,$inviter); if($ret == false) { continue; } else { $user = $users[$invitee]; $uinfo = new member_info($user); $this->mParticipants[$invitee] = ['nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => intval($invitee)]; $newusers[] = $invitee; } } catch (Exception $ex) { Log::record($ex->getMessage(),Log::ERR); } } return empty($result) ? false : $result; } public function change() { $this->clear(); $mod_room = Model('room'); $item = $mod_room->getRoom($this->room_id()); if(empty($item)) return false; $this->mRoomInfo = new room_info($item); return true; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public function leave($userid) { $this->clear(); $ret = $this->find($userid); if($ret != false) { unset($this->mParticipants[$userid]); $this->mod_room->leave($this->room_id(),$userid); $content = "{$ret['nickname']}退出群聊"; $type = proto_type::to_msgtype(proto_type::msg_stype_plain); $msgid = $this->record_message(0,$type,$content); $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $content,'send_time' => time()]); return true; } else { return false; } } public function notice($stype, $content,$user) { $this->clear(); try { $user = intval($user); if($user > 0) { $uinfo = new member_info($user); $from = ['nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => $user]; } else { $from = false; } } catch (Exception $ex) { $from = false; } if($stype == proto_type::msg_stype_donate) { $userid = $content['user']; $steps = $content['steps']; $amount = $content['amount']; $msg = $this->donate_msg($userid,$steps,$amount); $fplain = true; } elseif($stype == proto_type::msg_stype_spend) { $userid = $content['user']; $amount = $content['amount']; $msg = $this->spend_msg($userid,$amount); $fplain = true; } elseif($stype == proto_type::msg_stype_text) { $msg = $content; $fplain = false; } else { return false; } if($msg == false) return false; $type = proto_type::to_msgtype($stype); if($from != false) { $msgid = $this->record_message($user,$type,$msg,json_encode($content,JSON_UNESCAPED_UNICODE)); if($fplain) { $this->relay_broadcast('message',['msgid' => $msgid,'from' => $from,'type' => proto_type::msg_stype_plain,'content' => $msg,'send_time' => time(),'seq' => 0]); } else { $this->relay_broadcast('message',['msgid' => $msgid,'from' => $from,'type' => $stype,'content' => $msg,'send_time' => time(),'seq' => 0]); } } else { $msgid = $this->record_message(0,$type,$msg,json_encode($content,JSON_UNESCAPED_UNICODE)); if($fplain) { $this->relay_broadcast('message',['msgid' => $msgid,'type' => proto_type::msg_stype_plain,'content' => $msg,'send_time' => time()]); } else { $this->relay_broadcast('message',['msgid' => $msgid,'type' => $stype,'content' => $msg,'send_time' => time()]); } } return true; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public function messageOp($input) { $this->clear(); $user = $input['user']; $userinfo = $this->find($user); if($userinfo == false) { return false; } $type = proto_type::to_msgtype($input['type']); $content = $this->validate_content($input['content']); $seq = $input['seq']; if(empty($seq)) $seq = ""; if($type == false || $content == false) { return false; } $msgid = $this->record_message($userinfo['userid'],$type,$content); if($msgid > 0) { $this->relay_broadcast('message',['msgid' => $msgid, 'from' => $userinfo,'type' => $input['type'],'content' => $content,'send_time' => time(),'seq' => $seq]); return ['act' => 'room','op' => 'message','room' => $this->room_id()]; } else { return false; } } protected function validate_content($content) { return $content; } protected function record_message($userid,$type,$content,$orgmsg = '') { $mod_room = Model('room'); if($type == proto_type::msg_type_goods) { $data = json_decode($content,true); $goods_id = $data['goods_id']; return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content, 'orgmsg' => $goods_id, 'add_time' => time(),'msg_type' => 0]); } else { return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content, 'orgmsg' => $orgmsg, 'add_time' => time(),'msg_type' => 0]); } } public function relay_reply_msgs() { return $this->mRelayMsgs['reply']; } public function relay_broadcast_msgs() { return $this->mRelayMsgs['broadcast']; } protected function clear() { $this->mRelayMsgs['reply'] = []; $this->mRelayMsgs['broadcast'] = []; } protected function relay_reply($user, $op, $content) { $msg = []; $msg['act'] = "room"; $msg['op'] = "relay"; $msg['msgtype'] = "message"; $msg['relay_type'] = "roomusers"; $msg['receivers'] = [$user]; $msg['room'] = $this->mRoomid; $msg['receiver_type'] = $this->room_type(); $msg['body']['act'] = 'room'; $msg['body']['op'] = $op; $msg['body']['msgtype'] = "message"; $msg['body']['room'] = $this->mRoomid; $msg['body']['content'] = $content; $this->mRelayMsgs['reply'][] = $msg; } protected function relay_users(array $users, $op, $content) { $msg = []; $msg['act'] = "room"; $msg['op'] = "relay"; $msg['msgtype'] = "message"; $msg['relay_type'] = "roomusers"; $msg['receivers'] = $users; $msg['room'] = $this->mRoomid; $msg['receiver_type'] = $this->room_type(); $msg['body']['act'] = 'room'; $msg['body']['op'] = $op; $msg['body']['msgtype'] = "message"; $msg['body']['room'] = $this->mRoomid; $msg['body']['content'] = $content; $this->mRelayMsgs['broadcast'][] = $msg; } protected function relay_broadcast($op, $content) { $msg = []; $msg['act'] = "room"; $msg['op'] = "relay"; $msg['msgtype'] = "message"; $msg['relay_type'] = "room"; $msg['room'] = $this->mRoomid; $msg['receivers'] = []; $msg['receiver_type'] = $this->room_type(); $msg['body']['act'] = 'room'; $msg['body']['op'] = $op; $msg['body']['msgtype'] = "message"; $msg['body']['room'] = $this->mRoomid; $msg['body']['content'] = $content; $this->mRelayMsgs['broadcast'][] = $msg; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// protected function find($userid) { if($userid <= 0) return false; if(array_key_exists($userid,$this->mParticipants)) { return $this->mParticipants[$userid]; } else { return false; } } public function room_type() { return $this->mRoomType; } public function userinfos($users) { if(is_array($users)) { $result = []; foreach ($users as $user) { $info = $this->find($user); if($info == false) continue; $result[] = $info; } return $result; } else { return $this->find($users); } } protected function times($userid) { $userinfo = $this->find($userid); if($userinfo == false) { return 0; } else { return intval($this->mParticipants[$userid]['times']); } } protected function increase_times($userid) { $userinfo = $this->find($userid); if($userinfo != false) { $times = intval($this->mParticipants[$userid]['times']) + 1; $this->mParticipants[$userid]['times'] = $times; } } }