123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/12/14
- * Time: 下午5:47
- */
- namespace room;
- use member_info;
- use Log;
- abstract class base_room extends base_info
- {
- protected $mRoomid;
- protected $mRoomkeys; //每个人都有一个唯一的roomkey
- protected $mRoomType;
- protected $mod_room;
- protected $mAccReq;
- protected $mCurRespMsgs;
- public function __construct($cinfos, $roomkeys = [])
- {
- parent::__construct($cinfos);
- $this->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();
- $this->mAccReq = false;
- $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;
- }
- $this->mAccReq = true;
- 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;
- $this->mAccReq = true;
- 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 = $input['type'];
- $content = $input['content'];
- if(empty($content)) return false;
- $this->add_broad('message',['from' => $userinfo,'type' => $type,'content' => $content]);
- return true;
- }
- 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 acc_req() {
- return $this->mAccReq;
- }
- 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;
- }
- }
|