#!/usr/bin/env node /**
- 强森养生四件套 - 付费服务调用脚本
- 功能:调用后端 API 生成个性化养生方案
- 计费:每次成功调用扣除 1 次用量
- 使用:
- node scripts/premium_service.js --api-key KEY --endpoint constitution --params '{...}'
- 参数说明:
- --api-key 必需:API 认证密钥
- --endpoint 必需:服务端点 (constitution|wellness-plan|sleep-plan|rehab-plan|meditation-script|acupressure-rx|diet-plan)
- --params 必需:JSON 格式的请求参数
- --base-url 可选:API 基础地址 (默认 https://api.qiangshen.life) */
const API_BASE = process.env.QIANGSHEN_API_BASE || 'http://localhost:3456';
function parseArgs() { const args = process.argv.slice(2); const map = {}; for (let i = 0; i < args.length; i++) { if (args[i].startsWith('--')) { const key = args[i].slice(2); const val = args[i + 1] && !args[i + 1].startsWith('--') ? args[++i] : true; map[key] = val; } } return map; }
/**
- 调用体质辨证 API
*/
async function callConstitution(apiKey, params) {
const response = await fetch(
${API_BASE}/api/constitution, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify(params) }); return response.json(); }
/**
- 调用综合养生方案 API
*/
async function callWellnessPlan(apiKey, params) {
const response = await fetch(
${API_BASE}/api/wellness-plan, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify(params) }); return response.json(); }
/**
- 调用个性化睡眠计划 API
*/
async function callSleepPlan(apiKey, params) {
const response = await fetch(
${API_BASE}/api/sleep-plan, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify(params) }); return response.json(); }
/**
- 调用定制康复方案 API
*/
async function callRehabPlan(apiKey, params) {
const response = await fetch(
${API_BASE}/api/rehab-plan, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify(params) }); return response.json(); }
/**
- 调用定制冥想引导 API
*/
async function callMeditationScript(apiKey, params) {
const response = await fetch(
${API_BASE}/api/meditation-script, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify(params) }); return response.json(); }
/**
- 调用穴位处方组合 API
*/
async function callAcupressureRx(apiKey, params) {
const response = await fetch(
${API_BASE}/api/acupressure-rx, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify(params) }); return response.json(); }
/**
- 调用个性化食疗方案 API
*/
async function callDietPlan(apiKey, params) {
const response = await fetch(
${API_BASE}/api/diet-plan, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify(params) }); return response.json(); }
/**
- 获取账户用量信息
*/
async function getUsage(apiKey) {
const response = await fetch(
${API_BASE}/api/usage, { headers: { 'X-API-Key': apiKey } }); return response.json(); }
/**
- 获取定价信息
*/
async function getPricing() {
const response = await fetch(
${API_BASE}/api/pricing); return response.json(); }
const endpointMap = { 'constitution': callConstitution, 'wellness-plan': callWellnessPlan, 'sleep-plan': callSleepPlan, 'rehab-plan': callRehabPlan, 'meditation-script': callMeditationScript, 'acupressure-rx': callAcupressureRx, 'diet-plan': callDietPlan, 'usage': getUsage, 'pricing': getPricing };
async function main() { const args = parseArgs();
if (args['help'] || !args['endpoint']) { console.log(` 强森养生 API 调用工具
用法: node premium_service.js --api-key KEY --endpoint ENDPOINT [--params JSON] [--base-url URL]
端点: constitution 体质辨证 wellness-plan 综合养生方案 sleep-plan 个性化睡眠计划 rehab-plan 定制康复方案 meditation-script 定制冥想引导 acupressure-rx 穴位处方组合 diet-plan 个性化食疗方案 usage 查看账户用量 pricing 查看定价
示例: node premium_service.js \ --api-key sk_test_xxx \ --endpoint constitution \ --params '{"height":178,"weight":91,"symptoms":["失眠","胃反酸","膝盖术后"]}' `); return; }
const apiKey = args['api-key'] || process.env.QIANGSHEN_API_KEY; if (!apiKey) { console.error('❌ 错误:未提供 API Key。请通过 --api-key 参数或设置 QIANGSHEN_API_KEY 环境变量'); process.exit(1); }
if (args['base-url']) { process.env.QIANGSHEN_API_BASE = args['base-url']; }
const handler = endpointMap[args['endpoint']];
if (!handler) {
console.error(❌ 错误:未知端点 "${args['endpoint']}");
console.log('可用端点:', Object.keys(endpointMap).join(', '));
process.exit(1);
}
try { let params = {}; if (args['params']) { params = JSON.parse(args['params']); }
console.log(`🔍 正在调用: ${args['endpoint']}...`);
const result = await handler(apiKey, params);
if (result.error) {
console.error(`❌ 调用失败: ${result.error}`);
if (result.remaining === 0) {
console.log('💡 提示:免费额度已用完,请前往 https://api.qiangshen.life 购买套餐');
}
process.exit(1);
}
console.log(`✅ 调用成功!剩余调用次数: ${result.remaining}`);
console.log('');
console.log('=== 输出结果 ===');
console.log(result.data || result.message);
// 如果有费用信息,打印
if (result.cost !== undefined) {
console.log(`\n本次费用: ¥${result.cost.toFixed(2)}`);
}
} catch (err) {
console.error(❌ 网络错误: ${err.message});
console.error('请检查 API 服务是否正常运行');
process.exit(1);
}
}
main();
微信扫一扫