sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

账号希望支持上游sub2api或者new-api额度的查询和自定义账号查询,类似于cc-switch的用量查询

Open yanlongqi opened this issue 4 months ago • 8 comments

账号希望支持上游sub2api或者new-api额度的查询和自定义账号查询,类似于cc-switch的用量查询

yanlongqi avatar Jul 15 '26 04:07 yanlongqi

sub2api 网关已有 GET /v1/usage(API Key Bearer 鉴权),返回结构可直接对接 cc-switch,无需额外开发。{{baseUrl}} 填部署根地址(不含 /v1)。

({
  request: {
    url: "{{baseUrl}}/v1/usage",
    method: "GET",
    headers: {
      Authorization: "Bearer {{apiKey}}",
      "Content-Type": "application/json",
      "User-Agent": "cc-switch/1.0",
    },
  },
  extractor: function (response) {
    if (response.error || response.isValid === false) {
      var msg = "API Key 无效或查询失败";
      if (response.error && response.error.message) msg = response.error.message;
      else if (response.message) msg = response.message;
      return { isValid: false, invalidMessage: msg };
    }

    function pct(used, limit) {
      if (!limit || limit <= 0) return null;
      var p = ((used || 0) / limit) * 100;
      if (p < 0) p = 0;
      if (p > 100) p = 100;
      return Math.round(p);
    }

    // 1. 订阅模式
    if (response.subscription) {
      var sub = response.subscription;
      var tiers = [];
      var d = pct(sub.daily_usage_usd, sub.daily_limit_usd);
      var w = pct(sub.weekly_usage_usd, sub.weekly_limit_usd);
      var m = pct(sub.monthly_usage_usd, sub.monthly_limit_usd);
      if (d !== null) tiers.push("日 " + d + "%");
      if (w !== null) tiers.push("周 " + w + "%");
      if (m !== null) tiers.push("月 " + m + "%");

      if (response.remaining < 0) {
        return {
          planName: response.planName || "订阅",
          remaining: null, used: null, total: null,
          unit: response.unit || "USD",
          extra: "无限制",
        };
      }
      var limit = sub.monthly_limit_usd || sub.weekly_limit_usd || sub.daily_limit_usd;
      var used = sub.monthly_usage_usd || sub.weekly_usage_usd || sub.daily_usage_usd || 0;
      return {
        planName: response.planName || "订阅",
        remaining: response.remaining,
        used: used,
        total: limit || response.remaining,
        unit: response.unit || "USD",
        extra: tiers.length > 0 ? tiers.join(" · ") : "无限制",
      };
    }

    // 2. 配额受限模式(总额度)
    if (response.quota) {
      var q = response.quota;
      var qp = pct(q.used, q.limit);
      return {
        planName: "API Key 配额",
        remaining: q.remaining, used: q.used, total: q.limit,
        unit: q.unit || "USD",
        extra: qp !== null ? qp + "%" : "",
      };
    }

    // 3. 仅速率限制(无总额度)
    if (response.rate_limits && response.rate_limits.length > 0) {
      var rls = response.rate_limits;
      var binding = rls[0];
      var bindingRatio = pct(binding.used, binding.limit);
      for (var i = 1; i < rls.length; i++) {
        var r = pct(rls[i].used, rls[i].limit);
        if (r !== null && (bindingRatio === null || r > bindingRatio)) {
          binding = rls[i];
          bindingRatio = r;
        }
      }
      var windows = rls.map(function (x) {
        var p = pct(x.used, x.limit);
        return x.window + " " + (p !== null ? p + "%" : "∞");
      }).join(" · ");
      return {
        planName: "速率限制",
        remaining: binding.remaining, used: binding.used, total: binding.limit,
        unit: "USD",
        extra: windows,
      };
    }

    // 4. 余额模式
    return {
      planName: response.planName || "钱包余额",
      remaining: response.remaining, used: 0, total: response.remaining,
      unit: response.unit || "USD",
      extra: "",
    };
  },
});

caigee-cmd avatar Jul 15 '26 07:07 caigee-cmd

sub2api 网关已有 GET /v1/usage(API Key Bearer 鉴权),返回结构可直接对接 cc-switch,无需额外开发。{{baseUrl}} 填部署根地址(不含 /v1)。

({ request: { url: "{{baseUrl}}/v1/usage", method: "GET", headers: { Authorization: "Bearer {{apiKey}}", "Content-Type": "application/json", "User-Agent": "cc-switch/1.0", }, }, extractor: function (response) { if (response.error || response.isValid === false) { var msg = "API Key 无效或查询失败"; if (response.error && response.error.message) msg = response.error.message; else if (response.message) msg = response.message; return { isValid: false, invalidMessage: msg }; }

function pct(used, limit) {
  if (!limit || limit <= 0) return null;
  var p = ((used || 0) / limit) * 100;
  if (p < 0) p = 0;
  if (p > 100) p = 100;
  return Math.round(p);
}

// 1. 订阅模式
if (response.subscription) {
  var sub = response.subscription;
  var tiers = [];
  var d = pct(sub.daily_usage_usd, sub.daily_limit_usd);
  var w = pct(sub.weekly_usage_usd, sub.weekly_limit_usd);
  var m = pct(sub.monthly_usage_usd, sub.monthly_limit_usd);
  if (d !== null) tiers.push("日 " + d + "%");
  if (w !== null) tiers.push("周 " + w + "%");
  if (m !== null) tiers.push("月 " + m + "%");

  if (response.remaining < 0) {
    return {
      planName: response.planName || "订阅",
      remaining: null, used: null, total: null,
      unit: response.unit || "USD",
      extra: "无限制",
    };
  }
  var limit = sub.monthly_limit_usd || sub.weekly_limit_usd || sub.daily_limit_usd;
  var used = sub.monthly_usage_usd || sub.weekly_usage_usd || sub.daily_usage_usd || 0;
  return {
    planName: response.planName || "订阅",
    remaining: response.remaining,
    used: used,
    total: limit || response.remaining,
    unit: response.unit || "USD",
    extra: tiers.length > 0 ? tiers.join(" · ") : "无限制",
  };
}

// 2. 配额受限模式(总额度)
if (response.quota) {
  var q = response.quota;
  var qp = pct(q.used, q.limit);
  return {
    planName: "API Key 配额",
    remaining: q.remaining, used: q.used, total: q.limit,
    unit: q.unit || "USD",
    extra: qp !== null ? qp + "%" : "",
  };
}

// 3. 仅速率限制(无总额度)
if (response.rate_limits && response.rate_limits.length > 0) {
  var rls = response.rate_limits;
  var binding = rls[0];
  var bindingRatio = pct(binding.used, binding.limit);
  for (var i = 1; i < rls.length; i++) {
    var r = pct(rls[i].used, rls[i].limit);
    if (r !== null && (bindingRatio === null || r > bindingRatio)) {
      binding = rls[i];
      bindingRatio = r;
    }
  }
  var windows = rls.map(function (x) {
    var p = pct(x.used, x.limit);
    return x.window + " " + (p !== null ? p + "%" : "∞");
  }).join(" · ");
  return {
    planName: "速率限制",
    remaining: binding.remaining, used: binding.used, total: binding.limit,
    unit: "USD",
    extra: windows,
  };
}

// 4. 余额模式
return {
  planName: response.planName || "钱包余额",
  remaining: response.remaining, used: 0, total: response.remaining,
  unit: response.unit || "USD",
  extra: "",
};

}, });

你理解错我的意思了,我指的是管理员后台哪里,目前只能获取oauth的额度信息。如果我使用apikey对接了别人的sub2api,在账号管理里面是无法获取到上游的配额信息的。目前版本支持手动配额设置

Image

yanlongqi avatar Jul 15 '26 07:07 yanlongqi

+1,这个挺需要的,期待能加上。

caigee-cmd avatar Jul 15 '26 09:07 caigee-cmd

+1,这个挺需要的,期待能加上。

我正在开发,希望作者能合进去

yanlongqi avatar Jul 15 '26 09:07 yanlongqi

非常需要这个

dongyaoa avatar Jul 15 '26 09:07 dongyaoa

@Wei-Shaw 这个功能对对接多个上游很实用,希望考虑一下

yanlongqi avatar Jul 19 '26 13:07 yanlongqi

Image感谢

csoaron avatar Sep 17 '26 03:09 csoaron

补充一个明确的自动化使用场景,希望官方将此功能内置支持(参考 #4672、#5050):

  • 在管理员账号管理表格的「上游声明倍率」后增加独立「账户余额」列;不要把本地倍率或本地限额冒充上游余额。
  • 使用该 API Key 账号已有 Base URL / API Key / 代理配置,自动适配 Sub2API GET /v1/usage 和 New API 的 Key 余额/额度接口;不要求额外管理员登录凭据。
  • 后台按 60 秒周期查询并保存快照,管理员未打开页面也能持续更新;页面仅读取本地缓存,多个页面不会增加上游查询流量。周期可配置,需有受限并发、超时及避免重复任务的保护。
  • 区分钱包余额、API Key 配额、订阅剩余额度、无限额度;保留上游明确返回的币种/单位、套餐名、已用/总额以及查询时间。无法确认币种时不可默认 USD,New API Key 配额不可称为账户钱包余额。
  • 对不支持、认证失效、限流、网络错误、过期快照分别展示状态,不把失败显示成 0,不把历史成功结果冒充实时余额。
  • 余额查询应保持只读,不修改账号调度、可用状态、倍率或用户扣费。复用现有安全请求机制:SSRF 校验、禁止携带 Key 跟随重定向、响应大小上限、凭据脱敏;账号连接配置发生变化时丢弃旧查询结果。

CC Switch 自定义查询使用的接口示例:

GET {baseUrl去掉末尾/v1}/v1/usage
Authorization: Bearer <该账号现有API Key>

主要字段为 isValid、remaining、unit、planName、quota.used、quota.limit。这些字段应按服务端实际语义解析,而不是仅按是否 HTTP 200 判定成功。

#4672 已覆盖 Sub2API/New API 查询但明确不进行后台定时刷新;#5050 有自动同步但仅覆盖上游 Sub2API。希望官方支持两者的交集:跨平台只读自动查询 + 独立余额列 + 每分钟后台缓存。

yang96028 avatar Oct 14 '26 05:10 yang96028