@wkgeo/cli 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/api.mjs CHANGED
@@ -151,8 +151,11 @@ export function fnOf(action, { legacy = legacyEndpointsEnabled() } = {}) {
151
151
  /**
152
152
  * ★ 2026-11-17(task-627):账务域下单**只在 `/cli` 后面**(cli-fc 再转发到 `/billing-core`)。
153
153
  * 逃生阀也回不去:`/order` 没有 `orderCreate` 这个动作,`/user` 更没有。
154
+ *
155
+ * ★ 2026-11-20(cli-price-quote):账务域**只读报价** `orderQuote` 同理(cli-fc 白名单转发)——
156
+ * 加购价的唯一来源是它(与 `orderCreate` 实收同源),逃生阀也不许绕到别的函数。
154
157
  */
155
- if (action === 'orderCreate') return 'cli';
158
+ if (action === 'orderCreate' || action === 'orderQuote') return 'cli';
156
159
  if (legacy) {
157
160
  if (FN_ORDER.has(action)) return 'order';
158
161
  return 'user';
@@ -16,6 +16,14 @@
16
16
  * ⚠️ 没有做"把余额补到一个已存在的 pending 订单上"的命令 —— 服务端**没有这个 action**
17
17
  * (`placeOrder` 里余额抵扣只在建单那一刻发生)。要做需要服务端新增接口,
18
18
  * 这属于"要主线程决定"的事,已在报告里列出。
19
+ *
20
+ * ## 加购价从哪来(★ 2026-11-20 cli-price-quote):只认服务端报价
21
+ * `orders create` 的金额 / 单价 / 份数**全部来自** `billing.orderQuote{kind:'quota'}`
22
+ * (`/cli` → `wkgeo-cli-fc` 白名单转发 → 网关 `/billing-core`),与实收
23
+ * `orderCreate{kind:'quota'}` **同源**。
24
+ * CLI 不再本地「单价 × 数量」:旧逻辑 `list.find(quota_key === …)` 取到列表第一条
25
+ * (`mandatory` 行 ¥2.00),而服务端实扣 `addon` 行 ¥1.87 ⇒ 显示 ¥2.00、实扣 ¥1.87。
26
+ * 只查价不建单:`wkgeo orders create --product <代码> --units N --dry-run`。
19
27
  */
20
28
 
21
29
  import { UsageError, CliError, EXIT } from '../errors.mjs';
@@ -36,6 +44,137 @@ function newOrderIdempotencyKey() {
36
44
  return `cli-quota-${Date.now().toString(36)}-${rand}`;
37
45
  }
38
46
 
47
+ /* ------------------------------------------------------------------ */
48
+ /* 计价:只问服务端(CLI 一行乘法都不写) */
49
+ /* ------------------------------------------------------------------ */
50
+
51
+ /**
52
+ * 服务端加购报价 —— **CLI 唯一的取价口径**(★ 2026-11-20 cli-price-quote)。
53
+ *
54
+ * ## 为什么要改成"问服务端"(事故背景)
55
+ * 改造前这里是「本地 `list.find(quota_key === … && kind === 'resource')` + 单价 × 数量」:
56
+ * `listProducts` 的排序是 `order by p.sort_order, p.code`,同一个 `quota_key` 下后台有
57
+ * **两条正常配置的行** —— `mandatory`(套餐必选 / 含项,¥2.00)与 `addon`(资源包,¥1.87)。
58
+ * 本地 `find` 命中**列表第一条** = mandatory ⇒ CLI 显示 ¥2.00,而服务端实扣 addon 行 ¥1.87。
59
+ * 估算与余额预检共用那一刻的 `estimateCents` ⇒ 两处都错。
60
+ *
61
+ * ## 现在的口径
62
+ * `orderQuote{kind:'quota', quota_key, quota_amount}` = 会员端「订单确认页」用的那条报价,
63
+ * 它内部调的就是 `orderCreate` 的计价函数(服务端不变量
64
+ * `orderQuote().payable_cents === orderCreate().amount_cents`)。
65
+ * ⇒ 金额 / 单价 / 份数 / 月数**全部照抄服务端回包**,本地不乘、不取整、不挑行算钱。
66
+ * ⇒ 以后后台怎么调价、加购取哪条产品行,CLI 都自动跟对(不会再有第二套算法)。
67
+ *
68
+ * ## 失败策略:**不回落本地估算**
69
+ * 显示一个"看起来对"的金额比报错更糟(这就是钱的事故)。报价拿不到就明确报错、不给金额,
70
+ * 让用户稍后重试或去网页看价 —— 绝不拿目录价糊一个数出来。
71
+ *
72
+ * @returns {Promise<{payable_cents:number, original_cents:number, unit_price_cents:number|null,
73
+ * units:number|null, unit:string|null, months:number|null, product_code:string|null,
74
+ * product_name:string|null, quota_key:string, quota_amount:number,
75
+ * wallet_balance_cents:number|null, via:string}>}
76
+ */
77
+ async function fetchQuotaQuote(ctx, { quotaKey, quotaAmount }) {
78
+ const payload = { kind: 'quota', quota_key: quotaKey, quota_amount: quotaAmount };
79
+ let res;
80
+ try {
81
+ res = await ctx.callData('orderQuote', payload);
82
+ } catch (e) {
83
+ throw new CliError(`查询加购报价失败:${(e && e.message) || e}`, {
84
+ kind: (e && e.kind) || 'error',
85
+ code: e && e.code !== undefined ? e.code : null,
86
+ exitCode: e && e.exitCode !== undefined ? e.exitCode : EXIT.ERROR,
87
+ hint:
88
+ '报价由服务端账务域给出(与实收同一份算法),拿不到报价就**先不要下单**:' +
89
+ '稍后重试;要核对历史金额用 `wkgeo orders list`,要看网页价去会员中心「产品 / 加购」。',
90
+ details: { action: 'orderQuote', ...payload },
91
+ });
92
+ }
93
+
94
+ const numOf = (v) => {
95
+ if (v === undefined || v === null || v === '') return null;
96
+ const n = Number(v);
97
+ return Number.isFinite(n) ? n : null;
98
+ };
99
+ const payable = numOf(res && res.payable_cents);
100
+ if (payable === null || payable <= 0) {
101
+ /** 报价为空 / 0 ⇒ 绝不显示金额(宁可报错,也不给一个错的价格) */
102
+ throw new CliError('服务端没有给出可用的报价金额(未显示任何金额)', {
103
+ kind: 'quote_empty',
104
+ code: 502,
105
+ hint:
106
+ '报价与实收同源,拿不到就**不要下单**:稍后重试;若一直为空,请在会员中心「产品 / 加购」页核对价格,' +
107
+ '或把这条输出发给平台负责人(附 `--verbose` 的脱敏原文)。',
108
+ details: { action: 'orderQuote', ...payload, raw: res || null },
109
+ });
110
+ }
111
+
112
+ const originalCents = numOf(res && res.original_cents);
113
+ const quotedAmount = numOf(res && res.quota_amount);
114
+ return {
115
+ payable_cents: Math.trunc(payable),
116
+ original_cents: originalCents === null ? Math.trunc(payable) : Math.trunc(originalCents),
117
+ discount_cents: Math.trunc(numOf(res && res.discount_cents) || 0),
118
+ unit_price_cents: numOf(res && res.unit_price_cents),
119
+ units: numOf(res && res.units),
120
+ unit: (res && res.unit) || null,
121
+ months: numOf(res && res.months),
122
+ product_code: (res && res.product_code) || null,
123
+ product_name: (res && res.product_name) || null,
124
+ quota_key: String((res && res.quota_key) || quotaKey),
125
+ quota_amount: quotedAmount === null ? quotaAmount : quotedAmount,
126
+ wallet_balance_cents: numOf(res && res.wallet_balance_cents),
127
+ coupon: (res && res.coupon) || null,
128
+ via: (res && res.via) || 'billing.orderQuote',
129
+ };
130
+ }
131
+
132
+ /**
133
+ * 本地挑产品行 —— **只用来定"买多少"(`quota_amount`),绝不用来算钱**。
134
+ *
135
+ * 顺序:① 产品代码精确命中(用户点名的那条;非资源型直接交给调用方报错);
136
+ * ② **同一 `quota_key` 的资源行里优先 `sale_type='addon'`(资源包)** —— 服务端
137
+ * `orderCreate` / `orderQuote` 永远按 `(sale_type='addon') desc, sort_order, code`
138
+ * 取行(`_shared/addon-product-pick.js`),所以倍率必须按同一行算;
139
+ * ③ 没有 addon 行时回落:用户点的 code 行,再不行第一条。
140
+ *
141
+ * ⚠️ 兜底路径说明(方案 ① 的位置):金额一律来自上面的服务端报价,这里的 `price_cents`
142
+ * **一次都不读**;② 只为把 `quota_multiplier`(⇒ 基础单位数量)算成后台配的那条资源包行,
143
+ * 与 `orderCreate` / `orderQuote` 的取行口径一致。
144
+ * 今天的 `listProducts` **还没下发 `sale_type`**,所以 ② 暂时不生效、实际走 ③ —— 一旦后端
145
+ * 补上该列,这里自动与服务端同序,不需要再改 CLI。**没有"未登录也要显示价格"的命令**:
146
+ * `products list` / `orders create` 都必须是登录态,未登录时 CLI 直接报需要登录、不显示金额。
147
+ */
148
+ function pickProductRow(list, wanted) {
149
+ const byCode = list.find((p) => p.code === wanted);
150
+
151
+ /* 非资源型(套餐 / 一次性服务)交给调用方报"CLI 只支持加量包"——不要拿它去猜配额 */
152
+ if (byCode && byCode.kind !== 'resource') return byCode;
153
+
154
+ /** 目标配额键:`--product` 传的可能是产品代码,也可能是配额键,两种都要能落到同一个 quota_key */
155
+ const quotaKey = (byCode && byCode.params && byCode.params.quota_key) || '';
156
+
157
+ const candidates = list.filter(
158
+ (p) =>
159
+ p.kind === 'resource' &&
160
+ p.params &&
161
+ (quotaKey ? p.params.quota_key === quotaKey : p.params.quota_key === wanted),
162
+ );
163
+ const isAddon = (p) => String(p.sale_type || '').trim().toLowerCase() === 'addon';
164
+
165
+ /**
166
+ * ② 服务端为这个 quota_key 实际会取的那一行(addon 优先)—— **数量以它为准**:
167
+ * `orderCreate` / `orderQuote` 永远按 `(sale_type='addon') desc, sort_order, code` 取行,
168
+ * 所以即使用户点的是必选行的 code,倍率也应当按 addon 行算,别买成另一个数量。
169
+ */
170
+ const addon = candidates.find(isAddon);
171
+ if (addon) return addon;
172
+
173
+ /* ③ 没有 addon 行:服务端回落 `sort_order, code` 的第一条 —— 用户点了 code 就用那一行 */
174
+ return byCode || candidates[0] || null;
175
+ }
176
+
177
+
39
178
  /* ------------------------------------------------------------------ */
40
179
  /* products list */
41
180
  /* ------------------------------------------------------------------ */
@@ -78,6 +217,7 @@ export const productsListCommand = {
78
217
  rows,
79
218
  );
80
219
  out.info(' 下单示例:wkgeo orders create --product <产品代码> --units 1');
220
+ out.info(' 查加购价(不建单、不扣款):wkgeo orders create --product <产品代码> --units 1 --dry-run');
81
221
  }
82
222
  ctx.done(data);
83
223
  return data;
@@ -92,8 +232,8 @@ export const ordersCreateCommand = {
92
232
  name: 'orders create',
93
233
  summary: '下单(余额足够时**当场用钱包余额付清**;不会发起真实微信扣款)',
94
234
  usage:
95
- 'wkgeo orders create --product <产品代码> --units <份数> [--allow-wechat]\n' +
96
- 'wkgeo orders create --quota <配额键> --amount <基础单位数> [--allow-wechat]',
235
+ 'wkgeo orders create --product <产品代码> --units <份数> [--allow-wechat] [--dry-run]\n' +
236
+ 'wkgeo orders create --quota <配额键> --amount <基础单位数> [--allow-wechat] [--dry-run]',
97
237
  flags: {
98
238
  product: { type: 'string', desc: '产品代码或配额键(见 `wkgeo products list`)' },
99
239
  units: { type: 'string', desc: '购买份数(默认 1,按产品单位计价)' },
@@ -103,6 +243,10 @@ export const ordersCreateCommand = {
103
243
  type: 'boolean',
104
244
  desc: '余额不足时仍创建订单(会返回微信 code_url,**需要用户自己扫码**;CLI 不会扣款)',
105
245
  },
246
+ 'dry-run': {
247
+ type: 'boolean',
248
+ desc: '只向服务端查**报价**(不建单、不扣款):加购价以它为准,与实收同一份算法',
249
+ },
106
250
  },
107
251
  async run(ctx) {
108
252
  const { out, flags } = ctx;
@@ -117,8 +261,8 @@ export const ordersCreateCommand = {
117
261
  let quotaKey;
118
262
  let quotaAmount;
119
263
  let units = null;
120
- let estimateCents = null;
121
- let productInfo = null;
264
+ /** 本地命中的产品行:**只用来定数量 / 展示名称**,金额一律不从这里取 */
265
+ let catalogHit = null;
122
266
 
123
267
  if (flags.product) {
124
268
  units = flags.units === undefined ? 1 : Number(flags.units);
@@ -129,9 +273,7 @@ export const ordersCreateCommand = {
129
273
  ...p,
130
274
  params: p.params || {},
131
275
  }));
132
- const hit =
133
- list.find((p) => p.code === flags.product) ||
134
- list.find((p) => p.params.quota_key === flags.product && p.kind === 'resource');
276
+ const hit = pickProductRow(list, flags.product);
135
277
 
136
278
  if (!hit) {
137
279
  throw new CliError(`找不到产品(或配额键)「${flags.product}」`, {
@@ -149,15 +291,9 @@ export const ordersCreateCommand = {
149
291
  }
150
292
  const mult = Number(hit.params.quota_multiplier) || 1;
151
293
  quotaKey = hit.params.quota_key;
294
+ /** ★ 基础单位数量 = 份数 × 倍率;**这是"买多少",不是"多少钱"** */
152
295
  quotaAmount = units * mult;
153
- estimateCents = units * (Number(hit.price_cents) || 0);
154
- productInfo = {
155
- code: hit.code,
156
- name: hit.name,
157
- unit: hit.unit || '份',
158
- unit_price_cents: Number(hit.price_cents) || 0,
159
- quota_multiplier: mult,
160
- };
296
+ catalogHit = hit;
161
297
  } else {
162
298
  quotaKey = flags.quota;
163
299
  quotaAmount = Number(flags.amount);
@@ -167,13 +303,78 @@ export const ordersCreateCommand = {
167
303
  units = null;
168
304
  }
169
305
 
306
+ /**
307
+ * ★ 2026-11-20(cli-price-quote):**金额只问服务端**(不再 `单价 × 数量`)。
308
+ * 报价 = 会员端确认页那条 `orderQuote{kind:'quota'}`,与 `orderCreate` 实收同源。
309
+ * 失败直接抛(不显示任何金额),见 `fetchQuotaQuote()` 的说明。
310
+ */
311
+ const quote = await fetchQuotaQuote(ctx, { quotaKey, quotaAmount });
312
+ const estimateCents = quote.payable_cents;
313
+
314
+ /** 展示用的商品信息:优先服务端报价回显(它就是实收那一条产品行) */
315
+ const productInfo = {
316
+ code: quote.product_code || (catalogHit && catalogHit.code) || null,
317
+ name: quote.product_name || (catalogHit && catalogHit.name) || quotaKey,
318
+ unit: quote.unit || (catalogHit && catalogHit.unit) || '份',
319
+ unit_price_cents: quote.unit_price_cents,
320
+ units: quote.units,
321
+ months: quote.months,
322
+ quota_multiplier: catalogHit ? Number(catalogHit.params.quota_multiplier) || 1 : null,
323
+ /** 金额来源(排查用:永远应是服务端报价,不是本地估算) */
324
+ price_source: 'server_quote',
325
+ };
326
+ /** 下单用的配额数量以**报价回显**为准(保证报价与建单是同一单) */
327
+ const chargeQuotaAmount = quote.quota_amount;
328
+
329
+ /* ---- 只查价:不建单、不扣款(加购价的权威查询入口) ---- */
330
+ if (flags.dryRun === true) {
331
+ const data = {
332
+ dry_run: true,
333
+ writes: 0,
334
+ quote,
335
+ amount_cents: quote.payable_cents,
336
+ amount_text: formatMoney(quote.payable_cents),
337
+ product: productInfo,
338
+ quota: { quota_key: quote.quota_key, quota_amount: chargeQuotaAmount, units },
339
+ balance_before_cents: quote.wallet_balance_cents,
340
+ balance_before_text:
341
+ quote.wallet_balance_cents === null ? null : formatMoney(quote.wallet_balance_cents),
342
+ };
343
+ if (!out.json) {
344
+ out.head('加购报价(dry-run:**没有**下单、**没有**扣款)');
345
+ out.line(` 商品:${productInfo.name}${productInfo.code ? `(${productInfo.code})` : ''}`);
346
+ const unitPrice =
347
+ quote.unit_price_cents === null ? '-' : formatMoney(quote.unit_price_cents);
348
+ const unitLabel = productInfo.unit || '份';
349
+ const unitCount = quote.units === null ? quotaAmount : quote.units;
350
+ const months = quote.months === null ? 1 : quote.months;
351
+ out.line(` 单价:${unitPrice} / ${unitLabel} × ${unitCount} × ${months} 个月`);
352
+ if (quote.discount_cents > 0) {
353
+ out.line(` 原价:${formatMoney(quote.original_cents)}(优惠 ${formatMoney(quote.discount_cents)})`);
354
+ }
355
+ out.line(` 合计:${formatMoney(quote.payable_cents)}`);
356
+ if (quote.wallet_balance_cents !== null) {
357
+ const enough = quote.wallet_balance_cents >= quote.payable_cents;
358
+ out.line(
359
+ ` 钱包余额:${formatMoney(quote.wallet_balance_cents)}(${enough ? '够' : `不够,差 ${formatMoney(quote.payable_cents - quote.wallet_balance_cents)}`})`,
360
+ );
361
+ }
362
+ out.info(` 金额来源:${quote.via}(服务端报价,与实收同一份算法)`);
363
+ out.info(' 真下单:去掉 --dry-run(余额够就当场付清;CLI 不会发起微信扣款)。');
364
+ }
365
+ ctx.done(data);
366
+ return data;
367
+ }
368
+
170
369
  /* ---- 钱包预检:余额不足时**默认拒绝**,而不是悄悄建一张要扫码的单 ---- */
171
- let balanceBefore = null;
172
- try {
173
- const w = await ctx.callData('getMyWallet', { limit: 1 });
174
- balanceBefore = Number(w && w.balance_cents) || 0;
175
- } catch (e) {
176
- out.warn(`读取钱包余额失败(继续下单):${e.message}`);
370
+ let balanceBefore = quote.wallet_balance_cents;
371
+ if (balanceBefore === null) {
372
+ try {
373
+ const w = await ctx.callData('getMyWallet', { limit: 1 });
374
+ balanceBefore = Number(w && w.balance_cents) || 0;
375
+ } catch (e) {
376
+ out.warn(`读取钱包余额失败(继续下单):${e.message}`);
377
+ }
177
378
  }
178
379
  if (
179
380
  balanceBefore !== null &&
@@ -200,32 +401,52 @@ export const ordersCreateCommand = {
200
401
  * 不再打 `/order` 的 `createQuotaOrder`(钱路径只留 billing 一条:
201
402
  * `placeOrder` + `fulfillOrder`,金额只从 `product.price_cents` 算)。
202
403
  * ⚠️ 必须带 `idempotency_key`:同键重放回原单(不会多建单、不会二次扣余额)。
404
+ *
405
+ * ★ 2026-11-20(cli-price-quote):`quota_amount` 用**报价回显的那个数**
406
+ * (`chargeQuotaAmount`)—— 保证"报价的单"和"建出来的单"是同一份参数,
407
+ * 不会出现「报价按 A 行算、下单按 B 行算」。
203
408
  */
204
409
  const res = await ctx.callData('orderCreate', {
205
410
  kind: 'quota',
206
- quota_key: quotaKey,
207
- quota_amount: quotaAmount,
411
+ quota_key: quote.quota_key,
412
+ quota_amount: chargeQuotaAmount,
208
413
  idempotency_key: newOrderIdempotencyKey(),
209
414
  });
210
415
  const paid = !!(res && res.paid);
416
+ const chargedCents = res && res.amount_cents !== undefined ? Number(res.amount_cents) : null;
211
417
  const data = {
212
418
  order_id: (res && res.order_id) || null,
213
419
  out_trade_no: (res && res.out_trade_no) || null,
214
420
  paid,
215
421
  amount_cents: Number(res && res.amount_cents) || null,
216
422
  amount_text: res && res.amount_cents !== undefined ? formatMoney(res.amount_cents) : null,
423
+ /** 服务端报价(下单前展示 / 余额预检用的就是这个数) */
424
+ quoted_cents: quote.payable_cents,
425
+ quoted_text: formatMoney(quote.payable_cents),
217
426
  wallet_paid_cents: Number(res && res.wallet_paid_cents) || 0,
218
427
  pay_amount_cents: res && res.pay_amount_cents !== undefined ? Number(res.pay_amount_cents) : null,
219
428
  code_url: (res && res.code_url) || null,
220
429
  expires_at_minutes: (res && res.expires_at_minutes) || null,
221
430
  message: (res && res.message) || null,
222
431
  product: productInfo,
223
- quota: { quota_key: quotaKey, quota_amount: quotaAmount, units },
432
+ quota: { quota_key: quote.quota_key, quota_amount: chargeQuotaAmount, units },
224
433
  balance_before_cents: balanceBefore,
225
434
  balance_after_cents:
226
435
  balanceBefore !== null && paid ? balanceBefore - (Number(res && res.wallet_paid_cents) || 0) : balanceBefore,
436
+ /** 明细:服务端返回的金额行(来自同一份报价/下单快照) */
437
+ quote,
227
438
  };
228
439
 
440
+ /**
441
+ * 报价 ≠ 实收是一条**钱的不变量**(服务端保证 `orderQuote().payable_cents === orderCreate().amount_cents`)。
442
+ * 真出现不一致就明确提示(不改金额、不重试),把这条输出留给平台负责人排查。
443
+ */
444
+ if (chargedCents !== null && chargedCents !== quote.payable_cents) {
445
+ out.warn(
446
+ `注意:报价 ${formatMoney(quote.payable_cents)} 与实收 ${formatMoney(chargedCents)} 不一致 —— 请把这条输出发给平台负责人(订单 ${data.order_id})`,
447
+ );
448
+ }
449
+
229
450
  if (!out.json) {
230
451
  out.head('下单结果');
231
452
  out.line(` 订单号:${data.order_id}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wkgeo/cli",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "云站平台官方命令行客户端(@wkgeo/cli):授权登录一次,之后建站 / 下单 / 钱包支付 / 查余额都不用再登录(OAuth 授权码 + PKCE,公共客户端,零第三方依赖)",
5
5
  "keywords": [
6
6
  "wkgeo",