QQ互联WIKI分发系统

一套高效、安全的QQ互联WIKI解决方案,支持多应用分发和用户统一管理

系统概述

QQ互联WIKI集成

无缝对接QQ开放平台,支持QQ账号快速登录、用户信息获取等功能,提升用户体验。

多应用分发

一套系统支持多个应用使用QQ登录,统一管理用户数据,方便快捷地扩展业务。

安全可靠

采用先进的加密技术和安全机制,保障用户数据安全,防止信息泄露和恶意攻击。

功能特点

Token安全验证

通过Token进行安全验证,防止非法请求,保障系统安全。

后台管理系统

提供直观易用的后台管理界面,方便配置和管理系统参数。

灵活的回调机制

支持自定义回调地址,满足不同应用场景的需求。

完整的API接口

提供简洁明了的API接口文档,方便开发者集成和使用。

配置说明

系统配置信息

后台管理地址

通过此地址访问系统后台管理界面,进行系统配置和用户管理。

发起登录地址

前端页面通过此地址发起QQ登录请求,需将"后台保存的token"替换为实际在后台生成的token值。

测试回调地址

QQ登录成功后会回调此地址,用于测试和开发阶段验证登录流程。

源码展示

QQ互联WIKI分发系统核心源码
// QQ互联WIKI分发系统 - 核心配置文件 config.php
<?php
// 配置信息
define('QQ_APP_ID', '你的QQ应用AppID');
define('QQ_APP_KEY', '你的QQ应用AppKey');
define('CALLBACK_URL', '域名/demo.php');
define('TOKEN_SECRET', '用于生成token的密钥');
define('ADMIN_PATH', 'ay/manage.php');

// 数据库配置
define('DB_HOST', 'localhost');
define('DB_NAME', 'qq_connect');
define('DB_USER', '数据库用户名');
define('DB_PASS', '数据库密码');

// 生成安全Token
function generateToken($length = 32) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $token = '';
    for ($i = 0; $i < $length; $i++) {
        $token .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $token;
}

// 验证Token
function verifyToken($token) {
    // 这里应该有更复杂的验证逻辑
    return strlen($token) == 32;
}

// 数据库连接
function getDbConnection() {
    try {
        $conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $conn;
    } catch(PDOException $e) {
        die("数据库连接失败: " . $e->getMessage());
    }
}
?>

// QQ互联WIKI分发系统 - API接口文件 api.php
<?php
require_once 'config.php';

// 获取token参数
$token = isset($_GET['token']) ? $_GET['token'] : '';

// 验证token
if (!verifyToken($token)) {
    die('无效的Token');
}

// 获取QQ登录授权页面URL
function getQQAuthUrl($state) {
    $params = array(
        'response_type' => 'code',
        'client_id' => QQ_APP_ID,
        'redirect_uri' => urlencode(CALLBACK_URL),
        'state' => $state,
        'scope' => 'get_user_info'
    );
    return 'https://graph.qq.com/oauth2.0/authorize?' . http_build_query($params);
}

// 生成唯一的state参数,防止CSRF攻击
$state = md5(uniqid(rand(), TRUE));

// 存储state到session或数据库,用于回调时验证
$conn = getDbConnection();
$stmt = $conn->prepare("INSERT INTO qq_auth_states (state, token, created_at) VALUES (:state, :token, NOW())");
$stmt->execute(array(':state' => $state, ':token' => $token));

// 跳转到QQ登录页面
header('Location: ' . getQQAuthUrl($state));
exit;
?>

// QQ互联WIKI分发系统 - 回调处理文件 demo.php
<?php
require_once 'config.php';

// 获取授权码和state
$code = isset($_GET['code']) ? $_GET['code'] : '';
$state = isset($_GET['state']) ? $_GET['state'] : '';

// 验证state
$conn = getDbConnection();
$stmt = $conn->prepare("SELECT * FROM qq_auth_states WHERE state = :state AND created_at >= DATE_SUB(NOW(), INTERVAL 30 MINUTE)");
$stmt->execute(array(':state' => $state));
$authState = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$authState) {
    die('无效的state参数');
}

// 获取AccessToken
function getAccessToken($code) {
    $params = array(
        'grant_type' => 'authorization_code',
        'client_id' => QQ_APP_ID,
        'client_secret' => QQ_APP_KEY,
        'code' => $code,
        'redirect_uri' => urlencode(CALLBACK_URL)
    );
    
    $url = 'https://graph.qq.com/oauth2.0/token?' . http_build_query($params);
    $response = file_get_contents($url);
    
    if (strpos($response, 'access_token') === false) {
        return false;
    }
    
    parse_str($response, $output);
    return isset($output['access_token']) ? $output['access_token'] : false;
}

// 获取OpenID
function getOpenID($accessToken) {
    $url = 'https://graph.qq.com/oauth2.0/me?access_token=' . $accessToken;
    $response = file_get_contents($url);
    
    // 解析JSONP格式的响应
    if (preg_match('/callback\((.*)\)/', $response, $matches)) {
        $jsonData = json_decode($matches[1], true);
        return isset($jsonData['openid']) ? $jsonData['openid'] : false;
    }
    
    return false;
}

// 获取用户信息
function getUserInfo($accessToken, $openID) {
    $params = array(
        'access_token' => $accessToken,
        'oauth_consumer_key' => QQ_APP_ID,
        'openid' => $openID
    );
    
    $url = 'https://graph.qq.com/user/get_user_info?' . http_build_query($params);
    $response = file_get_contents($url);
    return json_decode($response, true);
}

// 获取AccessToken
$accessToken = getAccessToken($code);
if (!$accessToken) {
    die('获取AccessToken失败');
}

// 获取OpenID
$openID = getOpenID($accessToken);
if (!$openID) {
    die('获取OpenID失败');
}

// 获取用户信息
$userInfo = getUserInfo($accessToken, $openID);

// 在这里可以根据token参数将用户信息返回给对应的应用
// 也可以将用户信息存储到数据库中

// 输出用户信息(仅用于测试)
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>QQ登录测试 - 用户信息</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .container { max-width: 600px; margin: 0 auto; }
        .card { border: 1px solid #ddd; border-radius: 5px; padding: 20px; margin-bottom: 20px; }
        .avatar { width: 100px; height: 100px; border-radius: 50%; }
        .info { margin-top: 15px; }
        .info p { margin: 8px 0; }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <h3>QQ登录测试 - 用户信息</h3>
            <div class="info">
                <p><strong>昵称:</strong> <?php echo $userInfo['nickname']; ?></p>
                <p><strong>性别:</strong> <?php echo $userInfo['gender']; ?></p>
                <p><strong>头像:</strong> <img src="<?php echo $userInfo['figureurl_qq_2']; ?>" class="avatar" /></p>
                <p><strong>OpenID:</strong> <?php echo $openID; ?></p>
                <p><strong>Token:</strong> <?php echo $authState['token']; ?></p>
            </div>
        </div>
    </div>
</body>
</html>

// QQ互联WIKI分发系统 - 后台管理文件 manage.php (简化版)
<?php
require_once 'config.php';

// 简单的登录验证(实际应用中应该有更完善的验证机制)
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $username = isset($_POST['username']) ? $_POST['username'] : '';
        $password = isset($_POST['password']) ? $_POST['password'] : '';
        
        // 实际应用中应该从数据库验证用户名和密码
        if ($username == 'admin' && $password == 'password') {
            $_SESSION['admin_logged_in'] = true;
            header('Location: manage.php');
            exit;
        } else {
            $error = '用户名或密码错误';
        }
    }
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>QQ互联WIKI分发系统 - 登录</title>
    </head>
    <body>
        <h2>QQ互联WIKI分发系统 - 登录</h2>
        <?php if (isset($error)) { echo '<p style="color: red;">'.$error.'</p>'; } ?>
        <form method="post">
            <p>用户名: <input type="text" name="username" /></p>
            <p>密码: <input type="password" name="password" /></p>
            <p><input type="submit" value="登录" /></p>
        </form>
    </body>
    </html>
    <?php
    exit;
}

// 生成新Token
if (isset($_POST['generate_token'])) {
    $newToken = generateToken();
    $conn = getDbConnection();
    $stmt = $conn->prepare("INSERT INTO qq_tokens (token, created_at) VALUES (:token, NOW())");
    $stmt->execute(array(':token' => $newToken));
    $success = '新Token已生成: ' . $newToken;
}

// 获取所有Tokens
$conn = getDbConnection();
$stmt = $conn->prepare("SELECT * FROM qq_tokens ORDER BY created_at DESC");
$stmt->execute();
$tokens = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>QQ互联WIKI分发系统 - 后台管理</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .container { max-width: 900px; margin: 0 auto; }
        .header { background-color: #f1f1f1; padding: 10px; margin-bottom: 20px; }
        .card { border: 1px solid #ddd; border-radius: 5px; padding: 20px; margin-bottom: 20px; }
        .table { width: 100%; border-collapse: collapse; }
        .table th, .table td { border: 1px solid #ddd; padding: 8px; }
        .table th { background-color: #f2f2f2; }
        .success { color: green; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>QQ互联WIKI分发系统 - 后台管理</h1>
            <a href="?logout=1">退出登录</a>
        </div>
        
        <?php if (isset($success)) { echo '<p class="success">'.$success.'</p>'; } ?>
        
        <div class="card">
            <h3>生成新Token</h3>
            <form method="post">
                <input type="submit" name="generate_token" value="生成新Token" />
            </form>
        </div>
        
        <div class="card">
            <h3>已生成的Tokens</h3>
            <table class="table">
                <tr>
                    <th>ID</th>
                    <th>Token</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
                <?php foreach ($tokens as $token) { ?>
                <tr>
                    <td><?php echo $token['id']; ?></td>
                    <td><?php echo $token['token']; ?></td>
                    <td><?php echo $token['created_at']; ?></td>
                    <td><a href="#" onclick="copyToClipboard('<?php echo $token['token']; ?>'); return false;">复制</a></td>
                </tr>
                <?php } ?>
            </table>
        </div>
    </div>
    
    <script>
        function copyToClipboard(text) {
            var dummy = document.createElement("textarea");
            document.body.appendChild(dummy);
            dummy.value = text;
            dummy.select();
            document.execCommand("copy");
            document.body.removeChild(dummy);
            alert("已复制到剪贴板");
        }
    </script>
</body>
</html>
                        

测试演示

测试登录流程

点击下方按钮测试QQ登录流程,此演示将引导你完成完整的QQ登录过程,并展示获取到的用户信息。

API调用示例

以下是如何在前端页面中集成QQ登录的示例代码,你可以参考此代码在自己的应用中实现QQ登录功能。

<a href="域名/api.php?token=你的token">
    <img src="https://qzonestyle.gtimg.cn/qzone/v8/img/connect/qq_connect_logo_1.png" alt="QQ登录" />
</a>