getApigInfo.md 3.7 KB

getApigInfo 云函数

功能说明

通过 APIG objectId 查询 APIG 接口详情(标题、描述、套餐列表、当前余额等)。 用于支付页面加载接口信息,替代直接查询 Parse APIG 类。

参数

参数名 必填 说明
apigId APIG 表的 objectId

返回格式

{
  "code": 200,
  "success": true,
  "data": {
    "objectId": "Vo3ROWEvDy",
    "title": "国内外社媒数据中台",
    "content": "由未来飞马提供的电商数据中台相关接口",
    "count": 99999,
    "price": "0.1",
    "cost": 0.1,
    "priceStep": [
      { "count": 1000, "price": 100 },
      { "count": 26000, "price": 2500 },
      { "count": 60000, "price": 5000 }
    ],
    "service": "Fmode",
    "type": "AIGC",
    "path": "/apig/voc-social",
    "isCached": true
  }
}

云函数代码

async function handler(request, response) {
  console.log('🚀 执行 getApigInfo 云函数...');
  
  try {
    // 1. 获取参数 (兼容 params / body 两种方式)
    let apigId = null;
    if (request.params && request.params.apigId) apigId = request.params.apigId;
    else if (request.body && request.body.apigId) apigId = request.body.apigId;
    
    console.log('apigId:', apigId);

    // 2. 参数校验
    if (!apigId) {
      response.json({ code: 400, success: false, error: '缺少必需参数 apigId' });
      return;
    }

    // 3. 查询 APIG 表 (使用 SELECT * 避免列名问题)
    const sql = `SELECT * FROM "APIG" WHERE "objectId" = $1 LIMIT 1`;
    const result = await Psql.query(sql, [apigId]);
    
    console.log('查询结果数量:', result ? result.length : 0);

    // 4. 检查结果
    if (!result || result.length === 0) {
      response.json({ code: 404, success: false, error: '未找到对应的 APIG 记录' });
      return;
    }

    // 5. 格式化数据 — 直接返回行数据,前端自行解析
    const row = result[0];
    
    // 处理 priceStep (可能是 JSON string 或已解析的对象)
    let priceStep = row.priceStep;
    if (typeof priceStep === 'string') {
      try { priceStep = JSON.parse(priceStep); } catch(e) { priceStep = []; }
    }

    const data = {
      objectId: row.objectId,
      title: row.title || '',
      content: row.content || '',
      count: row.count || 0,
      price: row.price || '0',
      cost: row.cost || 0,
      priceStep: priceStep || [],
      service: row.service || '',
      type: row.type || '',
      path: row.path || '',
      isCached: row.isCached || false
    };

    console.log('返回数据:', JSON.stringify(data).substring(0, 200));

    // 6. 返回结果
    response.json({ 
      code: 200, 
      success: true, 
      data: data 
    });

  } catch (error) {
    console.error('❌ getApigInfo 执行失败:', error.message, error.stack);
    response.json({ code: 500, success: false, error: error.message });
  }
}

云函数部署说明

  • 云函数名称: getApigInfo
  • 功能: 通过 APIG objectId 查询接口详情
  • 调用方式: POST https://server.fmode.cn/parse/functions/getApigInfo
  • 请求体:

    {
    "_ApplicationId": "ncloudmaster",
    "apigId": "Vo3ROWEvDy"
    }
    

前端调用示例

// 方式一:直接 fetch
const resp = await fetch('https://server.fmode.cn/parse/functions/getApigInfo', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    _ApplicationId: 'ncloudmaster',
    apigId: 'Vo3ROWEvDy'
  })
});
const data = await resp.json();
// data.result => { code: 200, success: true, data: { ... } }

// 方式二:FmodeParse
const result = await Parse.Cloud.function({
  id: '云函数ID',
  apigId: 'Vo3ROWEvDy'
});