展开查看模板代码
const BASE = "https://user.stringzhao.life";
// SERVICE_KEY 需先通过 CLI 注册服务获得:
// npm install -g @stringzhao/base-account-cli
// ba admin services create --origin https://your-app.example.com
// 注册后会返回 serviceKey(格式:svc-xxx)
const SERVICE_KEY = "my-app";
// 1) 生成邀请码(邀请者操作)
const genRes = await fetch(BASE + "/api/auth/invitation-codes/generate", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ serviceKey: SERVICE_KEY }),
credentials: "include"
}).then((r) => r.json());
console.log("邀请码:", genRes.invitationCode.code); // e.g. "ABCD1234"
// 2) 校验邀请码(受邀人输入时实时校验)
const valRes = await fetch(BASE + "/api/auth/invitation-codes/validate", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ code: "ABCD1234" }),
credentials: "include"
}).then((r) => r.json());
if (!valRes.valid) {
console.error("邀请码无效");
}
// 3) 兑换邀请码(受邀人确认后)
const redeemRes = await fetch(BASE + "/api/auth/invitation-codes/redeem", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ code: "ABCD1234" }),
credentials: "include"
}).then((r) => r.json());
console.log("邀请者:", redeemRes.creatorId);
// 4) 查看我的邀请码和配额
const listRes = await fetch(
BASE + "/api/auth/invitation-codes?serviceKey=" + SERVICE_KEY,
{ credentials: "include" }
).then((r) => r.json());
console.log("已用/总配额:", listRes.quota.used, "/", listRes.quota.total);