array-golden.mjs 2.53 KB
/**
 * 数组参数的线上字节钉值:固定 timestamp 后发一次真实请求,让网关自己认证这份 sign。
 * 用法:node out/.temp/array-golden.mjs <sessionId>
 * 只读金额合计接口,不写任何数据。
 */
import { readFileSync } from 'node:fs';
import { createServer, loadConfigFromFile } from 'vite';

const SID = process.argv[2];
if (!SID) {
  console.log('用法:node out/.temp/array-golden.mjs <sessionId>');
  process.exit(1);
}

const lines = readFileSync('.env', 'utf8')
  .split('\n')
  .filter((line) => /^[A-Z_]+=/.test(line))
  .map((line) => line.split('='));
const val = (k) => lines.find(([key]) => key === k)?.[1]?.trim() ?? '';
const credential = {
  origin: val('VITE_POINTS_GATEWAY_ORIGIN'),
  appKey: val('VITE_POINTS_GATEWAY_APP_KEY'),
  secret: val('VITE_POINTS_GATEWAY_SECRET'),
  version: '1.0.0',
  format: 'json',
};

const TIMESTAMP = 1794300000000;
const realNow = Date.now;
Date.now = () => TIMESTAMP;

const cfg = await loadConfigFromFile({ command: 'serve', mode: 'development' }, 'vite.config.ts');
const server = await createServer({
  ...cfg.config,
  appType: 'custom',
  optimizeDeps: { noDiscovery: true, include: [] },
  server: { ...cfg.config.server, middlewareMode: true },
  logLevel: 'error',
});
const apiCore = await server.ssrLoadModule('@uj/api-core');
await server.ssrLoadModule('/src/adapter/gateways/index.ts');

const raw = await apiCore.prepareRpcCall(
  {
    gateway: 'scrm-shopmall',
    method: 'scrm.api.shopmall.integral.transfer.out.pay.amount',
    params: { ids: ['214', '213', '212'], canApprove: false, usedCarFlag: '1' },
  },
  {
    resolveCredential: () => credential,
    resolveToken: () => SID,
    session: { name: 'sessionId', value: SID },
    timeout: 60000,
  },
);
Date.now = realNow;

const body = raw.data;
const form = new URLSearchParams(
  Object.entries(body).map(([k, v]) => [k, typeof v === 'string' ? v : String(v)]),
).toString();
const reply = await fetch(raw.url, {
  method: 'POST',
  headers: { ...raw.headers, 'content-type': 'application/x-www-form-urlencoded' },
  body: form,
}).then(async (r) => ({ http: r.status, t: await r.text() }));

console.log('url       :', raw.url);
console.log('timestamp :', TIMESTAMP);
console.log('keys      :', JSON.stringify(Object.keys(body)));
console.log('body      :', form.replace(/(appKey=)[^&]+/, '$1…').replace(/(sign=)[^&]+/, '$1…'));
console.log('sign      :', body.sign);
console.log('reply     :', `HTTP ${reply.http}`, reply.t.slice(0, 220));
console.log('appKey    :', credential.appKey);
await server.close();