瀏覽代碼

add to local

stanley-king 6 年之前
父節點
當前提交
d57ed1d425

+ 60 - 62
data/model/room.model.php

@@ -14,7 +14,7 @@ class roomModel extends Model
     public function __construct() {
         parent::__construct();
     }
-
+    ///群聊接口/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     public function create_room($type,$name,$creator,$owner,$owner_name,$max_user = 0)
     {
         $params = ['type' => $type,'room_name' => $name,'room_creator' => $creator,'room_owner' => $owner,'owner_name' => $owner_name,'max_user' => $max_user,'add_time' => time()];
@@ -54,46 +54,82 @@ class roomModel extends Model
         return $this->table('room_msg')->field('*')->where(['room_id' => $room_id, 'type' => $msg_type])->order('add_time desc')->select();
     }
 
-    public function getRoomMsgList($condition, $pagesize = '', $field = '*', $order = 'msg_id asc',$master = false)
+    public function getRoomsgList($condition, $pagesize = '', $field = '*', $order = 'msg_id desc',$master = false)
     {
         return $this->table('room_msg')->field($field)->where($condition)->order($order)->limit($pagesize)->master($master)->select();
     }
 
-    public function getChatwoMsgList($condition, $pagesize = '', $field = '*', $order = 'msg_id asc',$master = false)
+    public function getPartRooms($cond,$field = '*')
     {
-        return $this->table('room_chatwo_msg')->field($field)->where($condition)->order($order)->limit($pagesize)->master($master)->select();
+        return  $this->table('room_participant')->field($field)->where($cond)->limit(false)->select();
+    }
 
+    public function getRooms($cond,$field = '*')
+    {
+        return $this->table('room')->field($field)->where($cond)->select();
     }
 
-    public function getJoinedRooms($cond)
+    public function getLastRoomMsgs($roomids)
+    {
+        $items = $this->table('room_msg')->field("MAX(msg_id) as msg_id,room_id")->where(['room_id'=>['in',$roomids]])->group('room_id')->select();
+        if(empty($items)) return [];
+
+        $msg_ids = [];
+        foreach ($items as $item) {
+            $msg_ids[] = intval($item['msg_id']);
+        }
+
+        return $this->table('room_msg')->field("*")->where(['msg_id' => ['in',$msg_ids]])->limit(count($msg_ids))->select();
+    }
+
+    public function addRoomMsg($datas)
+    {
+        return $this->table('room_msg')->insert($datas);
+    }
+    public function getRoomExtend($userid,$lock=false)
     {
-        return  $this->table('room_participant')->field('*')->where($cond)->select();
+        $item = $this->table('room_extend')->field('*')->where(['userid' => $userid])->lock($lock)->find();
+        return $item;
     }
 
-    public function getRooms($cond)
+    public function addExtend($userid,$datas)
     {
-        return $this->table('room')->field('room_id')->where($cond)->select();
+        $datas['userid'] = $userid;
+        return $this->table('room_extend')->insert($datas);
     }
 
+    public function editExtend($userid,$datas)
+    {
+        return $this->table('room_extend')->where(['userid' => $userid])->update($datas);
+    }
 
-    public function getLastRoomMsgs($roomids)
+    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+    public function findChatid($left,$right,$lock=false)
     {
-        if(!empty($roomids))
-        {
-            $last_msgs = $this->table('room_msg')->field("MAX(msg_id) as msg_id,room_id")->where(['room_id'=>['in',$roomids]])->group('room_id')->select();
-            if(!empty($last_msgs))
-            {
-                $msg_ids = [];
-                foreach ($last_msgs as $msg){$msg_ids[] = $msg['msg_id'];}
-                return $this->table('room_msg')->field("*")->where(['msg_id'=>['in',$msg_ids]])->limit(count($msg_ids))->select();
-            }else{
-                return [];
-            }
-        }else{
-            return [];
+        if($left > $right) {
+            $tmp = $left;
+            $left = $right;
+            $right = $tmp;
+        }
+
+        $item = $this->table('room_chatwo')->field('*')->where(['user_left' => $left,'user_right' => $right])->lock($lock)->find();
+        if(empty($item)) {
+            return false;
+        } else {
+            return intval($item['chat_id']);
         }
     }
 
+    public function addChatwo($left,$right)
+    {
+        if($left > $right) {
+            $tmp = $left;
+            $left = $right;
+            $right = $tmp;
+        }
+
+        return $this->table('room_chatwo')->insert(['user_left' => $left,'user_right' => $right,'add_time' => time()]);
+    }
 
     public function getLastChatwoMsg($user)
     {
@@ -152,24 +188,8 @@ class roomModel extends Model
         }
     }
 
-    public function getNotReadCnt($type,$from,$to,$member_cond=0){
-        if($type == 'chatwo'){
-            $table = "room_chatwo_msg";
-            $condtion = [
-                "msg_id"=>[['gt',$from],['elt',$to]]
-                // "from_user"=>$member_cond,  //只算对方的消息为未读
-            ];
-        }elseif($type == 'room'){
-            $table = "room_msg";
-            $condtion = [
-                "msg_id"=>[['gt',$from],['elt',$to]],
-                // "member_id"=>['not',$member_cond], //只算别人的消息为未读
-            ];
-        }else {
-            return 0;
-        }
-
-        return $this->table($table)->where($condtion)->count();
+    public function getRoomMsgsCount($cond){
+        return $this->table('room_msg')->where($cond)->count();
     }
 
     public function getChatsRoom($user)
@@ -206,30 +226,8 @@ class roomModel extends Model
         return $this->table('room_msg')->field('*')->where(['room_id' => $room_id,'member_id' => $userid,'type' => $msg_type])->order('add_time desc')->select();
     }
 
-    public function addRoomMsg($datas)
-    {
-        return $this->table('room_msg')->insert($datas);
-    }
-
     public function addChatwoMsg($datas)
     {
         return $this->table('room_chatwo_msg')->insert($datas);
     }
-
-    public function getRoomExtend($userid,$lock=false)
-    {
-        $item = $this->table('room_extend')->field('*')->where(['userid' => $userid])->lock($lock)->find();
-        return $item;
-    }
-
-    public function addExtend($userid,$datas)
-    {
-        $datas['userid'] = $userid;
-        return $this->table('room_extend')->insert($datas);
-    }
-
-    public function editExtend($userid,$datas)
-    {
-        return $this->table('room_extend')->where(['userid' => $userid])->update($datas);
-    }
 }

+ 2 - 1
helper/room/bargain_manager.php

@@ -222,7 +222,8 @@ class bargain_manager
                 $mod_bargain->editBargain($this->mBargainId,['discount' => ['exp',"discount+{$value}"],'user_num' => ['exp',"user_num+1"]]);
             }
 
-            $mod_room->addRoomMsg(['room_id' => $this->mRoomId,'member_id' => $userid, 'type' => proto_type::msg_type_bargain,'msg' => json_encode(['value' => $value]),'add_time' => time()]);
+            $mod_room->addRoomMsg(['room_id' => $this->mRoomId,'member_id' => $userid,
+                'type' => proto_type::msg_type_bargain,'msg' => json_encode(['value' => $value]),'add_time' => time(),'msg_type' => 0]);
             return ['value' => $value,'success' => true,'discount' => $this->mParams->discount(),'user_num' => $this->mParams->user_num()];
         }
     }

+ 1 - 1
helper/room/base_room.php

@@ -151,7 +151,7 @@ abstract class base_room extends base_info
     protected function record_message($userid,$type,$content)
     {
         $mod_room = Model('room');
-        return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content,'add_time' => time()]);
+        return $mod_room->addRoomMsg(['room_id' => $this->mRoomid,'member_id' => $userid, 'type' => $type,'msg' => $content,'add_time' => time(),'msg_type' => 0]);
     }
 
     public function relay_users_msgs()

+ 35 - 2
helper/room/chatwo.php

@@ -16,11 +16,12 @@ class chatwo
 {
     protected $mUserInfos;
     private   $mod_room;
-
+    private   $mMapChat;
 
     public function __construct()
     {
         $this->mUserInfos = [];
+        $this->mMapChat = [];
         $this->mod_room = Model('room');
     }
 
@@ -46,6 +47,32 @@ class chatwo
         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 = [];
@@ -99,6 +126,12 @@ class chatwo
 
     protected function record_message($from,$to,$type,$content)
     {
-        return $this->mod_room->addChatwoMsg(['from_user' => $from,'to_user' =>$to, 'type' => $type,'msg' => $content,'add_time' => time(),'state' => 0]);
+        $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;
+        }
     }
 }

+ 8 - 1
helper/room/factory_client.php

@@ -95,7 +95,14 @@ class factory_client extends tcp_client
     //////////////////////////////////////fcgi//////////////////////////////////////////////////////////////////////////
     public function invite($roomid, $inviter,$invitees)
     {
-        $param = ["act" => 'fcgi','op' => 'invite','room' => $roomid,'inviter' => $inviter,'invitees' => $invitees];
+        $users = [];
+        foreach ($invitees as $user) {
+            $users[] = intval($user);
+        }
+        $users = array_unique($users);
+        if(empty($users)) return false;
+
+        $param = ["act" => 'fcgi','op' => 'invite','room' => $roomid,'inviter' => $inviter,'invitees' => $users];
         $result = $this->request($param);
         if(empty($result)) return false;
 

+ 8 - 0
helper/room/proto_type.php

@@ -141,4 +141,12 @@ class base_info
     public function user_count() {
         return intval($this->mParams['users']);
     }
+    public function format()
+    {
+        $ret = [];
+        $ret['room_id'] = $this->room_id();
+        $ret['name'] = $this->name();
+
+        return $ret;
+    }
 }

+ 111 - 56
mobile/control/member_talk.php

@@ -153,7 +153,7 @@ class member_talkControl extends mbMemberControl
             }
 
             $result = $this->roomsg($msgid,$room);
-            $msgs = $this->format_message($result['uids'],$result['msgs'],$type);
+            $msgs = $this->format_message($result['uids'],$result['msgs'],$type,0);
 
         }
         elseif($type == 'chatwo')
@@ -163,7 +163,7 @@ class member_talkControl extends mbMemberControl
                 return self::outerr(errcode::ErrParamter);
             }
             $result = $this->chatwomsg($msgid,$user);
-            $msgs = $this->format_message($result['uids'],$result['msgs'],$type);
+            $msgs = $this->format_message($result['uids'],$result['msgs'],$type,$user);
 
         }
         else {
@@ -172,7 +172,7 @@ class member_talkControl extends mbMemberControl
         return self::outsuccess(["msgs"=>$msgs]);
     }
 
-    private function format_message($uids,$msgs,$type)
+    private function format_message($uids,$msgs,$type,$other)
     {
         $members = [];
 
@@ -205,19 +205,29 @@ class member_talkControl extends mbMemberControl
             $val["act"] = $type;
             $val["op"] = "message";
             $val["msgtype"] = "message";
-            $val["room"] = intval($msg['room_id']);
+
+            $from = intval($msg['member_id']);
+            $member = $members[$from];
 
             if($type == 'room') {
-                $from = $members[intval($msg['member_id'])];
+                $val["room"] = intval($msg['room_id']);
             }
-            elseif($type == 'chatwo') {
-                $from = $members[intval($msg['from_user'])];
+            elseif($type == 'chatwo')
+            {
+                $val['from'] = $from;
+
+                if($from == session_helper::memberid()) {
+                    $val['to'] = $other;
+                }
+                else {
+                    $val['to'] = session_helper::memberid();
+                }
             }
             else {
-                $from = [];
+
             }
 
-            $val['content'] = ['msgid' => intval($msg['msg_id']), 'from' => $from,'type' => $msgtype,'content' => $msg['msg'],'send_time' => intval($msg['add_time'])];
+            $val['content'] = ['msgid' => intval($msg['msg_id']), 'from' => $member,'type' => $msgtype,'content' => $msg['msg'],'send_time' => intval($msg['add_time'])];
 
             $result[] = $val;
         }
@@ -228,7 +238,7 @@ class member_talkControl extends mbMemberControl
     private function roomsg($msgid,$room)
     {
         $mod_room = Model('room');
-        $msgs = $mod_room->getRoomMsgList(['room_id' => $room,'msg_id' => ['lt',$msgid]], $this->page_size);
+        $msgs = $mod_room->getRoomsgList(['room_id' => $room,'msg_id' => ['lt',$msgid],'msg_type' => 0], $this->page_size);
 
         $uids = [];
         foreach ($msgs as $msg) {
@@ -240,15 +250,12 @@ class member_talkControl extends mbMemberControl
     private function chatwomsg($msgid,$user)
     {
         $mod_room = Model('room');
-        $user_1 = session_helper::memberid();
-        $user_2 = $user;
+        $left = session_helper::memberid();
+        $right = $user;
 
-        $cond['from_user&to_user&msg_id'] = ['_multi' => true,$user_1,$user_2,['lt',$msgid]];
-        $cond['to_user&from_user&msg_id'] = ['_multi' => true,$user_1,$user_2,['lt',$msgid]];
-        $cond['_op'] = 'OR';
-
-        $msgs = $mod_room->getChatwoMsgList($cond, $this->page_size);
-        return ['uids' => [$user_1,$user_2],'msgs' => $msgs];
+        $chat_id = $mod_room->findChatid($left,$right);
+        $msgs = $mod_room->getRoomsgList(['room_id' => $chat_id,'msg_id' => ['lt',$msgid],'msg_type' => 1], $this->page_size);
+        return ['uids' => [$left,$right],'msgs' => $msgs];
     }
 
     public function create_roomOp()
@@ -261,14 +268,10 @@ class member_talkControl extends mbMemberControl
     public function inviteOp()
     {
         $room = intval($_GET['room']);
-        $users = explode(',', trim($_GET['invitees']));
-        if(empty($users) || $room <= 0) {
+        $invitees = explode(',', trim($_GET['invitees']));
+        if(empty($invitees) || $room <= 0) {
             return self::outerr(errcode::ErrParamter);
         }
-        $invitees = [];
-        foreach ($users as $user) {
-            $invitees[] = intval($user);
-        }
 
         $result = room\factory_client::instance()->invite($room,session_helper::memberid(),$invitees);
         if($result === false) {
@@ -296,24 +299,100 @@ class member_talkControl extends mbMemberControl
     }
 
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+    private function chat_rooms($user,&$roomids)
+    {
+        $mod_room = Model('room');
+        $items = $mod_room->getPartRooms(['member_id' => $user,'state'=>0],'room_id');
+
+        if(empty($items)) return false;
+
+        $rooms = [];
+        foreach ($items as $room) {
+            $rooms[] = intval($room['room_id']);
+        }
+//        $items = $mod_room->getRooms(['type' => proto_type::room_chat, 'room_id'=>['in',$rooms]]);
+        $items = $mod_room->getRooms(['room_id'=>['in',$rooms]]);
+        if(empty($items)) return false;
+
+        $result = [];
+        $roomids = [];
+        foreach ($items as $item) {
+            $roomids[] = intval($item['room_id']);
+            $info = new room\base_info($item);
+            $result[] = $info->format();
+        }
+
+        return $result;
+    }
     public function talksOp()
     {
         $user = session_helper::memberid();
-        $post = $this->init_talk($_POST['post_data']);
-        $chats = $this->talk_chatwos($user,$post['local_chats']);
-        $rooms = $this->talk_rooms($user,$post['local_rooms']);
+        $params = $this->talk_params($_POST['content']);
+        $room_descs = $this->chat_rooms($user,$roomids);
+        $room_msgs = Model('room')->getLastRoomMsgs($roomids);
+        $room_list = $this->room_list($roomids,$room_msgs,$params['rooms']);
 
-        return self::outsuccess(["rooms"=>$rooms,"chats"=>$chats]);
+        return self::outsuccess(["room_list" => $room_list,"room_descs" => $room_descs,'room_msgs' => $room_msgs]);
     }
 
-    private function init_talk($json)
+    private function room_list($rooms,$room_msgs, $input)
     {
-        if(empty(trim($json))) {
-            return ["rooms" => [],"chatwos" => []];
+        $r_msg = [];
+        foreach ($room_msgs as $msg) {
+            $room = intval($msg['room_id']);
+            $msg_id = intval($msg['msg_id']);
+            $r_msg[$room] = $msg_id;
+        }
+
+        $pairs = [];
+        foreach ($rooms as $room)
+        {
+            if(array_key_exists($room,$r_msg)) {
+                $pairs[$room] = $r_msg[$room];
+            } else {
+                $pairs[$room] = 0;
+            }
+        }
+
+        if($input === false)
+        {
+            $result = [];
+            foreach ($pairs as $key => $msgid) {
+                $result[] = ['room' => $key,'count' => 0,'msgid' => $msgid];
+            }
+            return $result;
         }
+        else
+        {
+            $mod_room = Model('room');
+            $result = [];
+            foreach ($pairs as $key => $msgid)
+            {
+                if(array_key_exists($key,$input))
+                {
+                    if($input[$key] < $msgid) {
+                        $count = $not_read = $mod_room->getRoomMsgsCount(['room_id' => $key,"msg_id"=>[['gt',$input[$key]],['elt',$msgid]]]);
+                    } else {
+                        $count = 0;
+                    }
+                }
+                else {
+                    $count = $not_read = $mod_room->getRoomMsgsCount(['room_id' => $key,"msg_id"=>[['gt',0],['elt',$msgid]]]);
+                }
+
+                $result[] = ['room' => $key,'count' => intval($count),'msgid' => $msgid];
+            }
+
+            return $result;
+        }
+    }
+
+    private function talk_params($json)
+    {
+        if(empty(trim($json))) return ["rooms" => false,"chatwos" => false];
 
         $input = json_decode($json);
-        if($input == false) return ["rooms" => [],"chatwos" => []];
+        if($input == false) return ["rooms" => false,"chatwos" => false];
 
         $rooms = [];
         $chats = [];
@@ -344,30 +423,6 @@ class member_talkControl extends mbMemberControl
         return ["rooms"=>$rooms,"chatwos"=>$chats];
     }
 
-    private function talk_rooms($user,$local_last)
-    {
-        $mod_room = Model('room');
-
-        $join_rooms = $mod_room->getJoinedRooms(['member_id'=>$user,'state'=>0]);
-        $roomids = [];
-        if(!empty($join_rooms)) {
-            foreach ($join_rooms as $room){$roomids[] = $room['room_id'];}
-        }
-
-        $chat_rooms = $mod_room->getRooms(['type'=>proto_type::room_chat,'room_id'=>['in',$roomids]]);
-        $roomids = [];
-        if(!empty($chat_rooms)){
-            foreach ($chat_rooms as $room){$roomids[] = $room['room_id'];}
-        }
-
-
-        $last_msgs = $mod_room->getLastRoomMsgs($roomids);
-
-        $ret = $this->talk_format($last_msgs,'room',$user,$local_last);
-
-        return $ret;
-    }
-
     private function talk_chatwos($user,$local_last)
     {
         $mod_room = Model('room');

+ 1 - 1
room_srv.php

@@ -34,6 +34,6 @@ $host = $config['room_srv']['host'];
 $ports = $config['room_srv']['ports'];
 
 foreach ($ports as $port) {
-    event\util::fork_listen($host,$port,'roomwork',0);
+    event\util::fork_listen($host,$port,'roomwork',1);
 }
 

+ 0 - 1
test/TestTalk.php

@@ -71,7 +71,6 @@ class TestTalk extends PHPUnit_Framework_TestCase
 
         $mod_room = Model('room');
 //        $msgs = $mod_room->getRoomMsgList($cond,20);
-        $msgs = $mod_room->getChatwoMsgList($left,20);
     }
 
     public function testLastRoomMsg(){