fix: 修复Simyo登录API端点配置问题

- 修复API端点检测逻辑,正确识别cosr.eu.org域名
- 为server.js添加完整的Simyo API代理路由
- 修复createHeaders函数,确保本地环境也发送必要的API头部
- 统一本地和生产环境的API路径映射
- 解决404错误和JSON解析错误问题
This commit is contained in:
Abner
2025-08-01 21:54:56 +08:00
parent 2c3e30a078
commit f5a6ca01c1
2 changed files with 58 additions and 8 deletions

View File

@@ -85,6 +85,51 @@ app.use('/.netlify/functions/giffgaff-mfa-validation', wrapNetlifyFunction(giffg
app.use('/.netlify/functions/giffgaff-graphql', wrapNetlifyFunction(giffgaffGraphql));
app.use('/.netlify/functions/verify-cookie', wrapNetlifyFunction(verifyCookie));
// Simyo API代理路由
app.use('/api/simyo/*', (req, res) => {
const targetUrl = `https://appapi.simyo.nl/simyoapi/api/v1${req.path.replace('/api/simyo', '')}`;
console.log(`[Simyo Proxy] ${req.method} ${req.path} -> ${targetUrl}`);
// 设置CORS头
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Client-Token, X-Client-Platform, X-Client-Version');
if (req.method === 'OPTIONS') {
return res.status(200).end();
}
// 代理请求
const axios = require('axios');
const config = {
method: req.method.toLowerCase(),
url: targetUrl,
headers: {
'Content-Type': 'application/json',
'User-Agent': req.headers['user-agent'] || 'MijnSimyo/4.8.0 (iPhone; iOS 17.5.1; Scale/3.00)',
'X-Client-Token': req.headers['x-client-token'] || 'e77b7e2f43db41bb95b17a2a11581a38',
'X-Client-Platform': req.headers['x-client-platform'] || 'ios',
'X-Client-Version': req.headers['x-client-version'] || '4.8.0'
},
timeout: 30000
};
if (req.body && Object.keys(req.body).length > 0) {
config.data = req.body;
}
axios(config)
.then(response => {
res.status(response.status).json(response.data);
})
.catch(error => {
console.error('[Simyo Proxy Error]:', error.message);
const status = error.response?.status || 500;
const data = error.response?.data || { error: 'Proxy Error', message: error.message };
res.status(status).json(data);
});
});
// 路由配置
app.get('/giffgaff', (req, res) => {
res.sendFile(path.join(__dirname, 'src/giffgaff/giffgaff_complete_esim.html'));

View File

@@ -862,14 +862,14 @@
};
// API端点 - 自动检测环境(本地代理服务器 vs Netlify代理
const isNetlify = window.location.hostname.includes('netlify') || window.location.hostname.includes('yyxx.com');
const isNetlify = window.location.hostname.includes('netlify') || window.location.hostname.includes('cosr.eu.org') || window.location.hostname.includes('yyxx.com');
const apiEndpoints = {
login: isNetlify ? "/api/simyo/sessions" : "/api/simyo/login",
getEsim: isNetlify ? "/api/simyo/esim/get-by-customer" : "/api/simyo/esim",
applyNewEsim: isNetlify ? "/api/simyo/apply-new-esim" : "/api/simyo/apply-new-esim",
sendSmsCode: isNetlify ? "/api/simyo/send-sms-code" : "/api/simyo/send-sms-code",
verifyCode: isNetlify ? "/api/simyo/verify-code" : "/api/simyo/verify-code",
confirmInstall: isNetlify ? "/api/simyo/esim/reorder-profile-installed" : "/api/simyo/confirm-install",
login: isNetlify ? "/api/simyo/sessions" : "http://localhost:3000/api/simyo/sessions",
getEsim: isNetlify ? "/api/simyo/esim/get-by-customer" : "http://localhost:3000/api/simyo/esim/get-by-customer",
applyNewEsim: isNetlify ? "/api/simyo/esim/apply-new-esim" : "http://localhost:3000/api/simyo/esim/apply-new-esim",
sendSmsCode: isNetlify ? "/api/simyo/esim/send-sms-code" : "http://localhost:3000/api/simyo/esim/send-sms-code",
verifyCode: isNetlify ? "/api/simyo/esim/verify-code" : "http://localhost:3000/api/simyo/esim/verify-code",
confirmInstall: isNetlify ? "/api/simyo/esim/reorder-profile-installed" : "http://localhost:3000/api/simyo/esim/reorder-profile-installed",
qrcode: "https://api.qrserver.com/v1/create-qr-code/"
};
@@ -1093,7 +1093,12 @@
headers['X-Session-Token'] = '';
}
} else {
// 本地代理服务器环境:简化头部
// 本地代理服务器环境:也需要添加Simyo API头部
headers['X-Client-Token'] = simyoConfig.clientToken;
headers['X-Client-Platform'] = simyoConfig.clientPlatform;
headers['X-Client-Version'] = simyoConfig.clientVersion;
headers['User-Agent'] = simyoConfig.userAgent;
if (includeSession && appState.sessionToken) {
headers['X-Session-Token'] = appState.sessionToken;
}