/** * 测试 errorHandling 条件匹配 + 弹充值网址逻辑 */ const fs = require('fs'); const path = require('path'); // 加载 api-config.json const config = JSON.parse( fs.readFileSync(path.join(__dirname, '..', 'jimeng', 'jimeng-img-v4', 'api-config.json'), 'utf-8') ); // 条件匹配函数(同 skill-executor.js) function matchesErrorConditions(resp, errorDef) { if (!errorDef || !errorDef.conditions) return false; const results = errorDef.conditions.map(cond => { const v = resp[cond.responseField]; if (v == null) return false; if (cond.operator === 'in') return Array.isArray(cond.value) && cond.value.includes(v); if (cond.operator === 'contains' && typeof v === 'string') return cond.value.some(k => v.toLowerCase().includes(k.toLowerCase())); return false; }); return errorDef.matchMode === 'all' ? results.every(Boolean) : results.some(Boolean); } // 模板变量替换 function resolveTemplate(tpl, vars) { return tpl.replace(/\{\{(\w+)\}\}/g, (_, key) => vars[key] || ''); } // 模拟各种 API 响应 const testCases = [ { code: -2, msg: '余额不足' }, { code: -3, msg: 'quota exceeded' }, { code: 402, msg: 'insufficient balance' }, { code: 429, msg: 'rate limit' }, { code: -10, message: 'balance not enough' }, { code: 200, msg: 'ok' }, { code: 500, msg: 'server error' }, { code: 401, msg: 'unauthorized token' }, ]; console.log('=== 余额不足检测 测试 ===\n'); const balanceDef = config.errorHandling.balanceInsufficient; const unauthDef = config.errorHandling.unauthorized; testCases.forEach(resp => { const isBalance = matchesErrorConditions(resp, balanceDef); const isUnauth = matchesErrorConditions(resp, unauthDef); const label = isBalance ? '⚡ 触发充值' : isUnauth ? '🔒 触发授权' : '✅ 正常放行'; console.log(` code=${String(resp.code).padStart(4)} msg="${resp.msg || resp.message || ''}" => ${label}`); }); // 演示生成充值 URL console.log('\n=== 生成充值网址 ===\n'); const handler = config.tokenConfig.onBalanceInsufficient; const url = resolveTemplate(handler.qrCodeUrl, { user: 'E4KpGvTEto', apigid: 'Vo3ROWEvDy' }); console.log(' URL:', url); console.log(' title:', handler.title); console.log(' message:', handler.message);