100 lines
3.6 KiB
JavaScript
100 lines
3.6 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* 测试 vip/check 逻辑:用邮箱查 users -> user_id -> site_vip
|
|
* 用法: node scripts/test-vip-check.mjs 283810867@qq.com
|
|
*/
|
|
import { fileURLToPath } from "url";
|
|
import { dirname, join } from "path";
|
|
import { readFileSync, existsSync } from "fs";
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const root = join(__dirname, "..");
|
|
|
|
function loadEnv() {
|
|
const paths = [join(root, ".env.local"), join(root, ".env")];
|
|
for (const p of paths) {
|
|
if (existsSync(p)) {
|
|
const content = readFileSync(p, "utf-8");
|
|
for (const line of content.split("\n")) {
|
|
const m = line.match(/^([^#=]+)=(.*)$/);
|
|
if (m) {
|
|
const key = m[1].trim();
|
|
const val = m[2].trim().replace(/^["']|["']$/g, "");
|
|
if (!process.env[key]) process.env[key] = val;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
loadEnv();
|
|
|
|
const email = process.argv[2] || "283810867@qq.com";
|
|
const pbUrl = process.env.NEXT_PUBLIC_POCKETBASE_URL || process.env.POCKETBASE_URL || "https://pocketbase.hackrobot.cn";
|
|
const adminEmail = process.env.POCKETBASE_EMAIL || process.env.PB_ADMIN_EMAIL;
|
|
const adminPassword = process.env.POCKETBASE_PASSWORD || process.env.PB_ADMIN_PASSWORD;
|
|
|
|
async function main() {
|
|
console.log("=== VIP Check 测试 ===");
|
|
console.log("邮箱:", email);
|
|
console.log("PocketBase URL:", pbUrl);
|
|
console.log("Admin 已配置:", !!(adminEmail && adminPassword));
|
|
|
|
if (!adminEmail || !adminPassword) {
|
|
console.error("请配置 POCKETBASE_EMAIL 和 POCKETBASE_PASSWORD");
|
|
process.exit(1);
|
|
}
|
|
|
|
const authPaths = ["/api/admins/auth-with-password", "/api/collections/_superusers/auth-with-password"];
|
|
let authRes;
|
|
for (const path of authPaths) {
|
|
authRes = await fetch(`${pbUrl}${path}`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ identity: adminEmail, password: adminPassword }),
|
|
});
|
|
if (authRes.ok) break;
|
|
}
|
|
if (!authRes.ok) {
|
|
console.error("Admin 登录失败:", authRes.status, await authRes.text());
|
|
process.exit(1);
|
|
}
|
|
const { token } = await authRes.json();
|
|
console.log("Admin token 获取成功");
|
|
|
|
const userFilter = `email="${email}"`;
|
|
const userUrl = `${pbUrl}/api/collections/users/records?filter=${encodeURIComponent(userFilter)}&perPage=1`;
|
|
console.log("查询 users:", userUrl);
|
|
|
|
const userRes = await fetch(userUrl, { headers: { Authorization: `Bearer ${token}` } });
|
|
const userData = await userRes.json();
|
|
console.log("users 响应 status:", userRes.status);
|
|
console.log("users 响应:", JSON.stringify(userData, null, 2));
|
|
|
|
const userId = userData?.items?.[0]?.id;
|
|
if (!userId) {
|
|
console.error("未找到用户,请检查邮箱或 users 集合");
|
|
process.exit(1);
|
|
}
|
|
console.log("user_id:", userId);
|
|
|
|
const siteVipFilter = `user_id = "${userId}" && (site_id = "digital" || site_id = "vip")`;
|
|
const siteVipUrl = `${pbUrl}/api/collections/site_vip/records?filter=${encodeURIComponent(siteVipFilter)}&perPage=2&sort=-expires_at`;
|
|
console.log("查询 site_vip:", siteVipUrl);
|
|
|
|
const vipRes = await fetch(siteVipUrl, { headers: { Authorization: `Bearer ${token}` } });
|
|
const vipData = await vipRes.json();
|
|
console.log("site_vip 响应 status:", vipRes.status);
|
|
console.log("site_vip 响应:", JSON.stringify(vipData, null, 2));
|
|
|
|
const items = vipData?.items ?? [];
|
|
const valid = items.filter((r) => !r.expires_at || r.expires_at > new Date().toISOString());
|
|
console.log("\n=== 结果 ===");
|
|
console.log("VIP:", valid.length > 0);
|
|
if (valid[0]) console.log("记录:", valid[0]);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|