更新 Scripts/authorization&cartId.js
This commit is contained in:
@@ -1,86 +1,44 @@
|
||||
/**
|
||||
* Surge 脚本:捕获 cart 请求的 Authorization 和 cartId
|
||||
*
|
||||
* 功能:
|
||||
* - 保持原有:始终在 $persistentStore 中维护最新的 authorization 和 cartId
|
||||
* - 把每次捕获追加到 cart_records 数组(cartId 去重)
|
||||
* Surge 脚本:捕获特定 URL 请求的 Authorization 和 cartId(仅限 PUT 请求)
|
||||
* 并存储到持久化存储($persistentStore),只有在成功捕获到新值时才更新
|
||||
*/
|
||||
|
||||
const patterns = [
|
||||
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/cart\/eligibilityCheck\?/i,
|
||||
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/cart\/v1\.0\/cart\/eligibilityCheck\?/i,
|
||||
/^https:\/\/cart\.production\.store-web\.dynamics\.com\/.*eligibilityCheck\?/i
|
||||
];
|
||||
const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/Cart\/eligibilityCheck\?/;
|
||||
const url = $request.url;
|
||||
|
||||
const url = $request.url || "";
|
||||
const matched = patterns.some(p => p.test(url));
|
||||
|
||||
const MAX_RECORDS = 10; // cart 记录保留最近 10 条
|
||||
|
||||
// 兼容不同平台 Header 大小写差异
|
||||
function getHeader(name) {
|
||||
const headers = $request.headers || {};
|
||||
const target = name.toLowerCase();
|
||||
|
||||
for (const key in headers) {
|
||||
if (key.toLowerCase() === target) {
|
||||
return headers[key];
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (($request.method || "").toUpperCase() === "PUT" && matched) {
|
||||
// 仅处理 PUT 请求
|
||||
if ($request.method === "PUT" && pattern.test(url)) {
|
||||
try {
|
||||
const authorization = getHeader('authorization');
|
||||
// 获取 Authorization 请求头的值
|
||||
const authorization = $request.headers['authorization'];
|
||||
// 解析 URL 以提取 cartId 参数
|
||||
const urlObj = new URL(url);
|
||||
const cartId = urlObj.searchParams.get('cartId');
|
||||
const now = Date.now();
|
||||
|
||||
// 只有在成功捕获到新值时,才更新 $persistentStore 中的值
|
||||
if (authorization && authorization !== $persistentStore.read("authorization")) {
|
||||
$persistentStore.write(authorization, "authorization");
|
||||
console.log(`Stored authorization: ${authorization}`);
|
||||
}
|
||||
|
||||
if (cartId && cartId !== $persistentStore.read("cartId")) {
|
||||
$persistentStore.write(cartId, "cartId");
|
||||
console.log(`Stored cartId: ${cartId}`);
|
||||
}
|
||||
|
||||
if (cartId && authorization) {
|
||||
appendCartRecord({ cartId, authorization, ts: now });
|
||||
}
|
||||
|
||||
// 保留原始的这条通知
|
||||
// 发送通知,成功捕获并存储 Authorization 和 CartId
|
||||
$notification.post(
|
||||
"Surge 信息存储",
|
||||
"已捕获并存储 authorization 和 cartId"
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(`Error capturing authorization & cartId: ${error}`);
|
||||
$notification.post(
|
||||
"Surge 脚本错误",
|
||||
"无法捕获 authorization 和 cartId",
|
||||
`${error}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function appendCartRecord(entry) {
|
||||
let records = [];
|
||||
const raw = $persistentStore.read("cart_records");
|
||||
if (raw) {
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
if (Array.isArray(parsed)) records = parsed;
|
||||
} catch (e) { records = []; }
|
||||
}
|
||||
|
||||
if (records.some(r => r.cartId === entry.cartId)) {
|
||||
console.log(`[cart] SKIP: cartId=${entry.cartId} 已存在`);
|
||||
return;
|
||||
}
|
||||
|
||||
records.push(entry);
|
||||
if (records.length > MAX_RECORDS) records = records.slice(-MAX_RECORDS);
|
||||
$persistentStore.write(JSON.stringify(records), "cart_records");
|
||||
|
||||
console.log(`[cart] ✅ 已记录 cartId=${entry.cartId}, total=${records.length}`);
|
||||
}
|
||||
|
||||
$done({});
|
||||
Reference in New Issue
Block a user