<?php
    
    // 编码
    header("Content-type:application/json");
    
    // 付费群id
    $chatroom_id = $_GET['chatroom_id'];
    $t = $_GET['t'];

    // 数据库配置
	include '../../console/Db.php';

	// 实例化类
	$db = new DB_API($config);
    
    // 获取群信息
    $getChatroom = $db->set_table('ylb_ChatroomPay')->find(['chatroom_id' => $chatroom_id]);
    
    if($getChatroom) {
        
        // 如果没有设置群人数
        if(!$getChatroom['chatroom_memberNum']) {
            
            // 则随机生成
            $chatroom_memberNum = rand(100,500);
        }else {
            
            $chatroom_memberNum = $getChatroom['chatroom_memberNum'];
        }
        
        // 群名称特殊处理
        $chatroom_name = $getChatroom['chatroom_name'];
        
        // 获取当前客户端ip信息
        $ipinfo = file_get_contents('http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='.getClientIP().'&co=&resource_id=6006&oe=utf8');
        
        // 当前客户端ip地址所在的城市
        $ipinfo_city = json_decode($ipinfo,true)['data'][0]['location'];
        
        // 使用正则表达式匹配省份和城市
        preg_match('/^(.*?)(省)(.*?)(市)/', $ipinfo_city, $matches);
        
        if ($matches) {
            $province = $matches[1];  // 不包含"省"
            $city = $matches[3];      // 不包含"市"
            
            if($city) {
                
                // 如果获取到城市名则替换[城市]
                $chatroom_name_ = str_replace('[城市]', $city, $chatroom_name);
            }else if($province) {
                
                // 如果获取到省份则替换[城市]
                $chatroom_name_ = str_replace('[城市]', $province, $chatroom_name);
            }
        } else {
            
            // 去掉[城市]
            $chatroom_name_ = str_replace('[城市]', '', $chatroom_name);
        }
    
        // 获取成功
        $result = array(
            'code' => 200,
            'msg' => '获取成功',
            'chatroom_memberNum' => $chatroom_memberNum,
            'chatroom_template' => $getChatroom['chatroom_template'],
            'chatroom_amount' => $getChatroom['chatroom_amount'],
            'chatroom_desc' => $getChatroom['chatroom_desc'],
            'chatroom_avatar' => $getChatroom['chatroom_avatar'],
            'chatroom_status' => $getChatroom['chatroom_status'],
            'chatroom_name' => $chatroom_name_
        );
    }else {
        
        // 群信息获取失败
        $result = array(
            'code' => 201,
            'msg' => '群信息获取失败'
        );
    }
    
    // 获取客户端ip地址
    function getClientIP() {
        $ipaddress = '';
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        } else if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
            $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
        } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        } else if (isset($_SERVER['REMOTE_ADDR'])) {
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        } else {
            $ipaddress = 'UNKNOWN';
        }
        return $ipaddress;
    }
    
    // 返回JSON
    echo json_encode($result,JSON_UNESCAPED_UNICODE);
    
?>
