123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/7/16
- * Time: 下午4:36
- */
- define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
- require_once (BASE_ROOT_PATH . '/fooder.php');
- require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
- require_once(BASE_ROOT_PATH . '/helper/room/factory_client.php');
- require_once(BASE_ROOT_PATH . '/helper/room/proto_type.php');
- require_once(BASE_ROOT_PATH . '/helper/room/author.php');
- class TestTalk extends PHPUnit_Framework_TestCase
- {
- public static function setUpBeforeClass()
- {
- Base::run_util();
- }
- public function testGen()
- {
- global $config;
- $room_id = $config['special_rooms']['shake_bonus'];
- $webaddr = $config['access_addr'];
- $uids = [39625,39653];
- $creator = self::shake_creator($room_id);
- $ret = room\factory_client::instance()->invite($room_id,$creator,$uids);
- foreach ($uids as $uid)
- {
- $result = [];
- if($ret != false) {
- $result['addr'] = $webaddr;
- $result['token'] = room\author::sign_web($room_id,$uid);
- $result['room'] = intval($room_id);
- $result['user'] = $uid;
- }
- }
- }
- static private function shake_creator($room_id)
- {
- static $stCreator = 0;
- if($stCreator == 0) {
- $mod_room = Model('room');
- $params = $mod_room->getRoom($room_id);
- $rinfo = new room\base_info($params);
- $stCreator = $rinfo->creator();
- }
- return $stCreator;
- }
- public function testSql()
- {
- $left['from_user&to_user&msg_id'] = ['_multi' => true,1,2,['lt',100]];
- $left['to_user&from_user&msg_id'] = ['_multi' => true,1,2,['lt',100]];
- $left['_op'] = 'OR';
- // $right['msg_id'] = ['lt',100];
- //
- // $cond[] = $left;
- // $cond[] = $right;
- // $cond['_op'] = 'and';
- $mod_room = Model('room');
- // $msgs = $mod_room->getRoomMsgList($cond,20);
- $msgs = $mod_room->getChatwoMsgList($left,20);
- }
- public function testLastRoomMsg(){
- $user = 39623;
- $mod_room = Model('room');
- $items =$mod_room->table('room_msg')->field("MAX(msg_id) as msg_id,room_id,member_id")->group('room_id')->having('member_id = '.$user)->select();
- $last_msg = [];
- foreach ($items as $item){
- $last_msg[$items['room_id']]= $items['msg_id'];
- }
- $room_key =array_keys($last_msg);
- $msg_key = array_values($last_msg);
- $items =$mod_room->table('room_msg')->field("msg_id,room_id,member_id")->group('room_id')->having('member_id = '.$user)->select();
- echo 1;
- }
- public function testTalk_Chats(){
- $json = [
- "rooms" =>[
- "room_id"=> "msg_id",
- ],
- "friends" =>[
- "39679" => 265,
- ]
- ];
- $user = 39623;//自己
- $mod_room = Model('room');
- $items = $mod_room->getLastChatwoMsg($user);
- $ret = $this->talk_format($items,'chatwo',$user,$json['friends']);
- if(!empty($json["friends"]))
- {
- $local_last = $json["friends"];
- foreach ($ret as $member_id => $last_msg){
- if(isset($local_last[$member_id]) && $local_last[$member_id]<$last_msg["msg_id"]){
- $not_read = $mod_room->table("room_chatwo_msg")->where([
- "from_user"=>$member_id,
- "msg_id"=>[['gt',$local_last[$member_id]],['elt',$last_msg["msg_id"]]]
- ])->count();
- $ret[$member_id]["not_read"] = $not_read;
- }else{
- $ret[$member_id]["not_read"] = 0;
- }
- }
- }
- echo 0;
- }
- public function testTalk_Rooms(){
- $json = [
- "rooms" =>[
- 83=> "msg_id",
- ],
- "friends" =>[
- "39679" => 265,
- ]
- ];
- $user = 39623;//自己
- $mod_room = Model('room');
- $items = $mod_room->getLastRoomMsg($user);
- $ret = $this->talk_format($items,'room',$user,$json['rooms']);
- echo 0;
- }
- public function testTalks(){
- $arr = [
- ["type"=>'room',"room"=>83, "msg_id"=>2550927],
- ["type"=>'chat',"user"=>39679,"msg_id"=>265],
- ];
- $post_data = json_encode($arr);
- $user = 39623;
- $post = $this->init_talk($post_data);
- $chats = $this->talk_chatwos($user,$post['local_chats']);
- $rooms = $this->talk_rooms($user,$post['local_rooms']);
- echo 0;
- }
- private function init_talk($post){
- $local_rooms = [];
- $local_chats = [];
- if(empty(trim($post))){
- return ["local_rooms"=>$local_rooms,"local_chats"=>$local_chats];
- }
- $json = json_decode($post,true);
- if(!empty($json))
- {
- foreach ($json as $v)
- {
- if($v['type'] == 'room'){
- $local_rooms[$v['room']] = $v['msg_id'];
- }elseif($v['type'] == 'chat'){
- $local_chats[$v['user']] = $v['msg_id'];
- }else{
- continue;
- }
- }
- }
- return ["local_rooms"=>$local_rooms,"local_chats"=>$local_chats];
- }
- private function talk_rooms($user,$local_last)
- {
- $mod_room = Model('room');
- $items = $mod_room->getLastRoomMsg($user);
- $ret = $this->talk_format($items,'room',$user,$local_last);
- return $ret;
- }
- private function talk_chatwos($user,$local_last)
- {
- $mod_room = Model('room');
- $items = $mod_room->getLastChatwoMsg($user);
- $chats = $this->talk_format($items,'chatwo',$user,$local_last);
- return $chats;
- }
- private function talk_format($talks,$type,$user,$local_last)
- {
- $items = [];
- $mod_room = Model('room');
- foreach($talks as $last_msg)
- {
- if($type == 'room')
- {
- $id = $last_msg['room_id'];
- if(isset($local_last[$id]) && $local_last[$id]<$last_msg["msg_id"]) {
- $not_read = $mod_room->getNotReadCnt($type,$local_last[$id],$last_msg["msg_id"],$id);
- }else{
- $not_read = 0;
- }
- $item = [
- "msg_id" => $last_msg['msg_id'],
- "msg" => $last_msg['msg'],
- "add_time" => $last_msg['add_time'],
- "not_read" => $not_read,
- "room_id" => $id
- ];
- }
- elseif($type=='chatwo')
- {
- $id = $last_msg['from_user'] == $user?$last_msg['to_user']:$last_msg['from_user'];
- if(isset($local_last[$id]) && $local_last[$id]<$last_msg["msg_id"]) {
- $not_read = $mod_room->getNotReadCnt($type,$local_last[$id],$last_msg["msg_id"],$id);
- }else{
- $not_read = 0;
- }
- $item = [
- "msg_id" => $last_msg['msg_id'],
- "msg" => $last_msg['msg'],
- "add_time" => $last_msg['add_time'],
- "not_read" => $not_read,
- "member_id" => $id
- ];
- }else{
- $id = 0;
- $item = [];
- }
- $items[$id] = $item;
- }
- return $items;
- }
- public function testRoom_list(){
- $_SESSION['is_login'] =1;
- $_SESSION['member_id'] = 39623;
- $user = 39623;
- $case1 = $this->room_case1($user);
- $case2 = $this->room_case2($user);
- $case3 = $this->room_case3($user);
- echo 0;
- }
- private function room_case2($user)
- {
- $mod_room = Model("room");
- $chats_rooms = $mod_room->getChatsRoom($user);
- $rooms = [];
- $uids_total = [];
- if(!empty($chats_rooms))
- {
- foreach ($chats_rooms as $room)
- {
- $uids = [];
- $paticipates = $mod_room->getRoomMember($room['room_id']);
- if(!empty($paticipates))
- {
- foreach ($paticipates as $one)
- {
- $uids[] = $one['member_id'];
- }
- $uids_total = array_merge($uids_total,$uids);
- }
- $rooms[] = [
- "room_id"=>$room['room_id'],
- "room_name"=>$room['room_name'],
- "avatar"=>"",
- "room_members"=>$uids
- ];
- }
- $members = [];
- if(!empty($uids_total))
- {
- $uids_total = array_unique($uids_total);
- $items = Model('member')->getMemberList(['member_id' => ['in',$uids_total]]);
- foreach ($items as $item){
- try
- {
- $info = new member_info($item);
- $members[$item['member_id']] = $info->filter();
- }
- catch (Exception $ex) {
- Log::record($ex->getMessage(),Log::ERR);
- }
- }
- }
- if(!empty($members))
- {
- foreach ($rooms as $index => $room){
- $room_members = [];
- if(!empty($room['room_members']))
- {
- foreach ($room['room_members'] as $uid)
- {
- if(isset($members[$uid])){
- $room_members[] = $members[$uid];
- }
- }
- }
- $rooms[$index]['room_members'] = $room_members;
- }
- }
- }
- return $rooms;
- }
- private function room_case1($user)
- {
- $mod_room = Model('room');
- $chat_rooms = $mod_room->getJoinedChatRoom($user);
- $rooms = [];
- if(!empty($chat_rooms))
- {
- foreach ($chat_rooms as $item)
- {
- $rooms[] = $this->room_info($item);
- }
- }
- return $rooms;
- }
- private function room_info($room)
- {
- $mod_room = Model("room");
- $paticipates = $mod_room->getRoomMember($room['room_id']);
- $uids = [];
- if(!empty($paticipates))
- {
- foreach ($paticipates as $one){
- $uids[] = $one['member_id'];
- }
- }
- if(empty($uids)){
- $items = [];
- }else{
- $items = Model('member')->getMemberList(['member_id' => ['in',$uids]]);
- }
- $members = [];
- foreach ($items as $item){
- try
- {
- $info = new member_info($item);
- $members[] = $info->filter();
- }
- catch (Exception $ex) {
- Log::record($ex->getMessage(),Log::ERR);
- }
- }
- $ret = [
- "room_id"=>$room['room_id'],
- "room_name"=>$room["room_name"],
- "avatar"=> "", //头像和member查询维度太高
- "room_members"=>$members,
- ];
- return $ret;
- }
- private function room_case3($user){
- $mod_room = Model("room");
- $chats_rooms = $mod_room->getChatsRoom($user);
- $rooms = [];
- if(!empty($chats_rooms))
- {
- $roomids = [];
- foreach ($chats_rooms as $room)
- {
- $rooms[] = [
- "room_id"=>$room['room_id'],
- "room_name"=>$room['room_name'],
- "avatar"=>"",
- "room_members"=>[]
- ];
- $roomids[] = intval($room['room_id']);
- }
- $room_members = [];
- $uids_total = [];
- $roomTouser = $mod_room->table('room_msg')
- ->field('room_id,group_concat(DISTINCT member_id) as uidstr')
- ->where(['room_id'=>['in',$roomids]])
- ->group('room_id')
- ->select();
- if(!empty($roomTouser))
- {
- foreach ($roomTouser as $index => $item)
- {
- $uids = explode(',',$item['uidstr']);
- $uids_total = array_merge($uids_total,$uids);
- $room_members[$item['room_id']] = $uids;
- }
- }
- $members = [];
- if(!empty($uids_total))
- {
- $uids_total = array_unique($uids_total);
- $items = Model('member')->getMemberList(['member_id' => ['in',$uids_total]]);
- foreach ($items as $item){
- try
- {
- $info = new member_info($item);
- $members[$item['member_id']] = $info->filter();
- }
- catch (Exception $ex) {
- Log::record($ex->getMessage(),Log::ERR);
- }
- }
- }
- if(!empty($members))
- {
- foreach ($rooms as $index => $room)
- {
- $room_id = $room['room_id'];
- if(isset($room_members[$room_id]))
- {
- $user_list = [];
- foreach ($room_members[$room_id] as $i=>$uid)
- {
- if(isset($members[$uid])) $user_list[$i] = $members[$uid];
- }
- $rooms[$index]['room_members'] = $user_list;
- }
- }
- }
- }
- return $rooms;
- }
- }
|