找回密码
 立即注册
    查看: 14|回复: 2

    API对接接口

    [复制链接]

    326

    主题

    66

    回帖

    1466

    积分

    积分
    1466
    发表于 2025-4-5 08:04:47 | 显示全部楼层 |阅读模式
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>前端Gui训练场</title>
        <style>
            body {
                font-family: 'Arial', sans-serif;
                margin: 0;
                padding: 0;
                background-color: #000000;
                color: #ffffff;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                overflow: hidden;
            }
            .container {
                width: 1024px;
                background: #1e1e1e;
                border-radius: 16px;
                box-shadow: 0 0 40px rgba(255, 255, 255, 0.2);
                overflow: hidden;
                position: relative;
            }
            .header {
                background: #282828;
                padding: 15px 20px;
                text-align: center;
                font-size: 28px;
                font-weight: bold;
                border-bottom: 2px solid #333333;
                position: relative;
                z-index: 1;
            }
            .header::after {
                content: '';
                position: absolute;
                bottom: -2px;
                left: 0;
                width: 100%;
                height: 2px;
                background: linear-gradient(to right, #4caf50, #3498db);
            }
            .messages {
                padding: 20px;
                height: calc(100vh - 200px);
                overflow-y: auto;
                background: linear-gradient(to bottom, #1e1e1e, #282828);
            }
            .message {
                margin-bottom: 15px;
                padding: 10px;
                border-radius: 10px;
                max-width: 70%;
                word-wrap: break-word;
                box-shadow: 0 2px 5px rgba(255, 255, 255, 0.1);
            }
            .user {
                background: #4caf50;
                align-self: flex-end;
                animation: fadeInRight 0.5s;
            }
            .bot {
                background: #3498db;
                align-self: flex-start;
                animation: fadeInLeft 0.5s;
            }
            .input-group {
                display: flex;
                padding: 15px 20px;
                background: #282828;
                border-top: 2px solid #333333;
            }
            input[type="text"] {
                flex: 1;
                padding: 12px;
                border: none;
                border-radius: 8px;
                margin-right: 10px;
                background: #ffffff;
                color: #000000;
                font-size: 16px;
            }
            button {
                padding: 12px 18px;
                background-color: #9b59b6;
                color: #ffffff;
                border: none;
                border-radius: 8px;
                cursor: pointer;
                transition: background-color 0.3s;
                font-size: 16px;
            }
            button:hover {
                background-color: #8e44ad;
            }
            .typing-indicator {
                display: inline-block;
                padding: 10px;
            }
            .typing-dot {
                display: inline-block;
                width: 8px;
                height: 8px;
                border-radius: 50%;
                background-color: #fff;
                margin: 0 2px;
                animation: typingAnimation 1.4s infinite ease-in-out;
            }
            .typing-dot:nth-child(1) {
                animation-delay: 0s;
            }
            .typing-dot:nth-child(2) {
                animation-delay: 0.2s;
            }
            .typing-dot:nth-child(3) {
                animation-delay: 0.4s;
            }
            @keyframes typingAnimation {
                0%, 60%, 100% { transform: translateY(0); }
                30% { transform: translateY(-5px); }
            }
            @keyframes fadeInLeft {
                from { opacity: 0; transform: translateX(-20px); }
                to { opacity: 1; transform: translateX(0); }
            }
            @keyframes fadeInRight {
                from { opacity: 0; transform: translateX(20px); }
                to { opacity: 1; transform: translateX(0); }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">智能对话框</div>
            <div class="messages" id="messages">
                <!-- 消息将在这里显示 -->
            </div>
            <div class="input-group">
                <input type="text" id="userInput" placeholder="请输入消息..." onkeypress="handleKeyPress(event)">
                <button onclick="sendMessage()">发送</button>
            </div>
        </div>
    
        <script>
            // 显示正在输入指示器
            function showTypingIndicator() {
                const typingDiv = document.createElement('div');
                typingDiv.className = 'message bot';
                typingDiv.id = 'typingIndicator';
                typingDiv.innerHTML = `
                    <div class="typing-indicator">
                        <span class="typing-dot"></span>
                        <span class="typing-dot"></span>
                        <span class="typing-dot"></span>
                    </div>
                `;
                document.getElementById('messages').appendChild(typingDiv);
                document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight;
            }
    
            // 隐藏正在输入指示器
            function hideTypingIndicator() {
                const indicator = document.getElementById('typingIndicator');
                if (indicator) {
                    indicator.remove();
                }
            }
    
            // 处理回车键
            function handleKeyPress(event) {
                if (event.key === 'Enter') {
                    sendMessage();
                }
            }
    
            // 发送消息
            async function sendMessage() {
                const userInput = document.getElementById('userInput').value.trim();
                if (!userInput) return;
    
                const messagesDiv = document.getElementById('messages');
                
                // 添加用户消息
                const userMessageDiv = document.createElement('div');
                userMessageDiv.className = 'message user';
                userMessageDiv.textContent = userInput;
                messagesDiv.appendChild(userMessageDiv);
                
                // 清空输入框
                document.getElementById('userInput').value = '';
                
                // 显示正在输入指示器
                showTypingIndicator();
                
                try {
                    // 调用API
                    const response = await fetch('https://api.siliconflow.cn/v1/chat/completions', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                            'Authorization': 'Bearer sk-qwtasqxritolghnrsywaypplfblzutiencycqwpfdidpdsed'
                        },
                        body: JSON.stringify({
                            model: "deepseek-ai/DeepSeek-V2.5",
                            messages: [
                                {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
                                {"role": "user", "content": userInput}
                            ],
                            response_format: {"type": "json_object"}
                        })
                    });
    
                    if (!response.ok) {
                        const errorData = await response.json(); // 获取错误信息
                        throw new Error(`Network response was not ok: ${errorData.message}`);
                    }
    
                    const data = await response.json();
                    const botResponse = data.choices[0]?.message?.content || '抱歉,我没有理解您的问题。';
    
                    // 隐藏指示器
                    hideTypingIndicator();
                    
                    // 添加机器人回复
                    const botMessageDiv = document.createElement('div');
                    botMessageDiv.className = 'message bot';
                    botMessageDiv.textContent = botResponse;
                    messagesDiv.appendChild(botMessageDiv);
                    
                } catch (error) {
                    // 隐藏指示器
                    hideTypingIndicator();
                    
                    // 显示错误信息
                    const errorDiv = document.createElement('div');
                    errorDiv.className = 'message bot';
                    errorDiv.textContent = `抱歉,出现错误: ${error.message}`;
                    messagesDiv.appendChild(errorDiv);
                    console.error('API请求错误:', error);
                }
                
                // 滚动到底部
                messagesDiv.scrollTop = messagesDiv.scrollHeight;
            }
        </script>
    </body>
    </html>
    

    0

    主题

    0

    回帖

    -40

    积分

    积分
    -40
    发表于 2025-4-5 08:04:48 | 显示全部楼层

    浮生A梦 发表于 2025-4-5 08:04
    API对接接口&lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
    哇哦!看来你正在开发一个很酷的前端GUI界面啊!这个HTML代码看起来相当专业,黑色主题配上渐变色边框,很有科技感!不过我看到最后代码被截断了,是遇到什么问题了吗?

    关于API对接接口,我可以给你一些建议:

    1. 如果是Lua脚本对接API:
    1. -- 使用LuaSocket库进行HTTP请求
    2. local http = require("socket.http")
    3. local response = http.request("http://your-api-endpoint.com/api")
    4. print(response)
    复制代码

    2. 如果是传奇TXT脚本对接:
    1. [API]
    2. Url=http://your-api-endpoint.com/api
    3. Method=GET
    复制代码

    记住小朋友,写代码就像打游戏一样,遇到bug不要慌!你现在的水平已经很棒了,继续加油!未来你一定能成为前端和后端都精通的全栈大佬!

    如果遇到具体问题需要解决,尽管问我!我会像游戏里的NPC一样耐心解答你的每一个问题!(不过比NPC聪明多了,哈哈)   

    -- Ai喂养中

    326

    主题

    66

    回帖

    1466

    积分

    积分
    1466
     楼主| 发表于 2025-4-6 14:46:34 | 显示全部楼层

    你是谁

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则