cyymall-cli 0.1.8 → 0.1.10

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/README.md CHANGED
@@ -64,6 +64,10 @@ cyy shop sites --shop-id <门店ID>
64
64
  cyy shop use --shop-id <门店ID>
65
65
  cyy shop use-site --site-id <站点ID>
66
66
  cyy product search --keyword 牛奶
67
+ cyy product category-list
68
+ cyy product zone-tags --group-id <二级分组id>
69
+ cyy product category-skus --group-id <同上> [--zone-key <k>] [--after-key <k>]
70
+ cyy product skus-by-category --group-id <同上>
67
71
  cyy api call --method POST --module PRODUCT --path /app/product/getSkuList --body-file body.json
68
72
  cyy order quick --keyword 牛奶 --quantity 1 --unit 袋
69
73
  cyy serve --port 8787
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cyymall-cli",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "CyyMall / 菜洋洋商城 API CLI (per app-api-cli-spec)",
5
5
  "bin": {
6
6
  "cyy": "bin/cyy.js"
package/src/cli.js CHANGED
@@ -146,6 +146,54 @@ prod
146
146
  await product.search(opts);
147
147
  });
148
148
 
149
+ prod
150
+ .command("category-list")
151
+ .description("POST /app/product/getCategory — category tree (level-1 + children groups)")
152
+ .option("--shop-id <id>", "Override shopId (default from session)")
153
+ .option("--site-id <id>", "Override siteId")
154
+ .action(async (opts) => {
155
+ await product.categoryList(opts);
156
+ });
157
+
158
+ prod
159
+ .command("zone-tags")
160
+ .description("POST /app/product/getZoneTagList — zone tags for a second-level group (--group-id)")
161
+ .requiredOption("--group-id <id>", "Second-level group id (children[].id from category-list)")
162
+ .option("--shop-id <id>", "Override shopId")
163
+ .option("--site-id <id>", "Override siteId")
164
+ .action(async (opts) => {
165
+ await product.zoneTags(opts);
166
+ });
167
+
168
+ prod
169
+ .command("category-skus")
170
+ .description(
171
+ "POST /app/product/skuListByCategoryTagAll — SKUs under group + optional zone tag (cursor afterKey)",
172
+ )
173
+ .requiredOption("--group-id <id>", "Second-level group id")
174
+ .option("--zone-key <k>", "Zone tag key from zone-tags (required when zone-tags non-empty; App uses first tag, often all)")
175
+ .option("--after-key <k>", "Cursor from previous response data.afterKey")
176
+ .option("--page-size <n>", "pageSize (App TAG_PAGE_SIZE default 20)", "20")
177
+ .option("--shop-id <id>", "Override shopId")
178
+ .option("--site-id <id>", "Override siteId")
179
+ .action(async (opts) => {
180
+ await product.categorySkus(opts);
181
+ });
182
+
183
+ prod
184
+ .command("skus-by-category")
185
+ .description(
186
+ "POST /app/product/skuListByCategory — fallback when zone-tags is empty (pageNum pagination)",
187
+ )
188
+ .requiredOption("--group-id <id>", "Second-level group id")
189
+ .option("--page <n>", "pageNum", "1")
190
+ .option("--page-size <n>", "pageSize (App PAGE_SIZE default 10)", "10")
191
+ .option("--shop-id <id>", "Override shopId")
192
+ .option("--site-id <id>", "Override siteId")
193
+ .action(async (opts) => {
194
+ await product.skusByCategory(opts);
195
+ });
196
+
149
197
  const shopCmd = program.command("shop").description("Member shops and delivery sites");
150
198
 
151
199
  shopCmd
@@ -5,48 +5,171 @@ const http = require("../http");
5
5
  const config = require("../config");
6
6
  const biz = require("../biz");
7
7
 
8
- /**
9
- * @param {object} opts
10
- */
11
- async function search(opts) {
12
- const cfg = config.requireAuthSession();
13
-
14
- const shopId = Number(opts.shopId ?? cfg.shop_id);
15
- const siteId = Number(opts.siteId ?? cfg.site_id);
16
- const body = JSON.stringify({
17
- pageNum: Number(opts.page || 1),
18
- pageSize: Number(opts.pageSize || 10),
19
- shopId,
20
- siteId,
21
- spuName: opts.keyword,
22
- stockFlag: String(opts.stockFlag ?? "0"),
23
- });
24
-
25
- const url = http.moduleUrl("PRODUCT", "/app/product/getSkuList");
26
- const headers = /** @type {Record<string,string>} */ (http.buildAuthHeaders(cfg));
27
-
28
- const { ok, json } = await http.request(url, {
29
- method: "POST",
30
- headers,
31
- body,
32
- });
8
+ /** App ResponseCode.TAG_PAGE_SIZE — used by skuListByCategoryTagAll */
9
+ const DEFAULT_TAG_PAGE_SIZE = 20;
10
+ /** App ResponseCode.PAGE_SIZE — used by skuListByCategory */
11
+ const DEFAULT_CATEGORY_PAGE_SIZE = 10;
33
12
 
13
+ function envelope(success, message, upstream, exitCode) {
34
14
  const traceId = `local-${crypto.randomBytes(8).toString("hex")}`;
35
- const success = ok && biz.isBizSuccess(json);
36
15
  console.log(
37
16
  JSON.stringify(
38
17
  {
39
18
  success,
40
19
  code: success ? "OK" : "UPSTREAM_ERROR",
41
- message: success ? "success" : "search failed",
42
- data: { upstream: json },
20
+ message,
21
+ data: { upstream },
43
22
  traceId,
44
23
  },
45
24
  null,
46
25
  2,
47
26
  ),
48
27
  );
49
- process.exit(success ? 0 : 2);
28
+ process.exit(exitCode ?? (success ? 0 : 2));
29
+ }
30
+
31
+ /**
32
+ * @param {object} opts
33
+ * @param {object} cfg
34
+ */
35
+ function resolveShopSite(opts, cfg) {
36
+ const shopId = Number(opts.shopId ?? cfg.shop_id);
37
+ const siteId = Number(opts.siteId ?? cfg.site_id);
38
+ if (!Number.isFinite(shopId) || !Number.isFinite(siteId)) {
39
+ console.error("cyy: shop_id and site_id required (session or --shop-id / --site-id)");
40
+ process.exit(1);
41
+ }
42
+ return { shopId, siteId };
43
+ }
44
+
45
+ /**
46
+ * @param {string} path
47
+ * @param {object} bodyObj
48
+ * @param {string} failMessage
49
+ */
50
+ async function postProduct(path, bodyObj, failMessage) {
51
+ const cfg = config.requireAuthSession();
52
+ const url = http.moduleUrl("PRODUCT", path);
53
+ const headers = /** @type {Record<string,string>} */ (http.buildAuthHeaders(cfg));
54
+ const { ok, json } = await http.request(url, {
55
+ method: "POST",
56
+ headers,
57
+ body: JSON.stringify(bodyObj),
58
+ });
59
+ const success = ok && biz.isBizSuccess(json);
60
+ envelope(success, success ? "success" : failMessage, json, success ? 0 : 2);
61
+ }
62
+
63
+ /**
64
+ * POST /app/product/getSkuList
65
+ * @param {object} opts
66
+ */
67
+ async function search(opts) {
68
+ const cfg = config.requireAuthSession();
69
+ const { shopId, siteId } = resolveShopSite(opts, cfg);
70
+ await postProduct(
71
+ "/app/product/getSkuList",
72
+ {
73
+ pageNum: Number(opts.page || 1),
74
+ pageSize: Number(opts.pageSize || 10),
75
+ shopId,
76
+ siteId,
77
+ spuName: opts.keyword,
78
+ stockFlag: String(opts.stockFlag ?? "0"),
79
+ },
80
+ "search failed",
81
+ );
82
+ }
83
+
84
+ /**
85
+ * POST /app/product/getCategory — 一级分类树(含二级 children)
86
+ * @param {object} opts
87
+ */
88
+ async function categoryList(opts) {
89
+ const cfg = config.requireAuthSession();
90
+ const { shopId, siteId } = resolveShopSite(opts, cfg);
91
+ await postProduct("/app/product/getCategory", { shopId, siteId }, "category list failed");
92
+ }
93
+
94
+ /**
95
+ * POST /app/product/getZoneTagList — categoryId 为二级分组 id(groupId)
96
+ * @param {object} opts
97
+ */
98
+ async function zoneTags(opts) {
99
+ const groupId = opts.groupId;
100
+ if (groupId === undefined || groupId === null || String(groupId).trim() === "") {
101
+ console.error("cyy: --group-id required (second-level category id from category-list children[].id)");
102
+ process.exit(1);
103
+ }
104
+ const cfg = config.requireAuthSession();
105
+ const { shopId, siteId } = resolveShopSite(opts, cfg);
106
+ await postProduct(
107
+ "/app/product/getZoneTagList",
108
+ {
109
+ shopId,
110
+ siteId,
111
+ categoryId: Number(groupId),
112
+ },
113
+ "zone tag list failed",
114
+ );
115
+ }
116
+
117
+ /**
118
+ * POST /app/product/skuListByCategoryTagAll — 有专区标签时的商品列表(游标 afterKey)
119
+ * @param {object} opts
120
+ */
121
+ async function categorySkus(opts) {
122
+ const groupId = opts.groupId;
123
+ if (groupId === undefined || groupId === null || String(groupId).trim() === "") {
124
+ console.error("cyy: --group-id required (second-level category id)");
125
+ process.exit(1);
126
+ }
127
+ const cfg = config.requireAuthSession();
128
+ const { shopId, siteId } = resolveShopSite(opts, cfg);
129
+ const body = {
130
+ shopId,
131
+ siteId,
132
+ categoryId: Number(groupId),
133
+ pageSize: Number(opts.pageSize || DEFAULT_TAG_PAGE_SIZE),
134
+ };
135
+ if (opts.afterKey != null && String(opts.afterKey).length > 0) {
136
+ body.afterKey = String(opts.afterKey);
137
+ }
138
+ if (opts.zoneKey != null && String(opts.zoneKey).length > 0) {
139
+ body.zoneKey = String(opts.zoneKey);
140
+ }
141
+ await postProduct("/app/product/skuListByCategoryTagAll", body, "category skus (tag all) failed");
142
+ }
143
+
144
+ /**
145
+ * POST /app/product/skuListByCategory — 无专区标签时的兜底(pageNum 分页)
146
+ * @param {object} opts
147
+ */
148
+ async function skusByCategory(opts) {
149
+ const groupId = opts.groupId;
150
+ if (groupId === undefined || groupId === null || String(groupId).trim() === "") {
151
+ console.error("cyy: --group-id required (second-level category id)");
152
+ process.exit(1);
153
+ }
154
+ const cfg = config.requireAuthSession();
155
+ const { shopId, siteId } = resolveShopSite(opts, cfg);
156
+ await postProduct(
157
+ "/app/product/skuListByCategory",
158
+ {
159
+ shopId,
160
+ siteId,
161
+ categoryId: Number(groupId),
162
+ pageNum: Number(opts.page || 1),
163
+ pageSize: Number(opts.pageSize || DEFAULT_CATEGORY_PAGE_SIZE),
164
+ },
165
+ "category skus failed",
166
+ );
50
167
  }
51
168
 
52
- module.exports = { search };
169
+ module.exports = {
170
+ search,
171
+ categoryList,
172
+ zoneTags,
173
+ categorySkus,
174
+ skusByCategory,
175
+ };
package/src/config.js CHANGED
@@ -4,7 +4,7 @@ const fs = require("fs");
4
4
  const path = require("path");
5
5
  const os = require("os");
6
6
 
7
- const DEFAULT_BASE_URL = "https://yunping.ifoodbuy.com/";
7
+ const DEFAULT_BASE_URL = "https://yunping.ifoodbuy.com/applet/";
8
8
 
9
9
  /** BuildConfig-style module prefixes (app-api-cli-spec §1.2) */
10
10
  const MODULE_MAP = {