<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,viewport-fit=cover,user-scalable=0;">
    <meta content="telephone=no" name="format-detection">
    <link rel="stylesheet" href="../../static/css/common.css">
</head>

<body>

    <?php
    
        // 接收付费群id
        $crid = trim($_GET['crid']);
        
        // 对付费群id的base64解码
        $crid_base64_decode = intval(base64_decode($crid));
        
        if($crid_base64_decode) {
            
            // 数据库配置
        	include '../../console/Db.php';
        
        	// 实例化类
        	$db = new DB_API($config);
        	
            // 获取付费群id的信息
            $getChatroomInfo = $db->set_table('ylb_ChatroomPay')->find(['chatroom_id' => $crid_base64_decode]);
            
            // 获取网页授权配置
            $getAuthConfig = $db->set_table('ylb_ChatroomPayConfig')->find(['id' => 1]);
            
            if($getAuthConfig) {
                
                // 企业微信企业ID
                $qywx_Appid = $getAuthConfig['qywx_Appid'];
                
                // 企业微信应用网页授权域名	
                $qywx_auth_domain = $getAuthConfig['qywx_auth_domain'];
                
                // 企业微信应用ID	
                $qywx_AgentId = $getAuthConfig['qywx_AgentId'];
            }else {
                
                // 网页授权配置获取失败
                echo '<title>温馨提示</title>';
                echo warnInfo('温馨提示', '网页授权配置获取失败');
            }
            
            if($getChatroomInfo) {
                
                // 判断这个付费群id的状态
                $chatroom_status = $getChatroomInfo['chatroom_status'];
                
                if($chatroom_status == 1) {
                    
                    // 正常
                    // 1. 获取common目录URL
                    $common_url = $qywx_auth_domain . dirname(dirname($_SERVER["REQUEST_URI"]));
                    
                    // 2. 构建redirect_uri
                    $redirect_uri = $common_url . '/addchatroombyinvite/Auth.php';
                    
                    // 3. 构建完整的网页授权URL
                    $jump_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$qywx_Appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state='.$crid_base64_decode.'&agentid='.$qywx_AgentId.'#wechat_redirect';
                    
                    // 4. 跳转
                    echo '<title>授权中...</title>';
                    echo '<script>location.href="'.$jump_url.'";</script>';
                }else {
                    
                    // 暂停使用
                    echo '<title>温馨提示</title>';
                    echo warnInfo('温馨提示', '该群已被管理员暂停使用');
                }
            }else {
                
                // 不存在
                echo '<title>温馨提示</title>';
                echo warnInfo('温馨提示', '该群不存在或已被管理员删除');
            }
        }else {
            
            // 参数错误
            echo '<title>温馨提示</title>';
            echo warnInfo('温馨提示', '该链接参数错误无法正常加载');
        }
        
        // 提醒信息
        function warnInfo($title,$warnText){
            
            return '
            <title>'.$title.'</title>
            <div id="warnning">
                <img src="../../static/img/warn.png" />
            </div>
            <p id="warnText">'.$warnText.'</p>';
        }
    
    ?>

</body>

<script>

    // 屏蔽分享
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        WeixinJSBridge.call('hideOptionMenu');
    });
    
    // 防止页面滚动
    document.addEventListener('touchmove', function(e) {
        e.preventDefault();
    }, { passive: false });

    // 防止双指操作
    document.addEventListener('touchstart', function(e) {
        if (e.touches.length > 1) {
            e.preventDefault();
        }
    }, { passive: false });

    // 防止双击缩放
    var lastTouchEnd = 0;
    document.addEventListener('touchend', function(e) {
        var now = new Date().getTime();
        if (now - lastTouchEnd <= 300) {
            e.preventDefault();
        }
        lastTouchEnd = now;
    }, false);
</script>

</html>