@wkgeo/cli 2.0.3 → 2.2.0
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/CHANGELOG.md +19 -2
- package/README.md +7 -7
- package/SKILL.md +1038 -1038
- package/lib/api.mjs +351 -351
- package/lib/args.mjs +235 -235
- package/lib/build.mjs +1047 -0
- package/lib/cms.mjs +328 -328
- package/lib/commands/auth.mjs +1280 -1280
- package/lib/commands/data.mjs +694 -694
- package/lib/commands/index.mjs +17 -1
- package/lib/commands/init.mjs +111 -111
- package/lib/commands/orders.mjs +691 -691
- package/lib/commands/site.mjs +82 -0
- package/lib/commands/siteops.mjs +382 -0
- package/lib/commands/sites.mjs +968 -801
- package/lib/commands/spec.mjs +72 -72
- package/lib/commands/template.mjs +1484 -1484
- package/lib/config.mjs +57 -4
- package/lib/errors.mjs +446 -336
- package/lib/manifest-apply.mjs +1650 -1650
- package/lib/manifest-schema.json +480 -480
- package/lib/manifest.mjs +1106 -1106
- package/lib/runtime.mjs +798 -654
- package/lib/site-create.mjs +537 -537
- package/lib/site-credentials.mjs +157 -0
- package/lib/site-meta.mjs +474 -0
- package/lib/site-publish.mjs +410 -0
- package/lib/site-spec.mjs +302 -302
- package/lib/site-template.mjs +405 -0
- package/lib/template-gen.mjs +1354 -1354
- package/lib/template-publish.mjs +696 -696
- package/lib/templates-sync.mjs +676 -644
- package/lib/workspace-sync.mjs +386 -0
- package/package.json +56 -56
- package/spec/SITE-SPEC.md +198 -198
package/lib/commands/orders.mjs
CHANGED
|
@@ -1,691 +1,691 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 订单命令:`products list` / `orders create` / `orders pay` / `orders list`
|
|
3
|
-
*
|
|
4
|
-
* ## 「钱包余额支付」在本项目里到底发生在哪一步(重要,别被命令名骗了)
|
|
5
|
-
*
|
|
6
|
-
* 服务端 `placeOrder()` 的行为是:**下单时就先用余额抵扣**,
|
|
7
|
-
* 余额足够 → `moveWallet(-金额)` + `fulfillOrder()` → 直接返回 `paid: true`。
|
|
8
|
-
* 也就是说"用钱包支付"是**下单动作的一部分**,不是独立的一步。
|
|
9
|
-
*
|
|
10
|
-
* 所以本 CLI 的口径:
|
|
11
|
-
* * `orders create` = 下单 + (余额够就)完成支付,返回值里 `paid` 说明结果;
|
|
12
|
-
* * `orders pay <id>` = 对**还没支付**的订单做"查单/收敛状态"(服务端 checkOrder):
|
|
13
|
-
* 微信侧已支付而本地还是 pending → 就地履约;否则告诉你还在等支付。
|
|
14
|
-
* * CLI **永远不会**自己去扫微信码 / 发起任何真实扣款。
|
|
15
|
-
*
|
|
16
|
-
* ⚠️ 没有做"把余额补到一个已存在的 pending 订单上"的命令 —— 服务端**没有这个 action**
|
|
17
|
-
* (`placeOrder` 里余额抵扣只在建单那一刻发生)。要做需要服务端新增接口,
|
|
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`。
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
import { UsageError, CliError, EXIT } from '../errors.mjs';
|
|
30
|
-
import { formatMoney, formatTime } from '../format.mjs';
|
|
31
|
-
import { renderQrOptional } from '../qr.mjs';
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 下单幂等键(★ 2026-11-17 task-627)。
|
|
35
|
-
*
|
|
36
|
-
* 服务端 `billing.orderCreate` 的口径:**同一键同一单**(重放回原单,含新的收银台令牌);
|
|
37
|
-
* 同键带不同参数回 409。CLI 不自动重试(`api.mjs` 约定),所以这里只保证
|
|
38
|
-
* 「每次执行 = 一次新尝试 = 一个新键」,绝不会把两次不同的购买合成一张单。
|
|
39
|
-
* 格式受服务端硬约束:`^[A-Za-z0-9_-]{8,80}$`。
|
|
40
|
-
*/
|
|
41
|
-
function newOrderIdempotencyKey() {
|
|
42
|
-
const rand = Math.random().toString(36).slice(2, 11);
|
|
43
|
-
|
|
44
|
-
return `cli-quota-${Date.now().toString(36)}-${rand}`;
|
|
45
|
-
}
|
|
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
|
-
|
|
178
|
-
/* ------------------------------------------------------------------ */
|
|
179
|
-
/* products list */
|
|
180
|
-
/* ------------------------------------------------------------------ */
|
|
181
|
-
|
|
182
|
-
export const productsListCommand = {
|
|
183
|
-
name: 'products list',
|
|
184
|
-
summary: '可购买的产品/加量包(用来找 --product 该填什么)',
|
|
185
|
-
usage: 'wkgeo products list',
|
|
186
|
-
async run(ctx) {
|
|
187
|
-
const { out } = ctx;
|
|
188
|
-
const res = await ctx.callData('listProducts', {});
|
|
189
|
-
const rows = (res && Array.isArray(res.rows) ? res.rows : []).map((p) => {
|
|
190
|
-
const params = p.params || {};
|
|
191
|
-
return {
|
|
192
|
-
code: p.code,
|
|
193
|
-
name: p.name,
|
|
194
|
-
kind: p.kind,
|
|
195
|
-
unit: p.unit,
|
|
196
|
-
price_cents: p.price_cents,
|
|
197
|
-
price_text: formatMoney(p.price_cents),
|
|
198
|
-
quota_key: params.quota_key || null,
|
|
199
|
-
quota_multiplier: params.quota_multiplier ?? null,
|
|
200
|
-
min_qty: p.min_qty ?? null,
|
|
201
|
-
max_qty: p.max_qty ?? null,
|
|
202
|
-
};
|
|
203
|
-
});
|
|
204
|
-
const data = { rows };
|
|
205
|
-
if (!out.json) {
|
|
206
|
-
out.head(`可购买的产品(${rows.length} 项)`);
|
|
207
|
-
out.table(
|
|
208
|
-
[
|
|
209
|
-
{ title: '产品代码', key: 'code', width: 24 },
|
|
210
|
-
{ title: '名称', key: 'name', width: 20 },
|
|
211
|
-
{ title: '类型', key: 'kind', width: 14 },
|
|
212
|
-
{ title: '单价', key: 'price_text', width: 10 },
|
|
213
|
-
{ title: '单位', key: 'unit', width: 8 },
|
|
214
|
-
{ title: '配额键', key: 'quota_key', width: 16 },
|
|
215
|
-
{ title: '倍率', key: 'quota_multiplier', width: 6 },
|
|
216
|
-
],
|
|
217
|
-
rows,
|
|
218
|
-
);
|
|
219
|
-
out.info(' 下单示例:wkgeo orders create --product <产品代码> --units 1');
|
|
220
|
-
out.info(' 查加购价(不建单、不扣款):wkgeo orders create --product <产品代码> --units 1 --dry-run');
|
|
221
|
-
}
|
|
222
|
-
ctx.done(data);
|
|
223
|
-
return data;
|
|
224
|
-
},
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
/* ------------------------------------------------------------------ */
|
|
228
|
-
/* orders create */
|
|
229
|
-
/* ------------------------------------------------------------------ */
|
|
230
|
-
|
|
231
|
-
export const ordersCreateCommand = {
|
|
232
|
-
name: 'orders create',
|
|
233
|
-
summary: '下单(余额足够时**当场用钱包余额付清**;不会发起真实微信扣款)',
|
|
234
|
-
usage:
|
|
235
|
-
'wkgeo orders create --product <产品代码> --units <份数> [--allow-wechat] [--dry-run]\n' +
|
|
236
|
-
'wkgeo orders create --quota <配额键> --amount <基础单位数> [--allow-wechat] [--dry-run]',
|
|
237
|
-
flags: {
|
|
238
|
-
product: { type: 'string', desc: '产品代码或配额键(见 `wkgeo products list`)' },
|
|
239
|
-
units: { type: 'string', desc: '购买份数(默认 1,按产品单位计价)' },
|
|
240
|
-
quota: { type: 'string', desc: '直接指定配额键(如 storage_mb)' },
|
|
241
|
-
amount: { type: 'string', desc: '直接指定基础单位数量(配合 --quota)' },
|
|
242
|
-
'allow-wechat': {
|
|
243
|
-
type: 'boolean',
|
|
244
|
-
desc: '余额不足时仍创建订单(会返回微信 code_url,**需要用户自己扫码**;CLI 不会扣款)',
|
|
245
|
-
},
|
|
246
|
-
'dry-run': {
|
|
247
|
-
type: 'boolean',
|
|
248
|
-
desc: '只向服务端查**报价**(不建单、不扣款):加购价以它为准,与实收同一份算法',
|
|
249
|
-
},
|
|
250
|
-
},
|
|
251
|
-
async run(ctx) {
|
|
252
|
-
const { out, flags } = ctx;
|
|
253
|
-
|
|
254
|
-
if (!flags.product && !flags.quota) {
|
|
255
|
-
throw new UsageError('需要 --product <产品代码> 或 --quota <配额键>(用 `wkgeo products list` 查可用产品)');
|
|
256
|
-
}
|
|
257
|
-
if (flags.product && flags.quota) {
|
|
258
|
-
throw new UsageError('--product 与 --quota 不能同时使用');
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
let quotaKey;
|
|
262
|
-
let quotaAmount;
|
|
263
|
-
let units = null;
|
|
264
|
-
/** 本地命中的产品行:**只用来定数量 / 展示名称**,金额一律不从这里取 */
|
|
265
|
-
let catalogHit = null;
|
|
266
|
-
|
|
267
|
-
if (flags.product) {
|
|
268
|
-
units = flags.units === undefined ? 1 : Number(flags.units);
|
|
269
|
-
if (!Number.isInteger(units) || units <= 0) throw new UsageError(`--units 必须是正整数,收到:${flags.units}`);
|
|
270
|
-
|
|
271
|
-
const catalog = await ctx.callData('listProducts', {});
|
|
272
|
-
const list = (catalog && Array.isArray(catalog.rows) ? catalog.rows : []).map((p) => ({
|
|
273
|
-
...p,
|
|
274
|
-
params: p.params || {},
|
|
275
|
-
}));
|
|
276
|
-
const hit = pickProductRow(list, flags.product);
|
|
277
|
-
|
|
278
|
-
if (!hit) {
|
|
279
|
-
throw new CliError(`找不到产品(或配额键)「${flags.product}」`, {
|
|
280
|
-
kind: 'not_found',
|
|
281
|
-
code: 404,
|
|
282
|
-
hint: `用 \`wkgeo products list\` 看可用产品代码;也可用 --quota <配额键> --amount <数量> 直接按配额下单`,
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
if (hit.kind !== 'resource' || !hit.params.quota_key) {
|
|
286
|
-
throw new CliError(`产品「${hit.code}」是 ${hit.kind} 类型,当前 CLI 只支持"加量包/资源型"下单`, {
|
|
287
|
-
kind: 'unsupported',
|
|
288
|
-
code: 400,
|
|
289
|
-
hint: '订阅套餐请在网页后台自助购买(金额较大且会改订阅权益)',
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
const mult = Number(hit.params.quota_multiplier) || 1;
|
|
293
|
-
quotaKey = hit.params.quota_key;
|
|
294
|
-
/** ★ 基础单位数量 = 份数 × 倍率;**这是"买多少",不是"多少钱"** */
|
|
295
|
-
quotaAmount = units * mult;
|
|
296
|
-
catalogHit = hit;
|
|
297
|
-
} else {
|
|
298
|
-
quotaKey = flags.quota;
|
|
299
|
-
quotaAmount = Number(flags.amount);
|
|
300
|
-
if (!Number.isInteger(quotaAmount) || quotaAmount <= 0) {
|
|
301
|
-
throw new UsageError(`--amount 必须是正整数(基础单位数量),收到:${flags.amount}`);
|
|
302
|
-
}
|
|
303
|
-
units = null;
|
|
304
|
-
}
|
|
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
|
-
|
|
369
|
-
/* ---- 钱包预检:余额不足时**默认拒绝**,而不是悄悄建一张要扫码的单 ---- */
|
|
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
|
-
}
|
|
378
|
-
}
|
|
379
|
-
if (
|
|
380
|
-
balanceBefore !== null &&
|
|
381
|
-
estimateCents !== null &&
|
|
382
|
-
balanceBefore < estimateCents &&
|
|
383
|
-
flags.allowWechat !== true
|
|
384
|
-
) {
|
|
385
|
-
throw new CliError(
|
|
386
|
-
`钱包余额不足:本单需要 ${formatMoney(estimateCents)},当前余额 ${formatMoney(balanceBefore)}`,
|
|
387
|
-
{
|
|
388
|
-
kind: 'insufficient_balance',
|
|
389
|
-
code: 'insufficient_balance',
|
|
390
|
-
hint:
|
|
391
|
-
'CLI **不会**发起真实微信支付。请先在网页充够余额再下单;' +
|
|
392
|
-
'如果确实要用微信扫码,加 --allow-wechat 创建"待支付"订单(CLI 只打印支付链接,不扣款,也不替你确认支付)',
|
|
393
|
-
exitCode: EXIT.ERROR,
|
|
394
|
-
},
|
|
395
|
-
);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* ★ 2026-11-17(task-627):下单**改调账务域** ——
|
|
400
|
-
* `/cli` → `wkgeo-cli-fc` 白名单转发 → `billing.orderCreate{kind:'quota'}`(网关 `/billing-core`)。
|
|
401
|
-
* 不再打 `/order` 的 `createQuotaOrder`(钱路径只留 billing 一条:
|
|
402
|
-
* `placeOrder` + `fulfillOrder`,金额只从 `product.price_cents` 算)。
|
|
403
|
-
* ⚠️ 必须带 `idempotency_key`:同键重放回原单(不会多建单、不会二次扣余额)。
|
|
404
|
-
*
|
|
405
|
-
* ★ 2026-11-20(cli-price-quote):`quota_amount` 用**报价回显的那个数**
|
|
406
|
-
* (`chargeQuotaAmount`)—— 保证"报价的单"和"建出来的单"是同一份参数,
|
|
407
|
-
* 不会出现「报价按 A 行算、下单按 B 行算」。
|
|
408
|
-
*/
|
|
409
|
-
const res = await ctx.callData('orderCreate', {
|
|
410
|
-
kind: 'quota',
|
|
411
|
-
quota_key: quote.quota_key,
|
|
412
|
-
quota_amount: chargeQuotaAmount,
|
|
413
|
-
idempotency_key: newOrderIdempotencyKey(),
|
|
414
|
-
});
|
|
415
|
-
const paid = !!(res && res.paid);
|
|
416
|
-
const chargedCents = res && res.amount_cents !== undefined ? Number(res.amount_cents) : null;
|
|
417
|
-
const data = {
|
|
418
|
-
order_id: (res && res.order_id) || null,
|
|
419
|
-
out_trade_no: (res && res.out_trade_no) || null,
|
|
420
|
-
paid,
|
|
421
|
-
amount_cents: Number(res && res.amount_cents) || null,
|
|
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),
|
|
426
|
-
wallet_paid_cents: Number(res && res.wallet_paid_cents) || 0,
|
|
427
|
-
pay_amount_cents: res && res.pay_amount_cents !== undefined ? Number(res.pay_amount_cents) : null,
|
|
428
|
-
code_url: (res && res.code_url) || null,
|
|
429
|
-
expires_at_minutes: (res && res.expires_at_minutes) || null,
|
|
430
|
-
message: (res && res.message) || null,
|
|
431
|
-
product: productInfo,
|
|
432
|
-
quota: { quota_key: quote.quota_key, quota_amount: chargeQuotaAmount, units },
|
|
433
|
-
balance_before_cents: balanceBefore,
|
|
434
|
-
balance_after_cents:
|
|
435
|
-
balanceBefore !== null && paid ? balanceBefore - (Number(res && res.wallet_paid_cents) || 0) : balanceBefore,
|
|
436
|
-
/** 明细:服务端返回的金额行(来自同一份报价/下单快照) */
|
|
437
|
-
quote,
|
|
438
|
-
};
|
|
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
|
-
|
|
450
|
-
if (!out.json) {
|
|
451
|
-
out.head('下单结果');
|
|
452
|
-
out.line(` 订单号:${data.order_id}`);
|
|
453
|
-
out.line(` 商户单号:${data.out_trade_no || '-'}`);
|
|
454
|
-
out.line(` 金额:${data.amount_text}`);
|
|
455
|
-
out.line(` 钱包抵扣:${formatMoney(data.wallet_paid_cents)}`);
|
|
456
|
-
if (data.balance_before_cents !== null) {
|
|
457
|
-
out.line(` 余额:${formatMoney(data.balance_before_cents)}${paid ? ` → ${formatMoney(data.balance_after_cents)}` : ''}`);
|
|
458
|
-
}
|
|
459
|
-
out.line('');
|
|
460
|
-
if (paid) {
|
|
461
|
-
out.ok(`订单已用**钱包余额**支付完成${data.message ? `(${data.message})` : ''}`);
|
|
462
|
-
} else {
|
|
463
|
-
out.warn('订单已创建,但**尚未支付**(余额不足以覆盖全款)');
|
|
464
|
-
out.line(` 待支付金额:${formatMoney(data.pay_amount_cents)}`);
|
|
465
|
-
if (data.code_url) {
|
|
466
|
-
out.line('');
|
|
467
|
-
out.line(' 微信支付链接(需要用手机扫码/打开):');
|
|
468
|
-
out.line(` ${data.code_url}`);
|
|
469
|
-
const qrShown = await renderQrOptional(data.code_url, out);
|
|
470
|
-
if (!qrShown) {
|
|
471
|
-
out.info(' (未安装可选的 qrcode-terminal,只打印链接;装了会自动画二维码)');
|
|
472
|
-
}
|
|
473
|
-
out.line('');
|
|
474
|
-
out.line(` 支付完成后运行:wkgeo orders pay ${data.order_id}`);
|
|
475
|
-
}
|
|
476
|
-
out.info(' CLI 不会发起真实微信扣款,也不会替你确认支付。');
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
ctx.done(data);
|
|
480
|
-
return data;
|
|
481
|
-
},
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
/* ------------------------------------------------------------------ */
|
|
485
|
-
/* orders check / orders pay(同一实现,两个名字) */
|
|
486
|
-
/* ------------------------------------------------------------------ */
|
|
487
|
-
|
|
488
|
-
/**
|
|
489
|
-
* 轮询支付结果 + 展示最新余额。
|
|
490
|
-
*
|
|
491
|
-
* ⚠️ 这里是**只读**语义:`checkOrder` 只会问微信"这笔单付了没",
|
|
492
|
-
* 服务端在"微信侧已支付但本地还是 pending"时就地履约 —— 这是补状态,不是发起支付。
|
|
493
|
-
* CLI **绝不**模拟支付成功、也绝不自动扣款。
|
|
494
|
-
*/
|
|
495
|
-
async function checkOrderImpl(ctx, orderId) {
|
|
496
|
-
const { out } = ctx;
|
|
497
|
-
const res = await ctx.callData('checkOrder', { order_id: orderId });
|
|
498
|
-
const status = (res && res.status) || 'unknown';
|
|
499
|
-
const paid = !!(res && res.paid);
|
|
500
|
-
|
|
501
|
-
// 已支付时顺手把最新余额带出来(老板要的"成功后显示新余额")
|
|
502
|
-
let balanceCents = null;
|
|
503
|
-
if (paid) {
|
|
504
|
-
try {
|
|
505
|
-
const w = await ctx.callData('getMyWallet', { limit: 1 });
|
|
506
|
-
balanceCents = Number((w && w.balance_cents) || 0);
|
|
507
|
-
} catch {
|
|
508
|
-
/* 余额拿不到不影响"已支付"这个结论 */
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const data = {
|
|
513
|
-
order_id: orderId,
|
|
514
|
-
status,
|
|
515
|
-
paid,
|
|
516
|
-
fulfilled: !!(res && res.fulfilled),
|
|
517
|
-
trade_state: (res && res.trade_state) || null,
|
|
518
|
-
code_url: (res && res.code_url) || null,
|
|
519
|
-
balance_cents: balanceCents,
|
|
520
|
-
balance_text: balanceCents === null ? null : formatMoney(balanceCents),
|
|
521
|
-
order: (res && res.order) || null,
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
if (!out.json) {
|
|
525
|
-
if (paid) {
|
|
526
|
-
out.ok(`订单已支付:${orderId}(状态 ${status}${data.fulfilled ? ',已履约' : ''})`);
|
|
527
|
-
if (balanceCents !== null) out.line(` 当前余额:${data.balance_text}`);
|
|
528
|
-
} else if (status === 'pending') {
|
|
529
|
-
out.warn(`订单仍未支付:${orderId}(微信侧状态 ${data.trade_state || 'NOTPAY'})`);
|
|
530
|
-
if (data.code_url) {
|
|
531
|
-
out.line(` 支付链接:${data.code_url}`);
|
|
532
|
-
await renderQrOptional(data.code_url, out);
|
|
533
|
-
}
|
|
534
|
-
out.info(' 请在手机上完成支付后再运行本命令确认;CLI 不会替你付款,也不会模拟支付成功。');
|
|
535
|
-
} else {
|
|
536
|
-
out.line(`订单状态:${status}`);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
ctx.done(data);
|
|
540
|
-
return data;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
export const ordersCheckCommand = {
|
|
544
|
-
name: 'orders check',
|
|
545
|
-
summary: '查询某笔订单的支付结果(微信侧已支付则就地履约),并显示最新余额',
|
|
546
|
-
usage: 'wkgeo orders check <订单ID>',
|
|
547
|
-
args: [{ name: 'orderId', required: true, desc: '订单 ID' }],
|
|
548
|
-
run: (ctx) => checkOrderImpl(ctx, ctx.positional.orderId),
|
|
549
|
-
};
|
|
550
|
-
|
|
551
|
-
/** `orders pay` 与 `orders check` 是**同一实现**:保留 pay 这个名字是因为
|
|
552
|
-
* 老板给的任务表里写的是 `orders pay <orderId>`(核心:完成"支付操作")。
|
|
553
|
-
* 真实语义在下单那一步就支付完成了,这里只是查/收敛状态,详见本文件顶部说明。 */
|
|
554
|
-
export const ordersPayCommand = {
|
|
555
|
-
name: 'orders pay',
|
|
556
|
-
summary: '(`orders check` 的别名)确认/收敛订单支付状态并显示最新余额',
|
|
557
|
-
usage: 'wkgeo orders pay <订单ID>',
|
|
558
|
-
args: [{ name: 'orderId', required: true, desc: '订单 ID' }],
|
|
559
|
-
run: (ctx) => checkOrderImpl(ctx, ctx.positional.orderId),
|
|
560
|
-
};
|
|
561
|
-
|
|
562
|
-
/* ------------------------------------------------------------------ */
|
|
563
|
-
/* orders get */
|
|
564
|
-
/* ------------------------------------------------------------------ */
|
|
565
|
-
|
|
566
|
-
export const ordersGetCommand = {
|
|
567
|
-
name: 'orders get',
|
|
568
|
-
summary: '看某笔订单的详情(在自己的订单列表里查找,不触发任何微信查询)',
|
|
569
|
-
usage: 'wkgeo orders get <订单ID> [--max-scan 1000]',
|
|
570
|
-
args: [{ name: 'orderId', required: true, desc: '订单 ID' }],
|
|
571
|
-
flags: {
|
|
572
|
-
'max-scan': { type: 'string', desc: '最多翻查多少条历史订单(默认 1000)' },
|
|
573
|
-
},
|
|
574
|
-
async run(ctx) {
|
|
575
|
-
const { out, flags } = ctx;
|
|
576
|
-
const orderId = ctx.positional.orderId;
|
|
577
|
-
const maxScan = Math.min(Math.max(Number(flags.maxScan) || 1000, 100), 5000);
|
|
578
|
-
|
|
579
|
-
/**
|
|
580
|
-
* 为什么是"翻列表"而不是专用接口:
|
|
581
|
-
* 服务端**没有** `getMyOrder(id)` 这个 action(只有 `checkOrder`)。
|
|
582
|
-
* 而 `checkOrder` 对 pending 订单会去问微信 —— 一个"看详情"的命令不该有外部副作用,
|
|
583
|
-
* 所以这里只翻自己的订单列表(`listMyOrders`,每页 100)。
|
|
584
|
-
*/
|
|
585
|
-
let found = null;
|
|
586
|
-
let scanned = 0;
|
|
587
|
-
for (let offset = 0; offset < maxScan; offset += 100) {
|
|
588
|
-
const res = await ctx.callData('listMyOrders', { limit: 100, offset });
|
|
589
|
-
const rows = (res && Array.isArray(res.rows) ? res.rows : []);
|
|
590
|
-
scanned += rows.length;
|
|
591
|
-
found = rows.find((r) => r.id === orderId || r.out_trade_no === orderId) || null;
|
|
592
|
-
if (found || rows.length < 100) break;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
if (!found) {
|
|
596
|
-
throw new CliError(`在你最近的 ${scanned} 条订单里没找到「${orderId}」`, {
|
|
597
|
-
kind: 'not_found',
|
|
598
|
-
code: 404,
|
|
599
|
-
hint: `该订单可能更早(可加 --max-scan 扩大范围),或不是当前账号的订单;要看实时支付状态可用 \`wkgeo orders check ${orderId}\``,
|
|
600
|
-
});
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
const data = {
|
|
604
|
-
order: {
|
|
605
|
-
id: found.id,
|
|
606
|
-
out_trade_no: found.out_trade_no,
|
|
607
|
-
kind: found.kind,
|
|
608
|
-
status: found.status,
|
|
609
|
-
amount_cents: Number(found.amount_cents) || 0,
|
|
610
|
-
amount_text: formatMoney(found.amount_cents),
|
|
611
|
-
wallet_paid_cents: Number(found.wallet_paid_cents) || 0,
|
|
612
|
-
wallet_paid_text: formatMoney(found.wallet_paid_cents),
|
|
613
|
-
currency: found.currency || null,
|
|
614
|
-
transaction_id: found.transaction_id || null,
|
|
615
|
-
paid_at: found.paid_at || null,
|
|
616
|
-
expires_at: found.expires_at || null,
|
|
617
|
-
created_at: found.created_at || null,
|
|
618
|
-
payload: found.payload || {},
|
|
619
|
-
},
|
|
620
|
-
scanned,
|
|
621
|
-
};
|
|
622
|
-
|
|
623
|
-
if (!out.json) {
|
|
624
|
-
const o = data.order;
|
|
625
|
-
out.head('订单详情');
|
|
626
|
-
out.line(` 订单号:${o.id}`);
|
|
627
|
-
out.line(` 商户单号:${o.out_trade_no || '-'}`);
|
|
628
|
-
out.line(` 类型/状态:${o.kind} / ${o.status}`);
|
|
629
|
-
out.line(` 金额:${o.amount_text}(余额支付 ${o.wallet_paid_text})`);
|
|
630
|
-
out.line(` 创建时间:${formatTime(o.created_at)}`);
|
|
631
|
-
out.line(` 支付时间:${formatTime(o.paid_at)}`);
|
|
632
|
-
out.line(` 微信交易号:${o.transaction_id || '-'}`);
|
|
633
|
-
out.line(` 商品明细:${JSON.stringify(o.payload)}`);
|
|
634
|
-
}
|
|
635
|
-
ctx.done(data);
|
|
636
|
-
return data;
|
|
637
|
-
},
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
/* ------------------------------------------------------------------ */
|
|
641
|
-
/* orders list */
|
|
642
|
-
/* ------------------------------------------------------------------ */
|
|
643
|
-
|
|
644
|
-
export const ordersListCommand = {
|
|
645
|
-
name: 'orders list',
|
|
646
|
-
summary: '我的订单列表',
|
|
647
|
-
usage: 'wkgeo orders list [--limit 20] [--offset 0] [--status paid]',
|
|
648
|
-
flags: {
|
|
649
|
-
limit: { type: 'string', desc: '返回条数(1~100,默认 20)' },
|
|
650
|
-
offset: { type: 'string', desc: '偏移(默认 0)' },
|
|
651
|
-
status: { type: 'string', desc: '只看某个状态(pending/paid/refunded/failed/closed)' },
|
|
652
|
-
},
|
|
653
|
-
async run(ctx) {
|
|
654
|
-
const { out, flags } = ctx;
|
|
655
|
-
const payload = {};
|
|
656
|
-
if (flags.limit !== undefined) payload.limit = Number(flags.limit);
|
|
657
|
-
if (flags.offset !== undefined) payload.offset = Number(flags.offset);
|
|
658
|
-
const res = await ctx.callData('listMyOrders', payload);
|
|
659
|
-
let rows = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => ({
|
|
660
|
-
id: r.id,
|
|
661
|
-
out_trade_no: r.out_trade_no,
|
|
662
|
-
kind: r.kind,
|
|
663
|
-
status: r.status,
|
|
664
|
-
amount_cents: Number(r.amount_cents) || 0,
|
|
665
|
-
amount_text: formatMoney(r.amount_cents),
|
|
666
|
-
wallet_paid_cents: Number(r.wallet_paid_cents) || 0,
|
|
667
|
-
wallet_paid_text: formatMoney(r.wallet_paid_cents),
|
|
668
|
-
paid_at: r.paid_at || null,
|
|
669
|
-
created_at: r.created_at,
|
|
670
|
-
}));
|
|
671
|
-
if (flags.status) rows = rows.filter((r) => r.status === flags.status);
|
|
672
|
-
const data = { rows, total: flags.status ? rows.length : (res && res.total) || rows.length };
|
|
673
|
-
|
|
674
|
-
if (!out.json) {
|
|
675
|
-
out.head(`我的订单(${data.total} 条${flags.status ? `,已按状态 ${flags.status} 过滤` : ''})`);
|
|
676
|
-
out.table(
|
|
677
|
-
[
|
|
678
|
-
{ title: '订单号', key: 'id', width: 38 },
|
|
679
|
-
{ title: '类型', key: 'kind', width: 10 },
|
|
680
|
-
{ title: '状态', key: 'status', width: 10 },
|
|
681
|
-
{ title: '金额', key: 'amount_text', width: 10 },
|
|
682
|
-
{ title: '余额支付', key: 'wallet_paid_text', width: 10 },
|
|
683
|
-
{ title: '创建时间', key: 'created_at', format: (v) => formatTime(v), width: 20 },
|
|
684
|
-
],
|
|
685
|
-
rows,
|
|
686
|
-
);
|
|
687
|
-
}
|
|
688
|
-
ctx.done(data);
|
|
689
|
-
return data;
|
|
690
|
-
},
|
|
691
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* 订单命令:`products list` / `orders create` / `orders pay` / `orders list`
|
|
3
|
+
*
|
|
4
|
+
* ## 「钱包余额支付」在本项目里到底发生在哪一步(重要,别被命令名骗了)
|
|
5
|
+
*
|
|
6
|
+
* 服务端 `placeOrder()` 的行为是:**下单时就先用余额抵扣**,
|
|
7
|
+
* 余额足够 → `moveWallet(-金额)` + `fulfillOrder()` → 直接返回 `paid: true`。
|
|
8
|
+
* 也就是说"用钱包支付"是**下单动作的一部分**,不是独立的一步。
|
|
9
|
+
*
|
|
10
|
+
* 所以本 CLI 的口径:
|
|
11
|
+
* * `orders create` = 下单 + (余额够就)完成支付,返回值里 `paid` 说明结果;
|
|
12
|
+
* * `orders pay <id>` = 对**还没支付**的订单做"查单/收敛状态"(服务端 checkOrder):
|
|
13
|
+
* 微信侧已支付而本地还是 pending → 就地履约;否则告诉你还在等支付。
|
|
14
|
+
* * CLI **永远不会**自己去扫微信码 / 发起任何真实扣款。
|
|
15
|
+
*
|
|
16
|
+
* ⚠️ 没有做"把余额补到一个已存在的 pending 订单上"的命令 —— 服务端**没有这个 action**
|
|
17
|
+
* (`placeOrder` 里余额抵扣只在建单那一刻发生)。要做需要服务端新增接口,
|
|
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`。
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { UsageError, CliError, EXIT } from '../errors.mjs';
|
|
30
|
+
import { formatMoney, formatTime } from '../format.mjs';
|
|
31
|
+
import { renderQrOptional } from '../qr.mjs';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 下单幂等键(★ 2026-11-17 task-627)。
|
|
35
|
+
*
|
|
36
|
+
* 服务端 `billing.orderCreate` 的口径:**同一键同一单**(重放回原单,含新的收银台令牌);
|
|
37
|
+
* 同键带不同参数回 409。CLI 不自动重试(`api.mjs` 约定),所以这里只保证
|
|
38
|
+
* 「每次执行 = 一次新尝试 = 一个新键」,绝不会把两次不同的购买合成一张单。
|
|
39
|
+
* 格式受服务端硬约束:`^[A-Za-z0-9_-]{8,80}$`。
|
|
40
|
+
*/
|
|
41
|
+
function newOrderIdempotencyKey() {
|
|
42
|
+
const rand = Math.random().toString(36).slice(2, 11);
|
|
43
|
+
|
|
44
|
+
return `cli-quota-${Date.now().toString(36)}-${rand}`;
|
|
45
|
+
}
|
|
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
|
+
|
|
178
|
+
/* ------------------------------------------------------------------ */
|
|
179
|
+
/* products list */
|
|
180
|
+
/* ------------------------------------------------------------------ */
|
|
181
|
+
|
|
182
|
+
export const productsListCommand = {
|
|
183
|
+
name: 'products list',
|
|
184
|
+
summary: '可购买的产品/加量包(用来找 --product 该填什么)',
|
|
185
|
+
usage: 'wkgeo products list',
|
|
186
|
+
async run(ctx) {
|
|
187
|
+
const { out } = ctx;
|
|
188
|
+
const res = await ctx.callData('listProducts', {});
|
|
189
|
+
const rows = (res && Array.isArray(res.rows) ? res.rows : []).map((p) => {
|
|
190
|
+
const params = p.params || {};
|
|
191
|
+
return {
|
|
192
|
+
code: p.code,
|
|
193
|
+
name: p.name,
|
|
194
|
+
kind: p.kind,
|
|
195
|
+
unit: p.unit,
|
|
196
|
+
price_cents: p.price_cents,
|
|
197
|
+
price_text: formatMoney(p.price_cents),
|
|
198
|
+
quota_key: params.quota_key || null,
|
|
199
|
+
quota_multiplier: params.quota_multiplier ?? null,
|
|
200
|
+
min_qty: p.min_qty ?? null,
|
|
201
|
+
max_qty: p.max_qty ?? null,
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
const data = { rows };
|
|
205
|
+
if (!out.json) {
|
|
206
|
+
out.head(`可购买的产品(${rows.length} 项)`);
|
|
207
|
+
out.table(
|
|
208
|
+
[
|
|
209
|
+
{ title: '产品代码', key: 'code', width: 24 },
|
|
210
|
+
{ title: '名称', key: 'name', width: 20 },
|
|
211
|
+
{ title: '类型', key: 'kind', width: 14 },
|
|
212
|
+
{ title: '单价', key: 'price_text', width: 10 },
|
|
213
|
+
{ title: '单位', key: 'unit', width: 8 },
|
|
214
|
+
{ title: '配额键', key: 'quota_key', width: 16 },
|
|
215
|
+
{ title: '倍率', key: 'quota_multiplier', width: 6 },
|
|
216
|
+
],
|
|
217
|
+
rows,
|
|
218
|
+
);
|
|
219
|
+
out.info(' 下单示例:wkgeo orders create --product <产品代码> --units 1');
|
|
220
|
+
out.info(' 查加购价(不建单、不扣款):wkgeo orders create --product <产品代码> --units 1 --dry-run');
|
|
221
|
+
}
|
|
222
|
+
ctx.done(data);
|
|
223
|
+
return data;
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/* ------------------------------------------------------------------ */
|
|
228
|
+
/* orders create */
|
|
229
|
+
/* ------------------------------------------------------------------ */
|
|
230
|
+
|
|
231
|
+
export const ordersCreateCommand = {
|
|
232
|
+
name: 'orders create',
|
|
233
|
+
summary: '下单(余额足够时**当场用钱包余额付清**;不会发起真实微信扣款)',
|
|
234
|
+
usage:
|
|
235
|
+
'wkgeo orders create --product <产品代码> --units <份数> [--allow-wechat] [--dry-run]\n' +
|
|
236
|
+
'wkgeo orders create --quota <配额键> --amount <基础单位数> [--allow-wechat] [--dry-run]',
|
|
237
|
+
flags: {
|
|
238
|
+
product: { type: 'string', desc: '产品代码或配额键(见 `wkgeo products list`)' },
|
|
239
|
+
units: { type: 'string', desc: '购买份数(默认 1,按产品单位计价)' },
|
|
240
|
+
quota: { type: 'string', desc: '直接指定配额键(如 storage_mb)' },
|
|
241
|
+
amount: { type: 'string', desc: '直接指定基础单位数量(配合 --quota)' },
|
|
242
|
+
'allow-wechat': {
|
|
243
|
+
type: 'boolean',
|
|
244
|
+
desc: '余额不足时仍创建订单(会返回微信 code_url,**需要用户自己扫码**;CLI 不会扣款)',
|
|
245
|
+
},
|
|
246
|
+
'dry-run': {
|
|
247
|
+
type: 'boolean',
|
|
248
|
+
desc: '只向服务端查**报价**(不建单、不扣款):加购价以它为准,与实收同一份算法',
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
async run(ctx) {
|
|
252
|
+
const { out, flags } = ctx;
|
|
253
|
+
|
|
254
|
+
if (!flags.product && !flags.quota) {
|
|
255
|
+
throw new UsageError('需要 --product <产品代码> 或 --quota <配额键>(用 `wkgeo products list` 查可用产品)');
|
|
256
|
+
}
|
|
257
|
+
if (flags.product && flags.quota) {
|
|
258
|
+
throw new UsageError('--product 与 --quota 不能同时使用');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
let quotaKey;
|
|
262
|
+
let quotaAmount;
|
|
263
|
+
let units = null;
|
|
264
|
+
/** 本地命中的产品行:**只用来定数量 / 展示名称**,金额一律不从这里取 */
|
|
265
|
+
let catalogHit = null;
|
|
266
|
+
|
|
267
|
+
if (flags.product) {
|
|
268
|
+
units = flags.units === undefined ? 1 : Number(flags.units);
|
|
269
|
+
if (!Number.isInteger(units) || units <= 0) throw new UsageError(`--units 必须是正整数,收到:${flags.units}`);
|
|
270
|
+
|
|
271
|
+
const catalog = await ctx.callData('listProducts', {});
|
|
272
|
+
const list = (catalog && Array.isArray(catalog.rows) ? catalog.rows : []).map((p) => ({
|
|
273
|
+
...p,
|
|
274
|
+
params: p.params || {},
|
|
275
|
+
}));
|
|
276
|
+
const hit = pickProductRow(list, flags.product);
|
|
277
|
+
|
|
278
|
+
if (!hit) {
|
|
279
|
+
throw new CliError(`找不到产品(或配额键)「${flags.product}」`, {
|
|
280
|
+
kind: 'not_found',
|
|
281
|
+
code: 404,
|
|
282
|
+
hint: `用 \`wkgeo products list\` 看可用产品代码;也可用 --quota <配额键> --amount <数量> 直接按配额下单`,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
if (hit.kind !== 'resource' || !hit.params.quota_key) {
|
|
286
|
+
throw new CliError(`产品「${hit.code}」是 ${hit.kind} 类型,当前 CLI 只支持"加量包/资源型"下单`, {
|
|
287
|
+
kind: 'unsupported',
|
|
288
|
+
code: 400,
|
|
289
|
+
hint: '订阅套餐请在网页后台自助购买(金额较大且会改订阅权益)',
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
const mult = Number(hit.params.quota_multiplier) || 1;
|
|
293
|
+
quotaKey = hit.params.quota_key;
|
|
294
|
+
/** ★ 基础单位数量 = 份数 × 倍率;**这是"买多少",不是"多少钱"** */
|
|
295
|
+
quotaAmount = units * mult;
|
|
296
|
+
catalogHit = hit;
|
|
297
|
+
} else {
|
|
298
|
+
quotaKey = flags.quota;
|
|
299
|
+
quotaAmount = Number(flags.amount);
|
|
300
|
+
if (!Number.isInteger(quotaAmount) || quotaAmount <= 0) {
|
|
301
|
+
throw new UsageError(`--amount 必须是正整数(基础单位数量),收到:${flags.amount}`);
|
|
302
|
+
}
|
|
303
|
+
units = null;
|
|
304
|
+
}
|
|
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
|
+
|
|
369
|
+
/* ---- 钱包预检:余额不足时**默认拒绝**,而不是悄悄建一张要扫码的单 ---- */
|
|
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
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (
|
|
380
|
+
balanceBefore !== null &&
|
|
381
|
+
estimateCents !== null &&
|
|
382
|
+
balanceBefore < estimateCents &&
|
|
383
|
+
flags.allowWechat !== true
|
|
384
|
+
) {
|
|
385
|
+
throw new CliError(
|
|
386
|
+
`钱包余额不足:本单需要 ${formatMoney(estimateCents)},当前余额 ${formatMoney(balanceBefore)}`,
|
|
387
|
+
{
|
|
388
|
+
kind: 'insufficient_balance',
|
|
389
|
+
code: 'insufficient_balance',
|
|
390
|
+
hint:
|
|
391
|
+
'CLI **不会**发起真实微信支付。请先在网页充够余额再下单;' +
|
|
392
|
+
'如果确实要用微信扫码,加 --allow-wechat 创建"待支付"订单(CLI 只打印支付链接,不扣款,也不替你确认支付)',
|
|
393
|
+
exitCode: EXIT.ERROR,
|
|
394
|
+
},
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* ★ 2026-11-17(task-627):下单**改调账务域** ——
|
|
400
|
+
* `/cli` → `wkgeo-cli-fc` 白名单转发 → `billing.orderCreate{kind:'quota'}`(网关 `/billing-core`)。
|
|
401
|
+
* 不再打 `/order` 的 `createQuotaOrder`(钱路径只留 billing 一条:
|
|
402
|
+
* `placeOrder` + `fulfillOrder`,金额只从 `product.price_cents` 算)。
|
|
403
|
+
* ⚠️ 必须带 `idempotency_key`:同键重放回原单(不会多建单、不会二次扣余额)。
|
|
404
|
+
*
|
|
405
|
+
* ★ 2026-11-20(cli-price-quote):`quota_amount` 用**报价回显的那个数**
|
|
406
|
+
* (`chargeQuotaAmount`)—— 保证"报价的单"和"建出来的单"是同一份参数,
|
|
407
|
+
* 不会出现「报价按 A 行算、下单按 B 行算」。
|
|
408
|
+
*/
|
|
409
|
+
const res = await ctx.callData('orderCreate', {
|
|
410
|
+
kind: 'quota',
|
|
411
|
+
quota_key: quote.quota_key,
|
|
412
|
+
quota_amount: chargeQuotaAmount,
|
|
413
|
+
idempotency_key: newOrderIdempotencyKey(),
|
|
414
|
+
});
|
|
415
|
+
const paid = !!(res && res.paid);
|
|
416
|
+
const chargedCents = res && res.amount_cents !== undefined ? Number(res.amount_cents) : null;
|
|
417
|
+
const data = {
|
|
418
|
+
order_id: (res && res.order_id) || null,
|
|
419
|
+
out_trade_no: (res && res.out_trade_no) || null,
|
|
420
|
+
paid,
|
|
421
|
+
amount_cents: Number(res && res.amount_cents) || null,
|
|
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),
|
|
426
|
+
wallet_paid_cents: Number(res && res.wallet_paid_cents) || 0,
|
|
427
|
+
pay_amount_cents: res && res.pay_amount_cents !== undefined ? Number(res.pay_amount_cents) : null,
|
|
428
|
+
code_url: (res && res.code_url) || null,
|
|
429
|
+
expires_at_minutes: (res && res.expires_at_minutes) || null,
|
|
430
|
+
message: (res && res.message) || null,
|
|
431
|
+
product: productInfo,
|
|
432
|
+
quota: { quota_key: quote.quota_key, quota_amount: chargeQuotaAmount, units },
|
|
433
|
+
balance_before_cents: balanceBefore,
|
|
434
|
+
balance_after_cents:
|
|
435
|
+
balanceBefore !== null && paid ? balanceBefore - (Number(res && res.wallet_paid_cents) || 0) : balanceBefore,
|
|
436
|
+
/** 明细:服务端返回的金额行(来自同一份报价/下单快照) */
|
|
437
|
+
quote,
|
|
438
|
+
};
|
|
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
|
+
|
|
450
|
+
if (!out.json) {
|
|
451
|
+
out.head('下单结果');
|
|
452
|
+
out.line(` 订单号:${data.order_id}`);
|
|
453
|
+
out.line(` 商户单号:${data.out_trade_no || '-'}`);
|
|
454
|
+
out.line(` 金额:${data.amount_text}`);
|
|
455
|
+
out.line(` 钱包抵扣:${formatMoney(data.wallet_paid_cents)}`);
|
|
456
|
+
if (data.balance_before_cents !== null) {
|
|
457
|
+
out.line(` 余额:${formatMoney(data.balance_before_cents)}${paid ? ` → ${formatMoney(data.balance_after_cents)}` : ''}`);
|
|
458
|
+
}
|
|
459
|
+
out.line('');
|
|
460
|
+
if (paid) {
|
|
461
|
+
out.ok(`订单已用**钱包余额**支付完成${data.message ? `(${data.message})` : ''}`);
|
|
462
|
+
} else {
|
|
463
|
+
out.warn('订单已创建,但**尚未支付**(余额不足以覆盖全款)');
|
|
464
|
+
out.line(` 待支付金额:${formatMoney(data.pay_amount_cents)}`);
|
|
465
|
+
if (data.code_url) {
|
|
466
|
+
out.line('');
|
|
467
|
+
out.line(' 微信支付链接(需要用手机扫码/打开):');
|
|
468
|
+
out.line(` ${data.code_url}`);
|
|
469
|
+
const qrShown = await renderQrOptional(data.code_url, out);
|
|
470
|
+
if (!qrShown) {
|
|
471
|
+
out.info(' (未安装可选的 qrcode-terminal,只打印链接;装了会自动画二维码)');
|
|
472
|
+
}
|
|
473
|
+
out.line('');
|
|
474
|
+
out.line(` 支付完成后运行:wkgeo orders pay ${data.order_id}`);
|
|
475
|
+
}
|
|
476
|
+
out.info(' CLI 不会发起真实微信扣款,也不会替你确认支付。');
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
ctx.done(data);
|
|
480
|
+
return data;
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
/* ------------------------------------------------------------------ */
|
|
485
|
+
/* orders check / orders pay(同一实现,两个名字) */
|
|
486
|
+
/* ------------------------------------------------------------------ */
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* 轮询支付结果 + 展示最新余额。
|
|
490
|
+
*
|
|
491
|
+
* ⚠️ 这里是**只读**语义:`checkOrder` 只会问微信"这笔单付了没",
|
|
492
|
+
* 服务端在"微信侧已支付但本地还是 pending"时就地履约 —— 这是补状态,不是发起支付。
|
|
493
|
+
* CLI **绝不**模拟支付成功、也绝不自动扣款。
|
|
494
|
+
*/
|
|
495
|
+
async function checkOrderImpl(ctx, orderId) {
|
|
496
|
+
const { out } = ctx;
|
|
497
|
+
const res = await ctx.callData('checkOrder', { order_id: orderId });
|
|
498
|
+
const status = (res && res.status) || 'unknown';
|
|
499
|
+
const paid = !!(res && res.paid);
|
|
500
|
+
|
|
501
|
+
// 已支付时顺手把最新余额带出来(老板要的"成功后显示新余额")
|
|
502
|
+
let balanceCents = null;
|
|
503
|
+
if (paid) {
|
|
504
|
+
try {
|
|
505
|
+
const w = await ctx.callData('getMyWallet', { limit: 1 });
|
|
506
|
+
balanceCents = Number((w && w.balance_cents) || 0);
|
|
507
|
+
} catch {
|
|
508
|
+
/* 余额拿不到不影响"已支付"这个结论 */
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const data = {
|
|
513
|
+
order_id: orderId,
|
|
514
|
+
status,
|
|
515
|
+
paid,
|
|
516
|
+
fulfilled: !!(res && res.fulfilled),
|
|
517
|
+
trade_state: (res && res.trade_state) || null,
|
|
518
|
+
code_url: (res && res.code_url) || null,
|
|
519
|
+
balance_cents: balanceCents,
|
|
520
|
+
balance_text: balanceCents === null ? null : formatMoney(balanceCents),
|
|
521
|
+
order: (res && res.order) || null,
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
if (!out.json) {
|
|
525
|
+
if (paid) {
|
|
526
|
+
out.ok(`订单已支付:${orderId}(状态 ${status}${data.fulfilled ? ',已履约' : ''})`);
|
|
527
|
+
if (balanceCents !== null) out.line(` 当前余额:${data.balance_text}`);
|
|
528
|
+
} else if (status === 'pending') {
|
|
529
|
+
out.warn(`订单仍未支付:${orderId}(微信侧状态 ${data.trade_state || 'NOTPAY'})`);
|
|
530
|
+
if (data.code_url) {
|
|
531
|
+
out.line(` 支付链接:${data.code_url}`);
|
|
532
|
+
await renderQrOptional(data.code_url, out);
|
|
533
|
+
}
|
|
534
|
+
out.info(' 请在手机上完成支付后再运行本命令确认;CLI 不会替你付款,也不会模拟支付成功。');
|
|
535
|
+
} else {
|
|
536
|
+
out.line(`订单状态:${status}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
ctx.done(data);
|
|
540
|
+
return data;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export const ordersCheckCommand = {
|
|
544
|
+
name: 'orders check',
|
|
545
|
+
summary: '查询某笔订单的支付结果(微信侧已支付则就地履约),并显示最新余额',
|
|
546
|
+
usage: 'wkgeo orders check <订单ID>',
|
|
547
|
+
args: [{ name: 'orderId', required: true, desc: '订单 ID' }],
|
|
548
|
+
run: (ctx) => checkOrderImpl(ctx, ctx.positional.orderId),
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
/** `orders pay` 与 `orders check` 是**同一实现**:保留 pay 这个名字是因为
|
|
552
|
+
* 老板给的任务表里写的是 `orders pay <orderId>`(核心:完成"支付操作")。
|
|
553
|
+
* 真实语义在下单那一步就支付完成了,这里只是查/收敛状态,详见本文件顶部说明。 */
|
|
554
|
+
export const ordersPayCommand = {
|
|
555
|
+
name: 'orders pay',
|
|
556
|
+
summary: '(`orders check` 的别名)确认/收敛订单支付状态并显示最新余额',
|
|
557
|
+
usage: 'wkgeo orders pay <订单ID>',
|
|
558
|
+
args: [{ name: 'orderId', required: true, desc: '订单 ID' }],
|
|
559
|
+
run: (ctx) => checkOrderImpl(ctx, ctx.positional.orderId),
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
/* ------------------------------------------------------------------ */
|
|
563
|
+
/* orders get */
|
|
564
|
+
/* ------------------------------------------------------------------ */
|
|
565
|
+
|
|
566
|
+
export const ordersGetCommand = {
|
|
567
|
+
name: 'orders get',
|
|
568
|
+
summary: '看某笔订单的详情(在自己的订单列表里查找,不触发任何微信查询)',
|
|
569
|
+
usage: 'wkgeo orders get <订单ID> [--max-scan 1000]',
|
|
570
|
+
args: [{ name: 'orderId', required: true, desc: '订单 ID' }],
|
|
571
|
+
flags: {
|
|
572
|
+
'max-scan': { type: 'string', desc: '最多翻查多少条历史订单(默认 1000)' },
|
|
573
|
+
},
|
|
574
|
+
async run(ctx) {
|
|
575
|
+
const { out, flags } = ctx;
|
|
576
|
+
const orderId = ctx.positional.orderId;
|
|
577
|
+
const maxScan = Math.min(Math.max(Number(flags.maxScan) || 1000, 100), 5000);
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* 为什么是"翻列表"而不是专用接口:
|
|
581
|
+
* 服务端**没有** `getMyOrder(id)` 这个 action(只有 `checkOrder`)。
|
|
582
|
+
* 而 `checkOrder` 对 pending 订单会去问微信 —— 一个"看详情"的命令不该有外部副作用,
|
|
583
|
+
* 所以这里只翻自己的订单列表(`listMyOrders`,每页 100)。
|
|
584
|
+
*/
|
|
585
|
+
let found = null;
|
|
586
|
+
let scanned = 0;
|
|
587
|
+
for (let offset = 0; offset < maxScan; offset += 100) {
|
|
588
|
+
const res = await ctx.callData('listMyOrders', { limit: 100, offset });
|
|
589
|
+
const rows = (res && Array.isArray(res.rows) ? res.rows : []);
|
|
590
|
+
scanned += rows.length;
|
|
591
|
+
found = rows.find((r) => r.id === orderId || r.out_trade_no === orderId) || null;
|
|
592
|
+
if (found || rows.length < 100) break;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
if (!found) {
|
|
596
|
+
throw new CliError(`在你最近的 ${scanned} 条订单里没找到「${orderId}」`, {
|
|
597
|
+
kind: 'not_found',
|
|
598
|
+
code: 404,
|
|
599
|
+
hint: `该订单可能更早(可加 --max-scan 扩大范围),或不是当前账号的订单;要看实时支付状态可用 \`wkgeo orders check ${orderId}\``,
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const data = {
|
|
604
|
+
order: {
|
|
605
|
+
id: found.id,
|
|
606
|
+
out_trade_no: found.out_trade_no,
|
|
607
|
+
kind: found.kind,
|
|
608
|
+
status: found.status,
|
|
609
|
+
amount_cents: Number(found.amount_cents) || 0,
|
|
610
|
+
amount_text: formatMoney(found.amount_cents),
|
|
611
|
+
wallet_paid_cents: Number(found.wallet_paid_cents) || 0,
|
|
612
|
+
wallet_paid_text: formatMoney(found.wallet_paid_cents),
|
|
613
|
+
currency: found.currency || null,
|
|
614
|
+
transaction_id: found.transaction_id || null,
|
|
615
|
+
paid_at: found.paid_at || null,
|
|
616
|
+
expires_at: found.expires_at || null,
|
|
617
|
+
created_at: found.created_at || null,
|
|
618
|
+
payload: found.payload || {},
|
|
619
|
+
},
|
|
620
|
+
scanned,
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
if (!out.json) {
|
|
624
|
+
const o = data.order;
|
|
625
|
+
out.head('订单详情');
|
|
626
|
+
out.line(` 订单号:${o.id}`);
|
|
627
|
+
out.line(` 商户单号:${o.out_trade_no || '-'}`);
|
|
628
|
+
out.line(` 类型/状态:${o.kind} / ${o.status}`);
|
|
629
|
+
out.line(` 金额:${o.amount_text}(余额支付 ${o.wallet_paid_text})`);
|
|
630
|
+
out.line(` 创建时间:${formatTime(o.created_at)}`);
|
|
631
|
+
out.line(` 支付时间:${formatTime(o.paid_at)}`);
|
|
632
|
+
out.line(` 微信交易号:${o.transaction_id || '-'}`);
|
|
633
|
+
out.line(` 商品明细:${JSON.stringify(o.payload)}`);
|
|
634
|
+
}
|
|
635
|
+
ctx.done(data);
|
|
636
|
+
return data;
|
|
637
|
+
},
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
/* ------------------------------------------------------------------ */
|
|
641
|
+
/* orders list */
|
|
642
|
+
/* ------------------------------------------------------------------ */
|
|
643
|
+
|
|
644
|
+
export const ordersListCommand = {
|
|
645
|
+
name: 'orders list',
|
|
646
|
+
summary: '我的订单列表',
|
|
647
|
+
usage: 'wkgeo orders list [--limit 20] [--offset 0] [--status paid]',
|
|
648
|
+
flags: {
|
|
649
|
+
limit: { type: 'string', desc: '返回条数(1~100,默认 20)' },
|
|
650
|
+
offset: { type: 'string', desc: '偏移(默认 0)' },
|
|
651
|
+
status: { type: 'string', desc: '只看某个状态(pending/paid/refunded/failed/closed)' },
|
|
652
|
+
},
|
|
653
|
+
async run(ctx) {
|
|
654
|
+
const { out, flags } = ctx;
|
|
655
|
+
const payload = {};
|
|
656
|
+
if (flags.limit !== undefined) payload.limit = Number(flags.limit);
|
|
657
|
+
if (flags.offset !== undefined) payload.offset = Number(flags.offset);
|
|
658
|
+
const res = await ctx.callData('listMyOrders', payload);
|
|
659
|
+
let rows = (res && Array.isArray(res.rows) ? res.rows : []).map((r) => ({
|
|
660
|
+
id: r.id,
|
|
661
|
+
out_trade_no: r.out_trade_no,
|
|
662
|
+
kind: r.kind,
|
|
663
|
+
status: r.status,
|
|
664
|
+
amount_cents: Number(r.amount_cents) || 0,
|
|
665
|
+
amount_text: formatMoney(r.amount_cents),
|
|
666
|
+
wallet_paid_cents: Number(r.wallet_paid_cents) || 0,
|
|
667
|
+
wallet_paid_text: formatMoney(r.wallet_paid_cents),
|
|
668
|
+
paid_at: r.paid_at || null,
|
|
669
|
+
created_at: r.created_at,
|
|
670
|
+
}));
|
|
671
|
+
if (flags.status) rows = rows.filter((r) => r.status === flags.status);
|
|
672
|
+
const data = { rows, total: flags.status ? rows.length : (res && res.total) || rows.length };
|
|
673
|
+
|
|
674
|
+
if (!out.json) {
|
|
675
|
+
out.head(`我的订单(${data.total} 条${flags.status ? `,已按状态 ${flags.status} 过滤` : ''})`);
|
|
676
|
+
out.table(
|
|
677
|
+
[
|
|
678
|
+
{ title: '订单号', key: 'id', width: 38 },
|
|
679
|
+
{ title: '类型', key: 'kind', width: 10 },
|
|
680
|
+
{ title: '状态', key: 'status', width: 10 },
|
|
681
|
+
{ title: '金额', key: 'amount_text', width: 10 },
|
|
682
|
+
{ title: '余额支付', key: 'wallet_paid_text', width: 10 },
|
|
683
|
+
{ title: '创建时间', key: 'created_at', format: (v) => formatTime(v), width: 20 },
|
|
684
|
+
],
|
|
685
|
+
rows,
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
ctx.done(data);
|
|
689
|
+
return data;
|
|
690
|
+
},
|
|
691
|
+
};
|