123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/12/14
- * Time: 下午5:47
- */
- namespace room;
- use member_info;
- use Log;
- use Exception;
- abstract class base_room
- {
- protected $mRoomid;
- protected $mParticipants;
- protected $mRoomType;
- protected $mod_room;
- protected $mRelayMsgs;
- protected $mRoomInfo;
- public function __construct($cinfos, $participants = [])
- {
- $this->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 = "<font color='#3c78d8'>{$user['nickname']}</font>将";
- $contents = [];
- foreach ($kickinfos as $user) {
- $contents[] = "<font color='#3c78d8'>{$user['nickname']}</font>";
- }
- $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 = "<font color='#3c78d8'>{$inviter['nickname']}</font>邀请";
- $contents = [];
- foreach ($invitees as $user) {
- $contents[] = "<font color='#3c78d8'>{$user['nickname']}</font>";
- }
- $str .= implode('、',$contents);
- $str .= "加入群聊。也可以把群分享到微信邀请好友哦~";
- return $str;
- }
- private function donate_msg($user,$steps,$amount)
- {
- try
- {
- $minfo = $this->userinfos($user);
- $str = "{$minfo['nickname']}贡献了{$amount}元助力共享基金,兑换步数{$steps}步。<font color='#4A90E2'>一起捐赠步数吧!</font>";
- return $str;
- }
- catch (Exception $ex) {
- return false;
- }
- }
- private function spend_msg($user,$amount)
- {
- try
- {
- $minfo = $this->userinfos($user);
- $str = "{$minfo['nickname']}使用了{$amount}元共享基金,成功购物。<font color='#4A90E2'>一起捐赠步数吧!</font>";
- 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 = "<font color='#3c78d8'>{$ret['nickname']}</font>退出群聊";
- $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;
- }
- }
- }
|