getApigAuth.md 2.9 KB

getApigAuth 云函数

功能说明

通过 userId 和 APIG objectId 查询用户的 APIGAuth 授权记录(余额、使用情况等)。 用于 OpenClaw 检查用户是否有额度、支付页面显示当前余额。

参数

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

返回格式

{
  "code": 200,
  "success": true,
  "data": {
    "objectId": "authObjectId",
    "user": "userId",
    "apig": "Vo3ROWEvDy",
    "count": 1000,
    "totalRecharge": 2000,
    "createdAt": "2026-01-01T00:00:00.000Z",
    "updatedAt": "2026-04-02T00:00:00.000Z"
  }
}

如果用户没有授权记录,返回:

{
  "code": 200,
  "success": true,
  "data": null
}

云函数代码

async function handler(request, response) {
  console.log('🚀 执行 getApigAuth 云函数...');
  
  try {
    // 1. 获取参数
    let userId = null;
    let apigId = null;
    if (request.params) {
      userId = request.params.userId;
      apigId = request.params.apigId;
    }
    if (!userId && request.body) userId = request.body.userId;
    if (!apigId && request.body) apigId = request.body.apigId;
    
    // 2. 参数校验
    if (!userId || !apigId) {
      response.json({ code: 400, success: false, error: '缺少必需参数 userId 或 apigId' });
      return;
    }

    // 3. 查询 APIGAuth 表 — 通过 user 和 apig 字段查找
    const sql = `
      SELECT 
        a."objectId",
        a."count",
        a."createdAt",
        a."updatedAt",
        a."user",
        a."apig"
      FROM "APIGAuth" a
      WHERE a."user" = $1 AND a."apig" = $2
      LIMIT 1
    `;

    const result = await Psql.query(sql, [userId, apigId]);

    // 4. 返回结果
    if (!result || result.length === 0) {
      response.json({ 
        code: 200, 
        success: true, 
        data: null 
      });
      return;
    }

    const row = result[0];
    response.json({ 
      code: 200, 
      success: true, 
      data: {
        objectId: row.objectId,
        user: row.user,
        apig: row.apig,
        count: row.count || 0,
        createdAt: row.createdAt,
        updatedAt: row.updatedAt
      }
    });

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

云函数部署说明

  • 云函数名称: getApigAuth
  • 功能: 查询用户的 APIG 授权和余额
  • 调用方式: POST https://server.fmode.cn/parse/functions/getApigAuth
  • 请求体:

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

使用场景

  1. OpenClaw skill 调用前 — 检查用户是否有足够额度
  2. 支付页面 — 显示当前余额
  3. 支付成功后 — 轮询确认余额到账