mAccReq = false; $this->mRoomid = $this->room_id(); $this->mCurRespMsgs['return'] = []; $this->mCurRespMsgs['broadcast'] = []; $this->mRoomkeys = $roomkeys; $this->mod_room = Model('room'); } public function inviteOp($input) { $this->clear(); $userid = intval($input['user']); if($userid > 0) { $room_key = $this->invite($userid); $ret = ['room_key' => $room_key,'room' => $this->mRoomid]; return true; } else { return false; } } public function invite($userid) { $item = Model('member')->getMemberInfo(['member_id' => $userid]); if(empty($item)) return false; $uinfo = new member_info($item); $participant = $this->room_key($uinfo->unionid()); $ret = $this->find($participant); if($ret == false) { $ret = $this->mod_room->invite($this->room_id(),$userid,$participant); if($ret == false) { Log::record(__METHOD__ . " invite error",Log::ERR); return false; } else { $this->mRoomkeys[$participant] = ['active' => false, 'nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => intval($userid)]; } } return $participant; } public function reject($roomkey) { $ret = $this->find($roomkey); if($ret != false) { unset($this->mRoomkeys[$roomkey]); } return true; } protected function room_info($roomkey) { $result = []; $result['room'] = $this->mRoomid; $result['users'] = []; foreach ($this->mRoomkeys as $key => $item) { $result['users'][] = $item; if($key == $roomkey) { $result['me'] = $item['userid']; } } return $result; } public function joinOp($input) { $this->clear(); $room_key = $input['room_key']; if(empty($room_key)) { return false; } if($this->join($room_key)) { $this->add_return([$room_key],'ret_join',$this->room_info($room_key)); $this->add_broad('join',$this->mRoomkeys[$room_key]); return true; } else { return false; } } public function leaveOp($input) { $this->clear(); $room_key = $input['room_key']; $userinfo = $this->find($room_key); if($userinfo == false) return false; if($this->leave($room_key)) { $this->add_return([$room_key],'ret_leave',$userinfo); $this->add_broad('leave',$userinfo); return true; } else { return false; } } public function messageOp($input) { $this->clear(); $room_key = $input['room_key']; $userinfo = $this->find($room_key); if($userinfo == false) return false; $type = $this->validate_type($input['type']); $content = $this->validate_content($input['content']); if($type == false || $content == false) return false; $this->record_message($userinfo['userid'],$type,$content); $this->add_broad('message',['from' => $userinfo,'type' => $type,'content' => $content]); return true; } protected function validate_type($type,$content) { $stype = strtolower($type); if($stype == proto_type::msg_stype_text) { return proto_type::msg_type_text; } elseif($stype == proto_type::msg_stype_pic) { return proto_type::msg_type_text; } elseif($stype == proto_type::msg_stype_bargain) { return proto_type::msg_type_bargain; } else { return false; } } protected function validate_content($content) { return $content; } protected function record_message($userid,$type,$content) { $mod_room = Model('room'); $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content,'add_time' => time()]); } public function return_msgs() { return $this->mCurRespMsgs['return']; } public function broadcast_msgs() { return $this->mCurRespMsgs['broadcast']; } protected function clear() { $this->mCurRespMsgs['return'] = []; $this->mCurRespMsgs['broadcast'] = []; } protected function add_return(array $roomkeys,$op,$content) { $msg = []; $msg['receivers'] = ['type' => 'roomkey', 'users' => $roomkeys]; $msg['room'] = $this->mRoomid; $msg['act'] = "room"; $msg['op'] = $op; $msg['body']['act'] = 'room'; $msg['body']['op'] = $op; $msg['body']['room'] = $this->mRoomid; $msg['body']['content'] = $content; $this->mCurRespMsgs['return'][] = $msg; } protected function add_broad($op,$content) { $msg = []; $msg['receivers'] = ['type' => 'broadcast']; $msg['room'] = $this->mRoomid; $msg['act'] = "room"; $msg['op'] = $op; $msg['body']['act'] = 'room'; $msg['body']['op'] = $op; $msg['body']['room'] = $this->mRoomid; $msg['body']['content'] = $content; $this->mCurRespMsgs['broadcast'][] = $msg; } public function join($participant) { $ret = $this->find($participant); if($ret != false) { $this->mRoomkeys[$participant]['active'] = true; return true; } else { return false; } } public function leave($participant) { $ret = $this->find($participant); if($ret != false) { $this->mRoomkeys[$participant]['active'] = false; return true; } else { return false; } } protected function find($participant) { if(empty($participant)) return false; if(array_key_exists($participant,$this->mRoomkeys)) { return $this->mRoomkeys[$participant]; } else { return false; } } protected function room_key($unionid) { return md5("{$this->mRoomType}_{$this->mRoomid}_{$unionid}"); } public function room_type() { return $this->mRoomType; } public function cur_msgs() { return $this->mCurRespMsgs; } }