<?php
// 处理获取 UID 的请求
if (isset($_GET['action']) && $_GET['action'] === 'get_uid' && isset($_GET['qq'])) {
    header('Content-Type: application/json');
    $qq = $_GET['qq'];
    $apiUrl = "https://api.aineishe.com/api/v1/Api/secdl/qq_uid?qq=" . urlencode($qq) . "&apikey=faa0e0-e2da7e-41fd6a-d11ee2";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $response = curl_exec($ch);
    curl_close($ch);
    
    echo $response;
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>QQ群链接生成器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 440px;
        }

        /* 公告栏 */
        .notice {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 20px;
            margin-bottom: 16px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
        }

        .notice-header {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 14px;
        }

        .notice-icon {
            width: 32px;
            height: 32px;
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .notice-title {
            font-size: 16px;
            font-weight: 600;
            color: #1a1a2e;
        }

        .notice-list {
            list-style: none;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        .notice-item {
            display: flex;
            align-items: flex-start;
            gap: 10px;
            font-size: 13px;
            color: #4a4a6a;
            line-height: 1.6;
        }

        .notice-item::before {
            content: '';
            width: 6px;
            height: 6px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            margin-top: 7px;
            flex-shrink: 0;
        }

        .notice-item a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
        }

        .notice-item a:hover {
            text-decoration: underline;
        }

        .notice-item strong {
            color: #1a1a2e;
            font-weight: 600;
        }

        /* 主卡片 */
        .card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            padding: 32px 28px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
        }

        .card-header {
            text-align: center;
            margin-bottom: 28px;
        }

        .logo {
            width: 64px;
            height: 64px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 16px;
            box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
        }

        .card-title {
            font-size: 22px;
            font-weight: 700;
            color: #1a1a2e;
            margin-bottom: 6px;
        }

        .card-subtitle {
            font-size: 13px;
            color: #8888a0;
        }

        /* 表单 */
        .form-group {
            margin-bottom: 18px;
        }

        .form-label {
            display: block;
            font-size: 13px;
            font-weight: 600;
            color: #1a1a2e;
            margin-bottom: 8px;
        }

        .input-wrapper {
            position: relative;
        }

        .input-row {
            display: flex;
            gap: 10px;
        }

        input[type="text"] {
            width: 100%;
            padding: 14px 16px;
            background: #f7f7fb;
            border: 2px solid transparent;
            border-radius: 12px;
            font-size: 15px;
            color: #1a1a2e;
            transition: all 0.2s;
        }

        input[type="text"]:focus {
            outline: none;
            background: #fff;
            border-color: #667eea;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.15);
        }

        input[type="text"]::placeholder {
            color: #b0b0c0;
        }

        /* 按钮 */
        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 14px 20px;
            border: none;
            border-radius: 12px;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s;
            font-family: inherit;
        }

        .btn:active {
            transform: scale(0.97);
        }

        .btn-fetch {
            background: #f0f0f8;
            color: #667eea;
            flex-shrink: 0;
            white-space: nowrap;
        }

        .btn-fetch:hover {
            background: #e8e8f4;
        }

        .btn-fetch:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .btn-generate {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #fff;
            font-size: 15px;
            margin-top: 8px;
            box-shadow: 0 8px 24px rgba(102, 126, 234, 0.35);
        }

        .btn-generate:hover {
            transform: translateY(-2px);
            box-shadow: 0 12px 32px rgba(102, 126, 234, 0.45);
        }

        .btn-copy {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
            color: #fff;
            font-size: 14px;
        }

        .btn-copy:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 24px rgba(17, 153, 142, 0.35);
        }

        /* 消息提示 */
        .message {
            display: none;
            align-items: center;
            gap: 10px;
            padding: 14px 16px;
            border-radius: 12px;
            margin-top: 16px;
            font-size: 13px;
            font-weight: 500;
        }

        .message.show {
            display: flex;
        }

        .message.success {
            background: linear-gradient(135deg, rgba(17, 153, 142, 0.1) 0%, rgba(56, 239, 125, 0.1) 100%);
            color: #11998e;
        }

        .message.error {
            background: rgba(245, 87, 108, 0.1);
            color: #f5576c;
        }

        /* 结果框 */
        .result-box {
            display: none;
            margin-top: 20px;
            background: #1a1a2e;
            border-radius: 14px;
            overflow: hidden;
        }

        .result-box.show {
            display: block;
        }

        .result-header {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 12px 16px;
            background: #252542;
        }

        .result-dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
        }

        .result-label {
            color: #8888a0;
            font-size: 12px;
            margin-left: 8px;
        }

        .result-link {
            padding: 16px;
            color: #a0d8ef;
            word-break: break-all;
            font-size: 12px;
            line-height: 1.7;
            font-family: 'Monaco', 'Menlo', monospace;
        }

        .result-footer {
            padding: 14px 16px;
            background: #252542;
        }

        /* Loading */
        .spinner {
            width: 16px;
            height: 16px;
            border: 2px solid rgba(102, 126, 234, 0.2);
            border-radius: 50%;
            border-top-color: #667eea;
            animation: spin 0.6s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        /* 页脚 */
        .footer {
            text-align: center;
            margin-top: 16px;
            color: rgba(255, 255, 255, 0.6);
            font-size: 12px;
        }

        @media (max-width: 480px) {
            .card {
                padding: 24px 20px;
            }
            .input-row {
                flex-direction: column;
            }
            .btn-fetch {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 公告栏 -->
        <div class="notice">
            <div class="notice-header">
                <div class="notice-icon">
                    <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
                        <path d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10z" stroke="#fff" stroke-width="2"/>
                        <path d="M12 8v4m0 4h.01" stroke="#fff" stroke-width="2" stroke-linecap="round"/>
                    </svg>
                </div>
                <span class="notice-title">使用说明</span>
            </div>
            <ul class="notice-list">
                <li class="notice-item">复制链接后请粘贴到浏览器或聊天窗口打开</li>
                <li class="notice-item">若打开页面为空白，请检查当前 QQ 版本是否为 <strong>9.2.90.35975</strong> 或以上</li>
                <li class="notice-item">可前往 <a href="https://im.qq.com" target="_blank">im.qq.com</a> 下载最新版本 QQ</li>
            </ul>
        </div>

        <!-- 主卡片 -->
        <div class="card">
            <div class="card-header">
                <div class="logo">
                    <svg width="32" height="32" viewBox="0 0 24 24" fill="none">
                        <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        <path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                </div>
                <h1 class="card-title">QQ 群链接生成器</h1>
                <p class="card-subtitle">填写信息后一键生成群服务协议跳转链接</p>
            </div>

            <!-- 群号 -->
            <div class="form-group">
                <label class="form-label">群号</label>
                <input type="text" id="groupId" placeholder="请输入群号">
            </div>

            <!-- QQ号 -->
            <div class="form-group">
                <label class="form-label">机器人 QQ 号</label>
                <div class="input-row">
                    <input type="text" id="qqNumber" placeholder="请输入机器人 QQ 号">
                    <button class="btn btn-fetch" id="fetchBtn" onclick="getUid(this)">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
                            <circle cx="11" cy="11" r="8" stroke="currentColor" stroke-width="2"/>
                            <path d="m21 21-4.35-4.35" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
                        </svg>
                        获取 UID
                    </button>
                </div>
            </div>

            <!-- UID -->
            <div class="form-group">
                <label class="form-label">机器人 UID</label>
                <input type="text" id="uid" placeholder="请输入机器人 UID 或点击上方获取">
            </div>

            <button class="btn btn-generate" onclick="generateLink()">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
                    <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
                生成链接
            </button>

            <!-- 消息提示 -->
            <div id="message" class="message"></div>

            <!-- 结果框 -->
            <div id="resultBox" class="result-box">
                <div class="result-header">
                    <span class="result-dot" style="background:#f5576c;"></span>
                    <span class="result-dot" style="background:#ffbd2e;"></span>
                    <span class="result-dot" style="background:#27c93f;"></span>
                    <span class="result-label">生成的链接</span>
                </div>
                <div id="resultLink" class="result-link"></div>
                <div class="result-footer">
                    <button class="btn btn-copy" id="copyBtn" onclick="copyLink()">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
                            <rect x="9" y="9" width="13" height="13" rx="2" stroke="#fff" stroke-width="2"/>
                            <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" stroke="#fff" stroke-width="2"/>
                        </svg>
                        复制链接
                    </button>
                </div>
            </div>
        </div>

        <div class="footer">仅供学习交流使用</div>
    </div>

    <script>
        var generatedLink = '';

        function showMessage(text, type) {
            var msg = document.getElementById('message');
            var icon = type === 'success'
                ? '<svg width="16" height="16" viewBox="0 0 24 24" fill="none"><path d="M20 6 9 17l-5-5" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/></svg>'
                : '<svg width="16" height="16" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/><path d="M12 8v4m0 4h.01" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>';
            msg.innerHTML = icon + ' ' + text;
            msg.className = 'message show ' + type;
            clearTimeout(msg._timer);
            msg._timer = setTimeout(function() {
                msg.className = 'message';
            }, 4000);
        }

        function getUid(btn) {
            var qq = document.getElementById('qqNumber').value.trim();
            if (!qq) {
                showMessage('请先输入机器人 QQ 号', 'error');
                return;
            }

            btn.disabled = true;
            btn.innerHTML = '<span class="spinner"></span> 获取中...';

            var xhr = new XMLHttpRequest();
            xhr.open('GET', '?action=get_uid&qq=' + encodeURIComponent(qq), true);
            xhr.timeout = 15000;
            
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    btn.disabled = false;
                    btn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none"><circle cx="11" cy="11" r="8" stroke="currentColor" stroke-width="2"/><path d="m21 21-4.35-4.35" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg> 获取 UID';
                    
                    if (xhr.status === 200) {
                        try {
                            var data = JSON.parse(xhr.responseText);
                            // API 返回格式: { code: 0, msg: "获取成功", uid: "xxx", qq: "xxx" }
                            if (data.code === 0 && data.uid) {
                                document.getElementById('uid').value = data.uid;
                                showMessage('UID 获取成功', 'success');
                            } else {
                                showMessage(data.msg || '获取失败，请检查 QQ 号', 'error');
                            }
                        } catch(e) {
                            showMessage('解析响应失败，请重试', 'error');
                        }
                    } else {
                        showMessage('请求失败，请重试', 'error');
                    }
                }
            };
            
            xhr.ontimeout = function() {
                btn.disabled = false;
                btn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none"><circle cx="11" cy="11" r="8" stroke="currentColor" stroke-width="2"/><path d="m21 21-4.35-4.35" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg> 获取 UID';
                showMessage('请求超时，请重试', 'error');
            };
            
            xhr.send();
        }

        function generateLink() {
            var groupId  = document.getElementById('groupId').value.trim();
            var qqNumber = document.getElementById('qqNumber').value.trim();
            var uid      = document.getElementById('uid').value.trim();

            if (!groupId)  { showMessage('请填写群号', 'error'); return; }
            if (!qqNumber) { showMessage('请填写机器人 QQ 号', 'error'); return; }
            if (!uid)      { showMessage('请填写机器人 UID', 'error'); return; }

            var payload = JSON.stringify({
                page_name: "ai_group_service_agreement_pop_page",
                groupCode: groupId,
                botUin: qqNumber,
                botUid: uid,
                screen: 1
            });
            
            generatedLink = 'https://club.vip.qq.com/transfer?open_kuikly_info=' + encodeURIComponent(payload);

            document.getElementById('resultLink').textContent = generatedLink;
            document.getElementById('resultBox').className = 'result-box show';
            showMessage('链接生成成功', 'success');
        }

        function copyLink() {
            if (!generatedLink) return;
            var copyBtn = document.getElementById('copyBtn');

            function onCopied() {
                copyBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none"><path d="M20 6 9 17l-5-5" stroke="#fff" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/></svg> 已复制';
                showMessage('链接已复制到剪贴板', 'success');
                setTimeout(function() {
                    copyBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none"><rect x="9" y="9" width="13" height="13" rx="2" stroke="#fff" stroke-width="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" stroke="#fff" stroke-width="2"/></svg> 复制链接';
                }, 2000);
            }

            if (navigator.clipboard && navigator.clipboard.writeText) {
                navigator.clipboard.writeText(generatedLink).then(onCopied).catch(fallbackCopy);
            } else {
                fallbackCopy();
            }

            function fallbackCopy() {
                var ta = document.createElement('textarea');
                ta.value = generatedLink;
                ta.style.cssText = 'position:fixed;opacity:0;';
                document.body.appendChild(ta);
                ta.select();
                try {
                    document.execCommand('copy');
                    onCopied();
                } catch(e) {
                    showMessage('复制失败，请手动复制', 'error');
                }
                document.body.removeChild(ta);
            }
        }
    </script>
</body>
</html>
