eprolo-cli 1.0.108 → 1.0.109
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/bin/eprolo-cli.js +96 -86
- package/package.json +1 -1
package/bin/eprolo-cli.js
CHANGED
|
@@ -47,23 +47,23 @@ function readFlag(argv, flag) {
|
|
|
47
47
|
// HTTPS POST 请求
|
|
48
48
|
// ---------------------------------------------------------------------------
|
|
49
49
|
|
|
50
|
-
function postJson(url, body, headers = {},method = "POST") {
|
|
50
|
+
function postJson(url, body, headers = {}, method = "POST") {
|
|
51
51
|
const config = readConfig();
|
|
52
|
-
let signValue={}
|
|
53
|
-
|
|
52
|
+
let signValue = {}
|
|
53
|
+
if (config.timestamp) {
|
|
54
54
|
signValue.timestamp = config.timestamp;
|
|
55
55
|
signValue.sign = config.accessToken;
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
signValue = {...signValue, ...body};
|
|
57
|
+
if (method == "GET") {
|
|
58
|
+
signValue = { ...signValue, ...body };
|
|
59
59
|
}
|
|
60
60
|
url += "?" + new URLSearchParams(signValue).toString();
|
|
61
61
|
return new Promise((resolve, reject) => {
|
|
62
62
|
const target = new URL(url);
|
|
63
63
|
const payload = JSON.stringify(body);
|
|
64
|
-
let reqMethod=https;
|
|
65
|
-
if(String(target.protocol).startsWith("http:")){
|
|
66
|
-
reqMethod=http;
|
|
64
|
+
let reqMethod = https;
|
|
65
|
+
if (String(target.protocol).startsWith("http:")) {
|
|
66
|
+
reqMethod = http;
|
|
67
67
|
}
|
|
68
68
|
const req = reqMethod.request({
|
|
69
69
|
hostname: target.hostname,
|
|
@@ -108,7 +108,7 @@ async function postJsonWithRetry(url, body, config, method = "POST") {
|
|
|
108
108
|
let result = await postJson(url, body, headers, method);
|
|
109
109
|
if (result.data.code == '-1') {
|
|
110
110
|
// 刷新 token
|
|
111
|
-
const refreshRes = await postJson(LOGIN_API, {openApiKey: config.refreshToken});
|
|
111
|
+
const refreshRes = await postJson(LOGIN_API, { openApiKey: config.refreshToken });
|
|
112
112
|
|
|
113
113
|
if (refreshRes.data.code == 0) {
|
|
114
114
|
// 更新凭据文件
|
|
@@ -116,14 +116,14 @@ async function postJsonWithRetry(url, body, config, method = "POST") {
|
|
|
116
116
|
|
|
117
117
|
// 用新 token 重试
|
|
118
118
|
headers["apiKey"] = `${refreshRes.data.data.apiKey}`;
|
|
119
|
-
result = await postJson(url, body, headers,method);
|
|
119
|
+
result = await postJson(url, body, headers, method);
|
|
120
120
|
}
|
|
121
121
|
else {
|
|
122
122
|
const err = new Error("Token 刷新失败,请重新登录授权");
|
|
123
123
|
err.response = refreshRes.data;
|
|
124
124
|
throw err;
|
|
125
125
|
}
|
|
126
|
-
}else if (result.data.code != 0) {
|
|
126
|
+
} else if (result.data.code != 0) {
|
|
127
127
|
const err = new Error(`HTTP ${result.statusCode}`);
|
|
128
128
|
err.response = result.data.data.msg;
|
|
129
129
|
throw err;
|
|
@@ -208,7 +208,7 @@ async function queryProductId(productId) {
|
|
|
208
208
|
console.error(JSON.stringify({ success: false, errorMessage: "缺少产品 ID" }));
|
|
209
209
|
process.exit(1);
|
|
210
210
|
}
|
|
211
|
-
const data = await postJsonWithRetry(PRODUCT_TYPE_API, {id:productId}, config, "GET");
|
|
211
|
+
const data = await postJsonWithRetry(PRODUCT_TYPE_API, { id: productId }, config, "GET");
|
|
212
212
|
console.log(JSON.stringify(data, null, 2));
|
|
213
213
|
process.exit(0);
|
|
214
214
|
}
|
|
@@ -230,98 +230,108 @@ function readPayload(arg) {
|
|
|
230
230
|
}
|
|
231
231
|
return arg;
|
|
232
232
|
}
|
|
233
|
-
function getId(id){
|
|
234
|
-
let ids=String(id).split('/');
|
|
235
|
-
return ids[ids.length-1];
|
|
233
|
+
function getId(id) {
|
|
234
|
+
let ids = String(id).split('/');
|
|
235
|
+
return ids[ids.length - 1];
|
|
236
236
|
}
|
|
237
237
|
async function insertProduct(arg) {
|
|
238
238
|
const config = readConfig();
|
|
239
239
|
const product = JSON.parse(arg);
|
|
240
|
-
const info={};
|
|
241
|
-
const optionList=[];
|
|
242
|
-
const variantsList=[];
|
|
243
|
-
const imageList=[];
|
|
240
|
+
const info = {};
|
|
241
|
+
const optionList = [];
|
|
242
|
+
const variantsList = [];
|
|
243
|
+
const imageList = [];
|
|
244
244
|
info.title = product.title;
|
|
245
245
|
info.product_id = getId(product.id);
|
|
246
|
-
info.body_html=product.description;
|
|
247
|
-
info.optionList=optionList;
|
|
248
|
-
info.variantsList=variantsList;
|
|
249
|
-
info.imageList=imageList;
|
|
250
|
-
product.variants.edges
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if(
|
|
258
|
-
|
|
246
|
+
info.body_html = product.description;
|
|
247
|
+
info.optionList = optionList;
|
|
248
|
+
info.variantsList = variantsList;
|
|
249
|
+
info.imageList = imageList;
|
|
250
|
+
if (product.variants && product.variants.edges) {
|
|
251
|
+
product.variants.edges.forEach((variant, index) => {
|
|
252
|
+
let newInfo = {
|
|
253
|
+
image_id: getId(variant.id),
|
|
254
|
+
title: variant.title,
|
|
255
|
+
sku: variant.sku
|
|
256
|
+
};
|
|
257
|
+
if(variant.selectedOptions && variant.selectedOptions.length > 0){
|
|
258
|
+
variant.selectedOptions.forEach((option, optionIndex) => {
|
|
259
|
+
if (index == 0) {
|
|
260
|
+
optionList.push({ name: option.name });
|
|
261
|
+
}
|
|
262
|
+
newInfo['option' + optionIndex] = option.name;
|
|
263
|
+
})
|
|
259
264
|
}
|
|
260
|
-
newInfo
|
|
265
|
+
variantsList.push(newInfo);
|
|
261
266
|
})
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
267
|
+
}
|
|
268
|
+
if (product.media && product.media.edges) {
|
|
269
|
+
product.media.edges.forEach((edge) => {
|
|
270
|
+
imageList.push({ src: edge.url, images_id: getId(edge.id) });
|
|
271
|
+
})
|
|
272
|
+
}
|
|
273
|
+
const data = await postJsonWithRetry(BASE_URL + '/insert_product.html', info, config, "POST");
|
|
274
|
+
if (data.data.code == '-1') {
|
|
275
|
+
if (data.data.msg == "product_id exist") {
|
|
276
|
+
const result = await postJsonWithRetry(BASE_URL + '/getproduct.html', { product_id: info.product_id }, config, "GET");
|
|
277
|
+
if (result.data.code == 0) {
|
|
278
|
+
console.log(JSON.stringify({
|
|
279
|
+
variantlist: result.data.data.variantlist.map((item) => {
|
|
280
|
+
return {
|
|
281
|
+
variantsid: item.id,
|
|
282
|
+
sku: item.sku
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
}, null, 2));
|
|
286
|
+
process.exit(0);
|
|
279
287
|
}
|
|
280
|
-
}else{
|
|
281
|
-
|
|
282
|
-
|
|
288
|
+
} else {
|
|
289
|
+
console.log(JSON.stringify(data, null, 2));
|
|
290
|
+
process.exit(0);
|
|
283
291
|
}
|
|
284
292
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
293
|
+
console.log(JSON.stringify({
|
|
294
|
+
variantlist: data.data.data.variantsList.map((item) => {
|
|
295
|
+
return {
|
|
296
|
+
variantsid: item.id,
|
|
297
|
+
sku: item.sku
|
|
298
|
+
}
|
|
299
|
+
})
|
|
300
|
+
}, null, 2));
|
|
291
301
|
process.exit(0);
|
|
292
302
|
}
|
|
293
303
|
async function addOrder(arg) {
|
|
294
304
|
const config = readConfig();
|
|
295
305
|
const order = JSON.parse(arg);
|
|
296
|
-
console.log("-----------------",order);
|
|
297
|
-
const newOrder={};
|
|
298
|
-
const orderItemlist=[];
|
|
299
|
-
newOrder.note=order.name;
|
|
300
|
-
newOrder.shipping_country_code=order.currencyCode;
|
|
301
|
-
newOrder.order_id=getId(order.id);
|
|
302
|
-
newOrder.order_number=order.name;
|
|
303
|
-
if(order.shippingAddress){
|
|
304
|
-
newOrder.shipping_name=order.shippingAddress.firstName + ' ' + order.shippingAddress.lastName;
|
|
305
|
-
newOrder.shipping_phone=order.shippingAddress.phone
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
306
|
+
console.log("-----------------", order);
|
|
307
|
+
const newOrder = {};
|
|
308
|
+
const orderItemlist = [];
|
|
309
|
+
newOrder.note = order.name;
|
|
310
|
+
newOrder.shipping_country_code = order.currencyCode;
|
|
311
|
+
newOrder.order_id = getId(order.id);
|
|
312
|
+
newOrder.order_number = order.name;
|
|
313
|
+
if (order.shippingAddress) {
|
|
314
|
+
newOrder.shipping_name = order.shippingAddress.firstName + ' ' + order.shippingAddress.lastName;
|
|
315
|
+
newOrder.shipping_phone = order.shippingAddress.phone
|
|
316
|
+
// newOrder.shipping_company=order.
|
|
317
|
+
newOrder.shipping_country = order.shippingAddress.country;
|
|
318
|
+
newOrder.shipping_address = order.shippingAddress.address1;
|
|
319
|
+
newOrder.shipping_province = order.shippingAddress.province;
|
|
320
|
+
// newOrder.shipping_province_code=order.shippingAddress.;
|
|
321
|
+
newOrder.shipping_address2 = order.shippingAddress.address2
|
|
322
|
+
newOrder.shipping_city = order.shippingAddress.city;
|
|
323
|
+
// newOrder.shipping_taxNumber=order.shippingAddress.taxNumber;
|
|
314
324
|
}
|
|
315
|
-
if(order.lineItems && order.lineItems.edges){
|
|
316
|
-
order.lineItems.edges.forEach((edge)=>{
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
325
|
+
if (order.lineItems && order.lineItems.edges) {
|
|
326
|
+
order.lineItems.edges.forEach((edge) => {
|
|
327
|
+
orderItemlist.push({
|
|
328
|
+
variantsid: getId(edge.node.variant.id),
|
|
329
|
+
quantity: edge.node.quantity,
|
|
330
|
+
})
|
|
320
331
|
})
|
|
321
|
-
|
|
322
|
-
newOrder.orderItemlist=orderItemlist;
|
|
332
|
+
newOrder.orderItemlist = orderItemlist;
|
|
323
333
|
}
|
|
324
|
-
console.log("-------222----------",newOrder);
|
|
334
|
+
console.log("-------222----------", newOrder);
|
|
325
335
|
const data = await postJsonWithRetry(`http://43.153.35.208:3000/addInfo`, newOrder, config, "POST");
|
|
326
336
|
console.log(JSON.stringify(data, null, 2));
|
|
327
337
|
process.exit(0);
|
|
@@ -369,7 +379,7 @@ Credential file (~/.eprolo-toolkit/config.json):
|
|
|
369
379
|
|
|
370
380
|
async function main() {
|
|
371
381
|
const argv = process.argv.slice(2);
|
|
372
|
-
console.log("-----------------",argv);
|
|
382
|
+
console.log("-----------------", argv);
|
|
373
383
|
if (argv[0] === "auth" && argv[1] === "login") {
|
|
374
384
|
await login(argv.slice(2));
|
|
375
385
|
return;
|
package/package.json
CHANGED