cyymall-cli 0.1.10 → 0.1.11

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.
@@ -1,175 +1,175 @@
1
- "use strict";
2
-
3
- const crypto = require("crypto");
4
- const http = require("../http");
5
- const config = require("../config");
6
- const biz = require("../biz");
7
-
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;
12
-
13
- function envelope(success, message, upstream, exitCode) {
14
- const traceId = `local-${crypto.randomBytes(8).toString("hex")}`;
15
- console.log(
16
- JSON.stringify(
17
- {
18
- success,
19
- code: success ? "OK" : "UPSTREAM_ERROR",
20
- message,
21
- data: { upstream },
22
- traceId,
23
- },
24
- null,
25
- 2,
26
- ),
27
- );
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
- );
167
- }
168
-
169
- module.exports = {
170
- search,
171
- categoryList,
172
- zoneTags,
173
- categorySkus,
174
- skusByCategory,
175
- };
1
+ "use strict";
2
+
3
+ const crypto = require("crypto");
4
+ const http = require("../http");
5
+ const config = require("../config");
6
+ const biz = require("../biz");
7
+
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;
12
+
13
+ function envelope(success, message, upstream, exitCode) {
14
+ const traceId = `local-${crypto.randomBytes(8).toString("hex")}`;
15
+ console.log(
16
+ JSON.stringify(
17
+ {
18
+ success,
19
+ code: success ? "OK" : "UPSTREAM_ERROR",
20
+ message,
21
+ data: { upstream },
22
+ traceId,
23
+ },
24
+ null,
25
+ 2,
26
+ ),
27
+ );
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
+ );
167
+ }
168
+
169
+ module.exports = {
170
+ search,
171
+ categoryList,
172
+ zoneTags,
173
+ categorySkus,
174
+ skusByCategory,
175
+ };