123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/7/7
- * Time: 下午11:29
- */
- namespace room;
- use Exception;
- use Log;
- use member_info;
- class chatwo
- {
- protected $mUserInfos;
- private $mod_room;
- private $mMapChat;
- public function __construct()
- {
- $this->mUserInfos = [];
- $this->mMapChat = [];
- $this->mod_room = Model('room');
- }
- public function messageOp($input)
- {
- $from = intval($input['from']);
- $to = intval($input['to']);
- $seq = $input['seq'];
- if($from <= 0 || $to <= 0) return false;
- $finfo = $this->find($from);
- $tinfo = $this->find($to);
- if($tinfo == false || $tinfo == false) return false;
- $type = proto_type::to_msgtype($input['type']);
- $content = $this->validate_content($input['content']);
- if($type == false || $content == false) return false;
- $msgid = $this->record_message($from,$to,$type,$content);
- $msg = ['from' => $finfo,'content' => $content, 'type' => $input['type'],'send_time' => time(),'msgid' => $msgid,'seq' => $seq];
- return $this->format_msg($from,$to,"message",$msg);
- }
- private function chatid($from,$to)
- {
- if($from > $to) {
- $hash = "{$to}_{$from}";
- } else {
- $hash = "{$from}_{$to}";
- }
- if(array_key_exists($hash,$this->mMapChat)) {
- return $this->mMapChat[$hash];
- }
- else
- {
- $chat_id = $this->mod_room->findChatid($from,$to);
- if($chat_id === false) {
- $chat_id = $this->mod_room->addChatwo($from,$to);
- }
- if($chat_id != false) {
- $this->mMapChat[$hash] = $chat_id;
- }
- return $chat_id;
- }
- }
- private function format_msg($from,$to,$op,$content)
- {
- $msg = [];
- $msg['msgtype'] = proto_type::msgtype_message;
- $msg['act'] = "room";
- $msg['op'] = "relay";
- $msg['relay_type'] = "users";
- if($from == $to) {
- $msg['receivers'] = [$to];
- } else {
- $msg['receivers'] = [$from,$to];
- }
- $msg['room'] = 0;
- $msg['receiver_type'] = proto_type::sroom_chatwo;
- $msg['body']['act'] = proto_type::act_chatwo;
- $msg['body']['op'] = $op;
- $msg['body']['content'] = $content;
- $msg['body']['msgtype'] = proto_type::msgtype_message;
- return $msg;
- }
- protected function find($userid)
- {
- try
- {
- if(array_key_exists($userid,$this->mUserInfos)) {
- return $this->mUserInfos[$userid];
- }
- else {
- $item = Model('member')->getMemberInfoByID($userid);
- $uinfo = new member_info($item);
- $result = ['nickname' => $uinfo->nickname(),'avatar' => $uinfo->avatar(),'userid' => intval($userid)];
- $this->mUserInfos[$userid] = $result;
- return $result;
- }
- }
- catch (Exception $ex)
- {
- Log::record($ex->getMessage(),Log::ERR);
- return false;
- }
- }
- protected function validate_content($content)
- {
- return $content;
- }
- protected function record_message($from,$to,$type,$content)
- {
- $chat_id = $this->chatid($from,$to);
- if($chat_id != false) {
- return $this->mod_room->addRoomMsg(['room_id' => $chat_id,'member_id' => $from, 'type' => $type,'msg' => $content,'add_time' => time(),'msg_type' => 1]);
- }
- else {
- return false;
- }
- }
- }
|