settle-probe.mjs 3.4 KB
/**
 * 直连网关复现「累计结算金额」这一条:完全照子应用的发法(同一个方法名、同样不加签)。
 * 用法:node out/.temp/settle-probe.mjs <sessionId> [origin]
 * 只读统计接口,不写任何数据。
 */
import { readFileSync } from 'node:fs';
import { createServer, loadConfigFromFile } from 'vite';

const SID = process.argv[2];
if (!SID) {
  console.log('用法:node out/.temp/settle-probe.mjs <sessionId> [origin]');
  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: process.argv[3] || 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 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 deps = {
  resolveCredential: () => credential,
  resolveToken: () => SID,
  session: { name: 'sessionId', value: SID },
  timeout: 60000,
};

const METHOD = {
  settleAmount: 'scrm.api.shopmall.integral.transfer.out.settle.amount.get',
  sumAllPayAmount: 'scrm.api.shopmall.integral.transfer.out.all.pay.amount',
  checkTransferType: 'scrm.api.shopmall.integral.transfer.out.check.records.transferType',
  payChannels: 'scrm.api.shopmall.integral.transfer.out.getChannel',
  balance: 'scrm.api.shopmall.integral.transfer.out.balance.get',
};

const ask = async (method, mode) => {
  // 子应用的请求出口现在一律带 usedCarFlag=1,这里照搬,保证三种发法只差在签名上。
  const raw = await apiCore.prepareRpcCall(
    { gateway: 'scrm-shopmall', method, params: { usedCarFlag: '1' }, signed: mode.signed },
    deps,
  );
  const fields = { ...raw.data };
  if (mode.dropSign) delete fields.sign;
  const form = new URLSearchParams(
    Object.entries(fields).map(([k, v]) => [k, typeof v === 'string' ? v : JSON.stringify(v)]),
  ).toString();
  const reply = await fetch(raw.url, {
    method: 'POST',
    headers: { ...raw.headers, 'content-type': 'application/x-www-form-urlencoded' },
    body: form,
  })
    .then((r) => r.text().then((t) => ({ http: r.status, t })))
    .catch((e) => ({ http: 0, t: String(e) }));
  const code =
    (reply.t.match(/"code"\s*:\s*"(\d+)"/) ?? reply.t.match(/code="(\d+)"/) ?? [])[1] ?? '?';
  const message =
    (reply.t.match(/"message"\s*:\s*"([^"]*)"/) ?? reply.t.match(/<message>([^<]*)</) ?? [])[1] ??
    '';
  return `HTTP ${reply.http} code=${code} ${message.slice(0, 26)}`;
};

// 三种发法:照现在(signed:false,信封也不发)/ 信封齐但不带 sign / 信封齐且带 sign
for (const [name, method] of Object.entries(METHOD)) {
  const asIs = await ask(method, { signed: false });
  const noSign = await ask(method, { signed: true, dropSign: true });
  const withSign = await ask(method, { signed: true });
  console.log(`${name.padEnd(17)} 现状:${asIs}  |  信封无sign:${noSign}  |  带sign:${withSign}`);
}

await server.close();