addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); }); async function handleRequest(request) { const url = new URL(request.url); // 处理 CORS 预检请求 if (request.method === 'OPTIONS') { return new Response(null, { headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', }, }); } // 处理 POST 请求 if (request.method === 'POST' && url.pathname === '/recognize') { try { const { token, imageId } = await request.json(); if (!token || !imageId) { return new Response(JSON.stringify({ error: 'Missing token or imageId' }), { status: 400, headers: { 'Content-Type': 'application/json' }, }); } // 调用 QwenLM API const response = await fetch('https://chat.qwenlm.ai/api/chat/completions', { method: 'POST', headers: { 'accept': '*/*', 'authorization': `Bearer ${token}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ stream: false, model: 'qwen-vl-max-latest', messages: [ { role: 'user', content: [ { type: 'text', text: '请严格只返回图片中的内容,不要添加任何解释、描述或多余的文字' }, { type: 'image', image: imageId }, // 使用上传后的图片 ID ], }, ], session_id: '1', chat_id: '2', id: '3', }), }); const data = await response.json(); return new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, }); } catch (error) { return new Response(JSON.stringify({ error: 'Internal Server Error' }), { status: 500, headers: { 'Content-Type': 'application/json' }, }); } } // 返回前端界面 return new Response(getHTML(), { headers: { 'Content-Type': 'text/html' }, }); } function getHTML() { return ` 智能图片识别

智能图片识别

📸
拖拽图片到这里,或点击上传
支持复制粘贴图片
识别结果

识别历史

`; }