修复二维码生成器问题
This commit is contained in:
17
js/common.js
17
js/common.js
@@ -24,7 +24,6 @@ function isLoggedIn() {
|
||||
|
||||
// ========== API 请求封装 ==========
|
||||
async function apiRequest(endpoint, options = {}) {
|
||||
// 使用 window.API_BASE,如果未定义则使用默认值
|
||||
const baseUrl = window.API_BASE || '/api/v1';
|
||||
const url = endpoint.startsWith('http') ? endpoint : `${baseUrl}${endpoint}`;
|
||||
|
||||
@@ -52,11 +51,21 @@ async function apiRequest(endpoint, options = {}) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
// 先获取响应文本,用于调试
|
||||
const text = await response.text();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.detail || data.message || '请求失败');
|
||||
let errorMsg;
|
||||
try {
|
||||
const errorData = JSON.parse(text);
|
||||
errorMsg = errorData.detail || errorData.message || JSON.stringify(errorData);
|
||||
} catch (e) {
|
||||
errorMsg = text || '请求失败';
|
||||
}
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
return data;
|
||||
|
||||
return JSON.parse(text);
|
||||
} catch (error) {
|
||||
console.error('API 请求错误:', error);
|
||||
throw error;
|
||||
|
||||
@@ -47,19 +47,20 @@ async function generateQR() {
|
||||
return;
|
||||
}
|
||||
|
||||
const size = document.getElementById('size').value;
|
||||
const size = parseInt(document.getElementById('size').value);
|
||||
|
||||
try {
|
||||
const data = await apiRequest('/qrcode/generate', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ content, size: parseInt(size) })
|
||||
body: JSON.stringify({ content: content, size: size })
|
||||
});
|
||||
|
||||
currentQRCode = data.qr_code;
|
||||
document.getElementById('qrContainer').innerHTML = `<img src="${data.qr_code}" alt="二维码" class="max-w-full mx-auto">`;
|
||||
document.getElementById('downloadBtn').classList.remove('hidden');
|
||||
} catch (error) {
|
||||
showToast(error.message, 'error');
|
||||
console.error('生成失败:', error);
|
||||
showToast('生成失败: ' + (error.message || '未知错误'), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user