$t_r) return -1; elseif($t_l < $t_r) return 1; else return 0; } static public function msgid_asc($left,$right) { $t_l = intval($left['msg_id']); $t_r = intval($right['msg_id']); if($t_l > $t_r) return 1; elseif($t_l < $t_r) return -1; else return 0; } static public function steps_desc($left,$right) { $t_l = intval($left['steps']); $t_r = intval($right['steps']); if($t_l > $t_r) return -1; elseif($t_l < $t_r) return 1; else return 0; } } class talks_helper { const max_msgid = 1 << 32; private $mod_room; public function __construct() { $this->mod_room = Model('room'); } public function friends($user) { $relations = $this->relations($user); $invitees = $this->invitees($user); $friends = array_merge($relations,$invitees); $friends[] = $user; $friends = array_unique($friends); return $friends; } private function relations($user) { try { $relation = new relation\mem_relation($user); $follows = $relation->follower();//我关注的 $fans = $relation->subscriber();//关注我的 return array_merge($follows,$fans); } catch (Exception $ex) { return []; } } private function invitees($user) { $mod_member = Model('member'); $items = $mod_member->getMemberList(['inviter_id' => $user],'member_id', 0, ''); $users = []; foreach ($items as $item) { $users[] = intval($item['member_id']); } return $users; } public function talks($user,$input) { $params = $this->talk_params($input); $room_descs = $this->chatrooms($user,$roomids); $room_msgs = $this->lastmsgs($roomids,$room_senders,0); $room_list = $this->room_list($roomids,$room_msgs,$params['rooms']); $chatwo_uniquer = new uniquer(); $chatid_others = $this->chatwos($user,$chatwo_uniquer); $chatwo_msgs = $this->lastmsgs(array_keys($chatid_others),$chatwo_senders,1); $chatwo_list = $this->chatwo_list($chatid_others,$chatwo_msgs,$params['chatwos']); $push_descs = $this->pushs($pushids); $push_msgs = $this->push_lastmsg($user,$pushids); $push_list = $this->push_list($user,$pushids,$push_msgs,$params['pushs']); $pushmsgs = []; foreach ($push_msgs as $msg) { $pushmsgs[] = $msg; } $talks = array_merge($room_list,$chatwo_list); usort($talks,['room\sorter','msgid_desc']); $talk_list = array_merge($push_list,$talks); $friends = $this->friends($user); foreach ($friends as $friend) { if(!$chatwo_uniquer->find($friend)) { $talk_list[] = ['talk_type' => 'chatwo','talk_id' => $friend,'count' => 0,'msg_id' => 0]; } } $other = array_values($chatid_others); $uids = array_merge($other,$room_senders,$chatwo_senders,$friends); $uids = array_unique($uids); $members = member_info::get_members($uids); return ["talk_list" => $talk_list,'messages' => array_merge($room_msgs,$chatwo_msgs,$pushmsgs),'push_descs' => $push_descs,'room_descs' => $room_descs,'members' => $members]; } //pushid = 1; 命令消息,界面不限时 //pushid = 2; 系统消息 -》 type=51 and room_id = 0 AND member_id IN (0,user) //pushid = 3; 新的好友 -》 type=52 and room_id = 0 AND member_id IN (0,user) private function pushs(&$pushids) { $pushids = [2,3]; $result = []; { //pushid = 2; $item = []; $item['pushid'] = 2; $item['name'] = '系统消息'; $item['avatar'] = RESOURCE_SITE_URL . '/mobile/room/msg_notice.png'; $url = BASE_SITE_URL . "/mobile/index.php?act=member_talk&op=sys_notice"; $item['schema'] = schema_helper::openurl('系统消息',$url); } $result[] = $item; { //pushid = 3; $item = []; $item['pushid'] = 3; $item['name'] = '新的好友'; $item['avatar'] = RESOURCE_SITE_URL . '/mobile/room/apply_notice.png'; $url = BASE_SITE_URL . "/mobile/index.php?act=member_talk&op=new_applys"; $item['schema'] = schema_helper::openurl('系统消息',$url); } $result[] = $item; return $result; } private function push_lastmsg($user,$pushids) { $cond = ['room_id' => 0,'member_id' => ['in',[0,$user]]]; $result = []; foreach ($pushids as $pushid) { if($pushid == 2) { $cond['type'] = proto_type::msg_type_nofity; } elseif($pushid == 3) { $cond['type'] = proto_type::msg_type_apply; } else { continue; } $msgs = $this->mod_room->getRoomsgList($cond,1); if(!empty($msgs)) { $item = $msgs[0]; $info = [ "msg_id" => intval($item['msg_id']), "room_id" => intval($item['room_id']), "member_id" => intval($item['member_id']), "type" => proto_type::msg_stype_text, "add_time" => intval($item['add_time']), 'content' => $item['msg'] ]; $result[$pushid] = $info; } } return $result; } private function talk_params($json) { if(empty(trim($json))) return ["rooms" => false,"chatwos" => false,'pushs' => false]; $input = json_decode($json,true); if($input == false) return ["rooms" => false,"chatwos" => false,'pushs' => false]; $rooms = []; $chats = []; $pushs = []; foreach ($input as $item) { $msgid = intval($item['msg_id']); if($msgid < 0) $msgid = self::max_msgid; if($item['talk_type'] == 'room') { $room = intval($item['talk_id']); if($room <= 0) continue; $rooms[$room] = $msgid; } elseif($item['talk_type'] == 'chatwo') { $user = intval($item['talk_id']); if($user <= 0) continue; $chats[$user] = $msgid; } elseif($item['talk_type'] == 'push') { $push = intval($item['talk_id']); if($push <= 0) continue; $pushs[$push] = $msgid; } else { continue; } } return ["rooms" => $rooms,"chatwos" => $chats,'pushs' => $pushs]; } private function chatrooms($user, &$roomids) { $roomids = []; $items = $this->mod_room->getRoomParts(['member_id' => $user,'state' => 0],'room_id'); if(empty($items)) return []; $rooms = []; foreach ($items as $room) { $rooms[] = intval($room['room_id']); } $items = $this->mod_room->getRooms(['type' => proto_type::room_chat, 'room_id'=>['in',$rooms]]); if(empty($items)) return []; $result = []; foreach ($items as $item) { $roomids[] = intval($item['room_id']); $room_info = $this->room_info($item); $result[] =$room_info; } return $result; } //$msg_type == 0 表示 房间消息,1 表示单聊消息 private function lastmsgs($roomids,&$senders,$msg_type = 0) { $senders = []; if(empty($roomids)) return []; $msgs = $this->mod_room->getLastRoomMsgs(['room_id' => ['in',$roomids],"msg_type" => $msg_type]); if(empty($msgs)) return []; $result = []; foreach ($msgs as $key => $item) { $sender = intval($item['member_id']); $senders[] = $sender; $info = [ "msg_id" => intval($item['msg_id']), "room_id" => intval($item['room_id']), "member_id" => $sender, "type" => proto_type::from_msgtype(intval($item['type'])), "add_time" => intval($item['add_time']), 'content' => $item['msg'] ]; $result[] = $info; } return $result; } private function push_list($user,$pushids,$pushmsgs,$input) { $r_msg = []; foreach ($pushmsgs as $pushid => $msg) { $msg_id = intval($msg['msg_id']); $r_msg[$pushid] = $msg_id; } $pairs = []; foreach ($pushids as $pushid) { if(array_key_exists($pushid,$r_msg)) { $pairs[$pushid] = $r_msg[$pushid]; } else { $pairs[$pushid] = 0; } } if($input === false) { $result = []; foreach ($pairs as $pushid => $msgid) { $result[] = ['talk_type' => 'push','talk_id' => $pushid,'count' => 0,'msg_id' => $msgid]; } return $result; } else { $result = []; foreach ($pushids as $pushid) { $cond = ['room_id' => 0,'member_id' => ['in',[0,$user]]]; if($pushid == 2) { $cond['type'] = proto_type::msg_type_nofity; } elseif($pushid == 3) { $cond['type'] = proto_type::msg_type_apply; } else { continue; } $msgid = $pairs[$pushid]; if ($msgid == 0) { $count = 0; } else { if (array_key_exists($pushid, $input)) { if ($input[$pushid] >= 0 && $input[$pushid] < $msgid) { $cond['msg_id'] = [['gt', $input[$pushid]], ['elt', $msgid]]; $count = $this->mod_room->getRoomMsgsCount($cond); } else { $count = 0; } } else { $cond['msg_id'] = [['gt', 0], ['elt', $msgid]]; $count = $this->mod_room->getRoomMsgsCount($cond); } } $result[] = ['talk_type' => 'push', 'talk_id' => $pushid, 'count' => intval($count), 'msg_id' => $msgid]; } return $result; } } private function chatwo_list($chatid_others, $msgs, $input) { $r_msg = []; foreach ($msgs as $msg) { $room = intval($msg['room_id']); $msg_id = intval($msg['msg_id']); $r_msg[$room] = $msg_id; } $pairs = []; foreach ($chatid_others as $room => $other) { if(array_key_exists($room,$r_msg)) { $pairs[$other] = $r_msg[$room]; } else { $pairs[$other] = 0; } } if($input === false) { $result = []; foreach ($pairs as $user => $msgid) { $result[] = ['talk_type' => 'chatwo','talk_id' => $user,'count' => 0,'msg_id' => $msgid]; } return $result; } else { $result = []; foreach ($chatid_others as $room => $other) { $msgid = $pairs[$other]; if($msgid == 0) { $count = 0; } else { if(array_key_exists($other,$input)) { if($input[$room] >= 0 && $input[$other] < $msgid) { $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room,"msg_id"=>array(['gt',$input[$other]],['elt',$msgid]),"msg_type" => 1]); } else { $count = 0; } } else { $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room,"msg_id"=>[['gt',0],['elt',$msgid]],"msg_type" => 1]); } } $result[] = ['talk_type' => 'chatwo','talk_id' => $other,'count' => intval($count),'msg_id' => $msgid]; } return $result; } } private function room_list($rooms, $msgs, $input) { $r_msg = []; foreach ($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 $room => $msgid) { $result[] = ['talk_type' => 'room','talk_id' => $room,'count' => 0,'msg_id' => $msgid]; } return $result; } else { $result = []; foreach ($pairs as $room => $msgid) { if($msgid == 0) { $count = 0; } else { if(array_key_exists($room,$input)) { if($input[$room] >= 0 && $input[$room] < $msgid) { $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room,"msg_id"=>array(['gt',$input[$room]],['elt',$msgid]), "msg_type" => 0]); } else { $count = 0; } } else { $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room, "msg_id"=>[['gt',0],['elt',$msgid]], "msg_type" => 0]); } } $result[] = ['talk_type' => 'room','talk_id' => $room,'count' => intval($count),'msg_id' => $msgid]; } return $result; } } private function chatwos($user,uniquer $uniquer) { $items = $this->mod_room->getChatwoRooms(['user_left' => $user,'user_right' => $user,'_op' => 'OR']); if(empty($items)) return []; $result = []; foreach ($items as $item) { $id = intval($item['chat_id']); $left = intval($item['user_left']); $right = intval($item['user_right']); $friend = $left == $user ? $right : $left; $result[$id] = $friend; $uniquer->add_value($friend); } return $result; } private function room_info($room) { if(empty($room)) return false; $info = new room_info($room); $room_info = $info->format(); return $room_info; } } class uploader { public function upfile($upload_dir,&$upload_info) { if(empty($upload_dir)) { $upload_info = ['code' => errcode::ErrUpfile, 'msg' => "目标目录不能为空"]; return false; } $roomid = intval($_POST['room_id']); $file_path = $this->file_path(); $upload = new \FileUploader(); if($upload->init_files($file_path,$roomid,$upload_info,'room') == false) { return false; } $upload->set('default_dir',$upload_dir); $thumb_width = '480,1024'; $thumb_height = '480,1024'; $upload->set('max_size',C('image_max_filesize')); $upload->set('thumb_width', $thumb_width); $upload->set('thumb_height',$thumb_height); $upload->set('fprefix',$roomid); $upload->set('thumb_ext', '_240,_1024'); $result = $upload->upfile('file'); if($result == true) { $upload_info = ['code' => 200, 'msg' => $upload->file_name]; return true; } else { $upload_info = ['code' => errcode::ErrUpfile, 'msg' => $upload->error]; return false; } } private function file_path() { $file_path = $_POST["file_path"]; if(file_exists($file_path) == false) { $file_path = BASE_ROOT_PATH . '/data/upload/upfile' . $file_path; } return $file_path; } }